1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class DynamicRoutesInvalidCasesUnitTest 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 getParam for unexisting param |
19
|
|
|
*/ |
20
|
|
|
public function testGettingUnexistingParameter(): 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
|
|
|
$this->expectException(Exception::class); |
31
|
|
|
|
32
|
|
|
// test body and assertions |
33
|
|
|
$router->getParam('unexisting'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Testing exception throwing for unexisting request method |
38
|
|
|
*/ |
39
|
|
|
public function testExceptionForUnexistingRequestMethod(): void |
40
|
|
|
{ |
41
|
|
|
// setup |
42
|
|
|
$_SERVER['REQUEST_METHOD'] = 'OPTION'; |
43
|
|
|
$router = new \Mezon\Router\Router(); |
44
|
|
|
$router->addRoute('/catalog/[i:foo]/', function () { |
45
|
|
|
// do nothing |
46
|
|
|
}); |
47
|
|
|
|
48
|
|
|
// assertions |
49
|
|
|
$this->expectException(Exception::class); |
50
|
|
|
|
51
|
|
|
// test body |
52
|
|
|
$router->callRoute('/catalog/1/'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Testing invalid id list data types behaviour. |
57
|
|
|
*/ |
58
|
|
|
public function testInValidIdListParams(): void |
59
|
|
|
{ |
60
|
|
|
$exception = ''; |
61
|
|
|
$router = new \Mezon\Router\Router(); |
62
|
|
|
$router->addRoute('/catalog/[il:cat_id]/', [ |
63
|
|
|
$this, |
64
|
|
|
'helloWorldOutput' |
65
|
|
|
]); |
66
|
|
|
|
67
|
|
|
try { |
68
|
|
|
$router->callRoute('/catalog/12345./'); |
69
|
|
|
} catch (Exception $e) { |
70
|
|
|
$exception = $e->getMessage(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$msg = "The processor was not found for the route /catalog/12345./"; |
74
|
|
|
|
75
|
|
|
$this->assertNotFalse(strpos($exception, $msg), 'Invalid error response'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Testing dynamic routes for DELETE requests. |
80
|
|
|
*/ |
81
|
|
|
public function testDeleteRequestForUnExistingDynamicRoute(): void |
82
|
|
|
{ |
83
|
|
|
$_SERVER['REQUEST_METHOD'] = 'DELETE'; |
84
|
|
|
|
85
|
|
|
$exception = ''; |
86
|
|
|
$router = new \Mezon\Router\Router(); |
87
|
|
|
$router->addRoute('/catalog/[i:cat_id]', [ |
88
|
|
|
$this, |
89
|
|
|
'helloWorldOutput' |
90
|
|
|
]); |
91
|
|
|
|
92
|
|
|
try { |
93
|
|
|
$router->callRoute('/catalog/1024/'); |
94
|
|
|
} catch (Exception $e) { |
95
|
|
|
$exception = $e->getMessage(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$msg = "The processor was not found for the route /catalog/1024/"; |
99
|
|
|
|
100
|
|
|
$this->assertNotFalse(strpos($exception, $msg), 'Invalid error response'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Testing dynamic routes for PUT requests. |
105
|
|
|
*/ |
106
|
|
|
public function testPutRequestForUnExistingDynamicRoute(): void |
107
|
|
|
{ |
108
|
|
|
$_SERVER['REQUEST_METHOD'] = 'PUT'; |
109
|
|
|
|
110
|
|
|
$exception = ''; |
111
|
|
|
$router = new \Mezon\Router\Router(); |
112
|
|
|
$router->addRoute('/catalog/[i:cat_id]', [ |
113
|
|
|
$this, |
114
|
|
|
'helloWorldOutput' |
115
|
|
|
]); |
116
|
|
|
|
117
|
|
|
try { |
118
|
|
|
$router->callRoute('/catalog/1024/'); |
119
|
|
|
} catch (Exception $e) { |
120
|
|
|
$exception = $e->getMessage(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$msg = "The processor was not found for the route /catalog/1024/"; |
124
|
|
|
|
125
|
|
|
$this->assertNotFalse(strpos($exception, $msg), 'Invalid error response'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Testing dynamic routes for POST requests. |
130
|
|
|
*/ |
131
|
|
|
public function testPostRequestForUnExistingDynamicRoute(): void |
132
|
|
|
{ |
133
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST'; |
134
|
|
|
|
135
|
|
|
$exception = ''; |
136
|
|
|
$router = new \Mezon\Router\Router(); |
137
|
|
|
$router->addRoute('/catalog/[i:cat_id]', [ |
138
|
|
|
$this, |
139
|
|
|
'helloWorldOutput' |
140
|
|
|
]); |
141
|
|
|
|
142
|
|
|
try { |
143
|
|
|
$router->callRoute('/catalog/1024/'); |
144
|
|
|
} catch (Exception $e) { |
145
|
|
|
$exception = $e->getMessage(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$msg = "The processor was not found for the route /catalog/1024/"; |
149
|
|
|
|
150
|
|
|
$this->assertNotFalse(strpos($exception, $msg), 'Invalid error response'); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|