From fffb2872ef36cc34309615200fb421ef1443bc84 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 26 Nov 2019 22:18:55 +0100 Subject: [PATCH] Replaced hardcoded error response by the use of a problem details action --- .../src/Action/ShortUrl/EditShortUrlTagsAction.php | 13 ++++--------- ...nTagsTest.php => EditShortUrlTagsActionTest.php} | 2 +- .../Action/ShortUrl/EditShortUrlTagsActionTest.php | 5 +++-- 3 files changed, 8 insertions(+), 12 deletions(-) rename module/Rest/test-api/Action/{EditShortUrlActionTagsTest.php => EditShortUrlTagsActionTest.php} (95%) diff --git a/module/Rest/src/Action/ShortUrl/EditShortUrlTagsAction.php b/module/Rest/src/Action/ShortUrl/EditShortUrlTagsAction.php index 3f057e75..208be169 100644 --- a/module/Rest/src/Action/ShortUrl/EditShortUrlTagsAction.php +++ b/module/Rest/src/Action/ShortUrl/EditShortUrlTagsAction.php @@ -7,6 +7,7 @@ namespace Shlinkio\Shlink\Rest\Action\ShortUrl; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Log\LoggerInterface; +use Shlinkio\Shlink\Core\Exception\ValidationException; use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface; use Shlinkio\Shlink\Rest\Action\AbstractRestAction; use Zend\Diactoros\Response\JsonResponse; @@ -25,21 +26,15 @@ class EditShortUrlTagsAction extends AbstractRestAction $this->shortUrlService = $shortUrlService; } - /** - * @param Request $request - * @return Response - * @throws \InvalidArgumentException - */ public function handle(Request $request): Response { $shortCode = $request->getAttribute('shortCode'); $bodyParams = $request->getParsedBody(); if (! isset($bodyParams['tags'])) { - return new JsonResponse([ - 'error' => 'INVALID_ARGUMENT', - 'message' => 'A list of tags was not provided', - ], self::STATUS_BAD_REQUEST); + throw ValidationException::fromArray([ + 'tags' => 'List of tags has to be provided', + ]); } $tags = $bodyParams['tags']; diff --git a/module/Rest/test-api/Action/EditShortUrlActionTagsTest.php b/module/Rest/test-api/Action/EditShortUrlTagsActionTest.php similarity index 95% rename from module/Rest/test-api/Action/EditShortUrlActionTagsTest.php rename to module/Rest/test-api/Action/EditShortUrlTagsActionTest.php index e6cae9b6..5116d1e6 100644 --- a/module/Rest/test-api/Action/EditShortUrlActionTagsTest.php +++ b/module/Rest/test-api/Action/EditShortUrlTagsActionTest.php @@ -7,7 +7,7 @@ namespace ShlinkioApiTest\Shlink\Rest\Action; use GuzzleHttp\RequestOptions; use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase; -class EditShortUrlActionTagsTest extends ApiTestCase +class EditShortUrlTagsActionTest extends ApiTestCase { /** @test */ public function notProvidingTagsReturnsBadRequest(): void diff --git a/module/Rest/test/Action/ShortUrl/EditShortUrlTagsActionTest.php b/module/Rest/test/Action/ShortUrl/EditShortUrlTagsActionTest.php index 5c0ec628..17293f05 100644 --- a/module/Rest/test/Action/ShortUrl/EditShortUrlTagsActionTest.php +++ b/module/Rest/test/Action/ShortUrl/EditShortUrlTagsActionTest.php @@ -7,6 +7,7 @@ namespace ShlinkioTest\Shlink\Rest\Action\ShortUrl; use PHPUnit\Framework\TestCase; use Prophecy\Prophecy\ObjectProphecy; use Shlinkio\Shlink\Core\Entity\ShortUrl; +use Shlinkio\Shlink\Core\Exception\ValidationException; use Shlinkio\Shlink\Core\Service\ShortUrlService; use Shlinkio\Shlink\Rest\Action\ShortUrl\EditShortUrlTagsAction; use Zend\Diactoros\ServerRequest; @@ -27,8 +28,8 @@ class EditShortUrlTagsActionTest extends TestCase /** @test */ public function notProvidingTagsReturnsError(): void { - $response = $this->action->handle((new ServerRequest())->withAttribute('shortCode', 'abc123')); - $this->assertEquals(400, $response->getStatusCode()); + $this->expectException(ValidationException::class); + $this->action->handle((new ServerRequest())->withAttribute('shortCode', 'abc123')); } /** @test */