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

testInvalidType()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 10
c 1
b 0
f 1
dl 0
loc 15
rs 9.9332
cc 2
nc 3
nop 0
1
<?php
2
namespace Mezon\Router\Tests\Simple;
3
4
use PHPUnit\Framework\TestCase;
5
use Mezon\Router\SimpleRouter;
6
7
/**
8
 *
9
 * @psalm-suppress PropertyNotSetInConstructor
10
 */
11
class DynamicRoutesInvalidCasesUnitTest extends TestCase
12
{
13
14
    /**
15
     * Default setup
16
     *
17
     * {@inheritdoc}
18
     * @see \PHPUnit\Framework\TestCase::setUp()
19
     */
20
    public function setUp(): void
21
    {
22
        $_SERVER['REQUEST_METHOD'] = 'GET';
23
    }
24
25
    /**
26
     * Testing getParam for unexisting param
27
     */
28
    public function testGettingUnexistingParameter(): void
29
    {
30
        // setup
31
        $router = new SimpleRouter();
32
        $router->addRoute('/catalog/[i:foo]/', function () {
33
            // do nothing
34
        });
35
36
        $router->callRoute('/catalog/1/');
37
38
        $this->expectException(\Exception::class);
39
40
        // test body and assertions
41
        $router->getParam('unexisting');
42
    }
43
44
    /**
45
     * Testing exception throwing for unexisting request method
46
     */
47
    public function testExceptionForUnexistingRequestMethod(): void
48
    {
49
        // setup
50
        $_SERVER['REQUEST_METHOD'] = 'HEAD';
51
        $router = new SimpleRouter();
52
        $router->addRoute('/catalog/[i:foo]/', function () {
53
            // do nothing
54
        });
55
56
        // assertions
57
        $this->expectException(\Exception::class);
58
59
        // test body
60
        $router->callRoute('/catalog/1/');
61
    }
62
63
    /**
64
     * Testing invalid id list data types behaviour.
65
     */
66
    public function testInValidIdListParams(): void
67
    {
68
        // setup
69
        $router = new SimpleRouter();
70
        $router->addRoute('/catalog/[il:cat_id]/', [
71
            $this,
72
            function () {
73
                return 1;
74
            }
75
        ]);
76
77
        // assertion
78
        $this->expectException(\Exception::class);
79
        $this->expectExceptionMessage('The processor was not found for the route catalog/12345.');
80
81
        // test body
82
        $router->callRoute('/catalog/12345./');
83
    }
84
85
    /**
86
     * Testing dynamic routes for DELETE requests.
87
     */
88
    public function testDeleteRequestForUnExistingDynamicRoute(): void
89
    {
90
        $_SERVER['REQUEST_METHOD'] = 'DELETE';
91
92
        $exception = '';
93
        $router = new SimpleRouter();
94
        $router->addRoute('/catalog/[i:cat_id]', [
95
            $this,
96
            function () {
97
                return 1;
98
            }
99
        ]);
100
101
        try {
102
            $router->callRoute('/catalog/1025/');
103
        } catch (\Exception $e) {
104
            $exception = $e->getMessage();
105
        }
106
107
        $msg = "The processor was not found for the route catalog/1025";
108
109
        $this->assertNotFalse(strpos($exception, $msg));
110
    }
111
112
    /**
113
     * Testing dynamic routes for PUT requests.
114
     */
115
    public function testPutRequestForUnExistingDynamicRoute(): void
116
    {
117
        $_SERVER['REQUEST_METHOD'] = 'PUT';
118
119
        $exception = '';
120
        $router = new SimpleRouter();
121
        $router->addRoute('/catalog/[i:cat_id]', [
122
            $this,
123
            function () {
124
                return 1;
125
            }
126
        ]);
127
128
        try {
129
            $router->callRoute('/catalog/1024/');
130
        } catch (\Exception $e) {
131
            $exception = $e->getMessage();
132
        }
133
134
        $msg = "The processor was not found for the route catalog/1024";
135
136
        $this->assertNotFalse(strpos($exception, $msg));
137
    }
138
139
    /**
140
     * Testing dynamic routes for POST requests.
141
     */
142
    public function testPostRequestForUnExistingDynamicRoute(): void
143
    {
144
        $_SERVER['REQUEST_METHOD'] = 'POST';
145
146
        $exception = '';
147
        $router = new SimpleRouter();
148
        $router->addRoute('/catalog/[i:item_id]', [
149
            $this,
150
            function () {
151
                return 1;
152
            }
153
        ]);
154
155
        try {
156
            $router->callRoute('/catalog/1024/');
157
        } catch (\Exception $e) {
158
            $exception = $e->getMessage();
159
        }
160
161
        $msg = "The processor was not found for the route catalog/1024";
162
163
        $this->assertNotFalse(strpos($exception, $msg));
164
    }
165
166
    /**
167
     * Data provider for the testNotMatchingRoutes
168
     *
169
     * @return array testing data
170
     */
171
    public function invalidCasesDataProvider(): array
172
    {
173
        return [
174
            [
175
                '/catalog/[i:some_id]',
176
                '/catalog/1/a/'
177
            ],
178
            [
179
                '/existing/[i:bar]/',
180
                '/unexisting/1/'
181
            ]
182
        ];
183
    }
184
185
    /**
186
     * Testing that all invalid cases will be covered
187
     *
188
     * @dataProvider invalidCasesDataProvider
189
     */
190
    public function testNotMatchingRoutes(string $route, string $calling): void
191
    {
192
        // setup
193
        $router = new SimpleRouter();
194
        $router->addRoute($route, function () {
195
            // do nothing
196
        });
197
198
        // assertions
199
        $this->expectException(\Exception::class);
200
201
        // test body
202
        $router->callRoute($calling);
203
    }
204
205
    /**
206
     * Testing invalid data types behaviour.
207
     */
208
    public function testInvalidType(): void
209
    {
210
        $router = new SimpleRouter();
211
        $router->addRoute('/catalog/[unexisting-type:i]/item/', [
212
            $this,
213
            function () {
214
                return 1;
215
            }
216
        ]);
217
218
        try {
219
            $router->callRoute('/catalog/1024/item/');
220
            $this->assertFalse(true, 'Exception expected');
221
        } catch (\Exception $e) {
222
            $this->assertFalse(false, '');
223
        }
224
    }
225
226
    /**
227
     * Testing invalid data types behaviour.
228
     */
229
    public function testValidInvalidTypes(): void
230
    {
231
        $router = new SimpleRouter();
232
        $router->addRoute(
233
            '/catalog/[i:cat_id]/item/[unexisting-type-trace:item_id]/',
234
            [
235
                $this,
236
                function () {
237
                    return 1;
238
                }
239
            ]);
240
241
        try {
242
            $router->callRoute('/catalog/1024/item/2048/');
243
            $this->assertFalse(true, 'Exception expected');
244
        } catch (\Exception $e) {
245
            $this->assertFalse(false, '');
246
        }
247
    }
248
249
    /**
250
     * Function simply returns string.
251
     */
252
    public function helloWorldOutput(): string
253
    {
254
        return 'Hello world!';
255
    }
256
257
    /**
258
     * Testing unexisting route behaviour
259
     */
260
    public function testUnexistingRoute(): void
261
    {
262
        $exception = '';
263
        $router = new SimpleRouter();
264
        $router->addRoute('/existing-route/', [
265
            $this,
266
            'helloWorldOutput'
267
        ]);
268
269
        try {
270
            $router->callRoute('/unexisting-route/');
271
        } catch (\Exception $e) {
272
            $exception = $e->getMessage();
273
        }
274
275
        $msg = "The processor was not found for the route";
276
277
        $this->assertNotFalse(strpos($exception, $msg), 'Valid error handling expected');
278
    }
279
280
    /**
281
     * Testing invalid integer data types behaviour.
282
     */
283
    public function testInValidIntegerParams(): void
284
    {
285
        $exception = '';
286
        $router = new SimpleRouter();
287
        $router->addRoute('/catalog/[i:cat_id]/', [
288
            $this,
289
            'helloWorldOutput'
290
        ]);
291
292
        try {
293
            $router->callRoute('/catalog/a1024/');
294
        } catch (\Exception $e) {
295
            $exception = $e->getMessage();
296
        }
297
298
        $msg = "The processor was not found for the route catalog/a1024";
299
300
        $this->assertNotFalse(strpos($exception, $msg), 'Invalid error response');
301
    }
302
303
    /**
304
     * Testing invalid alnum data types behaviour.
305
     */
306
    public function testInValidAlnumParams(): void
307
    {
308
        $exception = '';
309
        $router = new SimpleRouter();
310
        $router->addRoute('/catalog/[a:cat_id]/', [
311
            $this,
312
            'helloWorldOutput'
313
        ]);
314
315
        try {
316
            $router->callRoute('/catalog/~foo/');
317
        } catch (\Exception $e) {
318
            $exception = $e->getMessage();
319
        }
320
321
        $msg = "The processor was not found for the route catalog/~foo";
322
323
        $this->assertNotFalse(strpos($exception, $msg), 'Invalid error response');
324
    }
325
}
326