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

testPostRequestForUnExistingDynamicRoute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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