Passed
Pull Request — develop (#572)
by Alejandro
06:02
created

ListShortUrlsTest::buildPagination()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ShlinkioApiTest\Shlink\Rest\Action;
6
7
use Cake\Chronos\Chronos;
8
use GuzzleHttp\RequestOptions;
9
use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase;
10
11
use function count;
12
13
class ListShortUrlsTest extends ApiTestCase
14
{
15
    private const SHORT_URL_SHLINK = [
16
        'shortCode' => 'abc123',
17
        'shortUrl' => 'http://doma.in/abc123',
18
        'longUrl' => 'https://shlink.io',
19
        'dateCreated' => '2018-05-01T00:00:00+00:00',
20
        'visitsCount' => 3,
21
        'tags' => ['foo'],
22
        'meta' => [
23
            'validSince' => null,
24
            'validUntil' => null,
25
            'maxVisits' => null,
26
        ],
27
        'originalUrl' => 'https://shlink.io',
28
    ];
29
    private const SHORT_URL_CUSTOM_SLUG_AND_DOMAIN = [
30
        'shortCode' => 'custom-with-domain',
31
        'shortUrl' => 'http://some-domain.com/custom-with-domain',
32
        'longUrl' => 'https://google.com',
33
        'dateCreated' => '2018-10-20T00:00:00+00:00',
34
        'visitsCount' => 0,
35
        'tags' => [],
36
        'meta' => [
37
            'validSince' => null,
38
            'validUntil' => null,
39
            'maxVisits' => null,
40
        ],
41
        'originalUrl' => 'https://google.com',
42
    ];
43
    private const SHORT_URL_META = [
44
        'shortCode' => 'def456',
45
        'shortUrl' => 'http://doma.in/def456',
46
        'longUrl' =>
47
            'https://blog.alejandrocelaya.com/2017/12/09'
48
            . '/acmailer-7-0-the-most-important-release-in-a-long-time/',
49
        'dateCreated' => '2019-01-01T00:00:00+00:00',
50
        'visitsCount' => 2,
51
        'tags' => ['bar', 'foo'],
52
        'meta' => [
53
            'validSince' => '2020-05-01T00:00:00+00:00',
54
            'validUntil' => null,
55
            'maxVisits' => null,
56
        ],
57
        'originalUrl' =>
58
            'https://blog.alejandrocelaya.com/2017/12/09'
59
            . '/acmailer-7-0-the-most-important-release-in-a-long-time/',
60
    ];
61
    private const SHORT_URL_CUSTOM_SLUG = [
62
        'shortCode' => 'custom',
63
        'shortUrl' => 'http://doma.in/custom',
64
        'longUrl' => 'https://shlink.io',
65
        'dateCreated' => '2019-01-01T00:00:00+00:00',
66
        'visitsCount' => 0,
67
        'tags' => [],
68
        'meta' => [
69
            'validSince' => null,
70
            'validUntil' => null,
71
            'maxVisits' => 2,
72
        ],
73
        'originalUrl' => 'https://shlink.io',
74
    ];
75
    private const SHORT_URL_CUSTOM_DOMAIN = [
76
        'shortCode' => 'ghi789',
77
        'shortUrl' => 'http://example.com/ghi789',
78
        'longUrl' =>
79
            'https://blog.alejandrocelaya.com/2019/04/27'
80
            . '/considerations-to-properly-use-open-source-software-projects/',
81
        'dateCreated' => '2019-01-01T00:00:00+00:00',
82
        'visitsCount' => 0,
83
        'tags' => [],
84
        'meta' => [
85
            'validSince' => null,
86
            'validUntil' => null,
87
            'maxVisits' => null,
88
        ],
89
        'originalUrl' =>
90
            'https://blog.alejandrocelaya.com/2019/04/27'
91
            . '/considerations-to-properly-use-open-source-software-projects/',
92
    ];
93
94
    /**
95
     * @test
96
     * @dataProvider provideFilteredLists
97
     */
98
    public function shortUrlsAreProperlyListed(array $query, array $expectedShortUrls): void
99
    {
100
        $resp = $this->callApiWithKey(self::METHOD_GET, '/short-urls', [RequestOptions::QUERY => $query]);
101
        $respPayload = $this->getJsonResponsePayload($resp);
102
103
        $this->assertEquals(self::STATUS_OK, $resp->getStatusCode());
104
        $this->assertEquals([
105
            'shortUrls' => [
106
                'data' => $expectedShortUrls,
107
                'pagination' => $this->buildPagination(count($expectedShortUrls)),
108
            ],
109
        ], $respPayload);
110
    }
111
112
    public function provideFilteredLists(): iterable
113
    {
114
        yield [[], [
115
            self::SHORT_URL_SHLINK,
116
            self::SHORT_URL_CUSTOM_SLUG_AND_DOMAIN,
117
            self::SHORT_URL_META,
118
            self::SHORT_URL_CUSTOM_SLUG,
119
            self::SHORT_URL_CUSTOM_DOMAIN,
120
        ]];
121
        yield [['orderBy' => 'shortCode'], [
122
            self::SHORT_URL_SHLINK,
123
            self::SHORT_URL_CUSTOM_SLUG,
124
            self::SHORT_URL_CUSTOM_SLUG_AND_DOMAIN,
125
            self::SHORT_URL_META,
126
            self::SHORT_URL_CUSTOM_DOMAIN,
127
        ]];
128
        yield [['startDate' => Chronos::parse('2018-12-01')->toAtomString()], [
129
            self::SHORT_URL_META,
130
            self::SHORT_URL_CUSTOM_SLUG,
131
            self::SHORT_URL_CUSTOM_DOMAIN,
132
        ]];
133
        yield [['endDate' => Chronos::parse('2018-12-01')->toAtomString()], [
134
            self::SHORT_URL_SHLINK,
135
            self::SHORT_URL_CUSTOM_SLUG_AND_DOMAIN,
136
        ]];
137
        yield [['tags' => ['foo']], [
138
            self::SHORT_URL_SHLINK,
139
            self::SHORT_URL_META,
140
        ]];
141
        yield [['tags' => ['bar']], [
142
            self::SHORT_URL_META,
143
        ]];
144
        yield [['tags' => ['foo'], 'endDate' => Chronos::parse('2018-12-01')->toAtomString()], [
145
            self::SHORT_URL_SHLINK,
146
        ]];
147
        yield [['searchTerm' => 'alejandro'], [
148
            self::SHORT_URL_META,
149
            self::SHORT_URL_CUSTOM_DOMAIN,
150
        ]];
151
    }
152
153
    private function buildPagination(int $itemsCount): array
154
    {
155
        return [
156
            'currentPage' => 1,
157
            'pagesCount' => 1,
158
            'itemsPerPage' => 10,
159
            'itemsInCurrentPage' => $itemsCount,
160
            'totalItems' => $itemsCount,
161
        ];
162
    }
163
}
164