mirror of
https://github.com/shlinkio/shlink.git
synced 2025-03-29 04:52:54 +03:00
Improved ListShortUrlsActionTest covering different scenarios in which date ranges are provided
This commit is contained in:
parent
839ca31821
commit
8ad8b08aa4
2 changed files with 49 additions and 18 deletions
|
@ -58,19 +58,20 @@ class ListShortUrlsAction extends AbstractRestAction
|
||||||
*/
|
*/
|
||||||
private function queryToListParams(array $query): array
|
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 [
|
return [
|
||||||
(int) ($query['page'] ?? 1),
|
(int) ($query['page'] ?? 1),
|
||||||
$query['searchTerm'] ?? null,
|
$query['searchTerm'] ?? null,
|
||||||
$query['tags'] ?? [],
|
$query['tags'] ?? [],
|
||||||
$query['orderBy'] ?? null,
|
$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
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,9 +4,11 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace ShlinkioTest\Shlink\Rest\Action\ShortUrl;
|
namespace ShlinkioTest\Shlink\Rest\Action\ShortUrl;
|
||||||
|
|
||||||
|
use Cake\Chronos\Chronos;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Prophecy\Prophecy\ObjectProphecy;
|
use Prophecy\Prophecy\ObjectProphecy;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
|
use Shlinkio\Shlink\Common\Util\DateRange;
|
||||||
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\Response\JsonResponse;
|
||||||
|
@ -43,14 +45,15 @@ class ListShortUrlsActionTest extends TestCase
|
||||||
int $expectedPage,
|
int $expectedPage,
|
||||||
?string $expectedSearchTerm,
|
?string $expectedSearchTerm,
|
||||||
array $expectedTags,
|
array $expectedTags,
|
||||||
?string $expectedOrderBy
|
?string $expectedOrderBy,
|
||||||
|
DateRange $expectedDateRange
|
||||||
): void {
|
): void {
|
||||||
$listShortUrls = $this->service->listShortUrls(
|
$listShortUrls = $this->service->listShortUrls(
|
||||||
$expectedPage,
|
$expectedPage,
|
||||||
$expectedSearchTerm,
|
$expectedSearchTerm,
|
||||||
$expectedTags,
|
$expectedTags,
|
||||||
$expectedOrderBy,
|
$expectedOrderBy,
|
||||||
null
|
$expectedDateRange
|
||||||
)->willReturn(new Paginator(new ArrayAdapter()));
|
)->willReturn(new Paginator(new ArrayAdapter()));
|
||||||
|
|
||||||
/** @var JsonResponse $response */
|
/** @var JsonResponse $response */
|
||||||
|
@ -66,17 +69,44 @@ class ListShortUrlsActionTest extends TestCase
|
||||||
|
|
||||||
public function provideFilteringData(): iterable
|
public function provideFilteringData(): iterable
|
||||||
{
|
{
|
||||||
yield [[], 1, null, [], null];
|
yield [[], 1, null, [], null, new DateRange()];
|
||||||
yield [['page' => 10], 10, null, [], null];
|
yield [['page' => 10], 10, null, [], null, new DateRange()];
|
||||||
yield [['page' => null], 1, null, [], null];
|
yield [['page' => null], 1, null, [], null, new DateRange()];
|
||||||
yield [['page' => '8'], 8, null, [], null];
|
yield [['page' => '8'], 8, null, [], null, new DateRange()];
|
||||||
yield [['searchTerm' => $searchTerm = 'foo'], 1, $searchTerm, [], null];
|
yield [['searchTerm' => $searchTerm = 'foo'], 1, $searchTerm, [], null, new DateRange()];
|
||||||
yield [['tags' => $tags = ['foo','bar']], 1, null, $tags, null];
|
yield [['tags' => $tags = ['foo','bar']], 1, null, $tags, null, new DateRange()];
|
||||||
yield [['orderBy' => $orderBy = 'something'], 1, null, [], $orderBy];
|
yield [['orderBy' => $orderBy = 'something'], 1, null, [], $orderBy, new DateRange()];
|
||||||
yield [[
|
yield [[
|
||||||
'page' => '2',
|
'page' => '2',
|
||||||
'orderBy' => $orderBy = 'something',
|
'orderBy' => $orderBy = 'something',
|
||||||
'tags' => $tags = ['one', 'two'],
|
'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)),
|
||||||
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue