Completed
Branch master (5d9bff)
by Alex
02:20 queued 30s
created

DynamicRoutesUnitTest::testPositiveFloatI()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 10
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
class DynamicRoutesUnitTest extends \PHPUnit\Framework\TestCase
4
{
5
6
    /**
7
     * Default setup
8
     *
9
     * {@inheritdoc}
10
     * @see \PHPUnit\Framework\TestCase::setUp()
11
     */
12
    public function setUp(): void
13
    {
14
        $_SERVER['REQUEST_METHOD'] = 'GET';
15
    }
16
17
    /**
18
     * Testing hasParam method
19
     */
20
    public function testValidatingParameter(): void
21
    {
22
        // setup
23
        $router = new \Mezon\Router\Router();
24
        $router->addRoute('/catalog/[i:foo]/', function () {
25
            // do nothing
26
        });
27
28
        $router->callRoute('/catalog/1/');
29
30
        // test body and assertions
31
        $this->assertTrue($router->hasParam('foo'));
32
        $this->assertFalse($router->hasParam('unexisting'));
33
    }
34
35
    /**
36
     * Testing getParam for existing param
37
     */
38
    public function testGettingExistingParameter(): void
39
    {
40
        // setup
41
        $router = new \Mezon\Router\Router();
42
        $router->addRoute('/catalog/[i:foo]/', function () {
43
            // do nothing
44
        });
45
46
        $router->callRoute('/catalog/1/');
47
48
        // test body
49
        $foo = $router->getParam('foo');
50
51
        // assertions
52
        $this->assertEquals(1, $foo);
53
    }
54
55
    /**
56
     * Testing getParam for unexisting param
57
     */
58
    public function testGettingUnexistingParameter(): void
59
    {
60
        // setup
61
        $router = new \Mezon\Router\Router();
62
        $router->addRoute('/catalog/[i:foo]/', function () {
63
            // do nothing
64
        });
65
66
        $router->callRoute('/catalog/1/');
67
68
        $this->expectException(Exception::class);
69
70
        // test body and assertions
71
        $router->getParam('unexisting');
72
    }
73
74
    /**
75
     * Testing exception throwing for unexisting request method
76
     */
77
    public function testExceptionForUnexistingRequestMethod(): void
78
    {
79
        // setup
80
        $_SERVER['REQUEST_METHOD'] = 'OPTION';
81
        $router = new \Mezon\Router\Router();
82
        $router->addRoute('/catalog/[i:foo]/', function () {
83
            // do nothing
84
        });
85
86
        // assertions
87
        $this->expectException(Exception::class);
88
89
        // test body
90
        $router->callRoute('/catalog/1/');
91
    }
92
93
    /**
94
     * Testing saving of the route parameters
95
     */
96
    public function testSavingParameters(): void
97
    {
98
        $router = new \Mezon\Router\Router();
99
        $router->addRoute('/catalog/[i:foo]/', function ($route, $parameters) {
100
            return $parameters['foo'];
101
        });
102
103
        $router->callRoute('/catalog/-1/');
104
105
        $this->assertEquals($router->getParam('foo'), '-1', 'Float data violation');
106
    }
107
108
    /**
109
     * Testing command special chars.
110
     */
111
    public function testCommandSpecialChars(): void
112
    {
113
        $router = new \Mezon\Router\Router();
114
115
        $router->addRoute('/[a:url]/', function () {
116
            return 'GET';
117
        }, 'GET');
118
119
        $result = $router->callRoute('/.-@/');
120
        $this->assertEquals($result, 'GET', 'Invalid selected route');
121
    }
122
123
    /**
124
     * Testing strings.
125
     */
126
    public function testStringSpecialChars(): void
127
    {
128
        $router = new \Mezon\Router\Router();
129
130
        $router->addRoute('/[s:url]/', function () {
131
            return 'GET';
132
        }, 'GET');
133
134
        $result = $router->callRoute('/, ;:/');
135
        $this->assertEquals($result, 'GET', 'Invalid selected route');
136
    }
137
138
    /**
139
     * Testing invalid id list data types behaviour.
140
     */
141
    public function testInValidIdListParams(): void
142
    {
143
        $exception = '';
144
        $router = new \Mezon\Router\Router();
145
        $router->addRoute('/catalog/[il:cat_id]/', [
146
            $this,
147
            'helloWorldOutput'
148
        ]);
149
150
        try {
151
            $router->callRoute('/catalog/12345./');
152
        } catch (Exception $e) {
153
            $exception = $e->getMessage();
154
        }
155
156
        $msg = "The processor was not found for the route /catalog/12345./";
157
158
        $this->assertNotFalse(strpos($exception, $msg), 'Invalid error response');
159
    }
160
161
    /**
162
     * Method for checking id list.
163
     */
164
    public function ilTest($route, $params): string
165
    {
166
        return $params['ids'];
167
    }
168
169
    /**
170
     * Testing valid id list data types behaviour.
171
     */
172
    public function testValidIdListParams(): void
173
    {
174
        $router = new \Mezon\Router\Router();
175
        $router->addRoute('/catalog/[il:ids]/', [
176
            $this,
177
            'ilTest'
178
        ]);
179
180
        $result = $router->callRoute('/catalog/123,456,789/');
181
182
        $this->assertEquals($result, '123,456,789', 'Invalid router response');
183
    }
184
185
    /**
186
     * Testing valid id list data types behaviour.
187
     */
188
    public function testStringParamSecurity(): void
189
    {
190
        $router = new \Mezon\Router\Router();
191
        $router->addRoute('/catalog/[s:foo]/', function ($route, $parameters) {
192
            return $parameters['foo'];
193
        });
194
195
        $result = $router->callRoute('/catalog/123&456/');
196
197
        $this->assertEquals($result, '123&amp;456', 'Security data violation');
198
    }
199
200
    /**
201
     * Testing float value.
202
     */
203
    public function testFloatI(): void
204
    {
205
        $router = new \Mezon\Router\Router();
206
        $router->addRoute('/catalog/[i:foo]/', function ($route, $parameters) {
207
            return $parameters['foo'];
208
        });
209
210
        $result = $router->callRoute('/catalog/1.1/');
211
212
        $this->assertEquals($result, '1.1', 'Float data violation');
213
    }
214
215
    /**
216
     * Testing negative float value.
217
     */
218
    public function testNegativeFloatI(): void
219
    {
220
        $router = new \Mezon\Router\Router();
221
        $router->addRoute('/catalog/[i:foo]/', function ($route, $parameters) {
222
            return $parameters['foo'];
223
        });
224
225
        $result = $router->callRoute('/catalog/-1.1/');
226
227
        $this->assertEquals($result, '-1.1', 'Float data violation');
228
    }
229
230
    /**
231
     * Testing positive float value.
232
     */
233
    public function testPositiveFloatI(): void
234
    {
235
        $router = new \Mezon\Router\Router();
236
        $router->addRoute('/catalog/[i:foo]/', function ($route, $parameters) {
237
            return $parameters['foo'];
238
        });
239
240
        $result = $router->callRoute('/catalog/+1.1/');
241
242
        $this->assertEquals($result, '+1.1', 'Float data violation');
243
    }
244
245
    /**
246
     * Testing negative integer value
247
     */
248
    public function testNegativeIntegerI(): void
249
    {
250
        $router = new \Mezon\Router\Router();
251
        $router->addRoute('/catalog/[i:foo]/', function ($route, $parameters) {
252
            return $parameters['foo'];
253
        });
254
255
        $result = $router->callRoute('/catalog/-1/');
256
257
        $this->assertEquals('-1', $result, 'Float data violation');
258
    }
259
260
    /**
261
     * Testing positive integer value
262
     */
263
    public function testPositiveIntegerI(): void
264
    {
265
        $router = new \Mezon\Router\Router();
266
        $router->addRoute('/catalog/[i:foo]/', function ($route, $parameters) {
267
            return $parameters['foo'];
268
        });
269
270
        $result = $router->callRoute('/catalog/1/');
271
272
        $this->assertEquals('1', $result, 'Float data violation');
273
    }
274
    
275
    /**
276
     * Testing dynamic routes for DELETE requests.
277
     */
278
    public function testDeleteRequestForUnExistingDynamicRoute(): void
279
    {
280
        $_SERVER['REQUEST_METHOD'] = 'DELETE';
281
        
282
        $exception = '';
283
        $router = new \Mezon\Router\Router();
284
        $router->addRoute('/catalog/[i:cat_id]', [
285
            $this,
286
            'helloWorldOutput'
287
        ]);
288
        
289
        try {
290
            $router->callRoute('/catalog/1024/');
291
        } catch (Exception $e) {
292
            $exception = $e->getMessage();
293
        }
294
        
295
        $msg = "The processor was not found for the route /catalog/1024/";
296
        
297
        $this->assertNotFalse(strpos($exception, $msg), 'Invalid error response');
298
    }
299
    
300
    /**
301
     * Testing dynamic routes for DELETE requests.
302
     */
303
    public function testDeleteRequestForExistingDynamicRoute(): void
304
    {
305
        $_SERVER['REQUEST_METHOD'] = 'DELETE';
306
        
307
        $router = new \Mezon\Router\Router();
308
        $router->addRoute('/catalog/[i:cat_id]', function ($route) {
309
            return $route;
310
        }, 'DELETE');
311
            
312
            $result = $router->callRoute('/catalog/1024/');
313
            
314
            $this->assertEquals($result, '/catalog/1024/', 'Invalid extracted route');
315
    }
316
    
317
    /**
318
     * Testing dynamic routes for PUT requests.
319
     */
320
    public function testPutRequestForUnExistingDynamicRoute(): void
321
    {
322
        $_SERVER['REQUEST_METHOD'] = 'PUT';
323
        
324
        $exception = '';
325
        $router = new \Mezon\Router\Router();
326
        $router->addRoute('/catalog/[i:cat_id]', [
327
            $this,
328
            'helloWorldOutput'
329
        ]);
330
        
331
        try {
332
            $router->callRoute('/catalog/1024/');
333
        } catch (Exception $e) {
334
            $exception = $e->getMessage();
335
        }
336
        
337
        $msg = "The processor was not found for the route /catalog/1024/";
338
        
339
        $this->assertNotFalse(strpos($exception, $msg), 'Invalid error response');
340
    }
341
    
342
    /**
343
     * Testing dynamic routes for PUT requests.
344
     */
345
    public function testPutRequestForExistingDynamicRoute(): void
346
    {
347
        $_SERVER['REQUEST_METHOD'] = 'PUT';
348
        
349
        $router = new \Mezon\Router\Router();
350
        $router->addRoute('/catalog/[i:cat_id]', function ($route) {
351
            return $route;
352
        }, 'PUT');
353
            
354
            $result = $router->callRoute('/catalog/1024/');
355
            
356
            $this->assertEquals($result, '/catalog/1024/', 'Invalid extracted route');
357
    }
358
}
359