Completed
Push — master ( b21196...c8fcb0 )
by Alex
01:48
created

RouterUnitTest::testGetCallback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 13
rs 10
1
<?php
2
3
class RouterUnitTest extends \PHPUnit\Framework\TestCase
4
{
5
6
    const DELETE_REQUEST_METHOD = 'DELETE';
7
8
    /**
9
     * Function sets $_SERVER['REQUEST_METHOD']
10
     *
11
     * @param string $requestMethod
12
     *            request method
13
     */
14
    public static function setRequestMethod(string $requestMethod): void
15
    {
16
        $_SERVER['REQUEST_METHOD'] = $requestMethod;
17
    }
18
19
    /**
20
     * Default setup
21
     *
22
     * {@inheritdoc}
23
     * @see \PHPUnit\Framework\TestCase::setUp()
24
     */
25
    public function setUp(): void
26
    {
27
        RouterUnitTest::setRequestMethod('GET');
28
    }
29
30
    /**
31
     * Function simply returns string.
32
     */
33
    public function helloWorldOutput(): string
34
    {
35
        return 'Hello world!';
36
    }
37
38
    /**
39
     * Testing action #1.
40
     */
41
    public function actionA1(): string
42
    {
43
        return 'action #1';
44
    }
45
46
    /**
47
     * Testing action #2.
48
     */
49
    public function actionA2(): string
50
    {
51
        return 'action #2';
52
    }
53
54
    public function actionDoubleWord(): string
55
    {
56
        return 'action double word';
57
    }
58
59
    /**
60
     * Testing one component router.
61
     */
62
    public function testOneComponentRouterClassMethod(): void
63
    {
64
        // TODO join this test with the next one via data provider
65
        $router = new \Mezon\Router\Router();
66
67
        $router->addRoute('/one-component-class-method/', [
68
            $this,
69
            'helloWorldOutput'
70
        ]);
71
72
        $content = $router->callRoute('/one-component-class-method/');
73
74
        $this->assertEquals('Hello world!', $content);
75
    }
76
77
    /**
78
     * Testing one component router.
79
     */
80
    public function testOneComponentRouterLambda(): void
81
    {
82
        $router = new \Mezon\Router\Router();
83
84
        $router->addRoute('/one-comonent-lambda/', function () {
85
            return 'Hello world!';
86
        });
87
88
        $content = $router->callRoute('/one-comonent-lambda/');
89
90
        $this->assertEquals('Hello world!', $content);
91
    }
92
93
    /**
94
     * Testing unexisting route behaviour.
95
     */
96
    public function testUnexistingRoute(): void
97
    {
98
        $exception = '';
99
        $router = new \Mezon\Router\Router();
100
        $router->addRoute('/existing-route/', [
101
            $this,
102
            'helloWorldOutput'
103
        ]);
104
105
        try {
106
            $router->callRoute('/unexisting-route/');
107
        } catch (Exception $e) {
108
            $exception = $e->getMessage();
109
        }
110
111
        $msg = "The processor was not found for the route";
112
113
        $this->assertNotFalse(strpos($exception, $msg), 'Valid error handling expected');
114
    }
115
116
    /**
117
     * Testing action fetching method.
118
     */
119
    public function testClassActions(): void
120
    {
121
        // TODO join this test with the next one via data provider
122
        $router = new \Mezon\Router\Router();
123
        $router->fetchActions($this);
124
125
        $content = $router->callRoute('/a1/');
126
        $this->assertEquals('action #1', $content, 'Invalid a1 route');
127
128
        $content = $router->callRoute('/a2/');
129
        $this->assertEquals('action #2', $content, 'Invalid a2 route');
130
131
        $content = $router->callRoute('/double-word/');
132
        $this->assertEquals('action double word', $content, 'Invalid a2 route');
133
    }
134
135
    /**
136
     * Method tests POST actions
137
     */
138
    public function testPostClassAction(): void
139
    {
140
        RouterUnitTest::setRequestMethod('POST');
141
142
        $router = new \Mezon\Router\Router();
143
        $router->fetchActions($this);
144
        $content = $router->callRoute('/a1/');
145
        $this->assertEquals('action #1', $content, 'Invalid a1 route');
146
    }
147
148
    /**
149
     * Testing case when all processors exist
150
     */
151
    public function testRequestMethodsConcurrency(): void
152
    {
153
        $route = '/catalog/';
154
        $router = new \Mezon\Router\Router();
155
        $router->addRoute($route, function () {
156
            return 'POST';
157
        }, 'POST');
158
        $router->addRoute($route, function () {
159
            return 'GET';
160
        }, 'GET');
161
        $router->addRoute($route, function () {
162
            return 'PUT';
163
        }, 'PUT');
164
        $router->addRoute($route, function () {
165
            return RouterUnitTest::DELETE_REQUEST_METHOD;
166
        }, RouterUnitTest::DELETE_REQUEST_METHOD);
167
168
        RouterUnitTest::setRequestMethod('POST');
169
        $result = $router->callRoute($route);
170
        $this->assertEquals($result, 'POST');
171
172
        RouterUnitTest::setRequestMethod('GET');
173
        $result = $router->callRoute($route);
174
        $this->assertEquals($result, 'GET');
175
176
        RouterUnitTest::setRequestMethod('PUT');
177
        $result = $router->callRoute($route);
178
        $this->assertEquals($result, 'PUT');
179
180
        RouterUnitTest::setRequestMethod(RouterUnitTest::DELETE_REQUEST_METHOD);
181
        $result = $router->callRoute($route);
182
        $this->assertEquals($result, RouterUnitTest::DELETE_REQUEST_METHOD);
183
    }
184
185
    /**
186
     * Data provider
187
     *
188
     * @return array
189
     */
190
    public function clearMethodTestDataProvider(): array
191
    {
192
        return [
193
            [
194
                'POST'
195
            ],
196
            [
197
                'GET'
198
            ],
199
            [
200
                'PUT'
201
            ],
202
            [
203
                RouterUnitTest::DELETE_REQUEST_METHOD
204
            ],
205
            [
206
                'OPTION'
207
            ]
208
        ];
209
    }
210
211
    /**
212
     * Testing 'clear' method
213
     *
214
     * @param string $method
215
     *            request method
216
     * @dataProvider clearMethodTestDataProvider
217
     */
218
    public function testClearMethod(string $method): void
219
    {
220
        $router = new \Mezon\Router\Router();
221
        $router->addRoute('/route-to-clear/', function () use ($method) {
222
            return $method;
223
        }, $method);
224
        $router->clear();
225
226
        try {
227
            RouterUnitTest::setRequestMethod($method);
228
            $router->callRoute('/route-to-clear/');
229
            $flag = 'not cleared';
230
        } catch (Exception $e) {
231
            $flag = 'cleared';
232
        }
233
        $this->assertEquals($flag, 'cleared', 'Data was not cleared');
234
    }
235
236
    /**
237
     * Method increments assertion count
238
     */
239
    protected function errorHandler(): void
240
    {
241
        $this->addToAssertionCount(1);
242
    }
243
244
    /**
245
     * Test validate custom error handlers.
246
     */
247
    public function testSetErrorHandler(): void
248
    {
249
        $router = new \Mezon\Router\Router();
250
        $router->setNoProcessorFoundErrorHandler(function () {
251
            $this->errorHandler();
252
        });
253
254
        RouterUnitTest::setRequestMethod('POST');
255
        $router->callRoute('/unexisting/');
256
    }
257
258
    /**
259
     * Data provider for the
260
     *
261
     * @return array testing dataset
262
     */
263
    public function getCallbackDataProvider(): array
264
    {
265
        return [
266
            [
267
                'some-static-route',
268
                'some-static-route'
269
            ],
270
            [
271
                'some/non-static/route/[i:id]',
272
                'some/non-static/route/1'
273
            ]
274
        ];
275
    }
276
277
    /**
278
     * Testing getting callback
279
     *
280
     * @param string $route
281
     *            route
282
     * @param string $url
283
     *            concrete URL
284
     * @dataProvider getCallbackDataProvider
285
     */
286
    public function testGetCallback(string $route, string $url): void
287
    {
288
        // setup
289
        $router = new \Mezon\Router\Router();
290
        $router->addRoute($route, function () {
291
            return 'route result';
292
        });
293
294
        // test body
295
        $callback = $router->getCallback($url);
296
297
        // assertions
298
        $this->assertEquals('route result', $callback());
299
    }
300
301
    /**
302
     * Testing case with unexisting callback
303
     */
304
    public function testGetCallbackWithUnexistingRoute(): void
305
    {
306
        // setup
307
        $router = new \Mezon\Router\Router();
308
        $router->addRoute('existing-route', function () {
309
            return 'existing route result';
310
        });
311
312
        // assertions
313
        $this->expectException(\Exception::class);
314
315
        // test body
316
        $router->getCallback('unexisting-route');
317
    }
318
}
319