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