@@ -44,10 +44,31 @@ public function testEmptyRoute()
44
44
}
45
45
}
46
46
47
+ public function testEmptyRouteInvoke ()
48
+ {
49
+ $ request = new Request (array (), array (), array ('route ' => '' , 'permanent ' => true ));
50
+ $ controller = new RedirectController ();
51
+
52
+ try {
53
+ $ controller ($ request );
54
+ $ this ->fail ('Expected Symfony\Component\HttpKernel\Exception\HttpException to be thrown ' );
55
+ } catch (HttpException $ e ) {
56
+ $ this ->assertSame (410 , $ e ->getStatusCode ());
57
+ }
58
+
59
+ $ request = new Request (array (), array (), array ('route ' => '' , 'permanent ' => false ));
60
+ try {
61
+ $ controller ($ request );
62
+ $ this ->fail ('Expected Symfony\Component\HttpKernel\Exception\HttpException to be thrown ' );
63
+ } catch (HttpException $ e ) {
64
+ $ this ->assertSame (404 , $ e ->getStatusCode ());
65
+ }
66
+ }
67
+
47
68
/**
48
69
* @dataProvider provider
49
70
*/
50
- public function testRoute ($ permanent , $ ignoreAttributes , $ expectedCode , $ expectedAttributes )
71
+ public function testRoute ($ permanent , $ ignoreAttributes , $ expectedCode , $ expectedAttributes, bool $ invoke )
51
72
{
52
73
$ request = new Request ();
53
74
@@ -56,15 +77,13 @@ public function testRoute($permanent, $ignoreAttributes, $expectedCode, $expecte
56
77
$ attributes = array (
57
78
'route ' => $ route ,
58
79
'permanent ' => $ permanent ,
59
- '_route ' => 'current-route ' ,
60
- '_route_params ' => array (
61
- 'route ' => $ route ,
62
- 'permanent ' => $ permanent ,
63
- 'additional-parameter ' => 'value ' ,
64
- 'ignoreAttributes ' => $ ignoreAttributes ,
65
- ),
80
+ 'additional-parameter ' => 'value ' ,
81
+ 'ignoreAttributes ' => $ ignoreAttributes ,
66
82
);
67
83
84
+ $ attributes ['_route_params ' ] = $ attributes ;
85
+ $ attributes ['_route ' ] = 'current-route ' ;
86
+
68
87
$ request ->attributes = new ParameterBag ($ attributes );
69
88
70
89
$ router = $ this ->getMockBuilder (UrlGeneratorInterface::class)->getMock ();
@@ -76,7 +95,7 @@ public function testRoute($permanent, $ignoreAttributes, $expectedCode, $expecte
76
95
77
96
$ controller = new RedirectController ($ router );
78
97
79
- $ returnResponse = $ controller ->redirectAction ($ request , $ route , $ permanent , $ ignoreAttributes );
98
+ $ returnResponse = $ invoke ? $ controller ( $ request ) : $ controller ->redirectAction ($ request , $ route , $ permanent , $ ignoreAttributes );
80
99
81
100
$ this ->assertRedirectUrl ($ returnResponse , $ url );
82
101
$ this ->assertEquals ($ expectedCode , $ returnResponse ->getStatusCode ());
@@ -85,10 +104,14 @@ public function testRoute($permanent, $ignoreAttributes, $expectedCode, $expecte
85
104
public function provider ()
86
105
{
87
106
return array (
88
- array (true , false , 301 , array ('additional-parameter ' => 'value ' )),
89
- array (false , false , 302 , array ('additional-parameter ' => 'value ' )),
90
- array (false , true , 302 , array ()),
91
- array (false , array ('additional-parameter ' ), 302 , array ()),
107
+ array (true , false , 301 , array ('additional-parameter ' => 'value ' ), false ),
108
+ array (true , false , 301 , array ('additional-parameter ' => 'value ' ), true ),
109
+ array (false , false , 302 , array ('additional-parameter ' => 'value ' ), false ),
110
+ array (false , false , 302 , array ('additional-parameter ' => 'value ' ), true ),
111
+ array (false , true , 302 , array (), false ),
112
+ array (false , true , 302 , array (), true ),
113
+ array (false , array ('additional-parameter ' ), 302 , array (), false ),
114
+ array (false , array ('additional-parameter ' ), 302 , array (), true ),
92
115
);
93
116
}
94
117
@@ -112,6 +135,27 @@ public function testEmptyPath()
112
135
}
113
136
}
114
137
138
+ public function testEmptyPathInvoke ()
139
+ {
140
+ $ request = new Request (array (), array (), array ('path ' => '' , 'permanent ' => true ));
141
+ $ controller = new RedirectController ();
142
+
143
+ try {
144
+ $ controller ($ request );
145
+ $ this ->fail ('Expected Symfony\Component\HttpKernel\Exception\HttpException to be thrown ' );
146
+ } catch (HttpException $ e ) {
147
+ $ this ->assertSame (410 , $ e ->getStatusCode ());
148
+ }
149
+
150
+ $ request = new Request (array (), array (), array ('path ' => '' , 'permanent ' => false ));
151
+ try {
152
+ $ controller ($ request );
153
+ $ this ->fail ('Expected Symfony\Component\HttpKernel\Exception\HttpException to be thrown ' );
154
+ } catch (HttpException $ e ) {
155
+ $ this ->assertSame (404 , $ e ->getStatusCode ());
156
+ }
157
+ }
158
+
115
159
public function testFullURL ()
116
160
{
117
161
$ request = new Request ();
@@ -122,6 +166,16 @@ public function testFullURL()
122
166
$ this ->assertEquals (302 , $ returnResponse ->getStatusCode ());
123
167
}
124
168
169
+ public function testFullURLInvoke ()
170
+ {
171
+ $ request = new Request (array (), array (), array ('path ' => 'http://foo.bar/ ' ));
172
+ $ controller = new RedirectController ();
173
+ $ returnResponse = $ controller ($ request );
174
+
175
+ $ this ->assertRedirectUrl ($ returnResponse , 'http://foo.bar/ ' );
176
+ $ this ->assertEquals (302 , $ returnResponse ->getStatusCode ());
177
+ }
178
+
125
179
public function testUrlRedirectDefaultPorts ()
126
180
{
127
181
$ host = 'www.example.com ' ;
@@ -143,84 +197,132 @@ public function testUrlRedirectDefaultPorts()
143
197
$ this ->assertRedirectUrl ($ returnValue , $ expectedUrl );
144
198
}
145
199
200
+ public function testUrlRedirectDefaultPortsInvoke ()
201
+ {
202
+ $ host = 'www.example.com ' ;
203
+ $ baseUrl = '/base ' ;
204
+ $ path = '/redirect-path ' ;
205
+ $ httpPort = 1080 ;
206
+ $ httpsPort = 1443 ;
207
+
208
+ $ expectedUrl = "https:// $ host: $ httpsPort$ baseUrl$ path " ;
209
+ $ request = $ this ->createRequestObject ('http ' , $ host , $ httpPort , $ baseUrl , '' , array ('path ' => $ path , 'permanent ' => false , 'scheme ' => 'https ' , 'httpPort ' => $ httpPort ));
210
+ $ controller = $ this ->createRedirectController (null , $ httpsPort );
211
+ $ returnValue = $ controller ($ request );
212
+ $ this ->assertRedirectUrl ($ returnValue , $ expectedUrl );
213
+
214
+ $ expectedUrl = "http:// $ host: $ httpPort$ baseUrl$ path " ;
215
+ $ request = $ this ->createRequestObject ('https ' , $ host , $ httpPort , $ baseUrl , '' , array ('path ' => $ path , 'permanent ' => false , 'scheme ' => 'http ' ));
216
+ $ controller = $ this ->createRedirectController ($ httpPort );
217
+ $ returnValue = $ controller ($ request );
218
+ $ this ->assertRedirectUrl ($ returnValue , $ expectedUrl );
219
+ }
220
+
146
221
public function urlRedirectProvider ()
147
222
{
148
223
return array (
149
224
// Standard ports
150
- array ('http ' , null , null , 'http ' , 80 , '' ),
151
- array ('http ' , 80 , null , 'http ' , 80 , '' ),
152
- array ('https ' , null , null , 'http ' , 80 , '' ),
153
- array ('https ' , 80 , null , 'http ' , 80 , '' ),
154
-
155
- array ('http ' , null , null , 'https ' , 443 , '' ),
156
- array ('http ' , null , 443 , 'https ' , 443 , '' ),
157
- array ('https ' , null , null , 'https ' , 443 , '' ),
158
- array ('https ' , null , 443 , 'https ' , 443 , '' ),
225
+ array ('http ' , null , null , 'http ' , 80 , '' , false ),
226
+ array ('http ' , null , null , 'http ' , 80 , '' , true ),
227
+ array ('http ' , 80 , null , 'http ' , 80 , '' , false ),
228
+ array ('http ' , 80 , null , 'http ' , 80 , '' , true ),
229
+ array ('https ' , null , null , 'http ' , 80 , '' , false ),
230
+ array ('https ' , null , null , 'http ' , 80 , '' , true ),
231
+ array ('https ' , 80 , null , 'http ' , 80 , '' , false ),
232
+ array ('https ' , 80 , null , 'http ' , 80 , '' , true ),
233
+
234
+ array ('http ' , null , null , 'https ' , 443 , '' , false ),
235
+ array ('http ' , null , null , 'https ' , 443 , '' , true ),
236
+ array ('http ' , null , 443 , 'https ' , 443 , '' , false ),
237
+ array ('http ' , null , 443 , 'https ' , 443 , '' , true ),
238
+ array ('https ' , null , null , 'https ' , 443 , '' , false ),
239
+ array ('https ' , null , null , 'https ' , 443 , '' , true ),
240
+ array ('https ' , null , 443 , 'https ' , 443 , '' , false ),
241
+ array ('https ' , null , 443 , 'https ' , 443 , '' , true ),
159
242
160
243
// Non-standard ports
161
- array ('http ' , null , null , 'http ' , 8080 , ':8080 ' ),
162
- array ('http ' , 4080 , null , 'http ' , 8080 , ':4080 ' ),
163
- array ('http ' , 80 , null , 'http ' , 8080 , '' ),
164
- array ('https ' , null , null , 'http ' , 8080 , '' ),
165
- array ('https ' , null , 8443 , 'http ' , 8080 , ':8443 ' ),
166
- array ('https ' , null , 443 , 'http ' , 8080 , '' ),
167
-
168
- array ('https ' , null , null , 'https ' , 8443 , ':8443 ' ),
169
- array ('https ' , null , 4443 , 'https ' , 8443 , ':4443 ' ),
170
- array ('https ' , null , 443 , 'https ' , 8443 , '' ),
171
- array ('http ' , null , null , 'https ' , 8443 , '' ),
172
- array ('http ' , 8080 , 4443 , 'https ' , 8443 , ':8080 ' ),
173
- array ('http ' , 80 , 4443 , 'https ' , 8443 , '' ),
244
+ array ('http ' , null , null , 'http ' , 8080 , ':8080 ' , false ),
245
+ array ('http ' , null , null , 'http ' , 8080 , ':8080 ' , true ),
246
+ array ('http ' , 4080 , null , 'http ' , 8080 , ':4080 ' , false ),
247
+ array ('http ' , 4080 , null , 'http ' , 8080 , ':4080 ' , true ),
248
+ array ('http ' , 80 , null , 'http ' , 8080 , '' , false ),
249
+ array ('http ' , 80 , null , 'http ' , 8080 , '' , true ),
250
+ array ('https ' , null , null , 'http ' , 8080 , '' , false ),
251
+ array ('https ' , null , null , 'http ' , 8080 , '' , true ),
252
+ array ('https ' , null , 8443 , 'http ' , 8080 , ':8443 ' , false ),
253
+ array ('https ' , null , 8443 , 'http ' , 8080 , ':8443 ' , true ),
254
+ array ('https ' , null , 443 , 'http ' , 8080 , '' , false ),
255
+ array ('https ' , null , 443 , 'http ' , 8080 , '' , true ),
256
+
257
+ array ('https ' , null , null , 'https ' , 8443 , ':8443 ' , false ),
258
+ array ('https ' , null , null , 'https ' , 8443 , ':8443 ' , true ),
259
+ array ('https ' , null , 4443 , 'https ' , 8443 , ':4443 ' , false ),
260
+ array ('https ' , null , 4443 , 'https ' , 8443 , ':4443 ' , true ),
261
+ array ('https ' , null , 443 , 'https ' , 8443 , '' , false ),
262
+ array ('https ' , null , 443 , 'https ' , 8443 , '' , true ),
263
+ array ('http ' , null , null , 'https ' , 8443 , '' , false ),
264
+ array ('http ' , null , null , 'https ' , 8443 , '' , true ),
265
+ array ('http ' , 8080 , 4443 , 'https ' , 8443 , ':8080 ' , false ),
266
+ array ('http ' , 8080 , 4443 , 'https ' , 8443 , ':8080 ' , true ),
267
+ array ('http ' , 80 , 4443 , 'https ' , 8443 , '' , false ),
268
+ array ('http ' , 80 , 4443 , 'https ' , 8443 , '' , true ),
174
269
);
175
270
}
176
271
177
272
/**
178
273
* @dataProvider urlRedirectProvider
179
274
*/
180
- public function testUrlRedirect ($ scheme , $ httpPort , $ httpsPort , $ requestScheme , $ requestPort , $ expectedPort )
275
+ public function testUrlRedirect ($ scheme , $ httpPort , $ httpsPort , $ requestScheme , $ requestPort , $ expectedPort, bool $ invoke )
181
276
{
182
277
$ host = 'www.example.com ' ;
183
278
$ baseUrl = '/base ' ;
184
279
$ path = '/redirect-path ' ;
185
280
$ expectedUrl = "$ scheme:// $ host$ expectedPort$ baseUrl$ path " ;
186
281
187
- $ request = $ this ->createRequestObject ($ requestScheme , $ host , $ requestPort , $ baseUrl );
282
+ $ attributes = $ invoke ? array ('path ' => $ path , 'permanent ' => false , 'scheme ' => $ scheme , 'httpPort ' => $ httpPort , 'httpsPort ' => $ httpsPort ) : array ();
283
+ $ request = $ this ->createRequestObject ($ requestScheme , $ host , $ requestPort , $ baseUrl , '' , $ attributes );
188
284
$ controller = $ this ->createRedirectController ();
189
285
190
- $ returnValue = $ controller ->urlRedirectAction ($ request , $ path , false , $ scheme , $ httpPort , $ httpsPort );
286
+ $ returnValue = $ invoke ? $ controller ( $ request ) : $ controller ->urlRedirectAction ($ request , $ path , false , $ scheme , $ httpPort , $ httpsPort );
191
287
$ this ->assertRedirectUrl ($ returnValue , $ expectedUrl );
192
288
}
193
289
194
290
public function pathQueryParamsProvider ()
195
291
{
196
292
return array (
197
- array ('http://www.example.com/base/redirect-path ' , '/redirect-path ' , '' ),
198
- array ('http://www.example.com/base/redirect-path?foo=bar ' , '/redirect-path?foo=bar ' , '' ),
199
- array ('http://www.example.com/base/redirect-path?foo=bar ' , '/redirect-path ' , 'foo=bar ' ),
200
- array ('http://www.example.com/base/redirect-path?foo=bar&abc=example ' , '/redirect-path?foo=bar ' , 'abc=example ' ),
201
- array ('http://www.example.com/base/redirect-path?foo=bar&abc=example&baz=def ' , '/redirect-path?foo=bar ' , 'abc=example&baz=def ' ),
293
+ array ('http://www.example.com/base/redirect-path ' , '/redirect-path ' , '' , false ),
294
+ array ('http://www.example.com/base/redirect-path ' , '/redirect-path ' , '' , true ),
295
+ array ('http://www.example.com/base/redirect-path?foo=bar ' , '/redirect-path?foo=bar ' , '' , false ),
296
+ array ('http://www.example.com/base/redirect-path?foo=bar ' , '/redirect-path?foo=bar ' , '' , true ),
297
+ array ('http://www.example.com/base/redirect-path?foo=bar ' , '/redirect-path ' , 'foo=bar ' , false ),
298
+ array ('http://www.example.com/base/redirect-path?foo=bar ' , '/redirect-path ' , 'foo=bar ' , true ),
299
+ array ('http://www.example.com/base/redirect-path?foo=bar&abc=example ' , '/redirect-path?foo=bar ' , 'abc=example ' , false ),
300
+ array ('http://www.example.com/base/redirect-path?foo=bar&abc=example ' , '/redirect-path?foo=bar ' , 'abc=example ' , true ),
301
+ array ('http://www.example.com/base/redirect-path?foo=bar&abc=example&baz=def ' , '/redirect-path?foo=bar ' , 'abc=example&baz=def ' , false ),
302
+ array ('http://www.example.com/base/redirect-path?foo=bar&abc=example&baz=def ' , '/redirect-path?foo=bar ' , 'abc=example&baz=def ' , true ),
202
303
);
203
304
}
204
305
205
306
/**
206
307
* @dataProvider pathQueryParamsProvider
207
308
*/
208
- public function testPathQueryParams ($ expectedUrl , $ path , $ queryString )
309
+ public function testPathQueryParams ($ expectedUrl , $ path , $ queryString, bool $ invoke )
209
310
{
210
311
$ scheme = 'http ' ;
211
312
$ host = 'www.example.com ' ;
212
313
$ baseUrl = '/base ' ;
213
314
$ port = 80 ;
214
315
215
- $ request = $ this ->createRequestObject ($ scheme , $ host , $ port , $ baseUrl , $ queryString );
316
+ $ attributes = $ invoke ? array ('path ' => $ path , 'permanent ' => false , 'scheme ' => $ scheme , 'port ' => $ port ) : array ();
317
+ $ request = $ this ->createRequestObject ($ scheme , $ host , $ port , $ baseUrl , $ queryString , $ attributes );
216
318
217
319
$ controller = $ this ->createRedirectController ();
218
320
219
- $ returnValue = $ controller ->urlRedirectAction ($ request , $ path , false , $ scheme , $ port , null );
321
+ $ returnValue = $ invoke ? $ controller ( $ request ) : $ controller ->urlRedirectAction ($ request , $ path , false , $ scheme , $ port , null );
220
322
$ this ->assertRedirectUrl ($ returnValue , $ expectedUrl );
221
323
}
222
324
223
- private function createRequestObject ($ scheme , $ host , $ port , $ baseUrl , $ queryString = '' )
325
+ private function createRequestObject ($ scheme , $ host , $ port , $ baseUrl , $ queryString = '' , $ attributes = array () )
224
326
{
225
327
$ request = $ this ->getMockBuilder ('Symfony\Component\HttpFoundation\Request ' )->getMock ();
226
328
$ request
@@ -244,6 +346,8 @@ private function createRequestObject($scheme, $host, $port, $baseUrl, $queryStri
244
346
->method ('getQueryString ' )
245
347
->will ($ this ->returnValue ($ queryString ));
246
348
349
+ $ request ->attributes = new ParameterBag ($ attributes );
350
+
247
351
return $ request ;
248
352
}
249
353
0 commit comments