|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace ShlinkioApiTest\Shlink\Rest\Action; |
|
5
|
|
|
|
|
6
|
|
|
use Cake\Chronos\Chronos; |
|
7
|
|
|
use GuzzleHttp\RequestOptions; |
|
8
|
|
|
use ShlinkioTest\Shlink\Common\ApiTest\ApiTestCase; |
|
9
|
|
|
|
|
10
|
|
|
class CreateShortUrlActionTest extends ApiTestCase |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @test |
|
14
|
|
|
*/ |
|
15
|
|
|
public function createsNewShortUrlWhenOnlyLongUrlIsProvided() |
|
16
|
|
|
{ |
|
17
|
|
|
$expectedKeys = ['shortCode', 'shortUrl', 'longUrl', 'dateCreated', 'visitsCount', 'tags']; |
|
18
|
|
|
[$statusCode, $payload] = $this->createShortUrl(); |
|
19
|
|
|
|
|
20
|
|
|
$this->assertEquals(self::STATUS_OK, $statusCode); |
|
21
|
|
|
foreach ($expectedKeys as $key) { |
|
22
|
|
|
$this->assertArrayHasKey($key, $payload); |
|
23
|
|
|
} |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @test |
|
28
|
|
|
*/ |
|
29
|
|
|
public function createsNewShortUrlWithCustomSlug() |
|
30
|
|
|
{ |
|
31
|
|
|
[$statusCode, $payload] = $this->createShortUrl(['customSlug' => 'my cool slug']); |
|
32
|
|
|
|
|
33
|
|
|
$this->assertEquals(self::STATUS_OK, $statusCode); |
|
34
|
|
|
$this->assertEquals('my-cool-slug', $payload['shortCode']); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @test |
|
39
|
|
|
*/ |
|
40
|
|
|
public function createsNewShortUrlWithTags() |
|
41
|
|
|
{ |
|
42
|
|
|
[$statusCode, $payload] = $this->createShortUrl(['tags' => ['foo', 'bar', 'baz']]); |
|
43
|
|
|
|
|
44
|
|
|
$this->assertEquals(self::STATUS_OK, $statusCode); |
|
45
|
|
|
$this->assertEquals(['foo', 'bar', 'baz'], $payload['tags']); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @test |
|
50
|
|
|
* @dataProvider provideMaxVisits |
|
51
|
|
|
*/ |
|
52
|
|
|
public function createsNewShortUrlWithVisitsLimit(int $maxVisits) |
|
53
|
|
|
{ |
|
54
|
|
|
[$statusCode, ['shortCode' => $shortCode]] = $this->createShortUrl(['maxVisits' => $maxVisits]); |
|
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
$this->assertEquals(self::STATUS_OK, $statusCode); |
|
57
|
|
|
|
|
58
|
|
|
// Last request to the short URL will return a 404, and the rest, a 302 |
|
59
|
|
|
for ($i = 0; $i < $maxVisits; $i++) { |
|
60
|
|
|
$this->assertEquals(self::STATUS_FOUND, $this->callShortUrl($shortCode)->getStatusCode()); |
|
61
|
|
|
} |
|
62
|
|
|
$lastResp = $this->callShortUrl($shortCode); |
|
63
|
|
|
$this->assertEquals(self::STATUS_NOT_FOUND, $lastResp->getStatusCode()); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function provideMaxVisits(): array |
|
67
|
|
|
{ |
|
68
|
|
|
return [ |
|
69
|
|
|
[1], |
|
70
|
|
|
[5], |
|
71
|
|
|
[3], |
|
72
|
|
|
]; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @test |
|
77
|
|
|
*/ |
|
78
|
|
|
public function createsShortUrlWithValidSince() |
|
79
|
|
|
{ |
|
80
|
|
|
[$statusCode, ['shortCode' => $shortCode]] = $this->createShortUrl([ |
|
|
|
|
|
|
81
|
|
|
'validSince' => Chronos::now()->addDay()->toAtomString(), |
|
82
|
|
|
]); |
|
83
|
|
|
|
|
84
|
|
|
$this->assertEquals(self::STATUS_OK, $statusCode); |
|
85
|
|
|
|
|
86
|
|
|
// Request to the short URL will return a 404 since ist' not valid yet |
|
87
|
|
|
$lastResp = $this->callShortUrl($shortCode); |
|
88
|
|
|
$this->assertEquals(self::STATUS_NOT_FOUND, $lastResp->getStatusCode()); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @test |
|
93
|
|
|
*/ |
|
94
|
|
|
public function createsShortUrlWithValidUntil() |
|
95
|
|
|
{ |
|
96
|
|
|
[$statusCode, ['shortCode' => $shortCode]] = $this->createShortUrl([ |
|
|
|
|
|
|
97
|
|
|
'validUntil' => Chronos::now()->subDay()->toAtomString(), |
|
98
|
|
|
]); |
|
99
|
|
|
|
|
100
|
|
|
$this->assertEquals(self::STATUS_OK, $statusCode); |
|
101
|
|
|
|
|
102
|
|
|
// Request to the short URL will return a 404 since it's no longer valid |
|
103
|
|
|
$lastResp = $this->callShortUrl($shortCode); |
|
104
|
|
|
$this->assertEquals(self::STATUS_NOT_FOUND, $lastResp->getStatusCode()); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return array { |
|
109
|
|
|
* @var int $statusCode |
|
110
|
|
|
* @var array $payload |
|
111
|
|
|
* } |
|
112
|
|
|
*/ |
|
113
|
|
|
private function createShortUrl(array $body = []): array |
|
114
|
|
|
{ |
|
115
|
|
|
$body['longUrl'] = 'https://app.shlink.io'; |
|
116
|
|
|
$resp = $this->callApiWithKey(self::METHOD_POST, '/short-urls', [RequestOptions::JSON => $body]); |
|
117
|
|
|
$payload = $this->getJsonResponsePayload($resp); |
|
118
|
|
|
|
|
119
|
|
|
return [$resp->getStatusCode(), $payload]; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|