Passed
Push — master ( 5b5ed9...47b84a )
by Alex
02:17
created

ApplicationUnitTest::testGetRequestParamsFetcher()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 10
rs 10
1
<?php
2
3
/**
4
 * Application for testing purposes.
5
 */
6
class TestApplication extends \Mezon\Application\Application
7
{
8
9
    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...
10
    {
11
        /* existing action */
12
        return 'OK!';
13
    }
14
15
    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...
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)
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

201
        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...
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 () {
0 ignored issues
show
Bug introduced by
The property fly does not seem to exist on Mezon\Application\Application.
Loading history...
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 () {
0 ignored issues
show
Bug introduced by
The property fly does not seem to exist on Mezon\Application\Application.
Loading history...
255
            return 'OK!';
256
        };
257
258
        // test body
259
        $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

259
        $application->/** @scrutinizer ignore-call */ 
260
                      fly();
Loading history...
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();
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

277
        $application->/** @scrutinizer ignore-call */ 
278
                      unexistingMethod();
Loading history...
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