Skip to content

Commit 8203c66

Browse files
committed
Merge branch '2.5'
* 2.5: [Routing] add tests for RequestContext [Routing] fix inconsistencies in RequestContext don't raise warnings when exception is thrown [Framework][DX] Set the proper validator class according to the configured api version
2 parents cf3cd95 + b4ed800 commit 8203c66

File tree

5 files changed

+83
-30
lines changed

5 files changed

+83
-30
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

+3
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,9 @@ private function registerValidationConfiguration(array $config, ContainerBuilder
750750
break;
751751
case '2.5':
752752
$api = Validation::API_VERSION_2_5;
753+
// the validation class needs to be changed only for the 2.5 api since the deprecated interface is
754+
// set as the default interface
755+
$container->setParameter('validator.class', 'Symfony\Component\Validator\Validator\ValidatorInterface');
753756
break;
754757
default:
755758
$api = Validation::API_VERSION_2_5_BC;

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ public function testValidation2Dot4Api()
399399
$this->assertSame(array('loadValidatorMetadata'), $calls[4][1]);
400400
$this->assertSame('setApiVersion', $calls[5][0]);
401401
$this->assertSame(array(Validation::API_VERSION_2_4), $calls[5][1]);
402+
$this->assertSame('Symfony\Component\Validator\ValidatorInterface', $container->getParameter('validator.class'));
402403
// no cache, no annotations
403404
}
404405

@@ -414,6 +415,7 @@ public function testValidation2Dot5Api()
414415
$this->assertSame(array('loadValidatorMetadata'), $calls[4][1]);
415416
$this->assertSame('setApiVersion', $calls[5][0]);
416417
$this->assertSame(array(Validation::API_VERSION_2_5), $calls[5][1]);
418+
$this->assertSame('Symfony\Component\Validator\Validator\ValidatorInterface', $container->getParameter('validator.class'));
417419
// no cache, no annotations
418420
}
419421

@@ -429,6 +431,7 @@ public function testValidation2Dot5BcApi()
429431
$this->assertSame(array('loadValidatorMetadata'), $calls[4][1]);
430432
$this->assertSame('setApiVersion', $calls[5][0]);
431433
$this->assertSame(array(Validation::API_VERSION_2_5_BC), $calls[5][1]);
434+
$this->assertSame('Symfony\Component\Validator\ValidatorInterface', $container->getParameter('validator.class'));
432435
// no cache, no annotations
433436
}
434437

src/Symfony/Component/CssSelector/XPath/Translator.php

+4-6
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,15 @@ public function cssToXPath($cssExpr, $prefix = 'descendant-or-self::')
123123
$selectors = $this->parseSelectors($cssExpr);
124124

125125
/** @var SelectorNode $selector */
126-
foreach ($selectors as $selector) {
126+
foreach ($selectors as $index => $selector) {
127127
if (null !== $selector->getPseudoElement()) {
128128
throw new ExpressionErrorException('Pseudo-elements are not supported.');
129129
}
130-
}
131130

132-
$translator = $this;
131+
$selectors[$index] = $this->selectorToXPath($selector, $prefix);
132+
}
133133

134-
return implode(' | ', array_map(function (SelectorNode $selector) use ($translator, $prefix) {
135-
return $translator->selectorToXPath($selector, $prefix);
136-
}, $selectors));
134+
return implode(' | ', $selectors);
137135
}
138136

139137
/**

src/Symfony/Component/Routing/RequestContext.php

+31-24
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* Holds information about the current request.
1818
*
1919
* @author Fabien Potencier <fabien@symfony.com>
20+
* @author Tobias Schultze <http://tobion.de>
2021
*
2122
* @api
2223
*/
@@ -52,16 +53,21 @@ class RequestContext
5253
*/
5354
public function __construct($baseUrl = '', $method = 'GET', $host = 'localhost', $scheme = 'http', $httpPort = 80, $httpsPort = 443, $path = '/', $queryString = '')
5455
{
55-
$this->baseUrl = $baseUrl;
56-
$this->method = strtoupper($method);
57-
$this->host = $host;
58-
$this->scheme = strtolower($scheme);
59-
$this->httpPort = $httpPort;
60-
$this->httpsPort = $httpsPort;
61-
$this->pathInfo = $path;
62-
$this->queryString = $queryString;
56+
$this->setBaseUrl($baseUrl);
57+
$this->setMethod($method);
58+
$this->setHost($host);
59+
$this->setScheme($scheme);
60+
$this->setHttpPort($httpPort);
61+
$this->setHttpsPort($httpsPort);
62+
$this->setPathInfo($path);
63+
$this->setQueryString($queryString);
6364
}
6465

66+
/**
67+
* Updates the RequestContext information based on a HttpFoundation Request.
68+
*
69+
* @param Request $request A Request instance
70+
*/
6571
public function fromRequest(Request $request)
6672
{
6773
$this->setBaseUrl($request->getBaseUrl());
@@ -71,7 +77,7 @@ public function fromRequest(Request $request)
7177
$this->setScheme($request->getScheme());
7278
$this->setHttpPort($request->isSecure() ? $this->httpPort : $request->getPort());
7379
$this->setHttpsPort($request->isSecure() ? $request->getPort() : $this->httpsPort);
74-
$this->setQueryString($request->server->get('QUERY_STRING'));
80+
$this->setQueryString($request->server->get('QUERY_STRING', ''));
7581
}
7682

7783
/**
@@ -143,6 +149,8 @@ public function setMethod($method)
143149
/**
144150
* Gets the HTTP host.
145151
*
152+
* The host is always lowercased because it must be treated case-insensitive.
153+
*
146154
* @return string The HTTP host
147155
*/
148156
public function getHost()
@@ -159,7 +167,7 @@ public function getHost()
159167
*/
160168
public function setHost($host)
161169
{
162-
$this->host = $host;
170+
$this->host = strtolower($host);
163171
}
164172

165173
/**
@@ -187,7 +195,7 @@ public function setScheme($scheme)
187195
/**
188196
* Gets the HTTP port.
189197
*
190-
* @return string The HTTP port
198+
* @return int The HTTP port
191199
*/
192200
public function getHttpPort()
193201
{
@@ -197,19 +205,19 @@ public function getHttpPort()
197205
/**
198206
* Sets the HTTP port.
199207
*
200-
* @param string $httpPort The HTTP port
208+
* @param int $httpPort The HTTP port
201209
*
202210
* @api
203211
*/
204212
public function setHttpPort($httpPort)
205213
{
206-
$this->httpPort = $httpPort;
214+
$this->httpPort = (int) $httpPort;
207215
}
208216

209217
/**
210218
* Gets the HTTPS port.
211219
*
212-
* @return string The HTTPS port
220+
* @return int The HTTPS port
213221
*/
214222
public function getHttpsPort()
215223
{
@@ -219,19 +227,19 @@ public function getHttpsPort()
219227
/**
220228
* Sets the HTTPS port.
221229
*
222-
* @param string $httpsPort The HTTPS port
230+
* @param int $httpsPort The HTTPS port
223231
*
224232
* @api
225233
*/
226234
public function setHttpsPort($httpsPort)
227235
{
228-
$this->httpsPort = $httpsPort;
236+
$this->httpsPort = (int) $httpsPort;
229237
}
230238

231239
/**
232240
* Gets the query string.
233241
*
234-
* @return string The query string
242+
* @return string The query string without the "?"
235243
*/
236244
public function getQueryString()
237245
{
@@ -241,13 +249,14 @@ public function getQueryString()
241249
/**
242250
* Sets the query string.
243251
*
244-
* @param string $queryString The query string
252+
* @param string $queryString The query string (after "?")
245253
*
246254
* @api
247255
*/
248256
public function setQueryString($queryString)
249257
{
250-
$this->queryString = $queryString;
258+
// string cast to be fault-tolerant, accepting null
259+
$this->queryString = (string) $queryString;
251260
}
252261

253262
/**
@@ -263,11 +272,9 @@ public function getParameters()
263272
/**
264273
* Sets the parameters.
265274
*
266-
* This method implements a fluent interface.
267-
*
268275
* @param array $parameters The parameters
269276
*
270-
* @return Route The current Route instance
277+
* @return RequestContext The current instance, implementing a fluent interface
271278
*/
272279
public function setParameters(array $parameters)
273280
{
@@ -281,7 +288,7 @@ public function setParameters(array $parameters)
281288
*
282289
* @param string $name A parameter name
283290
*
284-
* @return mixed The parameter value
291+
* @return mixed The parameter value or null if nonexistent
285292
*/
286293
public function getParameter($name)
287294
{
@@ -293,7 +300,7 @@ public function getParameter($name)
293300
*
294301
* @param string $name A parameter name
295302
*
296-
* @return bool true if the parameter value is set, false otherwise
303+
* @return bool True if the parameter value is set, false otherwise
297304
*/
298305
public function hasParameter($name)
299306
{

src/Symfony/Component/Routing/Tests/RequestContextTest.php

+42
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,46 @@ public function testSetParameter()
9898

9999
$this->assertEquals('bar', $requestContext->getParameter('foo'));
100100
}
101+
102+
public function testMethod()
103+
{
104+
$requestContext = new RequestContext();
105+
$requestContext->setMethod('post');
106+
107+
$this->assertSame('POST', $requestContext->getMethod());
108+
}
109+
110+
public function testScheme()
111+
{
112+
$requestContext = new RequestContext();
113+
$requestContext->setScheme('HTTPS');
114+
115+
$this->assertSame('https', $requestContext->getScheme());
116+
}
117+
118+
public function testHost()
119+
{
120+
$requestContext = new RequestContext();
121+
$requestContext->setHost('eXampLe.com');
122+
123+
$this->assertSame('example.com', $requestContext->getHost());
124+
}
125+
126+
public function testQueryString()
127+
{
128+
$requestContext = new RequestContext();
129+
$requestContext->setQueryString(null);
130+
131+
$this->assertSame('', $requestContext->getQueryString());
132+
}
133+
134+
public function testPort()
135+
{
136+
$requestContext = new RequestContext();
137+
$requestContext->setHttpPort('123');
138+
$requestContext->setHttpsPort('456');
139+
140+
$this->assertSame(123, $requestContext->getHttpPort());
141+
$this->assertSame(456, $requestContext->getHttpsPort());
142+
}
101143
}

0 commit comments

Comments
 (0)