Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4f48b5a

Browse files
committed
[FrameworkBundle] Improve DX of RedirectController
1 parent 5b360e2 commit 4f48b5a

File tree

2 files changed

+160
-47
lines changed

2 files changed

+160
-47
lines changed

src/Symfony/Bundle/FrameworkBundle/Controller/RedirectController.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,13 @@ public function urlRedirectAction(Request $request, string $path, bool $permanen
146146

147147
return new RedirectResponse($url, $statusCode);
148148
}
149+
150+
public function __invoke(Request $request): Response
151+
{
152+
if (null !== $route = $request->attributes->get('route')) {
153+
return $this->redirectAction($request, $route, $request->attributes->getBoolean('permanent', false), $request->attributes->get('ignoreAttributes', false));
154+
}
155+
156+
return $this->urlRedirectAction($request, $request->attributes->get('path'), $request->attributes->getBoolean('permanent', false), $request->attributes->get('scheme'), $request->attributes->get('httpPort'), $request->attributes->get('httpsPort'));
157+
}
149158
}

src/Symfony/Bundle/FrameworkBundle/Tests/Controller/RedirectControllerTest.php

Lines changed: 151 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,31 @@ public function testEmptyRoute()
4444
}
4545
}
4646

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+
4768
/**
4869
* @dataProvider provider
4970
*/
50-
public function testRoute($permanent, $ignoreAttributes, $expectedCode, $expectedAttributes)
71+
public function testRoute($permanent, $ignoreAttributes, $expectedCode, $expectedAttributes, bool $invoke)
5172
{
5273
$request = new Request();
5374

@@ -56,15 +77,13 @@ public function testRoute($permanent, $ignoreAttributes, $expectedCode, $expecte
5677
$attributes = array(
5778
'route' => $route,
5879
'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,
6682
);
6783

84+
$attributes['_route_params'] = $attributes;
85+
$attributes['_route'] = 'current-route';
86+
6887
$request->attributes = new ParameterBag($attributes);
6988

7089
$router = $this->getMockBuilder(UrlGeneratorInterface::class)->getMock();
@@ -76,7 +95,7 @@ public function testRoute($permanent, $ignoreAttributes, $expectedCode, $expecte
7695

7796
$controller = new RedirectController($router);
7897

79-
$returnResponse = $controller->redirectAction($request, $route, $permanent, $ignoreAttributes);
98+
$returnResponse = $invoke ? $controller($request) : $controller->redirectAction($request, $route, $permanent, $ignoreAttributes);
8099

81100
$this->assertRedirectUrl($returnResponse, $url);
82101
$this->assertEquals($expectedCode, $returnResponse->getStatusCode());
@@ -85,10 +104,14 @@ public function testRoute($permanent, $ignoreAttributes, $expectedCode, $expecte
85104
public function provider()
86105
{
87106
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),
92115
);
93116
}
94117

@@ -112,6 +135,27 @@ public function testEmptyPath()
112135
}
113136
}
114137

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+
115159
public function testFullURL()
116160
{
117161
$request = new Request();
@@ -122,6 +166,16 @@ public function testFullURL()
122166
$this->assertEquals(302, $returnResponse->getStatusCode());
123167
}
124168

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+
125179
public function testUrlRedirectDefaultPorts()
126180
{
127181
$host = 'www.example.com';
@@ -143,84 +197,132 @@ public function testUrlRedirectDefaultPorts()
143197
$this->assertRedirectUrl($returnValue, $expectedUrl);
144198
}
145199

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+
146221
public function urlRedirectProvider()
147222
{
148223
return array(
149224
// 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),
159242

160243
// 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),
174269
);
175270
}
176271

177272
/**
178273
* @dataProvider urlRedirectProvider
179274
*/
180-
public function testUrlRedirect($scheme, $httpPort, $httpsPort, $requestScheme, $requestPort, $expectedPort)
275+
public function testUrlRedirect($scheme, $httpPort, $httpsPort, $requestScheme, $requestPort, $expectedPort, bool $invoke)
181276
{
182277
$host = 'www.example.com';
183278
$baseUrl = '/base';
184279
$path = '/redirect-path';
185280
$expectedUrl = "$scheme://$host$expectedPort$baseUrl$path";
186281

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);
188284
$controller = $this->createRedirectController();
189285

190-
$returnValue = $controller->urlRedirectAction($request, $path, false, $scheme, $httpPort, $httpsPort);
286+
$returnValue = $invoke ? $controller($request) : $controller->urlRedirectAction($request, $path, false, $scheme, $httpPort, $httpsPort);
191287
$this->assertRedirectUrl($returnValue, $expectedUrl);
192288
}
193289

194290
public function pathQueryParamsProvider()
195291
{
196292
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),
202303
);
203304
}
204305

205306
/**
206307
* @dataProvider pathQueryParamsProvider
207308
*/
208-
public function testPathQueryParams($expectedUrl, $path, $queryString)
309+
public function testPathQueryParams($expectedUrl, $path, $queryString, bool $invoke)
209310
{
210311
$scheme = 'http';
211312
$host = 'www.example.com';
212313
$baseUrl = '/base';
213314
$port = 80;
214315

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);
216318

217319
$controller = $this->createRedirectController();
218320

219-
$returnValue = $controller->urlRedirectAction($request, $path, false, $scheme, $port, null);
321+
$returnValue = $invoke ? $controller($request) : $controller->urlRedirectAction($request, $path, false, $scheme, $port, null);
220322
$this->assertRedirectUrl($returnValue, $expectedUrl);
221323
}
222324

223-
private function createRequestObject($scheme, $host, $port, $baseUrl, $queryString = '')
325+
private function createRequestObject($scheme, $host, $port, $baseUrl, $queryString = '', $attributes = array())
224326
{
225327
$request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock();
226328
$request
@@ -244,6 +346,8 @@ private function createRequestObject($scheme, $host, $port, $baseUrl, $queryStri
244346
->method('getQueryString')
245347
->will($this->returnValue($queryString));
246348

349+
$request->attributes = new ParameterBag($attributes);
350+
247351
return $request;
248352
}
249353

0 commit comments

Comments
 (0)