Replaced hardcoded error response by the use of a problem details action

This commit is contained in:
Alejandro Celaya 2019-11-26 22:18:55 +01:00
parent 3b56fc3760
commit fffb2872ef
3 changed files with 8 additions and 12 deletions

View file

@ -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'];

View file

@ -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

View file

@ -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 */