|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace GacelaTest\Unit\Router; |
|
6
|
|
|
|
|
7
|
|
|
use Gacela\Router\Route; |
|
8
|
|
|
use Gacela\Router\RouteEntity; |
|
9
|
|
|
use Generator; |
|
10
|
|
|
use PHPUnit\Framework\TestCase; |
|
11
|
|
|
|
|
12
|
|
|
final class RouteTest extends TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
private const PROVIDER_TRIES = 10; |
|
15
|
|
|
|
|
16
|
|
|
protected function tearDown(): void |
|
17
|
|
|
{ |
|
18
|
|
|
RouteEntity::reset(); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function test_it_should_respond_if_everything_matches(): void |
|
22
|
|
|
{ |
|
23
|
|
|
$_SERVER['REQUEST_URI'] = 'https://example.org/expected/uri'; |
|
24
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
|
25
|
|
|
|
|
26
|
|
|
$this->expectOutputString('Expected!'); |
|
27
|
|
|
|
|
28
|
|
|
Route::get('expected/uri', FakeController::class, 'basicAction'); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function test_it_should_not_respond_if_the_uri_does_not_matches(): void |
|
32
|
|
|
{ |
|
33
|
|
|
$_SERVER['REQUEST_URI'] = 'https://example.org/unexpected/uri'; |
|
34
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
|
35
|
|
|
|
|
36
|
|
|
$this->expectOutputString(''); |
|
37
|
|
|
|
|
38
|
|
|
Route::get('other/uri', FakeController::class, 'basicAction'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function test_it_should_not_respond_if_the_method_does_not_matches(): void |
|
42
|
|
|
{ |
|
43
|
|
|
$_SERVER['REQUEST_URI'] = 'https://example.org/expected/uri'; |
|
44
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
|
45
|
|
|
|
|
46
|
|
|
$this->expectOutputString(''); |
|
47
|
|
|
|
|
48
|
|
|
Route::post('expected/uri', FakeController::class, 'basicAction'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function test_it_should_pass_many_params_to_the_action(): void |
|
52
|
|
|
{ |
|
53
|
|
|
/** @var list<string> $params */ |
|
54
|
|
|
$params = ['foo','bar','baz']; |
|
55
|
|
|
|
|
56
|
|
|
$_SERVER['REQUEST_URI'] = "https://example.org/{$params[0]}/{$params[1]}/{$params[2]}"; |
|
57
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
|
58
|
|
|
|
|
59
|
|
|
$this->expectOutputString("The params are '{$params[0]}', '{$params[1]}' and '{$params[2]}'!"); |
|
60
|
|
|
|
|
61
|
|
|
Route::get('{firstParam}/{secondParam}/{thirdParam}', FakeController::class, 'manyParamsAction'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function test_it_should_pass_associated_params_by_name_to_the_action(): void |
|
65
|
|
|
{ |
|
66
|
|
|
/** @var list<string> $params */ |
|
67
|
|
|
$params = ['foo','bar','baz']; |
|
68
|
|
|
|
|
69
|
|
|
$_SERVER['REQUEST_URI'] = "https://example.org/{$params[0]}/{$params[1]}/{$params[2]}"; |
|
70
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
|
71
|
|
|
|
|
72
|
|
|
$this->expectOutputString("The params are '{$params[1]}', '{$params[0]}' and '{$params[2]}'!"); |
|
73
|
|
|
|
|
74
|
|
|
Route::get('{secondParam}/{firstParam}/{thirdParam}', FakeController::class, 'manyParamsAction'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @dataProvider stringProvider |
|
79
|
|
|
*/ |
|
80
|
|
|
public function test_it_should_pass_string_params_to_the_action(string $string): void |
|
81
|
|
|
{ |
|
82
|
|
|
$_SERVER['REQUEST_URI'] = "https://example.org/expected/string/is/{$string}"; |
|
83
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
|
84
|
|
|
|
|
85
|
|
|
$this->expectOutputString("The 'string' param is '{$string}'!"); |
|
86
|
|
|
|
|
87
|
|
|
Route::get('expected/string/is/{param}', FakeController::class, 'stringParamAction'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function stringProvider(): Generator |
|
91
|
|
|
{ |
|
92
|
|
|
for ($try = 0; $try < self::PROVIDER_TRIES; ++$try) { |
|
93
|
|
|
$randomString = (string)'word-' . mt_rand(); |
|
94
|
|
|
yield $randomString => ['string' => $randomString]; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @dataProvider intProvider |
|
100
|
|
|
*/ |
|
101
|
|
|
public function test_it_should_pass_int_params_to_the_action(string $int): void |
|
102
|
|
|
{ |
|
103
|
|
|
$_SERVER['REQUEST_URI'] = "https://example.org/expected/integer/is/{$int}"; |
|
104
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
|
105
|
|
|
|
|
106
|
|
|
$this->expectOutputString("The 'int' param is '{$int}'!"); |
|
107
|
|
|
|
|
108
|
|
|
Route::get('expected/integer/is/{param}', FakeController::class, 'intParamAction'); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function intProvider(): Generator |
|
112
|
|
|
{ |
|
113
|
|
|
for ($try = 0; $try < self::PROVIDER_TRIES; ++$try) { |
|
114
|
|
|
$randomInt = (string)random_int(1, 9999); |
|
115
|
|
|
yield "#{$randomInt}" => ['int' => $randomInt]; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @dataProvider floatProvider |
|
121
|
|
|
*/ |
|
122
|
|
|
public function test_it_should_pass_float_params_to_the_action(string $float): void |
|
123
|
|
|
{ |
|
124
|
|
|
$_SERVER['REQUEST_URI'] = "https://example.org/expected/float/is/{$float}"; |
|
125
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
|
126
|
|
|
|
|
127
|
|
|
$this->expectOutputString("The 'float' param is '{$float}'!"); |
|
128
|
|
|
|
|
129
|
|
|
Route::get('expected/float/is/{param}', FakeController::class, 'floatParamAction'); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function floatProvider(): Generator |
|
133
|
|
|
{ |
|
134
|
|
|
for ($try = 0; $try < self::PROVIDER_TRIES; ++$try) { |
|
135
|
|
|
$randomFloat = (string)mt_rand(); |
|
136
|
|
|
yield "#{$randomFloat}" => ['float' => $randomFloat]; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @dataProvider boolProvider |
|
142
|
|
|
*/ |
|
143
|
|
|
public function test_it_should_pass_bool_params_to_the_action(string $given, string $expected): void |
|
144
|
|
|
{ |
|
145
|
|
|
$_SERVER['REQUEST_URI'] = "https://example.org/expected/bool/is/{$given}"; |
|
146
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
|
147
|
|
|
|
|
148
|
|
|
$this->expectOutputString("The 'bool' param is '{$expected}'!"); |
|
149
|
|
|
|
|
150
|
|
|
Route::get('expected/bool/is/{param}', FakeController::class, 'boolParamAction'); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function boolProvider(): iterable |
|
154
|
|
|
{ |
|
155
|
|
|
yield 'true' => ['given' => 'true', 'expected' => 'true']; |
|
156
|
|
|
yield 'false' => ['given' => 'false', 'expected' => 'false']; |
|
157
|
|
|
yield '1' => ['given' => '1', 'expected' => 'true']; |
|
158
|
|
|
yield '0' => ['given' => '0', 'expected' => 'false']; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
public function test_it_should_respond_only_the_first_match(): void |
|
162
|
|
|
{ |
|
163
|
|
|
$_SERVER['REQUEST_URI'] = 'https://example.org/expected/uri'; |
|
164
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
|
165
|
|
|
|
|
166
|
|
|
$this->expectOutputString('Expected!'); |
|
167
|
|
|
|
|
168
|
|
|
Route::get('expected/uri', FakeController::class, 'basicAction'); |
|
169
|
|
|
Route::get('expected/{param}', FakeController::class, 'stringParamAction'); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|