1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GacelaTest\Feature\Router; |
6
|
|
|
|
7
|
|
|
use Gacela\Router\Configure\Routes; |
8
|
|
|
use Gacela\Router\Entities\Request; |
9
|
|
|
use Gacela\Router\Exceptions\UnsupportedHttpMethodException; |
10
|
|
|
use Gacela\Router\Router; |
11
|
|
|
use GacelaTest\Feature\Router\Fixtures\FakeController; |
12
|
|
|
use Generator; |
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
|
15
|
|
|
final class RouterMatchTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
public function test_respond_when_everything_matches(): void |
18
|
|
|
{ |
19
|
|
|
$_SERVER['REQUEST_URI'] = 'https://example.org/expected/uri'; |
20
|
|
|
$_SERVER['REQUEST_METHOD'] = Request::METHOD_GET; |
21
|
|
|
|
22
|
|
|
$this->expectOutputString('Expected!'); |
23
|
|
|
|
24
|
|
|
$router = new Router(static function (Routes $routes): void { |
25
|
|
|
$routes->get('expected/uri', FakeController::class, 'basicAction'); |
26
|
|
|
}); |
27
|
|
|
$router->run(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function test_respond_only_the_first_match(): void |
31
|
|
|
{ |
32
|
|
|
$_SERVER['REQUEST_URI'] = 'https://example.org/expected/uri'; |
33
|
|
|
$_SERVER['REQUEST_METHOD'] = Request::METHOD_GET; |
34
|
|
|
|
35
|
|
|
$this->expectOutputString('Expected!'); |
36
|
|
|
|
37
|
|
|
$router = new Router(static function (Routes $routes): void { |
38
|
|
|
$routes->get('expected/uri', FakeController::class, 'basicAction'); |
39
|
|
|
$routes->get('expected/{param}', FakeController::class, 'stringParamAction'); |
40
|
|
|
}); |
41
|
|
|
$router->run(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function test_optional_argument(): void |
45
|
|
|
{ |
46
|
|
|
$_SERVER['REQUEST_URI'] = 'https://example.org/optional'; |
47
|
|
|
$_SERVER['REQUEST_METHOD'] = Request::METHOD_GET; |
48
|
|
|
|
49
|
|
|
$this->expectOutputString('Expected!'); |
50
|
|
|
|
51
|
|
|
$router = new Router(static function (Routes $routes): void { |
52
|
|
|
$routes->get('optional/{param?}', FakeController::class, 'basicAction'); |
53
|
|
|
}); |
54
|
|
|
$router->run(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function test_multiple_optional_argument(): void |
58
|
|
|
{ |
59
|
|
|
$_SERVER['REQUEST_URI'] = 'https://example.org/optional'; |
60
|
|
|
$_SERVER['REQUEST_METHOD'] = Request::METHOD_GET; |
61
|
|
|
|
62
|
|
|
$this->expectOutputString('Expected!'); |
63
|
|
|
|
64
|
|
|
$router = new Router(static function (Routes $routes): void { |
65
|
|
|
$routes->get('optional/{param1?}/{param2?}', FakeController::class, 'basicAction'); |
66
|
|
|
}); |
67
|
|
|
$router->run(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function test_mandatory_argument(): void |
71
|
|
|
{ |
72
|
|
|
$_SERVER['REQUEST_URI'] = 'https://example.org/mandatory/bob'; |
73
|
|
|
$_SERVER['REQUEST_METHOD'] = Request::METHOD_GET; |
74
|
|
|
|
75
|
|
|
$this->expectOutputString('Expected!'); |
76
|
|
|
|
77
|
|
|
$router = new Router(static function (Routes $routes): void { |
78
|
|
|
$routes->get('mandatory/{param1}', FakeController::class, 'basicAction'); |
79
|
|
|
}); |
80
|
|
|
$router->run(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function test_mandatory_and_optional_argument(): void |
84
|
|
|
{ |
85
|
|
|
$_SERVER['REQUEST_URI'] = 'https://example.org/mandatory_and_optional/bob'; |
86
|
|
|
$_SERVER['REQUEST_METHOD'] = Request::METHOD_GET; |
87
|
|
|
|
88
|
|
|
$this->expectOutputString('Expected!'); |
89
|
|
|
|
90
|
|
|
$router = new Router(static function (Routes $routes): void { |
91
|
|
|
$routes->get('mandatory_and_optional/{param1}/{param2?}', FakeController::class, 'basicAction'); |
92
|
|
|
}); |
93
|
|
|
$router->run(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function test_thrown_exception_when_method_does_not_exist(): void |
97
|
|
|
{ |
98
|
|
|
$this->expectException(UnsupportedHttpMethodException::class); |
99
|
|
|
|
100
|
|
|
$router = new Router(static function (Routes $routes): void { |
101
|
|
|
$routes->invalidName('', FakeController::class); |
|
|
|
|
102
|
|
|
}); |
103
|
|
|
$router->run(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @dataProvider anyHttpMethodProvider |
108
|
|
|
*/ |
109
|
|
|
public function test_any_http_method(string $httpMethod): void |
110
|
|
|
{ |
111
|
|
|
$_SERVER['REQUEST_URI'] = 'https://example.org/expected/uri'; |
112
|
|
|
$_SERVER['REQUEST_METHOD'] = $httpMethod; |
113
|
|
|
|
114
|
|
|
$this->expectOutputString('Expected!'); |
115
|
|
|
|
116
|
|
|
$router = new Router(static function (Routes $routes): void { |
117
|
|
|
$routes->any('expected/uri', FakeController::class, 'basicAction'); |
118
|
|
|
}); |
119
|
|
|
$router->run(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function anyHttpMethodProvider(): Generator |
123
|
|
|
{ |
124
|
|
|
yield ['METHOD_GET' => Request::METHOD_GET]; |
125
|
|
|
yield ['METHOD_CONNECT' => Request::METHOD_CONNECT]; |
126
|
|
|
yield ['METHOD_DELETE' => Request::METHOD_DELETE]; |
127
|
|
|
yield ['METHOD_HEAD' => Request::METHOD_HEAD]; |
128
|
|
|
yield ['METHOD_OPTIONS' => Request::METHOD_OPTIONS]; |
129
|
|
|
yield ['METHOD_PATCH' => Request::METHOD_PATCH]; |
130
|
|
|
yield ['METHOD_POST' => Request::METHOD_POST]; |
131
|
|
|
yield ['METHOD_PUT' => Request::METHOD_PUT]; |
132
|
|
|
yield ['METHOD_TRACE' => Request::METHOD_TRACE]; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @dataProvider matchesMethodsProvider |
137
|
|
|
*/ |
138
|
|
|
public function test_match_matches_all_its_methods(string $testMethod, array $givenMethods): void |
139
|
|
|
{ |
140
|
|
|
$_SERVER['REQUEST_URI'] = 'https://example.org/expected/uri'; |
141
|
|
|
$_SERVER['REQUEST_METHOD'] = $testMethod; |
142
|
|
|
|
143
|
|
|
$this->expectOutputString('Expected!'); |
144
|
|
|
|
145
|
|
|
$router = new Router(static function (Routes $routes) use ($givenMethods): void { |
146
|
|
|
$routes->match($givenMethods, 'expected/uri', FakeController::class, 'basicAction'); |
147
|
|
|
}); |
148
|
|
|
$router->run(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function matchesMethodsProvider(): Generator |
152
|
|
|
{ |
153
|
|
|
yield [Request::METHOD_GET, [Request::METHOD_GET, Request::METHOD_POST]]; |
154
|
|
|
yield [Request::METHOD_POST, [Request::METHOD_GET, Request::METHOD_POST]]; |
155
|
|
|
yield [Request::METHOD_PATCH, [Request::METHOD_PATCH, Request::METHOD_PUT]]; |
156
|
|
|
yield [Request::METHOD_PUT, [Request::METHOD_PATCH, Request::METHOD_PUT]]; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|