|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace GacelaTest\Unit\Router; |
|
6
|
|
|
|
|
7
|
|
|
use Gacela\Router\Router; |
|
8
|
|
|
use Gacela\Router\Routes; |
|
9
|
|
|
use GacelaTest\Unit\Router\Fixtures\FakeController; |
|
10
|
|
|
use Generator; |
|
11
|
|
|
use PHPUnit\Framework\TestCase; |
|
12
|
|
|
|
|
13
|
|
|
final class RouterParamTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
private const PROVIDER_TRIES = 10; |
|
16
|
|
|
|
|
17
|
|
|
public function test_pass_many_params_to_the_action(): void |
|
18
|
|
|
{ |
|
19
|
|
|
$params = ['foo', 'bar', 'baz']; |
|
20
|
|
|
|
|
21
|
|
|
$_SERVER['REQUEST_URI'] = "https://example.org/{$params[0]}/{$params[1]}/{$params[2]}"; |
|
22
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
|
23
|
|
|
|
|
24
|
|
|
$this->expectOutputString("The params are '{$params[0]}', '{$params[1]}' and '{$params[2]}'!"); |
|
25
|
|
|
|
|
26
|
|
|
Router::configure(static function (Routes $routes): void { |
|
27
|
|
|
$routes->get('{firstParam}/{secondParam}/{thirdParam}', FakeController::class, 'manyParamsAction'); |
|
28
|
|
|
}); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function test_pass_associated_params_by_name_to_the_action(): void |
|
32
|
|
|
{ |
|
33
|
|
|
$params = ['foo', 'bar', 'baz']; |
|
34
|
|
|
|
|
35
|
|
|
$_SERVER['REQUEST_URI'] = "https://example.org/{$params[0]}/{$params[1]}/{$params[2]}"; |
|
36
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
|
37
|
|
|
|
|
38
|
|
|
$this->expectOutputString("The params are '{$params[1]}', '{$params[0]}' and '{$params[2]}'!"); |
|
39
|
|
|
|
|
40
|
|
|
Router::configure(static function (Routes $routes): void { |
|
41
|
|
|
$routes->get('{secondParam}/{firstParam}/{thirdParam}', FakeController::class, 'manyParamsAction'); |
|
42
|
|
|
}); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @dataProvider stringProvider |
|
47
|
|
|
*/ |
|
48
|
|
|
public function test_pass_string_params_to_the_action(string $string): void |
|
49
|
|
|
{ |
|
50
|
|
|
$_SERVER['REQUEST_URI'] = "https://example.org/expected/string/is/{$string}"; |
|
51
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
|
52
|
|
|
|
|
53
|
|
|
$this->expectOutputString("The 'string' param is '{$string}'!"); |
|
54
|
|
|
|
|
55
|
|
|
Router::configure(static function (Routes $routes): void { |
|
56
|
|
|
$routes->get('expected/string/is/{param}', FakeController::class, 'stringParamAction'); |
|
57
|
|
|
}); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function stringProvider(): Generator |
|
61
|
|
|
{ |
|
62
|
|
|
for ($try = 0; $try < self::PROVIDER_TRIES; ++$try) { |
|
63
|
|
|
$randomString = (string)'word-' . mt_rand(); |
|
64
|
|
|
yield $randomString => ['string' => $randomString]; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @dataProvider intProvider |
|
70
|
|
|
*/ |
|
71
|
|
|
public function test_pass_int_params_to_the_action(string $int): void |
|
72
|
|
|
{ |
|
73
|
|
|
$_SERVER['REQUEST_URI'] = "https://example.org/expected/integer/is/{$int}"; |
|
74
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
|
75
|
|
|
|
|
76
|
|
|
$this->expectOutputString("The 'int' param is '{$int}'!"); |
|
77
|
|
|
|
|
78
|
|
|
Router::configure(static function (Routes $routes): void { |
|
79
|
|
|
$routes->get('expected/integer/is/{param}', FakeController::class, 'intParamAction'); |
|
80
|
|
|
}); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function intProvider(): Generator |
|
84
|
|
|
{ |
|
85
|
|
|
for ($try = 0; $try < self::PROVIDER_TRIES; ++$try) { |
|
86
|
|
|
$randomInt = (string)random_int(1, 9999); |
|
87
|
|
|
yield "#{$randomInt}" => ['int' => $randomInt]; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @dataProvider floatProvider |
|
93
|
|
|
*/ |
|
94
|
|
|
public function test_pass_float_params_to_the_action(string $float): void |
|
95
|
|
|
{ |
|
96
|
|
|
$_SERVER['REQUEST_URI'] = "https://example.org/expected/float/is/{$float}"; |
|
97
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
|
98
|
|
|
|
|
99
|
|
|
$this->expectOutputString("The 'float' param is '{$float}'!"); |
|
100
|
|
|
|
|
101
|
|
|
Router::configure(static function (Routes $routes): void { |
|
102
|
|
|
$routes->get('expected/float/is/{param}', FakeController::class, 'floatParamAction'); |
|
103
|
|
|
}); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function floatProvider(): Generator |
|
107
|
|
|
{ |
|
108
|
|
|
for ($try = 0; $try < self::PROVIDER_TRIES; ++$try) { |
|
109
|
|
|
$randomFloat = (string)mt_rand(); |
|
110
|
|
|
yield "#{$randomFloat}" => ['float' => $randomFloat]; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @dataProvider boolProvider |
|
116
|
|
|
*/ |
|
117
|
|
|
public function test_pass_bool_params_to_the_action(string $given, string $expected): void |
|
118
|
|
|
{ |
|
119
|
|
|
$_SERVER['REQUEST_URI'] = "https://example.org/expected/bool/is/{$given}"; |
|
120
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
|
121
|
|
|
|
|
122
|
|
|
$this->expectOutputString("The 'bool' param is '{$expected}'!"); |
|
123
|
|
|
|
|
124
|
|
|
Router::configure(static function (Routes $routes): void { |
|
125
|
|
|
$routes->get('expected/bool/is/{param}', FakeController::class, 'boolParamAction'); |
|
126
|
|
|
}); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function boolProvider(): iterable |
|
130
|
|
|
{ |
|
131
|
|
|
yield 'true' => ['given' => 'true', 'expected' => 'true']; |
|
132
|
|
|
yield 'false' => ['given' => 'false', 'expected' => 'false']; |
|
133
|
|
|
yield '1' => ['given' => '1', 'expected' => 'true']; |
|
134
|
|
|
yield '0' => ['given' => '0', 'expected' => 'false']; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|