Improved ListShortUrlsActionTest

This commit is contained in:
Alejandro Celaya 2019-08-08 16:43:47 +02:00
parent 564b65c8ca
commit a304cca3b6

View File

@ -6,8 +6,10 @@ namespace ShlinkioTest\Shlink\Rest\Action\ShortUrl;
use Exception; use Exception;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy; use Prophecy\Prophecy\ObjectProphecy;
use Psr\Log\LoggerInterface;
use Shlinkio\Shlink\Core\Service\ShortUrlService; use Shlinkio\Shlink\Core\Service\ShortUrlService;
use Shlinkio\Shlink\Rest\Action\ShortUrl\ListShortUrlsAction; use Shlinkio\Shlink\Rest\Action\ShortUrl\ListShortUrlsAction;
use Zend\Diactoros\Response\JsonResponse;
use Zend\Diactoros\ServerRequest; use Zend\Diactoros\ServerRequest;
use Zend\Paginator\Adapter\ArrayAdapter; use Zend\Paginator\Adapter\ArrayAdapter;
use Zend\Paginator\Paginator; use Zend\Paginator\Paginator;
@ -18,39 +20,84 @@ class ListShortUrlsActionTest extends TestCase
private $action; private $action;
/** @var ObjectProphecy */ /** @var ObjectProphecy */
private $service; private $service;
/** @var ObjectProphecy */
private $logger;
public function setUp(): void public function setUp(): void
{ {
$this->service = $this->prophesize(ShortUrlService::class); $this->service = $this->prophesize(ShortUrlService::class);
$this->logger = $this->prophesize(LoggerInterface::class);
$this->action = new ListShortUrlsAction($this->service->reveal(), [ $this->action = new ListShortUrlsAction($this->service->reveal(), [
'hostname' => 'doma.in', 'hostname' => 'doma.in',
'schema' => 'https', 'schema' => 'https',
]); ], $this->logger->reveal());
} }
/** @test */ /**
public function properListReturnsSuccessResponse() * @test
{ * @dataProvider provideFilteringData
$page = 3; */
$this->service->listShortUrls($page, null, [], null)->willReturn(new Paginator(new ArrayAdapter())) public function properListReturnsSuccessResponse(
->shouldBeCalledOnce(); array $query,
int $expectedPage,
?string $expectedSearchTerm,
array $expectedTags,
?string $expectedOrderBy
): void {
$listShortUrls = $this->service->listShortUrls(
$expectedPage,
$expectedSearchTerm,
$expectedTags,
$expectedOrderBy
)->willReturn(new Paginator(new ArrayAdapter()));
$response = $this->action->handle((new ServerRequest())->withQueryParams([ /** @var JsonResponse $response */
'page' => $page, $response = $this->action->handle((new ServerRequest())->withQueryParams($query));
])); $payload = $response->getPayload();
$this->assertArrayHasKey('shortUrls', $payload);
$this->assertArrayHasKey('data', $payload['shortUrls']);
$this->assertEquals([], $payload['shortUrls']['data']);
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());
$listShortUrls->shouldHaveBeenCalledOnce();
}
public function provideFilteringData(): iterable
{
yield [[], 1, null, [], null];
yield [['page' => 10], 10, null, [], null];
yield [['page' => null], 1, null, [], null];
yield [['page' => '8'], 8, null, [], null];
yield [['searchTerm' => $searchTerm = 'foo'], 1, $searchTerm, [], null];
yield [['tags' => $tags = ['foo','bar']], 1, null, $tags, null];
yield [['orderBy' => $orderBy = 'something'], 1, null, [], $orderBy];
yield [[
'page' => '2',
'orderBy' => $orderBy = 'something',
'tags' => $tags = ['one', 'two'],
], 2, null, $tags, $orderBy];
} }
/** @test */ /** @test */
public function anExceptionsReturnsErrorResponse() public function anExceptionReturnsErrorResponse(): void
{ {
$page = 3; $page = 3;
$this->service->listShortUrls($page, null, [], null)->willThrow(Exception::class) $e = new Exception();
$this->service->listShortUrls($page, null, [], null)->willThrow($e)
->shouldBeCalledOnce(); ->shouldBeCalledOnce();
$logError = $this->logger->error(
'Unexpected error while listing short URLs. {e}',
['e' => $e]
)->will(function () {
});
$response = $this->action->handle((new ServerRequest())->withQueryParams([ $response = $this->action->handle((new ServerRequest())->withQueryParams([
'page' => $page, 'page' => $page,
])); ]));
$this->assertEquals(500, $response->getStatusCode()); $this->assertEquals(500, $response->getStatusCode());
$logError->shouldHaveBeenCalledOnce();
} }
} }