1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mouf\Mvc\Splash\Store; |
4
|
|
|
use Mouf\Mvc\Splash\Services\SplashRoute; |
5
|
|
|
use Mouf\Mvc\Splash\Utils\SplashException; |
6
|
|
|
use Zend\Diactoros\ServerRequest; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* A SplashUrlNode is a datastructure optimised to navigate all possible URLs known to the application. |
11
|
|
|
* A SplashUrlNode represents all possible routes starting at the current position (just after a / in a URL). |
12
|
|
|
* |
13
|
|
|
* @author David Negrier |
14
|
|
|
*/ |
15
|
|
|
class SplashUrlNodeTest extends \PHPUnit_Framework_TestCase |
16
|
|
|
{ |
17
|
|
View Code Duplication |
public function testAddUrl() |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
$splashUrlNode = new SplashUrlNode(); |
20
|
|
|
$callback = new SplashRoute('toto/tata', 'myController', 'myMethod', 'myTitle', 'myComment', 'fullComment', array('GET', 'POST')); |
21
|
|
|
$splashUrlNode->registerCallback($callback); |
22
|
|
|
|
23
|
|
|
$result = $splashUrlNode->walk('toto/tata', new ServerRequest([], [], 'toto/tata', 'GET')); |
24
|
|
|
|
25
|
|
|
/* @var $result SplashRoute */ |
26
|
|
|
$this->assertInstanceOf(SplashRoute::class, $result); |
27
|
|
|
$this->assertEquals('myController', $result->controllerInstanceName); |
28
|
|
|
$this->assertEquals('myMethod', $result->methodName); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
View Code Duplication |
public function testTrailingSlashUrl() |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
$splashUrlNode = new SplashUrlNode(); |
34
|
|
|
$callback = new SplashRoute('toto/tata/', 'myController', 'myMethod', 'myTitle', 'myComment', 'fullComment', array('GET', 'POST')); |
35
|
|
|
$splashUrlNode->registerCallback($callback); |
36
|
|
|
|
37
|
|
|
$result = $splashUrlNode->walk('toto/tata/', new ServerRequest([], [], 'toto/tata', 'GET')); |
38
|
|
|
/* @var $result SplashRoute */ |
39
|
|
|
$this->assertInstanceOf(SplashRoute::class, $result); |
40
|
|
|
$this->assertEquals('myController', $result->controllerInstanceName); |
41
|
|
|
$this->assertEquals('myMethod', $result->methodName); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
View Code Duplication |
public function testRootUrl() |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
$splashUrlNode = new SplashUrlNode(); |
47
|
|
|
$callback = new SplashRoute('/', 'myController', 'myMethod', 'myTitle', 'myComment', 'fullComment', array('GET', 'POST')); |
48
|
|
|
$splashUrlNode->registerCallback($callback); |
49
|
|
|
|
50
|
|
|
$result = $splashUrlNode->walk('/', new ServerRequest([], [], '/', 'GET')); |
51
|
|
|
/* @var $result SplashRoute */ |
52
|
|
|
$this->assertInstanceOf(SplashRoute::class, $result); |
53
|
|
|
$this->assertEquals('myController', $result->controllerInstanceName); |
54
|
|
|
$this->assertEquals('myMethod', $result->methodName); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
View Code Duplication |
public function testSameUrls() |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$splashUrlNode = new SplashUrlNode(); |
60
|
|
|
$callback = new SplashRoute('/', 'myController', 'myMethod', 'myTitle', 'myComment', 'fullComment', array('GET', 'POST')); |
61
|
|
|
$splashUrlNode->registerCallback($callback); |
62
|
|
|
$callback = new SplashRoute('/', 'myController', 'myMethod', 'myTitle', 'myComment', 'fullComment', array('GET', 'POST')); |
63
|
|
|
|
64
|
|
|
$this->expectException(SplashException::class); |
65
|
|
|
$splashUrlNode->registerCallback($callback); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* |
70
|
|
|
*/ |
71
|
|
View Code Duplication |
public function testGlobalUrlCatchGet() |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$splashUrlNode = new SplashUrlNode(); |
74
|
|
|
$callback = new SplashRoute('/toto', 'myController', 'myMethod', 'myTitle', 'myComment', 'fullComment', array()); |
75
|
|
|
$splashUrlNode->registerCallback($callback); |
76
|
|
|
|
77
|
|
|
$result = $splashUrlNode->walk('/toto', new ServerRequest([], [], '/toto', 'GET')); |
78
|
|
|
/* @var $result SplashRoute */ |
79
|
|
|
$this->assertInstanceOf(SplashRoute::class, $result); |
80
|
|
|
$this->assertEquals('myController', $result->controllerInstanceName); |
81
|
|
|
$this->assertEquals('myMethod', $result->methodName); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* |
86
|
|
|
*/ |
87
|
|
|
public function testMultiUrls() |
88
|
|
|
{ |
89
|
|
|
$splashUrlNode = new SplashUrlNode(); |
90
|
|
|
$callback = new SplashRoute('/toto', 'myControllerOk', 'myMethodOk', 'myTitle', 'myComment', 'fullComment', array()); |
91
|
|
|
$splashUrlNode->registerCallback($callback); |
92
|
|
|
$callback = new SplashRoute('/toto/tata', 'myController', 'myMethod', 'myTitle', 'myComment', 'fullComment', array()); |
93
|
|
|
$splashUrlNode->registerCallback($callback); |
94
|
|
|
$callback = new SplashRoute('/tata', 'myController', 'myMethod', 'myTitle', 'myComment', 'fullComment', array()); |
95
|
|
|
$splashUrlNode->registerCallback($callback); |
96
|
|
|
|
97
|
|
|
$result = $splashUrlNode->walk('/toto', new ServerRequest([], [], '/toto', 'POST')); |
98
|
|
|
/* @var $result SplashRoute */ |
99
|
|
|
$this->assertInstanceOf(SplashRoute::class, $result); |
100
|
|
|
$this->assertEquals('myControllerOk', $result->controllerInstanceName); |
101
|
|
|
$this->assertEquals('myMethodOk', $result->methodName); |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* |
107
|
|
|
*/ |
108
|
|
View Code Duplication |
public function testParametersUrls() |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
$splashUrlNode = new SplashUrlNode(); |
111
|
|
|
$callback = new SplashRoute('/toto/{var}/tata', 'myController', 'myMethod', 'myTitle', 'myComment', 'fullComment', []); |
112
|
|
|
$splashUrlNode->registerCallback($callback); |
113
|
|
|
|
114
|
|
|
$result = $splashUrlNode->walk('/toto/12/tata', new ServerRequest([], [], '/toto/12/tata', 'POST')); |
115
|
|
|
/* @var $result SplashRoute */ |
116
|
|
|
$this->assertInstanceOf(SplashRoute::class, $result); |
117
|
|
|
$this->assertEquals('myController', $result->controllerInstanceName); |
118
|
|
|
$this->assertEquals('myMethod', $result->methodName); |
119
|
|
|
$this->assertEquals(12, $result->filledParameters['var']); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* |
124
|
|
|
*/ |
125
|
|
|
public function testWildcardUrls() |
126
|
|
|
{ |
127
|
|
|
$splashUrlNode = new SplashUrlNode(); |
128
|
|
|
$callback = new SplashRoute('/toto/*', 'myController', 'myMethod', 'myTitle', 'myComment', 'fullComment', array()); |
129
|
|
|
$splashUrlNode->registerCallback($callback); |
130
|
|
|
$callback2 = new SplashRoute('/toto/*', 'myControllerPost', 'myMethodPost', 'myTitle', 'myComment', 'fullComment', array('POST')); |
131
|
|
|
$splashUrlNode->registerCallback($callback2); |
132
|
|
|
|
133
|
|
|
$result = $splashUrlNode->walk('/toto/tata/titi', new ServerRequest([], [], '/toto', 'GET')); |
134
|
|
|
/* @var $result SplashRoute */ |
135
|
|
|
$this->assertInstanceOf(SplashRoute::class, $result); |
136
|
|
|
$this->assertEquals('myController', $result->controllerInstanceName); |
137
|
|
|
$this->assertEquals('myMethod', $result->methodName); |
138
|
|
|
|
139
|
|
|
$result = $splashUrlNode->walk('/toto/', new ServerRequest([], [], '/toto', 'GET')); |
140
|
|
|
/* @var $result SplashRoute */ |
141
|
|
|
$this->assertInstanceOf(SplashRoute::class, $result); |
142
|
|
|
$this->assertEquals('myController', $result->controllerInstanceName); |
143
|
|
|
$this->assertEquals('myMethod', $result->methodName); |
144
|
|
|
|
145
|
|
|
// Now, let's test an URL with HTTP method set. |
146
|
|
|
$result = $splashUrlNode->walk('/toto/tata/titi', new ServerRequest([], [], '/toto', 'POST')); |
147
|
|
|
$this->assertEquals('myControllerPost', $result->controllerInstanceName); |
148
|
|
|
$this->assertEquals('myMethodPost', $result->methodName); |
149
|
|
|
|
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function testUnsupportedWildcard() |
153
|
|
|
{ |
154
|
|
|
$splashUrlNode = new SplashUrlNode(); |
155
|
|
|
$callback = new SplashRoute('toto/*/tata', 'myController', 'myMethod', 'myTitle', 'myComment'); |
156
|
|
|
$this->expectException(SplashException::class); |
157
|
|
|
$splashUrlNode->registerCallback($callback); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
View Code Duplication |
public function testDoubleMethod() |
|
|
|
|
161
|
|
|
{ |
162
|
|
|
$splashUrlNode = new SplashUrlNode(); |
163
|
|
|
$callback = new SplashRoute('foo/bar', 'myController', 'myMethod', 'myTitle', 'myComment'); |
164
|
|
|
$splashUrlNode->registerCallback($callback); |
165
|
|
|
$callback2 = new SplashRoute('foo/bar', 'myController2', 'myMethod2', 'myTitle', 'myComment'); |
166
|
|
|
$this->expectException(SplashException::class); |
167
|
|
|
$splashUrlNode->registerCallback($callback2); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
View Code Duplication |
public function testDoubleWildcardMethod() |
|
|
|
|
171
|
|
|
{ |
172
|
|
|
$splashUrlNode = new SplashUrlNode(); |
173
|
|
|
$callback = new SplashRoute('foo/*', 'myController', 'myMethod', 'myTitle', 'myComment'); |
174
|
|
|
$splashUrlNode->registerCallback($callback); |
175
|
|
|
$callback2 = new SplashRoute('foo/*', 'myController2', 'myMethod2', 'myTitle', 'myComment'); |
176
|
|
|
$this->expectException(SplashException::class); |
177
|
|
|
$splashUrlNode->registerCallback($callback2); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
View Code Duplication |
public function testDoubleWildcardMethodWithHttpMethod() |
|
|
|
|
181
|
|
|
{ |
182
|
|
|
$splashUrlNode = new SplashUrlNode(); |
183
|
|
|
$callback = new SplashRoute('foo/*', 'myController', 'myMethod', 'myTitle', 'myComment', 'fullComment', ['GET', 'POST']); |
184
|
|
|
$splashUrlNode->registerCallback($callback); |
185
|
|
|
$callback2 = new SplashRoute('foo/*', 'myController2', 'myMethod2', 'myTitle', 'myComment', 'fullComment', ['GET']); |
186
|
|
|
$this->expectException(SplashException::class); |
187
|
|
|
$splashUrlNode->registerCallback($callback2); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function testDoubleParameter() |
191
|
|
|
{ |
192
|
|
|
$splashUrlNode = new SplashUrlNode(); |
193
|
|
|
$callback = new SplashRoute('foo/{var}/bar/{var}/', 'myController', 'myMethod', 'myTitle', 'myComment'); |
194
|
|
|
$splashUrlNode->registerCallback($callback); |
195
|
|
|
|
196
|
|
|
$this->expectException(SplashException::class); |
197
|
|
|
$splashUrlNode->walk('foo/12/bar/42/', new ServerRequest([], [], '/toto', 'GET')); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function testFallbackToWilcard() |
201
|
|
|
{ |
202
|
|
|
$splashUrlNode = new SplashUrlNode(); |
203
|
|
|
$callback = new SplashRoute('foo/bar/baz', 'myController', 'myMethod', 'myTitle', 'myComment', 'fullComment'); |
204
|
|
|
$splashUrlNode->registerCallback($callback); |
205
|
|
|
$callback2 = new SplashRoute('foo/*', 'myController2', 'myMethod2', 'myTitle', 'myComment', 'fullComment'); |
206
|
|
|
$splashUrlNode->registerCallback($callback2); |
207
|
|
|
|
208
|
|
|
$result = $splashUrlNode->walk('foo/bar', new ServerRequest([], [], '/foo/bar/biz', 'POST')); |
209
|
|
|
$this->assertEquals('myController2', $result->controllerInstanceName); |
210
|
|
|
$this->assertEquals('myMethod2', $result->methodName); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
|
214
|
|
|
|
215
|
|
|
} |
216
|
|
|
|
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.