Completed
Push — master ( c47e52...96c2bc )
by Alex
01:48
created

testDeleteRequestForUnExistingStaticRoute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 20
rs 9.8666
cc 2
nc 2
nop 0
1
<?php
2
3
class StaticRoutesUnitTest extends \PHPUnit\Framework\TestCase
4
{
5
6
    const HELLO_WORLD = 'Hello world!';
7
8
    const HELLO_STATIC_WORLD = 'Hello static world!';
9
10
    /**
11
     * Function simply returns string.
12
     */
13
    public function helloWorldOutput(): string
14
    {
15
        return StaticRoutesUnitTest::HELLO_WORLD;
16
    }
17
18
    /**
19
     * Default setup
20
     *
21
     * {@inheritdoc}
22
     * @see \PHPUnit\Framework\TestCase::setUp()
23
     */
24
    public function setUp(): void
25
    {
26
        $_SERVER['REQUEST_METHOD'] = 'GET';
27
    }
28
29
    /**
30
     * Testing one processor for all routes.
31
     */
32
    public function testSingleAllProcessor(): void
33
    {
34
        $router = new \Mezon\Router\Router();
35
        $router->addRoute('*', [
36
            $this,
37
            'helloWorldOutput'
38
        ]);
39
40
        $content = $router->callRoute('/some-star-compatible-route/');
41
42
        $this->assertEquals(StaticRoutesUnitTest::HELLO_WORLD, $content);
43
    }
44
45
    /**
46
     * Function simply returns string.
47
     */
48
    static public function staticHelloWorldOutput(): string
49
    {
50
        return StaticRoutesUnitTest::HELLO_STATIC_WORLD;
51
    }
52
53
    /**
54
     * Testing one component router.
55
     */
56
    public function testOneComponentRouterStatic(): void
57
    {
58
        $router = new \Mezon\Router\Router();
59
        $router->addRoute('/one-component-static/', 'StaticRoutesUnitTest::staticHelloWorldOutput');
60
61
        $content = $router->callRoute('/one-component-static/');
62
63
        $this->assertEquals(StaticRoutesUnitTest::HELLO_STATIC_WORLD, $content);
64
    }
65
66
    /**
67
     * Testing one processor for all routes overlap.
68
     */
69
    public function testSingleAllProcessorOverlapUnexisting(): void
70
    {
71
        $router = new \Mezon\Router\Router();
72
        $router->addRoute('*', [
73
            $this,
74
            'helloWorldOutput'
75
        ]);
76
        $router->addRoute('/index/', 'StaticRoutesUnitTest::staticHelloWorldOutput');
77
78
        $content = $router->callRoute('/some-route/');
79
80
        $this->assertEquals(StaticRoutesUnitTest::HELLO_WORLD, $content);
81
    }
82
83
    /**
84
     * Testing one processor for all routes overlap.
85
     */
86
    public function testSingleAllProcessorOverlapExisting(): void
87
    {
88
        $router = new \Mezon\Router\Router();
89
        $router->addRoute('*', [
90
            $this,
91
            'helloWorldOutput'
92
        ]);
93
        $router->addRoute('/index/', 'StaticRoutesUnitTest::staticHelloWorldOutput');
94
95
        $content = $router->callRoute('/index/');
96
97
        $this->assertEquals(StaticRoutesUnitTest::HELLO_WORLD, $content);
98
    }
99
100
    /**
101
     * Testing one processor for all routes overlap.
102
     */
103
    public function testSingleAllProcessorExisting(): void
104
    {
105
        // setup
106
        $router = new \Mezon\Router\Router();
107
        $router->addRoute('/index/', 'StaticRoutesUnitTest::staticHelloWorldOutput');
108
        $router->addRoute('*', [
109
            $this,
110
            'helloWorldOutput'
111
        ]);
112
113
        // test body
114
        $content = $router->callRoute('/index/');
115
116
        // assertions
117
        $this->assertEquals(StaticRoutesUnitTest::HELLO_STATIC_WORLD, $content);
118
    }
119
120
    /**
121
     * Testing one processor for all routes overlap.
122
     */
123
    public function testSingleAllProcessorUnexisting(): void
124
    {
125
        // setup
126
        $router = new \Mezon\Router\Router();
127
        $router->addRoute('/index/', 'StaticRoutesUnitTest::staticHelloWorldOutput');
128
        $router->addRoute('*', [
129
            $this,
130
            'helloWorldOutput'
131
        ]);
132
133
        // test body
134
        $content = $router->callRoute('/some-route/');
135
136
        // assertions
137
        $this->assertEquals(StaticRoutesUnitTest::HELLO_WORLD, $content);
138
    }
139
140
    /**
141
     * Testing routeExists
142
     */
143
    public function testRouteExists(): void
144
    {
145
        // setup
146
        $router = new \Mezon\Router\Router();
147
        $router->addRoute('/searching-route/', function (string $route) {return $route;});
148
149
        // test body and assertions
150
        $this->assertTrue($router->routeExists('searching-route'));
151
        $this->assertFalse($router->routeExists('not-searching-route'));
152
    }
153
154
    /**
155
     * Testing exception throwing if the method was not found
156
     */
157
    public function testUnknownMethodException(): void
158
    {
159
        // setup
160
        $_GET['r'] = 'unexisting-route-method';
161
        $router = new \Mezon\Router\Router();
162
        $router->addRoute('/unexisting-route-method/', [
163
            $this,
164
            'unexistingMethod'
165
        ]);
166
167
        // assertions
168
        $this->expectException(\Exception::class);
169
170
        // test body
171
        $router->callRoute('/unexisting-route-method/');
172
    }
173
174
    /**
175
     * Method returns some testing string
176
     *
177
     * @return string
178
     */
179
    public function subArray(): string
180
    {
181
        return 'subArrayResult';
182
    }
183
184
    /**
185
     * Testing completely not callable trash
186
     */
187
    public function testNotCallableTrash(): void
188
    {
189
        // setup
190
        $_GET['r'] = 'trash';
191
        $router = new \Mezon\Router\Router();
192
        $router->addRoute('/trash/', []);
193
194
        // assertions
195
        $this->expectException(\Exception::class);
196
197
        // test body
198
        $router->callRoute('/trash/');
199
    }
200
201
    /**
202
     * Testing array routes
203
     */
204
    public function testArrayRoutes(): void
205
    {
206
        $router = new \Mezon\Router\Router();
207
        $router->addRoute('/part1/part2/', function ($route) {
208
            return $route;
209
        }, 'GET');
210
211
        $result = $router->callRoute([
212
            'part1',
213
            'part2'
214
        ]);
215
216
        $this->assertEquals($result, 'part1/part2');
217
    }
218
219
    /**
220
     * Testing empty array routes
221
     */
222
    public function testEmptyArrayRoutes(): void
223
    {
224
        $_SERVER['REQUEST_URI'] = '/catalog/item/';
225
226
        $router = new \Mezon\Router\Router();
227
        $router->addRoute('/catalog/item/', function ($route) {
228
            return $route;
229
        }, 'GET');
230
231
        $result = $router->callRoute([
232
            0 => ''
233
        ]);
234
235
        $this->assertEquals($result, 'catalog/item');
236
    }
237
238
    /**
239
     * Testing empty array routes
240
     */
241
    public function testIndexRoute(): void
242
    {
243
        $_SERVER['REQUEST_URI'] = '/';
244
245
        $router = new \Mezon\Router\Router();
246
        $router->addRoute('/index/', function ($route) {
247
            return $route;
248
        }, 'GET');
249
250
        $result = $router->callRoute([
251
            0 => ''
252
        ]);
253
254
        $this->assertEquals($result, 'index');
255
    }
256
257
    /**
258
     * Testing empty array routes
259
     */
260
    public function testMultipleRequestTypes(): void
261
    {
262
        // setup
263
        $_SERVER['REQUEST_URI'] = '/';
264
265
        $router = new \Mezon\Router\Router();
266
        $router->addRoute('/index/', function ($route) {
267
            return $route;
268
        }, [
269
            'GET',
270
            'POST'
271
        ]);
272
273
        $router->callRoute([
274
            0 => ''
275
        ]);
276
277
        $_SERVER['REQUEST_METHOD'] = 'POST';
278
        $result = $router->callRoute([
279
            0 => ''
280
        ]);
281
282
        $this->assertEquals($result, 'index');
283
    }
284
285
    /**
286
     * Testing static routes for DELETE requests.
287
     */
288
    public function testDeleteRequestForUnExistingStaticRoute(): void
289
    {
290
        $_SERVER['REQUEST_METHOD'] = RouterUnitTest::DELETE_REQUEST_METHOD;
291
292
        $exception = '';
293
        $router = new \Mezon\Router\Router();
294
        $router->addRoute('/static-delete-unexisting/', [
295
            $this,
296
            'helloWorldOutput'
297
        ]);
298
299
        try {
300
            $router->callRoute('/static-delete-unexisting/');
301
        } catch (Exception $e) {
302
            $exception = $e->getMessage();
303
        }
304
305
        $msg = "The processor was not found for the route static-delete-unexisting";
306
307
        $this->assertNotFalse(strpos($exception, $msg));
308
    }
309
310
    /**
311
     * Testing static routes for DELETE requests.
312
     */
313
    public function testDeleteRequestForExistingStaticRoute(): void
314
    {
315
        $_SERVER['REQUEST_METHOD'] = RouterUnitTest::DELETE_REQUEST_METHOD;
316
317
        $router = new \Mezon\Router\Router();
318
        $router->addRoute('/static-delete-existing/', function ($route) {
319
            return $route;
320
        }, RouterUnitTest::DELETE_REQUEST_METHOD);
321
322
        $result = $router->callRoute('/static-delete-existing/');
323
324
        $this->assertEquals($result, 'static-delete-existing');
325
    }
326
327
    /**
328
     * Testing static routes for PUT requests.
329
     */
330
    public function testPutRequestForUnExistingStaticRoute(): void
331
    {
332
        $_SERVER['REQUEST_METHOD'] = 'PUT';
333
334
        $exception = '';
335
        $router = new \Mezon\Router\Router();
336
        $router->addRoute('/static-put-unexisting/', [
337
            $this,
338
            'helloWorldOutput'
339
        ]);
340
341
        try {
342
            $router->callRoute('/static-put-unexisting/');
343
        } catch (Exception $e) {
344
            $exception = $e->getMessage();
345
        }
346
347
        $msg = "The processor was not found for the route static-put-unexisting";
348
349
        $this->assertNotFalse(strpos($exception, $msg));
350
    }
351
352
    /**
353
     * Testing static routes for PUT requests.
354
     */
355
    public function testPutRequestForExistingStaticRoute(): void
356
    {
357
        $_SERVER['REQUEST_METHOD'] = 'PUT';
358
359
        $router = new \Mezon\Router\Router();
360
        $router->addRoute('/static-put-existing/', function ($route) {
361
            return $route;
362
        }, 'PUT');
363
364
        $result = $router->callRoute('/static-put-existing/');
365
366
        $this->assertEquals($result, 'static-put-existing');
367
    }
368
369
    /**
370
     * Testing static routes for POST requests.
371
     */
372
    public function testPostRequestForUnExistingStaticRoute(): void
373
    {
374
        $_SERVER['REQUEST_METHOD'] = 'POST';
375
376
        $exception = '';
377
        $router = new \Mezon\Router\Router();
378
        $router->addRoute('/static-post-unexisting/', [
379
            $this,
380
            'helloWorldOutput'
381
        ]);
382
383
        try {
384
            $router->callRoute('/static-post-unexisting/');
385
        } catch (Exception $e) {
386
            $exception = $e->getMessage();
387
        }
388
389
        $msg = "The processor was not found for the route static-post-unexisting";
390
391
        $this->assertNotFalse(strpos($exception, $msg));
392
    }
393
394
    /**
395
     * Testing static routes for POST requests.
396
     */
397
    public function testPostRequestForExistingStaticRoute(): void
398
    {
399
        $_SERVER['REQUEST_METHOD'] = 'POST';
400
401
        $router = new \Mezon\Router\Router();
402
        $router->addRoute('/catalog/', function ($route) {
403
            return $route;
404
        }, 'POST');
405
406
        $result = $router->callRoute('/catalog/');
407
408
        $this->assertEquals($result, 'catalog');
409
    }
410
}
411