Created API tests for errors when editting short URL tags

This commit is contained in:
Alejandro Celaya 2019-11-21 18:49:55 +01:00
parent 34e60ec5b8
commit 8607d58e18
3 changed files with 41 additions and 39 deletions

View file

@ -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
))]);
}
/**

View file

@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace ShlinkioApiTest\Shlink\Rest\Action;
use GuzzleHttp\RequestOptions;
use Shlinkio\Shlink\Rest\Util\RestUtils;
use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase;
class EditShortUrlActionTagsTest extends ApiTestCase
{
/** @test */
public function notProvidingTagsReturnsBadRequest(): void
{
$resp = $this->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);
}
}

View file

@ -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();
}
}