1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Application for testing purposes. |
5
|
|
|
*/ |
6
|
|
|
class TestApplication extends \Mezon\Application\Application |
7
|
|
|
{ |
8
|
|
|
|
9
|
|
|
function actionExisting() |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
/* existing action */ |
12
|
|
|
return 'OK!'; |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
function compound() |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
return 'compond'; |
18
|
|
|
} |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Application for testing purposes. |
23
|
|
|
*/ |
24
|
|
|
class TestApplication2 extends \Mezon\Application\Application |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
var $counter = 0; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Method loads routes from config file in *.php or *.json format |
31
|
|
|
* |
32
|
|
|
* @param string $configPath |
33
|
|
|
* Path of the config for routes |
34
|
|
|
*/ |
35
|
|
|
public function loadRoutesFromConfig( |
36
|
|
|
string $configPath = \Mezon\Application\CommonApplication::DEFAULT_PHP_ROUTES_PATH): void |
37
|
|
|
{ |
38
|
|
|
$this->counter ++; |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
class ApplicationUnitTest extends \PHPUnit\Framework\TestCase |
43
|
|
|
{ |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Running with correct router. |
47
|
|
|
*/ |
48
|
|
|
public function testCorrectRoute(): void |
49
|
|
|
{ |
50
|
|
|
$application = new TestApplication(); |
51
|
|
|
|
52
|
|
|
$_GET['r'] = '/existing/'; |
53
|
|
|
|
54
|
|
|
$this->expectOutputString('OK!'); |
55
|
|
|
|
56
|
|
|
$application->run(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Running with incorrect router. |
61
|
|
|
*/ |
62
|
|
|
public function testIncorrectRoute(): void |
63
|
|
|
{ |
64
|
|
|
$application = new TestApplication(); |
65
|
|
|
|
66
|
|
|
$_GET['r'] = '/unexisting/'; |
67
|
|
|
|
68
|
|
|
ob_start(); |
69
|
|
|
$application->run(); |
70
|
|
|
$output = ob_get_contents(); |
71
|
|
|
ob_end_clean(); |
72
|
|
|
|
73
|
|
|
$this->assertTrue( |
74
|
|
|
strpos($output, 'The processor was not found for the route') !== false, |
75
|
|
|
'Invalid behavior with incorrect route'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Test config structure validators. |
80
|
|
|
*/ |
81
|
|
|
public function testConfigValidatorsRoute(): void |
82
|
|
|
{ |
83
|
|
|
$application = new TestApplication(); |
84
|
|
|
|
85
|
|
|
$msg = ''; |
86
|
|
|
|
87
|
|
|
$this->expectException(Exception::class); |
88
|
|
|
$application->loadRoutesFromConfig(__DIR__ . '/TestInvalidRoutes1.php'); |
89
|
|
|
|
90
|
|
|
$this->assertEquals('Field "route" must be set', $msg, 'Invalid behavior for config validation'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Test config structure validators. |
95
|
|
|
*/ |
96
|
|
|
public function testConfigValidatorsCallback(): void |
97
|
|
|
{ |
98
|
|
|
$application = new TestApplication(); |
99
|
|
|
|
100
|
|
|
$msg = ''; |
101
|
|
|
|
102
|
|
|
try { |
103
|
|
|
$application->loadRoutesFromConfig(__DIR__ . '/TestInvalidRoutes2.php'); |
104
|
|
|
} catch (Exception $e) { |
105
|
|
|
$msg = $e->getMessage(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$this->assertEquals('Field "callback" must be set', $msg, 'Invalid behavior for callback'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Method constructs application with the default routes |
113
|
|
|
* |
114
|
|
|
* @return \Mezon\Application\Application |
115
|
|
|
*/ |
116
|
|
|
protected function getTestApplicationWithTestRoutes(): \Mezon\Application\Application |
117
|
|
|
{ |
118
|
|
|
$application = new TestApplication(); |
119
|
|
|
|
120
|
|
|
$application->loadRoutesFromConfig(__DIR__ . '/TestRoutes.php'); |
121
|
|
|
|
122
|
|
|
return $application; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Testing loading routes from config file. |
127
|
|
|
*/ |
128
|
|
|
public function testRoutesPhpConfig(): void |
129
|
|
|
{ |
130
|
|
|
// setup |
131
|
|
|
$application = $this->getTestApplicationWithTestRoutes(); |
132
|
|
|
|
133
|
|
|
$_GET['r'] = '/get-route/'; |
134
|
|
|
|
135
|
|
|
// assertions |
136
|
|
|
$this->expectOutputString('OK!'); |
137
|
|
|
|
138
|
|
|
// test body |
139
|
|
|
$application->run(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Testing loading routes from config file. |
144
|
|
|
*/ |
145
|
|
|
public function testRoutesJsonConfig(): void |
146
|
|
|
{ |
147
|
|
|
// setup |
148
|
|
|
$application = new TestApplication(); |
149
|
|
|
|
150
|
|
|
$application->loadRoutesFromConfig(__DIR__ . '/TestRoutes.json'); |
151
|
|
|
|
152
|
|
|
$_GET['r'] = '/get-route/'; |
153
|
|
|
|
154
|
|
|
// assertions |
155
|
|
|
$this->expectOutputString('OK!'); |
156
|
|
|
|
157
|
|
|
// test body |
158
|
|
|
$application->run(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Testing loading POST routes from config file. |
163
|
|
|
*/ |
164
|
|
|
public function testPostRoutesConfig(): void |
165
|
|
|
{ |
166
|
|
|
// setup |
167
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST'; |
168
|
|
|
|
169
|
|
|
$application = $this->getTestApplicationWithTestRoutes(); |
170
|
|
|
|
171
|
|
|
$_GET['r'] = '/post-route/'; |
172
|
|
|
|
173
|
|
|
// assertions |
174
|
|
|
$this->expectOutputString('OK!'); |
175
|
|
|
|
176
|
|
|
// test body |
177
|
|
|
$application->run(); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Trying to load unexisting config. |
182
|
|
|
*/ |
183
|
|
|
public function testLoadingFromUnexistingRoute(): void |
184
|
|
|
{ |
185
|
|
|
try { |
186
|
|
|
$application = new TestApplication(); |
187
|
|
|
|
188
|
|
|
$application->loadRoutesFromConfig('unexisting'); |
189
|
|
|
|
190
|
|
|
$this->assertEquals(true, false, 'Exception was not thrown'); |
191
|
|
|
} catch (Exception $e) { |
192
|
|
|
$this->assertEquals(true, true, 'OK'); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Method returns mocko bject of the application. |
198
|
|
|
*/ |
199
|
|
|
protected function getMock(): object |
200
|
|
|
{ |
201
|
|
|
return $this->getMockBuilder(\Mezon\Application\Application::class) |
|
|
|
|
202
|
|
|
->disableOriginalConstructor() |
203
|
|
|
->setMethods([ |
204
|
|
|
'handleException' |
205
|
|
|
]) |
206
|
|
|
->getMock(); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Trying to load unexisting config. |
211
|
|
|
*/ |
212
|
|
|
public function testUnexistingRouter(): void |
213
|
|
|
{ |
214
|
|
|
$this->expectException(Exception::class); |
215
|
|
|
|
216
|
|
|
$application = $this->getMock(); |
217
|
|
|
$application->method('handleException')->willThrowException(new Exception()); |
218
|
|
|
|
219
|
|
|
$application->run(); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Testing call of the method added onthe fly. |
224
|
|
|
*/ |
225
|
|
|
public function testOnTheFlyMethod(): void |
226
|
|
|
{ |
227
|
|
|
$application = new \Mezon\Application\Application(); |
228
|
|
|
|
229
|
|
|
$application->fly = function () { |
|
|
|
|
230
|
|
|
return 'OK!'; |
231
|
|
|
}; |
232
|
|
|
|
233
|
|
|
$application->loadRoute([ |
234
|
|
|
'route' => '/fly-route/', |
235
|
|
|
'callback' => 'fly' |
236
|
|
|
]); |
237
|
|
|
|
238
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
239
|
|
|
$_GET['r'] = '/fly-route/'; |
240
|
|
|
|
241
|
|
|
$this->expectOutputString('OK!'); |
242
|
|
|
|
243
|
|
|
$application->run(); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Testing call of the method added in runtime |
248
|
|
|
*/ |
249
|
|
|
public function testOnTheFlyUnexistingMethod(): void |
250
|
|
|
{ |
251
|
|
|
// setup |
252
|
|
|
$application = new \Mezon\Application\Application(); |
253
|
|
|
|
254
|
|
|
$application->fly = function () { |
|
|
|
|
255
|
|
|
return 'OK!'; |
256
|
|
|
}; |
257
|
|
|
|
258
|
|
|
// test body |
259
|
|
|
$application->fly(); |
|
|
|
|
260
|
|
|
|
261
|
|
|
// assertions |
262
|
|
|
$this->addToAssertionCount(1); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Testing unexisting method call |
267
|
|
|
*/ |
268
|
|
|
public function testUnexistingMethodCall(): void |
269
|
|
|
{ |
270
|
|
|
// setup |
271
|
|
|
$application = new \Mezon\Application\Application(); |
272
|
|
|
|
273
|
|
|
// assertions |
274
|
|
|
$this->expectException(\Exception::class); |
275
|
|
|
|
276
|
|
|
// test body |
277
|
|
|
$application->unexistingMethod(); |
|
|
|
|
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Testing loading configs from multyple files |
282
|
|
|
*/ |
283
|
|
|
public function testLoadRoutesFromConfigs(): void |
284
|
|
|
{ |
285
|
|
|
// setup |
286
|
|
|
$application = new TestApplication(); |
287
|
|
|
|
288
|
|
|
// test body |
289
|
|
|
$application->loadRoutesFromConfigs([ |
290
|
|
|
__DIR__ . '/TestRoutes.php', |
291
|
|
|
__DIR__ . '/TestRoutes.json' |
292
|
|
|
]); |
293
|
|
|
|
294
|
|
|
// assertions |
295
|
|
|
$this->assertTrue($application->routeExists('/php-route/')); |
296
|
|
|
$this->assertTrue($application->routeExists('/json-route/')); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Testing method |
301
|
|
|
*/ |
302
|
|
|
public function testLoadingDefaultConfigs(): void |
303
|
|
|
{ |
304
|
|
|
// setup and test body |
305
|
|
|
$application = new TestApplication2(); |
306
|
|
|
|
307
|
|
|
// assertions |
308
|
|
|
$this->assertEquals(2, $application->counter); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Testing compound routes ['callback'=>[object, function-name]] |
313
|
|
|
*/ |
314
|
|
|
public function testCompondCallback(): void |
315
|
|
|
{ |
316
|
|
|
// setup and test body |
317
|
|
|
$application = new TestApplication(); |
318
|
|
|
$application->loadRoute([ |
319
|
|
|
'callback' => [ |
320
|
|
|
$this, |
321
|
|
|
'compound' |
322
|
|
|
], |
323
|
|
|
'route' => '/compound/' |
324
|
|
|
]); |
325
|
|
|
|
326
|
|
|
// assertions |
327
|
|
|
$this->assertTrue($application->routeExists('/compound/')); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* Testing loadRoutesFromDirectory method |
332
|
|
|
*/ |
333
|
|
|
public function testLoadRoutesFromDirectory(): void |
334
|
|
|
{ |
335
|
|
|
// setup |
336
|
|
|
$application = new TestApplication(); |
337
|
|
|
|
338
|
|
|
// test body |
339
|
|
|
$application->loadRoutesFromDirectory(__DIR__ . '/../conf'); |
340
|
|
|
|
341
|
|
|
// assertions |
342
|
|
|
$this->assertTrue($application->routeExists('/php-route/')); |
343
|
|
|
$this->assertTrue($application->routeExists('/json-route/')); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Testing method getRequestParamsFetcher |
348
|
|
|
*/ |
349
|
|
|
public function testGetRequestParamsFetcher(): void |
350
|
|
|
{ |
351
|
|
|
// setup |
352
|
|
|
$application = new \Mezon\Application\Application(); |
353
|
|
|
|
354
|
|
|
// test body |
355
|
|
|
$requestParams = $application->getRequestParamsFetcher(); |
356
|
|
|
|
357
|
|
|
// assertions |
358
|
|
|
$this->assertInstanceOf(\Mezon\Transport\HttpRequestParams::class, $requestParams); |
359
|
|
|
} |
360
|
|
|
} |
361
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.