From a304cca3b671f7c4e25580edab83d67e8ce4978e Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 8 Aug 2019 16:43:47 +0200 Subject: [PATCH] Improved ListShortUrlsActionTest --- .../ShortUrl/ListShortUrlsActionTest.php | 71 +++++++++++++++---- 1 file changed, 59 insertions(+), 12 deletions(-) diff --git a/module/Rest/test/Action/ShortUrl/ListShortUrlsActionTest.php b/module/Rest/test/Action/ShortUrl/ListShortUrlsActionTest.php index 65f2a3bb..1c413235 100644 --- a/module/Rest/test/Action/ShortUrl/ListShortUrlsActionTest.php +++ b/module/Rest/test/Action/ShortUrl/ListShortUrlsActionTest.php @@ -6,8 +6,10 @@ namespace ShlinkioTest\Shlink\Rest\Action\ShortUrl; use Exception; use PHPUnit\Framework\TestCase; use Prophecy\Prophecy\ObjectProphecy; +use Psr\Log\LoggerInterface; use Shlinkio\Shlink\Core\Service\ShortUrlService; use Shlinkio\Shlink\Rest\Action\ShortUrl\ListShortUrlsAction; +use Zend\Diactoros\Response\JsonResponse; use Zend\Diactoros\ServerRequest; use Zend\Paginator\Adapter\ArrayAdapter; use Zend\Paginator\Paginator; @@ -18,39 +20,84 @@ class ListShortUrlsActionTest extends TestCase private $action; /** @var ObjectProphecy */ private $service; + /** @var ObjectProphecy */ + private $logger; public function setUp(): void { $this->service = $this->prophesize(ShortUrlService::class); + $this->logger = $this->prophesize(LoggerInterface::class); + $this->action = new ListShortUrlsAction($this->service->reveal(), [ 'hostname' => 'doma.in', 'schema' => 'https', - ]); + ], $this->logger->reveal()); } - /** @test */ - public function properListReturnsSuccessResponse() - { - $page = 3; - $this->service->listShortUrls($page, null, [], null)->willReturn(new Paginator(new ArrayAdapter())) - ->shouldBeCalledOnce(); + /** + * @test + * @dataProvider provideFilteringData + */ + public function properListReturnsSuccessResponse( + 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([ - 'page' => $page, - ])); + /** @var JsonResponse $response */ + $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()); + $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 */ - public function anExceptionsReturnsErrorResponse() + public function anExceptionReturnsErrorResponse(): void { $page = 3; - $this->service->listShortUrls($page, null, [], null)->willThrow(Exception::class) + $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(); } }