ListShortUrlsActionTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ShlinkioTest\Shlink\Rest\Action\ShortUrl;
6
7
use Cake\Chronos\Chronos;
8
use Laminas\Diactoros\Response\JsonResponse;
9
use Laminas\Diactoros\ServerRequest;
10
use Laminas\Paginator\Adapter\ArrayAdapter;
11
use Laminas\Paginator\Paginator;
12
use PHPUnit\Framework\TestCase;
13
use Prophecy\PhpUnit\ProphecyTrait;
14
use Prophecy\Prophecy\ObjectProphecy;
15
use Shlinkio\Shlink\Core\Model\ShortUrlsParams;
16
use Shlinkio\Shlink\Core\Service\ShortUrlService;
17
use Shlinkio\Shlink\Rest\Action\ShortUrl\ListShortUrlsAction;
18
19
class ListShortUrlsActionTest extends TestCase
20
{
21
    use ProphecyTrait;
22
23
    private ListShortUrlsAction $action;
24
    private ObjectProphecy $service;
25
26
    public function setUp(): void
27
    {
28
        $this->service = $this->prophesize(ShortUrlService::class);
29
30
        $this->action = new ListShortUrlsAction($this->service->reveal(), [
31
            'hostname' => 'doma.in',
32
            'schema' => 'https',
33
        ]);
34
    }
35
36
    /**
37
     * @test
38
     * @dataProvider provideFilteringData
39
     */
40
    public function properListReturnsSuccessResponse(
41
        array $query,
42
        int $expectedPage,
43
        ?string $expectedSearchTerm,
44
        array $expectedTags,
45
        ?string $expectedOrderBy,
46
        ?string $startDate = null,
47
        ?string $endDate = null
48
    ): void {
49
        $listShortUrls = $this->service->listShortUrls(ShortUrlsParams::fromRawData([
50
            'page' => $expectedPage,
51
            'searchTerm' => $expectedSearchTerm,
52
            'tags' => $expectedTags,
53
            'orderBy' => $expectedOrderBy,
54
            'startDate' => $startDate,
55
            'endDate' => $endDate,
56
        ]))->willReturn(new Paginator(new ArrayAdapter()));
57
58
        /** @var JsonResponse $response */
59
        $response = $this->action->handle((new ServerRequest())->withQueryParams($query));
60
        $payload = $response->getPayload();
61
62
        self::assertArrayHasKey('shortUrls', $payload);
63
        self::assertArrayHasKey('data', $payload['shortUrls']);
64
        self::assertEquals([], $payload['shortUrls']['data']);
65
        self::assertEquals(200, $response->getStatusCode());
66
        $listShortUrls->shouldHaveBeenCalledOnce();
67
    }
68
69
    public function provideFilteringData(): iterable
70
    {
71
        yield [[], 1, null, [], null];
72
        yield [['page' => 10], 10, null, [], null];
73
        yield [['page' => null], 1, null, [], null];
74
        yield [['page' => '8'], 8, null, [], null];
75
        yield [['searchTerm' => $searchTerm = 'foo'], 1, $searchTerm, [], null];
76
        yield [['tags' => $tags = ['foo','bar']], 1, null, $tags, null];
77
        yield [['orderBy' => $orderBy = 'something'], 1, null, [], $orderBy];
78
        yield [[
79
            'page' => '2',
80
            'orderBy' => $orderBy = 'something',
81
            'tags' => $tags = ['one', 'two'],
82
        ], 2, null, $tags, $orderBy];
83
        yield [
84
            ['startDate' => $date = Chronos::now()->toAtomString()],
85
            1,
86
            null,
87
            [],
88
            null,
89
            $date,
90
        ];
91
        yield [
92
            ['endDate' => $date = Chronos::now()->toAtomString()],
93
            1,
94
            null,
95
            [],
96
            null,
97
            null,
98
            $date,
99
        ];
100
        yield [
101
            [
102
                'startDate' => $startDate = Chronos::now()->subDays(10)->toAtomString(),
103
                'endDate' => $endDate = Chronos::now()->toAtomString(),
104
            ],
105
            1,
106
            null,
107
            [],
108
            null,
109
            $startDate,
110
            $endDate,
111
        ];
112
    }
113
}
114