Passed
Push — master ( 4cfdc5...37c7d0 )
by Alex
07:00
created

StaticRoutesUnitTest   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 325
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 22
eloc 125
c 1
b 0
f 1
dl 0
loc 325
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A testUnknownMethodException() 0 15 1
A testArrayRoutes() 0 13 1
A clearMethodTestDataProvider() 0 11 2
A testMultipleRequestTypes() 0 23 1
A testEmptyArrayRoutes() 0 14 1
A testNotCallableTrash() 0 12 1
A testPutRequestForUnExistingStaticRoute() 0 20 2
A subArray() 0 3 1
A setUp() 0 3 1
A helloWorldOutput() 0 3 1
A testIndexRoute() 0 14 1
A testRequestForExistingStaticRoute() 0 12 1
A testPostRequestForUnExistingStaticRoute() 0 20 2
A testDeleteRequestForUnExistingStaticRoute() 0 20 2
A testRouteExists() 0 15 1
A testClearMethod() 0 16 2
A staticHelloWorldOutput() 0 3 1
1
<?php
2
namespace Mezon\Router\Tests\Simple;
3
4
use Mezon\Router\Tests\Base\RouterUnitTestUtils;
5
use PHPUnit\Framework\TestCase;
6
use Mezon\Router\Tests\Utils;
7
use Mezon\Router\SimpleRouter;
8
9
/**
10
 *
11
 * @psalm-suppress PropertyNotSetInConstructor
12
 */
13
class StaticRoutesUnitTest extends TestCase
14
{
15
16
    const HELLO_WORLD = 'Hello world!';
17
18
    const HELLO_STATIC_WORLD = 'Hello static world!';
19
20
    const HELLO_STATIC_WORLD_METHOD = '\Mezon\Router\Tests\StaticRoutesUnitTest::staticHelloWorldOutput';
21
22
    use Utils;
23
24
    /**
25
     * Function simply returns string.
26
     */
27
    public function helloWorldOutput(): string
28
    {
29
        return StaticRoutesUnitTest::HELLO_WORLD;
30
    }
31
32
    /**
33
     * Function simply returns string.
34
     */
35
    static public function staticHelloWorldOutput(): string
36
    {
37
        return StaticRoutesUnitTest::HELLO_STATIC_WORLD;
38
    }
39
40
    /**
41
     * Default setup
42
     *
43
     * {@inheritdoc}
44
     * @see \PHPUnit\Framework\TestCase::setUp()
45
     */
46
    public function setUp(): void
47
    {
48
        $_SERVER['REQUEST_METHOD'] = 'GET';
49
    }
50
51
    /**
52
     * Testing exception throwing if the method was not found
53
     */
54
    public function testUnknownMethodException(): void
55
    {
56
        // setup
57
        $_GET['r'] = 'unexisting-route-method';
58
        $router = new SimpleRouter();
59
        $router->addRoute('/unexisting-route-method/', [
60
            $this,
61
            'unexistingMethod'
62
        ]);
63
64
        // assertions
65
        $this->expectException(\Exception::class);
66
67
        // test body
68
        $router->callRoute('/unexisting-route-method/');
69
    }
70
71
    /**
72
     * Method returns some testing string
73
     *
74
     * @return string
75
     */
76
    public function subArray(): string
77
    {
78
        return 'subArrayResult';
79
    }
80
81
    /**
82
     * Testing completely not callable trash
83
     */
84
    public function testNotCallableTrash(): void
85
    {
86
        // setup
87
        $_GET['r'] = 'trash';
88
        $router = new SimpleRouter();
89
        $router->addRoute('/trash/', []);
90
91
        // assertions
92
        $this->expectException(\Exception::class);
93
94
        // test body
95
        $router->callRoute('/trash/');
96
    }
97
98
    /**
99
     * Testing array routes
100
     */
101
    public function testArrayRoutes(): void
102
    {
103
        $router = new SimpleRouter();
104
        $router->addRoute('/part1/part2/', function (string $route): string {
105
            return $route;
106
        }, 'GET');
107
108
        $result = $router->callRoute([
109
            'part1',
110
            'part2'
111
        ]);
112
113
        $this->assertEquals($result, 'part1/part2');
114
    }
115
116
    /**
117
     * Testing empty array routes
118
     */
119
    public function testEmptyArrayRoutes(): void
120
    {
121
        $this->setRequestUri('/catalog/item/');
122
123
        $router = new SimpleRouter();
124
        $router->addRoute('/catalog/item/', function (string $route): string {
125
            return $route;
126
        }, 'GET');
127
128
        $result = $router->callRoute([
129
            0 => ''
130
        ]);
131
132
        $this->assertEquals($result, 'catalog/item');
133
    }
134
135
    /**
136
     * Testing empty array routes
137
     */
138
    public function testIndexRoute(): void
139
    {
140
        $this->setRequestUri('/');
141
142
        $router = new SimpleRouter();
143
        $router->addRoute('/index/', function (string $route): string {
144
            return $route;
145
        }, 'GET');
146
147
        $result = $router->callRoute([
148
            0 => ''
149
        ]);
150
151
        $this->assertEquals($result, 'index');
152
    }
153
154
    /**
155
     * Testing empty array routes
156
     */
157
    public function testMultipleRequestTypes(): void
158
    {
159
        // setup
160
        $this->setRequestUri('/');
161
162
        $router = new SimpleRouter();
163
        $router->addRoute('/index/', function (string $route): string {
164
            return $route;
165
        }, [
166
            'GET',
167
            'POST'
168
        ]);
169
170
        $router->callRoute([
171
            0 => ''
172
        ]);
173
174
        $_SERVER['REQUEST_METHOD'] = 'POST';
175
        $result = $router->callRoute([
176
            0 => ''
177
        ]);
178
179
        $this->assertEquals($result, 'index');
180
    }
181
182
    /**
183
     * Testing static routes for DELETE requests.
184
     */
185
    public function testDeleteRequestForUnExistingStaticRoute(): void
186
    {
187
        $_SERVER['REQUEST_METHOD'] = RouterUnitTestUtils::DELETE_REQUEST_METHOD;
188
189
        $exception = '';
190
        $router = new SimpleRouter();
191
        $router->addRoute('/static-delete-unexisting/', [
192
            $this,
193
            'helloWorldOutput'
194
        ]);
195
196
        try {
197
            $router->callRoute('/static-delete-unexisting/');
198
        } catch (\Exception $e) {
199
            $exception = $e->getMessage();
200
        }
201
202
        $msg = "The processor was not found for the route static-delete-unexisting";
203
204
        $this->assertNotFalse(strpos($exception, $msg));
205
    }
206
207
    /**
208
     * Testing static routes for PUT requests.
209
     */
210
    public function testPutRequestForUnExistingStaticRoute(): void
211
    {
212
        $_SERVER['REQUEST_METHOD'] = 'PUT';
213
214
        $exception = '';
215
        $router = new SimpleRouter();
216
        $router->addRoute('/static-put-unexisting/', [
217
            $this,
218
            'helloWorldOutput'
219
        ]);
220
221
        try {
222
            $router->callRoute('/static-put-unexisting/');
223
        } catch (\Exception $e) {
224
            $exception = $e->getMessage();
225
        }
226
227
        $msg = "The processor was not found for the route static-put-unexisting";
228
229
        $this->assertNotFalse(strpos($exception, $msg));
230
    }
231
232
    /**
233
     * Testing static routes for POST requests.
234
     */
235
    public function testPostRequestForUnExistingStaticRoute(): void
236
    {
237
        $_SERVER['REQUEST_METHOD'] = 'POST';
238
239
        $exception = '';
240
        $router = new SimpleRouter();
241
        $router->addRoute('/static-post-unexisting/', [
242
            $this,
243
            'helloWorldOutput'
244
        ]);
245
246
        try {
247
            $router->callRoute('/static-post-unexisting/');
248
        } catch (\Exception $e) {
249
            $exception = $e->getMessage();
250
        }
251
252
        $msg = "The processor was not found for the route static-post-unexisting";
253
254
        $this->assertNotFalse(strpos($exception, $msg));
255
    }
256
257
    /**
258
     * Data provider
259
     *
260
     * @return array
261
     */
262
    public function clearMethodTestDataProvider(): array
263
    {
264
        $result = [];
265
266
        foreach (SimpleRouter::getListOfSupportedRequestMethods() as $method) {
267
            $result[] = [
268
                $method
269
            ];
270
        }
271
272
        return $result;
273
    }
274
275
    /**
276
     * Testing 'clear' method
277
     *
278
     * @param string $method
279
     *            request method
280
     * @dataProvider clearMethodTestDataProvider
281
     */
282
    public function testClearMethod(string $method): void
283
    {
284
        $router = new SimpleRouter();
285
        $router->addRoute('/route-to-clear/', function () use ($method) {
286
            return $method;
287
        }, $method);
288
        $router->clear();
289
290
        try {
291
            RouterUnitTestUtils::setRequestMethod($method);
292
            $router->callRoute('/route-to-clear/');
293
            $flag = 'not cleared';
294
        } catch (\Exception $e) {
295
            $flag = 'cleared';
296
        }
297
        $this->assertEquals($flag, 'cleared', 'Data was not cleared');
298
    }
299
300
    /**
301
     * Testing static routes calls for all possible request methods
302
     *
303
     * @param string $method
304
     * @dataProvider clearMethodTestDataProvider
305
     */
306
    public function testRequestForExistingStaticRoute(string $method): void
307
    {
308
        $_SERVER['REQUEST_METHOD'] = $method;
309
310
        $router = new SimpleRouter();
311
        $router->addRoute('/catalog/', function (string $route): string {
312
            return $route;
313
        }, $method);
314
315
        $result = $router->callRoute('/catalog/');
316
317
        $this->assertEquals($result, 'catalog');
318
    }
319
320
    /**
321
     * Testing routeExists
322
     */
323
    public function testRouteExists(): void
324
    {
325
        // setup
326
        $router = new SimpleRouter();
327
        $router->addRoute('/searching-static-route/', function (string $route) {
328
            return $route;
329
        });
330
        $router->addRoute('/searching-param-route/[i:id]/', function (string $route) {
331
            return $route;
332
        });
333
334
        // test body and assertions
335
        $this->assertTrue($router->routeExists('searching-static-route'));
336
        $this->assertTrue($router->routeExists('searching-param-route/[i:id]'));
337
        $this->assertFalse($router->routeExists('not-searching-route'));
338
    }
339
}
340