1
|
|
|
<?php |
2
|
|
|
namespace Mezon\Router\Tests; |
3
|
|
|
|
4
|
|
|
class DynamicRoutesInvalidCasesUnitTest extends \PHPUnit\Framework\TestCase |
5
|
|
|
{ |
6
|
|
|
|
7
|
|
|
const HELLO_WORLD = 'Hello world!'; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Function simply returns string. |
11
|
|
|
*/ |
12
|
|
|
public function helloWorldOutput(): string |
13
|
|
|
{ |
14
|
|
|
return DynamicRoutesInvalidCasesUnitTest::HELLO_WORLD; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Default setup |
19
|
|
|
* |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
* @see \PHPUnit\Framework\TestCase::setUp() |
22
|
|
|
*/ |
23
|
|
|
public function setUp(): void |
24
|
|
|
{ |
25
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Testing getParam for unexisting param |
30
|
|
|
*/ |
31
|
|
|
public function testGettingUnexistingParameter(): void |
32
|
|
|
{ |
33
|
|
|
// setup |
34
|
|
|
$router = new \Mezon\Router\Router(); |
35
|
|
|
$router->addRoute('/catalog/[i:foo]/', function () { |
36
|
|
|
// do nothing |
37
|
|
|
}); |
38
|
|
|
|
39
|
|
|
$router->callRoute('/catalog/1/'); |
40
|
|
|
|
41
|
|
|
$this->expectException(\Exception::class); |
42
|
|
|
|
43
|
|
|
// test body and assertions |
44
|
|
|
$router->getParam('unexisting'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Testing exception throwing for unexisting request method |
49
|
|
|
*/ |
50
|
|
|
public function testExceptionForUnexistingRequestMethod(): void |
51
|
|
|
{ |
52
|
|
|
// setup |
53
|
|
|
$_SERVER['REQUEST_METHOD'] = 'HEAD'; |
54
|
|
|
$router = new \Mezon\Router\Router(); |
55
|
|
|
$router->addRoute('/catalog/[i:foo]/', function () { |
56
|
|
|
// do nothing |
57
|
|
|
}); |
58
|
|
|
|
59
|
|
|
// assertions |
60
|
|
|
$this->expectException(\Exception::class); |
61
|
|
|
|
62
|
|
|
// test body |
63
|
|
|
$router->callRoute('/catalog/1/'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Testing invalid id list data types behaviour. |
68
|
|
|
*/ |
69
|
|
|
public function testInValidIdListParams(): void |
70
|
|
|
{ |
71
|
|
|
// setup |
72
|
|
|
$router = new \Mezon\Router\Router(); |
73
|
|
|
$router->addRoute('/catalog/[il:cat_id]/', [ |
74
|
|
|
$this, |
75
|
|
|
'helloWorldOutput' |
76
|
|
|
]); |
77
|
|
|
|
78
|
|
|
// assertion |
79
|
|
|
$this->expectException(\Exception::class); |
80
|
|
|
$this->expectExceptionMessage('The processor was not found for the route catalog/12345.'); |
81
|
|
|
|
82
|
|
|
// test body |
83
|
|
|
$router->callRoute('/catalog/12345./'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Testing dynamic routes for DELETE requests. |
88
|
|
|
*/ |
89
|
|
|
public function testDeleteRequestForUnExistingDynamicRoute(): void |
90
|
|
|
{ |
91
|
|
|
$_SERVER['REQUEST_METHOD'] = 'DELETE'; |
92
|
|
|
|
93
|
|
|
$exception = ''; |
94
|
|
|
$router = new \Mezon\Router\Router(); |
95
|
|
|
$router->addRoute('/catalog/[i:cat_id]', [ |
96
|
|
|
$this, |
97
|
|
|
'helloWorldOutput' |
98
|
|
|
]); |
99
|
|
|
|
100
|
|
|
try { |
101
|
|
|
$router->callRoute('/catalog/1025/'); |
102
|
|
|
} catch (\Exception $e) { |
103
|
|
|
$exception = $e->getMessage(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$msg = "The processor was not found for the route catalog/1025"; |
107
|
|
|
|
108
|
|
|
$this->assertNotFalse(strpos($exception, $msg)); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Testing dynamic routes for PUT requests. |
113
|
|
|
*/ |
114
|
|
|
public function testPutRequestForUnExistingDynamicRoute(): void |
115
|
|
|
{ |
116
|
|
|
$_SERVER['REQUEST_METHOD'] = 'PUT'; |
117
|
|
|
|
118
|
|
|
$exception = ''; |
119
|
|
|
$router = new \Mezon\Router\Router(); |
120
|
|
|
$router->addRoute('/catalog/[i:cat_id]', [ |
121
|
|
|
$this, |
122
|
|
|
'helloWorldOutput' |
123
|
|
|
]); |
124
|
|
|
|
125
|
|
|
try { |
126
|
|
|
$router->callRoute('/catalog/1024/'); |
127
|
|
|
} catch (\Exception $e) { |
128
|
|
|
$exception = $e->getMessage(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$msg = "The processor was not found for the route catalog/1024"; |
132
|
|
|
|
133
|
|
|
$this->assertNotFalse(strpos($exception, $msg)); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Testing dynamic routes for POST requests. |
138
|
|
|
*/ |
139
|
|
|
public function testPostRequestForUnExistingDynamicRoute(): void |
140
|
|
|
{ |
141
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST'; |
142
|
|
|
|
143
|
|
|
$exception = ''; |
144
|
|
|
$router = new \Mezon\Router\Router(); |
145
|
|
|
$router->addRoute('/catalog/[i:item_id]', [ |
146
|
|
|
$this, |
147
|
|
|
'helloWorldOutput' |
148
|
|
|
]); |
149
|
|
|
|
150
|
|
|
try { |
151
|
|
|
$router->callRoute('/catalog/1024/'); |
152
|
|
|
} catch (\Exception $e) { |
153
|
|
|
$exception = $e->getMessage(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$msg = "The processor was not found for the route catalog/1024"; |
157
|
|
|
|
158
|
|
|
$this->assertNotFalse(strpos($exception, $msg)); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Data provider for the testNotMatchingRoutes |
163
|
|
|
* |
164
|
|
|
* @return array testing data |
165
|
|
|
*/ |
166
|
|
|
public function invalidCasesDataProvider(): array |
167
|
|
|
{ |
168
|
|
|
return [ |
169
|
|
|
[ |
170
|
|
|
'/catalog/[i:some_id]', |
171
|
|
|
'/catalog/1/a/' |
172
|
|
|
], |
173
|
|
|
[ |
174
|
|
|
'/existing/[i:bar]/', |
175
|
|
|
'/unexisting/1/' |
176
|
|
|
] |
177
|
|
|
]; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Testing that all invalid cases will be covered |
182
|
|
|
* |
183
|
|
|
* @dataProvider invalidCasesDataProvider |
184
|
|
|
*/ |
185
|
|
|
public function testNotMatchingRoutes(string $route, string $calling): void |
186
|
|
|
{ |
187
|
|
|
// setup |
188
|
|
|
$router = new \Mezon\Router\Router(); |
189
|
|
|
$router->addRoute($route, function () { |
190
|
|
|
// do nothing |
191
|
|
|
}); |
192
|
|
|
|
193
|
|
|
// assertions |
194
|
|
|
$this->expectException(\Exception::class); |
195
|
|
|
|
196
|
|
|
// test body |
197
|
|
|
$router->callRoute($calling); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|