From 8607d58e186db5a2dd1edb49bcbf9968e9b3ca2d Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 21 Nov 2019 18:49:55 +0100 Subject: [PATCH] Created API tests for errors when editting short URL tags --- .../Action/ShortUrl/ListShortUrlsAction.php | 23 ++++--------- .../Action/EditShortUrlActionTagsTest.php | 34 +++++++++++++++++++ .../ShortUrl/ListShortUrlsActionTest.php | 23 ------------- 3 files changed, 41 insertions(+), 39 deletions(-) create mode 100644 module/Rest/test-api/Action/EditShortUrlActionTagsTest.php diff --git a/module/Rest/src/Action/ShortUrl/ListShortUrlsAction.php b/module/Rest/src/Action/ShortUrl/ListShortUrlsAction.php index 8c4211c8..157fbe06 100644 --- a/module/Rest/src/Action/ShortUrl/ListShortUrlsAction.php +++ b/module/Rest/src/Action/ShortUrl/ListShortUrlsAction.php @@ -4,7 +4,7 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Rest\Action\ShortUrl; -use Exception; +use InvalidArgumentException; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Log\LoggerInterface; @@ -12,7 +12,6 @@ use Shlinkio\Shlink\Common\Paginator\Util\PaginatorUtilsTrait; use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface; use Shlinkio\Shlink\Core\Transformer\ShortUrlDataTransformer; use Shlinkio\Shlink\Rest\Action\AbstractRestAction; -use Shlinkio\Shlink\Rest\Util\RestUtils; use Zend\Diactoros\Response\JsonResponse; class ListShortUrlsAction extends AbstractRestAction @@ -40,23 +39,15 @@ class ListShortUrlsAction extends AbstractRestAction /** * @param Request $request * @return Response - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function handle(Request $request): Response { - try { - $params = $this->queryToListParams($request->getQueryParams()); - $shortUrls = $this->shortUrlService->listShortUrls(...$params); - return new JsonResponse(['shortUrls' => $this->serializePaginator($shortUrls, new ShortUrlDataTransformer( - $this->domainConfig - ))]); - } catch (Exception $e) { - $this->logger->error('Unexpected error while listing short URLs. {e}', ['e' => $e]); - return new JsonResponse([ - 'error' => RestUtils::UNKNOWN_ERROR, - 'message' => 'Unexpected error occurred', - ], self::STATUS_INTERNAL_SERVER_ERROR); - } + $params = $this->queryToListParams($request->getQueryParams()); + $shortUrls = $this->shortUrlService->listShortUrls(...$params); + return new JsonResponse(['shortUrls' => $this->serializePaginator($shortUrls, new ShortUrlDataTransformer( + $this->domainConfig + ))]); } /** diff --git a/module/Rest/test-api/Action/EditShortUrlActionTagsTest.php b/module/Rest/test-api/Action/EditShortUrlActionTagsTest.php new file mode 100644 index 00000000..d033fcee --- /dev/null +++ b/module/Rest/test-api/Action/EditShortUrlActionTagsTest.php @@ -0,0 +1,34 @@ +callApiWithKey(self::METHOD_PUT, '/short-urls/abc123/tags', [RequestOptions::JSON => []]); + ['error' => $error] = $this->getJsonResponsePayload($resp); + + $this->assertEquals(self::STATUS_BAD_REQUEST, $resp->getStatusCode()); + $this->assertEquals(RestUtils::INVALID_ARGUMENT_ERROR, $error); + } + + /** @test */ + public function providingInvalidShortCodeReturnsBadRequest(): void + { + $resp = $this->callApiWithKey(self::METHOD_PUT, '/short-urls/invalid/tags', [RequestOptions::JSON => [ + 'tags' => ['foo', 'bar'], + ]]); + ['error' => $error] = $this->getJsonResponsePayload($resp); + + $this->assertEquals(self::STATUS_NOT_FOUND, $resp->getStatusCode()); + $this->assertEquals(RestUtils::INVALID_SHORTCODE_ERROR, $error); + } +} diff --git a/module/Rest/test/Action/ShortUrl/ListShortUrlsActionTest.php b/module/Rest/test/Action/ShortUrl/ListShortUrlsActionTest.php index 7bb2d623..4197aba8 100644 --- a/module/Rest/test/Action/ShortUrl/ListShortUrlsActionTest.php +++ b/module/Rest/test/Action/ShortUrl/ListShortUrlsActionTest.php @@ -4,7 +4,6 @@ declare(strict_types=1); namespace ShlinkioTest\Shlink\Rest\Action\ShortUrl; -use Exception; use PHPUnit\Framework\TestCase; use Prophecy\Prophecy\ObjectProphecy; use Psr\Log\LoggerInterface; @@ -79,26 +78,4 @@ class ListShortUrlsActionTest extends TestCase 'tags' => $tags = ['one', 'two'], ], 2, null, $tags, $orderBy]; } - - /** @test */ - public function anExceptionReturnsErrorResponse(): void - { - $page = 3; - $e = new Exception(); - - $this->service->listShortUrls($page, null, [], null)->willThrow($e) - ->shouldBeCalledOnce(); - $logError = $this->logger->error( - 'Unexpected error while listing short URLs. {e}', - ['e' => $e] - )->will(function () { - }); - - $response = $this->action->handle((new ServerRequest())->withQueryParams([ - 'page' => $page, - ])); - - $this->assertEquals(500, $response->getStatusCode()); - $logError->shouldHaveBeenCalledOnce(); - } }