Improved ListShortUrlsActionTest covering different scenarios in which date ranges are provided

This commit is contained in:
Alejandro Celaya 2019-12-16 21:55:48 +01:00
parent 839ca31821
commit 8ad8b08aa4
2 changed files with 49 additions and 18 deletions

View File

@ -58,19 +58,20 @@ class ListShortUrlsAction extends AbstractRestAction
*/
private function queryToListParams(array $query): array
{
$dateRange = null;
$startDate = isset($query['startDate']) ? Chronos::parse($query['startDate']) : null;
$endDate = isset($query['endDate']) ? Chronos::parse($query['endDate']) : null;
if ($startDate !== null || $endDate !== null) {
$dateRange = new DateRange($startDate, $endDate);
}
return [
(int) ($query['page'] ?? 1),
$query['searchTerm'] ?? null,
$query['tags'] ?? [],
$query['orderBy'] ?? null,
$dateRange,
$this->determineDateRangeFromQuery($query),
];
}
private function determineDateRangeFromQuery(array $query): DateRange
{
return new DateRange(
isset($query['startDate']) ? Chronos::parse($query['startDate']) : null,
isset($query['endDate']) ? Chronos::parse($query['endDate']) : null
);
}
}

View File

@ -4,9 +4,11 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Action\ShortUrl;
use Cake\Chronos\Chronos;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Log\LoggerInterface;
use Shlinkio\Shlink\Common\Util\DateRange;
use Shlinkio\Shlink\Core\Service\ShortUrlService;
use Shlinkio\Shlink\Rest\Action\ShortUrl\ListShortUrlsAction;
use Zend\Diactoros\Response\JsonResponse;
@ -43,14 +45,15 @@ class ListShortUrlsActionTest extends TestCase
int $expectedPage,
?string $expectedSearchTerm,
array $expectedTags,
?string $expectedOrderBy
?string $expectedOrderBy,
DateRange $expectedDateRange
): void {
$listShortUrls = $this->service->listShortUrls(
$expectedPage,
$expectedSearchTerm,
$expectedTags,
$expectedOrderBy,
null
$expectedDateRange
)->willReturn(new Paginator(new ArrayAdapter()));
/** @var JsonResponse $response */
@ -66,17 +69,44 @@ class ListShortUrlsActionTest extends TestCase
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 [[], 1, null, [], null, new DateRange()];
yield [['page' => 10], 10, null, [], null, new DateRange()];
yield [['page' => null], 1, null, [], null, new DateRange()];
yield [['page' => '8'], 8, null, [], null, new DateRange()];
yield [['searchTerm' => $searchTerm = 'foo'], 1, $searchTerm, [], null, new DateRange()];
yield [['tags' => $tags = ['foo','bar']], 1, null, $tags, null, new DateRange()];
yield [['orderBy' => $orderBy = 'something'], 1, null, [], $orderBy, new DateRange()];
yield [[
'page' => '2',
'orderBy' => $orderBy = 'something',
'tags' => $tags = ['one', 'two'],
], 2, null, $tags, $orderBy];
], 2, null, $tags, $orderBy, new DateRange()];
yield [
['startDate' => $date = Chronos::now()->toAtomString()],
1,
null,
[],
null,
new DateRange(Chronos::parse($date)),
];
yield [
['endDate' => $date = Chronos::now()->toAtomString()],
1,
null,
[],
null,
new DateRange(null, Chronos::parse($date)),
];
yield [
[
'startDate' => $startDate = Chronos::now()->subDays(10)->toAtomString(),
'endDate' => $endDate = Chronos::now()->toAtomString(),
],
1,
null,
[],
null,
new DateRange(Chronos::parse($startDate), Chronos::parse($endDate)),
];
}
}