1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GacelaTest\Feature\Router; |
6
|
|
|
|
7
|
|
|
use Gacela\Router\Entities\Request; |
8
|
|
|
use Gacela\Router\Exceptions\UnsupportedHttpMethodException; |
9
|
|
|
use Gacela\Router\Router; |
10
|
|
|
use Gacela\Router\Routes; |
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_and_optional_argument(): void |
71
|
|
|
{ |
72
|
|
|
$_SERVER['REQUEST_URI'] = 'https://example.org/optional/bob'; |
73
|
|
|
$_SERVER['REQUEST_METHOD'] = Request::METHOD_GET; |
74
|
|
|
|
75
|
|
|
$this->expectOutputString("The 'string' param is 'bob'!"); |
76
|
|
|
|
77
|
|
|
$router = new Router(static function (Routes $routes): void { |
78
|
|
|
$routes->get('optional/{param}/{param?}', FakeController::class, 'stringParamAction'); |
79
|
|
|
}); |
80
|
|
|
$router->run(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function test_thrown_exception_when_method_does_not_exist(): void |
84
|
|
|
{ |
85
|
|
|
$this->expectException(UnsupportedHttpMethodException::class); |
86
|
|
|
|
87
|
|
|
$router = new Router(static function (Routes $routes): void { |
88
|
|
|
$routes->invalidName('', FakeController::class); |
|
|
|
|
89
|
|
|
}); |
90
|
|
|
$router->run(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @dataProvider anyHttpMethodProvider |
95
|
|
|
*/ |
96
|
|
|
public function test_any_http_method(string $httpMethod): void |
97
|
|
|
{ |
98
|
|
|
$_SERVER['REQUEST_URI'] = 'https://example.org/expected/uri'; |
99
|
|
|
$_SERVER['REQUEST_METHOD'] = $httpMethod; |
100
|
|
|
|
101
|
|
|
$this->expectOutputString('Expected!'); |
102
|
|
|
|
103
|
|
|
$router = new Router(static function (Routes $routes): void { |
104
|
|
|
$routes->any('expected/uri', FakeController::class, 'basicAction'); |
105
|
|
|
}); |
106
|
|
|
$router->run(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function anyHttpMethodProvider(): Generator |
110
|
|
|
{ |
111
|
|
|
yield ['METHOD_GET' => Request::METHOD_GET]; |
112
|
|
|
yield ['METHOD_CONNECT' => Request::METHOD_CONNECT]; |
113
|
|
|
yield ['METHOD_DELETE' => Request::METHOD_DELETE]; |
114
|
|
|
yield ['METHOD_HEAD' => Request::METHOD_HEAD]; |
115
|
|
|
yield ['METHOD_OPTIONS' => Request::METHOD_OPTIONS]; |
116
|
|
|
yield ['METHOD_PATCH' => Request::METHOD_PATCH]; |
117
|
|
|
yield ['METHOD_POST' => Request::METHOD_POST]; |
118
|
|
|
yield ['METHOD_PUT' => Request::METHOD_PUT]; |
119
|
|
|
yield ['METHOD_TRACE' => Request::METHOD_TRACE]; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @dataProvider matchesMethodsProvider |
124
|
|
|
*/ |
125
|
|
|
public function test_match_matches_all_its_methods(string $testMethod, array $givenMethods): void |
126
|
|
|
{ |
127
|
|
|
$_SERVER['REQUEST_URI'] = 'https://example.org/expected/uri'; |
128
|
|
|
$_SERVER['REQUEST_METHOD'] = $testMethod; |
129
|
|
|
|
130
|
|
|
$this->expectOutputString('Expected!'); |
131
|
|
|
|
132
|
|
|
$router = new Router(static function (Routes $routes) use ($givenMethods): void { |
133
|
|
|
$routes->match($givenMethods, 'expected/uri', FakeController::class, 'basicAction'); |
134
|
|
|
}); |
135
|
|
|
$router->run(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function matchesMethodsProvider(): Generator |
139
|
|
|
{ |
140
|
|
|
yield [Request::METHOD_GET, [Request::METHOD_GET, Request::METHOD_POST]]; |
141
|
|
|
yield [Request::METHOD_POST, [Request::METHOD_GET, Request::METHOD_POST]]; |
142
|
|
|
yield [Request::METHOD_PATCH, [Request::METHOD_PATCH, Request::METHOD_PUT]]; |
143
|
|
|
yield [Request::METHOD_PUT, [Request::METHOD_PATCH, Request::METHOD_PUT]]; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|