mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Created API tests for errors when editting short URL tags
This commit is contained in:
parent
34e60ec5b8
commit
8607d58e18
@ -4,7 +4,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Shlinkio\Shlink\Rest\Action\ShortUrl;
|
namespace Shlinkio\Shlink\Rest\Action\ShortUrl;
|
||||||
|
|
||||||
use Exception;
|
use InvalidArgumentException;
|
||||||
use Psr\Http\Message\ResponseInterface as Response;
|
use Psr\Http\Message\ResponseInterface as Response;
|
||||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||||
use Psr\Log\LoggerInterface;
|
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\Service\ShortUrlServiceInterface;
|
||||||
use Shlinkio\Shlink\Core\Transformer\ShortUrlDataTransformer;
|
use Shlinkio\Shlink\Core\Transformer\ShortUrlDataTransformer;
|
||||||
use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
|
use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
|
||||||
use Shlinkio\Shlink\Rest\Util\RestUtils;
|
|
||||||
use Zend\Diactoros\Response\JsonResponse;
|
use Zend\Diactoros\Response\JsonResponse;
|
||||||
|
|
||||||
class ListShortUrlsAction extends AbstractRestAction
|
class ListShortUrlsAction extends AbstractRestAction
|
||||||
@ -40,23 +39,15 @@ class ListShortUrlsAction extends AbstractRestAction
|
|||||||
/**
|
/**
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
* @return Response
|
* @return Response
|
||||||
* @throws \InvalidArgumentException
|
* @throws InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
public function handle(Request $request): Response
|
public function handle(Request $request): Response
|
||||||
{
|
{
|
||||||
try {
|
$params = $this->queryToListParams($request->getQueryParams());
|
||||||
$params = $this->queryToListParams($request->getQueryParams());
|
$shortUrls = $this->shortUrlService->listShortUrls(...$params);
|
||||||
$shortUrls = $this->shortUrlService->listShortUrls(...$params);
|
return new JsonResponse(['shortUrls' => $this->serializePaginator($shortUrls, new ShortUrlDataTransformer(
|
||||||
return new JsonResponse(['shortUrls' => $this->serializePaginator($shortUrls, new ShortUrlDataTransformer(
|
$this->domainConfig
|
||||||
$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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
34
module/Rest/test-api/Action/EditShortUrlActionTagsTest.php
Normal file
34
module/Rest/test-api/Action/EditShortUrlActionTagsTest.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@ -4,7 +4,6 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace ShlinkioTest\Shlink\Rest\Action\ShortUrl;
|
namespace ShlinkioTest\Shlink\Rest\Action\ShortUrl;
|
||||||
|
|
||||||
use Exception;
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Prophecy\Prophecy\ObjectProphecy;
|
use Prophecy\Prophecy\ObjectProphecy;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
@ -79,26 +78,4 @@ class ListShortUrlsActionTest extends TestCase
|
|||||||
'tags' => $tags = ['one', 'two'],
|
'tags' => $tags = ['one', 'two'],
|
||||||
], 2, null, $tags, $orderBy];
|
], 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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user