Passed
Push — master ( a4ddd3...82bd79 )
by Alex
06:44
created

testPutRequestForUnExistingStaticRoute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 12
c 1
b 1
f 0
dl 0
loc 20
rs 9.8666
cc 2
nc 2
nop 0
1
<?php
2
declare(strict_types = 1);
3
namespace Mezon\Router\Tests\Base;
4
5
use Mezon\Router\Tests\Utils;
6
use PHPUnit\Framework\TestCase;
7
use Mezon\Router\SuppportedRequestMethods;
8
9
/**
10
 *
11
 * @psalm-suppress PropertyNotSetInConstructor
12
 */
13
abstract class StaticRoutesTestClass extends BaseRouterUnitTestClass
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 StaticRoutesTestClass::HELLO_WORLD;
30
    }
31
32
    /**
33
     * Function simply returns string.
34
     */
35
    static public function staticHelloWorldOutput(): string
36
    {
37
        return StaticRoutesTestClass::HELLO_STATIC_WORLD;
38
    }
39
40
    /**
41
     * Default setup
42
     *
43
     * {@inheritdoc}
44
     * @see 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 = $this->getRouter();
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
     * @psalm-suppress InvalidArgument
85
     */
86
    public function testNotCallableTrash(): void
87
    {
88
        // setup
89
        $_GET['r'] = 'trash';
90
        $router = $this->getRouter();
91
        $router->addRoute('/trash/', [
92
            $this,
93
            function (): void {}
94
        ]);
95
96
        // assertions
97
        $this->expectException(\TypeError::class);
98
99
        // test body
100
        $router->callRoute('/trash/');
101
    }
102
103
    /**
104
     * Testing array routes
105
     */
106
    public function testArrayRoutes(): void
107
    {
108
        $router = $this->getRouter();
109
        $router->addRoute('/part1/part2/', function (string $route): string {
110
            return $route;
111
        }, 'GET');
112
113
        /** @var string $result */
114
        $result = $router->callRoute([
115
            'part1',
116
            'part2'
117
        ]);
118
119
        $this->assertEquals($result, 'part1/part2');
120
    }
121
122
    /**
123
     * Testing empty array routes
124
     */
125
    public function testEmptyArrayRoutes(): void
126
    {
127
        $this->setRequestUri('/catalog/item/');
128
129
        $router = $this->getRouter();
130
        $router->addRoute('/catalog/item/', function (string $route): string {
131
            return $route;
132
        }, 'GET');
133
134
        /** @var string $result */
135
        $result = $router->callRoute([
136
            0 => ''
137
        ]);
138
139
        $this->assertEquals($result, 'catalog/item');
140
    }
141
142
    /**
143
     * Testing empty array routes
144
     */
145
    public function testIndexRoute(): void
146
    {
147
        $this->setRequestUri('/');
148
149
        $router = $this->getRouter();
150
        $router->addRoute('/index/', function (string $route): string {
151
            return $route;
152
        }, 'GET');
153
154
        /** @var string $result */
155
        $result = $router->callRoute([
156
            0 => ''
157
        ]);
158
159
        $this->assertEquals($result, 'index');
160
    }
161
162
    /**
163
     * Testing empty array routes
164
     */
165
    public function testMultipleRequestTypes(): void
166
    {
167
        // setup
168
        $this->setRequestUri('/');
169
170
        $router = $this->getRouter();
171
        $router->addRoute('/index/', function (string $route): string {
172
            return $route;
173
        }, [
174
            'GET',
175
            'POST'
176
        ]);
177
178
        $router->callRoute([
179
            0 => ''
180
        ]);
181
182
        $_SERVER['REQUEST_METHOD'] = 'POST';
183
        /** @var string $result */
184
        $result = $router->callRoute([
185
            0 => ''
186
        ]);
187
188
        $this->assertEquals($result, 'index');
189
    }
190
191
    /**
192
     * Testing static routes for DELETE requests.
193
     */
194
    public function testDeleteRequestForUnExistingStaticRoute(): void
195
    {
196
        $_SERVER['REQUEST_METHOD'] = RouterUnitTestUtils::DELETE_REQUEST_METHOD;
197
198
        $exception = '';
199
        $router = $this->getRouter();
200
        $router->addRoute('/static-delete-unexisting/', [
201
            $this,
202
            'helloWorldOutput'
203
        ]);
204
205
        try {
206
            $router->callRoute('/static-delete-unexisting/');
207
        } catch (\Exception $e) {
208
            $exception = $e->getMessage();
209
        }
210
211
        $msg = "The processor was not found for the route static-delete-unexisting";
212
213
        $this->assertNotFalse(strpos($exception, $msg));
214
    }
215
216
    /**
217
     * Testing static routes for PUT requests.
218
     */
219
    public function testPutRequestForUnExistingStaticRoute(): void
220
    {
221
        $_SERVER['REQUEST_METHOD'] = 'PUT';
222
223
        $exception = '';
224
        $router = $this->getRouter();
225
        $router->addRoute('/static-put-unexisting/', [
226
            $this,
227
            'helloWorldOutput'
228
        ]);
229
230
        try {
231
            $router->callRoute('/static-put-unexisting/');
232
        } catch (\Exception $e) {
233
            $exception = $e->getMessage();
234
        }
235
236
        $msg = "The processor was not found for the route static-put-unexisting";
237
238
        $this->assertNotFalse(strpos($exception, $msg));
239
    }
240
241
    /**
242
     * Testing static routes for POST requests.
243
     */
244
    public function testPostRequestForUnExistingStaticRoute(): void
245
    {
246
        $_SERVER['REQUEST_METHOD'] = 'POST';
247
248
        $exception = '';
249
        $router = $this->getRouter();
250
        $router->addRoute('/static-post-unexisting/', [
251
            $this,
252
            'helloWorldOutput'
253
        ]);
254
255
        try {
256
            $router->callRoute('/static-post-unexisting/');
257
        } catch (\Exception $e) {
258
            $exception = $e->getMessage();
259
        }
260
261
        $msg = "The processor was not found for the route static-post-unexisting";
262
263
        $this->assertNotFalse(strpos($exception, $msg));
264
    }
265
266
    /**
267
     * Data provider
268
     *
269
     * @return array test data
270
     */
271
    public function clearMethodTestDataProvider(): array
272
    {
273
        $result = [];
274
275
        foreach (SuppportedRequestMethods::getListOfSupportedRequestMethods() as $method) {
276
            $result[] = [
277
                $method
278
            ];
279
        }
280
281
        return $result;
282
    }
283
284
    /**
285
     * Testing 'clear' method
286
     *
287
     * @param string $method
288
     *            request method
289
     * @dataProvider clearMethodTestDataProvider
290
     */
291
    public function testClearMethod(string $method): void
292
    {
293
        // setup
294
        $router = $this->getRouter();
295
        $router->addRoute('/route-to-clear/', function () use ($method) {
296
            return $method;
297
        }, $method);
298
299
        // test body
300
        $router->clear();
301
302
        // assertions
303
        $this->expectException(\Exception::class);
304
        $router->callRoute('/route-to-clear/');
305
    }
306
307
    /**
308
     * Testing static routes calls for all possible request methods
309
     *
310
     * @param string $method
311
     * @dataProvider clearMethodTestDataProvider
312
     */
313
    public function testRequestForExistingStaticRoute(string $method): void
314
    {
315
        $_SERVER['REQUEST_METHOD'] = $method;
316
317
        $router = $this->getRouter();
318
        $router->addRoute('/catalog/', function (string $route): string {
319
            return $route;
320
        }, $method);
321
322
        /** @var string $result */
323
        $result = $router->callRoute('/catalog/');
324
325
        $this->assertEquals($result, 'catalog');
326
    }
327
328
    /**
329
     * Testing routeExists
330
     */
331
    public function testRouteExists(): void
332
    {
333
        // setup
334
        $router = $this->getRouter();
335
        $router->addRoute('/searching-static-route/', function (string $route) {
336
            return $route;
337
        });
338
        $router->addRoute('/searching-param-route/[i:id]/', function (string $route) {
339
            return $route;
340
        });
341
342
        // test body and assertions
343
        $this->assertTrue($router->routeExists('searching-static-route'));
344
        $this->assertTrue($router->routeExists('searching-param-route/[i:id]'));
345
        $this->assertFalse($router->routeExists('not-searching-route'));
346
    }
347
}
348