1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rad\Routing\Tests; |
4
|
|
|
|
5
|
|
|
use App\AppBundle; |
6
|
|
|
use Composer\Autoload\ClassLoader; |
7
|
|
|
use PHPUnit_Framework_TestCase; |
8
|
|
|
use Rad\Core\Bundles; |
9
|
|
|
use Rad\Routing\Router; |
10
|
|
|
use Test\TestBundle; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Config Test |
14
|
|
|
* |
15
|
|
|
* @package Rad\Configure\Tests |
16
|
|
|
*/ |
17
|
|
|
class RoutingTest extends PHPUnit_Framework_TestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var Router |
21
|
|
|
*/ |
22
|
|
|
private $router; |
23
|
|
|
|
24
|
|
View Code Duplication |
public static function setUpBeforeClass() |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
$classLoader = new ClassLoader(); |
27
|
|
|
$classLoader->addPsr4('App\\', __DIR__ . '/Fixtures/src/App'); |
28
|
|
|
$classLoader->addPsr4('Test\\', __DIR__ . '/Fixtures/src/Test'); |
29
|
|
|
$classLoader->register(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
View Code Duplication |
protected function setUp() |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
$this->router = new Router(); |
35
|
|
|
if (!defined('SRC_DIR')) { |
36
|
|
|
define('SRC_DIR', __DIR__ . '/Fixtures/src'); |
37
|
|
|
} |
38
|
|
|
if (!defined('DS')) { |
39
|
|
|
define('DS', '/'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$bundles = [ |
43
|
|
|
new AppBundle(), |
44
|
|
|
new TestBundle() |
45
|
|
|
]; |
46
|
|
|
Bundles::loadAll($bundles); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Test root URL handler behaviours |
51
|
|
|
*/ |
52
|
|
|
public function testRootUrlHandler() |
53
|
|
|
{ |
54
|
|
|
$this->router->handle('/'); |
55
|
|
|
|
56
|
|
|
$this->assertEquals($this->router->getActionNamespace(), 'App\Action\IndexAction'); |
57
|
|
|
$this->assertEquals($this->router->getResponderNamespace(), 'App\Responder\IndexResponder'); |
58
|
|
|
$this->assertEquals($this->router->getAction(), 'index'); |
59
|
|
|
$this->assertEquals($this->router->getBundle(), 'app'); |
60
|
|
|
$this->assertEquals($this->router->getParams(), []); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Test bundle only URL handler behaviours |
65
|
|
|
*/ |
66
|
|
|
public function testBundleUrlHandler() |
67
|
|
|
{ |
68
|
|
|
$this->router->handle('/test/'); |
69
|
|
|
|
70
|
|
|
$this->assertEquals($this->router->getActionNamespace(), 'Test\Action\IndexAction'); |
71
|
|
|
$this->assertEquals($this->router->getResponderNamespace(), 'Test\Responder\IndexResponder'); |
72
|
|
|
$this->assertEquals($this->router->getAction(), 'index'); |
73
|
|
|
$this->assertEquals($this->router->getBundle(), 'test'); |
74
|
|
|
$this->assertEquals($this->router->getParams(), []); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Test bundle with method URL handler behaviours |
79
|
|
|
*/ |
80
|
|
View Code Duplication |
public function testBundleMethodUrlHandler() |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$this->router->handle('/test/method/param1'); |
83
|
|
|
|
84
|
|
|
$this->assertEquals($this->router->getActionNamespace(), 'Test\Action\Method\CliMethodAction'); |
85
|
|
|
$this->assertEquals($this->router->getResponderNamespace(), 'Test\Responder\Method\CliMethodResponder'); |
86
|
|
|
$this->assertEquals($this->router->getAction(), 'CliMethod'); |
87
|
|
|
$this->assertEquals($this->router->getBundle(), 'test'); |
88
|
|
|
$this->assertEquals($this->router->getParams(), ['param1']); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Test bundle and action URL handler behaviours |
93
|
|
|
*/ |
94
|
|
|
public function testBundleActionUrlHandler() |
95
|
|
|
{ |
96
|
|
|
$this->router->handle('/test/test/'); |
97
|
|
|
|
98
|
|
|
$this->assertEquals($this->router->getActionNamespace(), 'Test\Action\TestAction'); |
99
|
|
|
$this->assertEquals($this->router->getResponderNamespace(), 'Test\Responder\TestResponder'); |
100
|
|
|
$this->assertEquals($this->router->getAction(), 'test'); |
101
|
|
|
$this->assertEquals($this->router->getBundle(), 'test'); |
102
|
|
|
$this->assertEquals($this->router->getParams(), []); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Test bundle and action and params URL handler behaviours |
107
|
|
|
*/ |
108
|
|
View Code Duplication |
public function testBundleActionParamsUrlHandler() |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
$this->router->handle('/test/test/param1/param2/'); |
111
|
|
|
|
112
|
|
|
$this->assertEquals($this->router->getActionNamespace(), 'Test\Action\TestAction'); |
113
|
|
|
$this->assertEquals($this->router->getResponderNamespace(), 'Test\Responder\TestResponder'); |
114
|
|
|
$this->assertEquals($this->router->getAction(), 'test'); |
115
|
|
|
$this->assertEquals($this->router->getBundle(), 'test'); |
116
|
|
|
$this->assertEquals($this->router->getParams(), ['param1', 'param2']); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Test bundle with method URL handler behaviours |
121
|
|
|
*/ |
122
|
|
View Code Duplication |
public function testBundleMethodParamsUrlHandler() |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
$this->router->handle('/test/method/param1/param2'); |
125
|
|
|
|
126
|
|
|
$this->assertEquals($this->router->getActionNamespace(), 'Test\Action\Method\CliMethodAction'); |
127
|
|
|
$this->assertEquals($this->router->getResponderNamespace(), 'Test\Responder\Method\CliMethodResponder'); |
128
|
|
|
$this->assertEquals($this->router->getAction(), 'CliMethod'); |
129
|
|
|
$this->assertEquals($this->router->getBundle(), 'test'); |
130
|
|
|
$this->assertEquals($this->router->getParams(), ['param1', 'param2']); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Test bundle only in subdirectory URL handler behaviours |
135
|
|
|
*/ |
136
|
|
|
public function testBundleSubdirUrlHandler() |
137
|
|
|
{ |
138
|
|
|
$this->router->handle('/test/subdir/'); |
139
|
|
|
|
140
|
|
|
$this->assertEquals($this->router->getActionNamespace(), 'Test\Action\Subdir\IndexAction'); |
141
|
|
|
$this->assertEquals($this->router->getResponderNamespace(), 'Test\Responder\Subdir\IndexResponder'); |
142
|
|
|
$this->assertEquals($this->router->getAction(), 'index'); |
143
|
|
|
$this->assertEquals($this->router->getBundle(), 'test'); |
144
|
|
|
$this->assertEquals($this->router->getParams(), []); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Test bundle in subdirectory with method URL handler behaviours |
149
|
|
|
*/ |
150
|
|
|
public function testBundleSubdirMethodUrlHandler() |
151
|
|
|
{ |
152
|
|
|
$this->router->handle('/test/subdir/method/'); |
153
|
|
|
|
154
|
|
|
$this->assertEquals($this->router->getActionNamespace(), 'Test\Action\Subdir\Method\CliMethodAction'); |
155
|
|
|
$this->assertEquals($this->router->getResponderNamespace(), 'Test\Responder\Subdir\Method\CliMethodResponder'); |
156
|
|
|
$this->assertEquals($this->router->getAction(), 'CliMethod'); |
157
|
|
|
$this->assertEquals($this->router->getBundle(), 'test'); |
158
|
|
|
$this->assertEquals($this->router->getParams(), []); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Test bundle and action in subdirectory URL handler behaviours |
163
|
|
|
*/ |
164
|
|
|
public function testBundleSubdirActionUrlHandler() |
165
|
|
|
{ |
166
|
|
|
$this->router->handle('/test/subdir/test/'); |
167
|
|
|
|
168
|
|
|
$this->assertEquals($this->router->getActionNamespace(), 'Test\Action\Subdir\TestAction'); |
169
|
|
|
$this->assertEquals($this->router->getResponderNamespace(), 'Test\Responder\Subdir\TestResponder'); |
170
|
|
|
$this->assertEquals($this->router->getAction(), 'test'); |
171
|
|
|
$this->assertEquals($this->router->getBundle(), 'test'); |
172
|
|
|
$this->assertEquals($this->router->getParams(), []); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Test bundle and action in subdirectory and params URL handler behaviours |
177
|
|
|
*/ |
178
|
|
View Code Duplication |
public function testBundleSubdirActionParamsUrlHandler() |
|
|
|
|
179
|
|
|
{ |
180
|
|
|
$this->router->handle('/test/subdir/test/param1/param2/'); |
181
|
|
|
|
182
|
|
|
$this->assertEquals($this->router->getActionNamespace(), 'Test\Action\Subdir\TestAction'); |
183
|
|
|
$this->assertEquals($this->router->getResponderNamespace(), 'Test\Responder\Subdir\TestResponder'); |
184
|
|
|
$this->assertEquals($this->router->getAction(), 'test'); |
185
|
|
|
$this->assertEquals($this->router->getBundle(), 'test'); |
186
|
|
|
$this->assertEquals($this->router->getParams(), ['param1', 'param2']); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Test bundle with method in subdirectory URL handler behaviours |
191
|
|
|
*/ |
192
|
|
View Code Duplication |
public function testBundleSubdirMethodParamsUrlHandler() |
|
|
|
|
193
|
|
|
{ |
194
|
|
|
$this->router->handle('/test/subdir/method/param1/param2'); |
195
|
|
|
|
196
|
|
|
$this->assertEquals($this->router->getActionNamespace(), 'Test\Action\Subdir\Method\CliMethodAction'); |
197
|
|
|
$this->assertEquals($this->router->getResponderNamespace(), 'Test\Responder\Subdir\Method\CliMethodResponder'); |
198
|
|
|
$this->assertEquals($this->router->getAction(), 'CliMethod'); |
199
|
|
|
$this->assertEquals($this->router->getBundle(), 'test'); |
200
|
|
|
$this->assertEquals($this->router->getParams(), ['param1', 'param2']); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Test root URL handler behaviours + language |
205
|
|
|
*/ |
206
|
|
|
public function testLangRootUrlHandler() |
207
|
|
|
{ |
208
|
|
|
$this->router->handle('/en/'); |
209
|
|
|
|
210
|
|
|
$this->assertEquals($this->router->getActionNamespace(), 'App\Action\IndexAction'); |
211
|
|
|
$this->assertEquals($this->router->getResponderNamespace(), 'App\Responder\IndexResponder'); |
212
|
|
|
$this->assertEquals($this->router->getAction(), 'index'); |
213
|
|
|
$this->assertEquals($this->router->getBundle(), 'app'); |
214
|
|
|
$this->assertEquals($this->router->getParams(), []); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Test bundle only URL handler behaviours + language |
219
|
|
|
*/ |
220
|
|
|
public function testLangBundleUrlHandler() |
221
|
|
|
{ |
222
|
|
|
$this->router->handle('/en/test/'); |
223
|
|
|
|
224
|
|
|
$this->assertEquals($this->router->getActionNamespace(), 'Test\Action\IndexAction'); |
225
|
|
|
$this->assertEquals($this->router->getResponderNamespace(), 'Test\Responder\IndexResponder'); |
226
|
|
|
$this->assertEquals($this->router->getAction(), 'index'); |
227
|
|
|
$this->assertEquals($this->router->getBundle(), 'test'); |
228
|
|
|
$this->assertEquals($this->router->getParams(), []); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Test bundle with method URL handler behaviours + language |
233
|
|
|
*/ |
234
|
|
|
public function testLangBundleMethodUrlHandler() |
235
|
|
|
{ |
236
|
|
|
$this->router->handle('/en/test/method/'); |
237
|
|
|
|
238
|
|
|
$this->assertEquals($this->router->getActionNamespace(), 'Test\Action\Method\CliMethodAction'); |
239
|
|
|
$this->assertEquals($this->router->getResponderNamespace(), 'Test\Responder\Method\CliMethodResponder'); |
240
|
|
|
$this->assertEquals($this->router->getAction(), 'CliMethod'); |
241
|
|
|
$this->assertEquals($this->router->getBundle(), 'test'); |
242
|
|
|
$this->assertEquals($this->router->getParams(), []); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Test bundle and action URL handler behaviours + language |
247
|
|
|
*/ |
248
|
|
|
public function testLangBundleActionUrlHandler() |
249
|
|
|
{ |
250
|
|
|
$this->router->handle('/en/test/test/'); |
251
|
|
|
|
252
|
|
|
$this->assertEquals($this->router->getActionNamespace(), 'Test\Action\TestAction'); |
253
|
|
|
$this->assertEquals($this->router->getResponderNamespace(), 'Test\Responder\TestResponder'); |
254
|
|
|
$this->assertEquals($this->router->getAction(), 'test'); |
255
|
|
|
$this->assertEquals($this->router->getBundle(), 'test'); |
256
|
|
|
$this->assertEquals($this->router->getParams(), []); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Test bundle and action and params URL handler behaviours + language |
261
|
|
|
*/ |
262
|
|
View Code Duplication |
public function testLangBundleActionParamsUrlHandler() |
|
|
|
|
263
|
|
|
{ |
264
|
|
|
$this->router->handle('/en/test/test/param1/param2/'); |
265
|
|
|
|
266
|
|
|
$this->assertEquals($this->router->getActionNamespace(), 'Test\Action\TestAction'); |
267
|
|
|
$this->assertEquals($this->router->getResponderNamespace(), 'Test\Responder\TestResponder'); |
268
|
|
|
$this->assertEquals($this->router->getAction(), 'test'); |
269
|
|
|
$this->assertEquals($this->router->getBundle(), 'test'); |
270
|
|
|
$this->assertEquals($this->router->getParams(), ['param1', 'param2']); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Test bundle with method URL handler behaviours |
275
|
|
|
*/ |
276
|
|
View Code Duplication |
public function testLangBundleMethodParamsUrlHandler() |
|
|
|
|
277
|
|
|
{ |
278
|
|
|
$this->router->handle('/en/test/method/param1/param2'); |
279
|
|
|
|
280
|
|
|
$this->assertEquals($this->router->getActionNamespace(), 'Test\Action\Method\CliMethodAction'); |
281
|
|
|
$this->assertEquals($this->router->getResponderNamespace(), 'Test\Responder\Method\CliMethodResponder'); |
282
|
|
|
$this->assertEquals($this->router->getAction(), 'CliMethod'); |
283
|
|
|
$this->assertEquals($this->router->getBundle(), 'test'); |
284
|
|
|
$this->assertEquals($this->router->getParams(), ['param1', 'param2']); |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.