Completed
Push — master ( 04f83a...b71153 )
by Alex
09:15
created

ApplicationUnitTest::testBuildRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
use Mezon\Application\Application;
4
5
/**
6
 * Application for testing purposes.
7
 */
8
class TestApplication extends \Mezon\Application\Application
9
{
10
11
    function actionExisting()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
12
    {
13
        /* existing action */
14
        return 'OK!';
15
    }
16
17
    function compound()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
18
    {
19
        return 'compond';
20
    }
21
}
22
23
/**
24
 * Application for testing purposes.
25
 */
26
class TestApplication2 extends \Mezon\Application\Application
27
{
28
29
    var $counter = 0;
30
31
    /**
32
     * Method loads routes from config file in *.php or *.json format
33
     *
34
     * @param string $configPath
35
     *            Path of the config for routes
36
     */
37
    public function loadRoutesFromConfig(string $configPath): void
38
    {
39
        $this->counter ++;
40
    }
41
}
42
43
class ApplicationUnitTest extends \PHPUnit\Framework\TestCase
44
{
45
46
    /**
47
     * Running with correct router.
48
     */
49
    public function testCorrectRoute(): void
50
    {
51
        $application = new TestApplication();
52
53
        $_GET['r'] = '/existing/';
54
55
        $this->expectOutputString('OK!');
56
57
        $application->run();
58
    }
59
60
    /**
61
     * Running with incorrect router.
62
     */
63
    public function testIncorrectRoute(): void
64
    {
65
        $application = new TestApplication();
66
67
        $_GET['r'] = '/unexisting/';
68
69
        ob_start();
70
        $application->run();
71
        $output = ob_get_contents();
72
        ob_end_clean();
73
74
        $this->assertTrue(
75
            strpos($output, 'The processor was not found for the route') !== false,
76
            'Invalid behavior with incorrect route');
77
    }
78
79
    /**
80
     * Test config structure validators.
81
     */
82
    public function testConfigValidatorsRoute(): void
83
    {
84
        $application = new TestApplication();
85
86
        $msg = '';
87
88
        $this->expectException(Exception::class);
89
        $application->loadRoutesFromConfig(__DIR__ . '/TestInvalidRoutes1.php');
90
91
        $this->assertEquals('Field "route" must be set', $msg, 'Invalid behavior for config validation');
92
    }
93
94
    /**
95
     * Test config structure validators.
96
     */
97
    public function testConfigValidatorsCallback(): void
98
    {
99
        $application = new TestApplication();
100
101
        $msg = '';
102
103
        try {
104
            $application->loadRoutesFromConfig(__DIR__ . '/TestInvalidRoutes2.php');
105
        } catch (Exception $e) {
106
            $msg = $e->getMessage();
107
        }
108
109
        $this->assertEquals('Field "callback" must be set', $msg, 'Invalid behavior for callback');
110
    }
111
112
    /**
113
     * Method constructs application with the default routes
114
     *
115
     * @return \Mezon\Application\Application
116
     */
117
    protected function getTestApplicationWithTestRoutes(): \Mezon\Application\Application
118
    {
119
        $application = new TestApplication();
120
121
        $application->loadRoutesFromConfig(__DIR__ . '/TestRoutes.php');
122
123
        return $application;
124
    }
125
126
    /**
127
     * Testing loading routes from config file.
128
     */
129
    public function testRoutesPhpConfig(): void
130
    {
131
        // setup
132
        $application = $this->getTestApplicationWithTestRoutes();
133
134
        $_GET['r'] = '/get-route/';
135
136
        // assertions
137
        $this->expectOutputString('OK!');
138
139
        // test body
140
        $application->run();
141
    }
142
143
    /**
144
     * Testing loading routes from config file.
145
     */
146
    public function testRoutesJsonConfig(): void
147
    {
148
        // setup
149
        $application = new TestApplication();
150
151
        $application->loadRoutesFromConfig(__DIR__ . '/TestRoutes.json');
152
153
        $_GET['r'] = '/get-route/';
154
155
        // assertions
156
        $this->expectOutputString('OK!');
157
158
        // test body
159
        $application->run();
160
    }
161
162
    /**
163
     * Testing loading POST routes from config file.
164
     */
165
    public function testPostRoutesConfig(): void
166
    {
167
        // setup
168
        $_SERVER['REQUEST_METHOD'] = 'POST';
169
170
        $application = $this->getTestApplicationWithTestRoutes();
171
172
        $_GET['r'] = '/post-route/';
173
174
        // assertions
175
        $this->expectOutputString('OK!');
176
177
        // test body
178
        $application->run();
179
    }
180
181
    /**
182
     * Trying to load unexisting config.
183
     */
184
    public function testLoadingFromUnexistingRoute(): void
185
    {
186
        try {
187
            $application = new TestApplication();
188
189
            $application->loadRoutesFromConfig('unexisting');
190
191
            $this->assertEquals(true, false, 'Exception was not thrown');
192
        } catch (Exception $e) {
193
            $this->assertEquals(true, true, 'OK');
194
        }
195
    }
196
197
    /**
198
     * Method returns mocko bject of the application.
199
     */
200
    protected function getMock(): object
201
    {
202
        return $this->getMockBuilder(\Mezon\Application\Application::class)
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated: https://github.com/sebastianbergmann/phpunit/pull/3687 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

202
        return /** @scrutinizer ignore-deprecated */ $this->getMockBuilder(\Mezon\Application\Application::class)

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.

Loading history...
203
            ->disableOriginalConstructor()
204
            ->setMethods([
205
            'handleException'
206
        ])
207
            ->getMock();
208
    }
209
210
    /**
211
     * Trying to load unexisting config.
212
     */
213
    public function testUnexistingRouter(): void
214
    {
215
        $this->expectException(Exception::class);
216
217
        $application = $this->getMock();
218
        $application->method('handleException')->willThrowException(new Exception());
219
220
        $application->run();
221
    }
222
223
    /**
224
     * Testing call of the method added onthe fly.
225
     */
226
    public function testOnTheFlyMethod(): void
227
    {
228
        $application = new \Mezon\Application\Application();
229
230
        $application->fly = function () {
0 ignored issues
show
Bug introduced by
The property fly does not seem to exist on Mezon\Application\Application.
Loading history...
231
            return 'OK!';
232
        };
233
234
        $application->loadRoute([
235
            'route' => '/fly-route/',
236
            'callback' => 'fly'
237
        ]);
238
239
        $_SERVER['REQUEST_METHOD'] = 'GET';
240
        $_GET['r'] = '/fly-route/';
241
242
        $this->expectOutputString('OK!');
243
244
        $application->run();
245
    }
246
247
    /**
248
     * Testing call of the method added in runtime
249
     */
250
    public function testOnTheFlyUnexistingMethod(): void
251
    {
252
        // setup
253
        $application = new \Mezon\Application\Application();
254
255
        $application->fly = function () {
0 ignored issues
show
Bug introduced by
The property fly does not seem to exist on Mezon\Application\Application.
Loading history...
256
            return 'OK!';
257
        };
258
259
        // test body
260
        $application->fly();
0 ignored issues
show
Bug introduced by
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 ignore-call  annotation

260
        $application->/** @scrutinizer ignore-call */ 
261
                      fly();
Loading history...
261
262
        // assertions
263
        $this->addToAssertionCount(1);
264
    }
265
266
    /**
267
     * Testing unexisting method call
268
     */
269
    public function testUnexistingMethodCall(): void
270
    {
271
        // setup
272
        $application = new \Mezon\Application\Application();
273
274
        // assertions
275
        $this->expectException(\Exception::class);
276
277
        // test body
278
        $application->unexistingMethod();
0 ignored issues
show
Bug introduced by
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 ignore-call  annotation

278
        $application->/** @scrutinizer ignore-call */ 
279
                      unexistingMethod();
Loading history...
279
    }
280
281
    /**
282
     * Testing loading configs from multyple files
283
     */
284
    public function testLoadRoutesFromConfigs(): void
285
    {
286
        // setup
287
        $application = new TestApplication();
288
289
        // test body
290
        $application->loadRoutesFromConfigs([
291
            __DIR__ . '/TestRoutes.php',
292
            __DIR__ . '/TestRoutes.json'
293
        ]);
294
295
        // assertions
296
        $this->assertTrue($application->routeExists('/php-route/'));
297
        $this->assertTrue($application->routeExists('/json-route/'));
298
    }
299
300
    /**
301
     * Testing method
302
     */
303
    public function testLoadingDefaultConfigs(): void
304
    {
305
        // setup and test body
306
        $application = new TestApplication2();
307
308
        // assertions
309
        $this->assertEquals(2, $application->counter);
310
    }
311
312
    /**
313
     * Testing compound routes ['callback'=>[object, function-name]]
314
     */
315
    public function testCompondCallback(): void
316
    {
317
        // setup and test body
318
        $application = new TestApplication();
319
        $application->loadRoute([
320
            'callback' => [
321
                $this,
322
                'compound'
323
            ],
324
            'route' => '/compound/'
325
        ]);
326
327
        // assertions
328
        $this->assertTrue($application->routeExists('/compound/'));
329
    }
330
331
    /**
332
     * Testing loadRoutesFromDirectory method
333
     */
334
    public function testLoadRoutesFromDirectory(): void
335
    {
336
        // setup
337
        $application = new TestApplication();
338
339
        // test body
340
        $application->loadRoutesFromDirectory(__DIR__ . '/conf');
341
342
        // assertions
343
        $this->assertTrue($application->routeExists('/php-route/'));
344
        $this->assertTrue($application->routeExists('/json-route/'));
345
    }
346
347
    /**
348
     * Testing method getRequestParamsFetcher
349
     */
350
    public function testGetRequestParamsFetcher(): void
351
    {
352
        // setup
353
        $application = new \Mezon\Application\Application();
354
355
        // test body
356
        $requestParams = $application->getRequestParamsFetcher();
357
358
        // assertions
359
        $this->assertInstanceOf(\Mezon\Transport\HttpRequestParams::class, $requestParams);
360
    }
361
362
    /**
363
     * Testing method
364
     */
365
    public function testBuildRoute() : void
366
    {
367
        // test body
368
        $result = Application::buildRoute('/route/', 'GET', $this, 'func');
369
370
        // assertions
371
        $this->assertEquals('/route/', $result['route']);
372
        $this->assertEquals('GET', $result['method']);
373
        $this->assertInstanceOf(ApplicationUnitTest::class, $result['callback'][0]);
374
        $this->assertEquals('func', $result['callback'][1]);
375
    }
376
}
377