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

testExceptionForUnexistingRequestMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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