From 4b113e578125459f172bcfa3fd700f08be245790 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 17 Dec 2019 10:06:18 +0100 Subject: [PATCH] Added tests covering how orderBy is parsed on ListShortUrlsCommand --- .../Command/ShortUrl/ListShortUrlsCommand.php | 10 +++++--- .../ShortUrl/ListShortUrlsCommandTest.php | 23 +++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/module/CLI/src/Command/ShortUrl/ListShortUrlsCommand.php b/module/CLI/src/Command/ShortUrl/ListShortUrlsCommand.php index 5871cc76..52918afa 100644 --- a/module/CLI/src/Command/ShortUrl/ListShortUrlsCommand.php +++ b/module/CLI/src/Command/ShortUrl/ListShortUrlsCommand.php @@ -109,12 +109,12 @@ class ListShortUrlsCommand extends AbstractWithDateRangeCommand $showTags = (bool) $input->getOption('showTags'); $startDate = $this->getDateOption($input, $output, 'startDate'); $endDate = $this->getDateOption($input, $output, 'endDate'); + $orderBy = $this->processOrderBy($input); $transformer = new ShortUrlDataTransformer($this->domainConfig); do { $result = $this->renderPage( - $input, $output, $page, $searchTerm, @@ -122,6 +122,7 @@ class ListShortUrlsCommand extends AbstractWithDateRangeCommand $showTags, $startDate, $endDate, + $orderBy, $transformer ); $page++; @@ -138,7 +139,6 @@ class ListShortUrlsCommand extends AbstractWithDateRangeCommand } private function renderPage( - InputInterface $input, OutputInterface $output, int $page, ?string $searchTerm, @@ -146,13 +146,14 @@ class ListShortUrlsCommand extends AbstractWithDateRangeCommand bool $showTags, ?Chronos $startDate, ?Chronos $endDate, + $orderBy, DataTransformerInterface $transformer ): Paginator { $result = $this->shortUrlService->listShortUrls( $page, $searchTerm, $tags, - $this->processOrderBy($input), + $orderBy, new DateRange($startDate, $endDate) ); @@ -181,6 +182,9 @@ class ListShortUrlsCommand extends AbstractWithDateRangeCommand return $result; } + /** + * @return array|string|null + */ private function processOrderBy(InputInterface $input) { $orderBy = $input->getOption('orderBy'); diff --git a/module/CLI/test/Command/ShortUrl/ListShortUrlsCommandTest.php b/module/CLI/test/Command/ShortUrl/ListShortUrlsCommandTest.php index 71babc47..0bf3bfab 100644 --- a/module/CLI/test/Command/ShortUrl/ListShortUrlsCommandTest.php +++ b/module/CLI/test/Command/ShortUrl/ListShortUrlsCommandTest.php @@ -162,4 +162,27 @@ class ListShortUrlsCommandTest extends TestCase new DateRange(Chronos::parse($startDate), Chronos::parse($endDate)), ]; } + + /** + * @test + * @dataProvider provideOrderBy + */ + public function orderByIsProperlyComputed(array $commandArgs, $expectedOrderBy): void + { + $listShortUrls = $this->shortUrlService->listShortUrls(1, null, [], $expectedOrderBy, new DateRange()) + ->willReturn(new Paginator(new ArrayAdapter())); + + $this->commandTester->setInputs(['n']); + $this->commandTester->execute($commandArgs); + + $listShortUrls->shouldHaveBeenCalledOnce(); + } + + public function provideOrderBy(): iterable + { + yield [[], null]; + yield [['--orderBy' => 'foo'], 'foo']; + yield [['--orderBy' => 'foo,ASC'], ['foo' => 'ASC']]; + yield [['--orderBy' => 'bar,DESC'], ['bar' => 'DESC']]; + } }