Merge branch '2.3' into 2.5
* 2.3: [Routing] add tests for RequestContext [Routing] fix inconsistencies in RequestContext don't raise warnings when exception is thrown
This commit is contained in:
commit
b4ed800529
@ -123,17 +123,15 @@ class Translator implements TranslatorInterface
|
|||||||
$selectors = $this->parseSelectors($cssExpr);
|
$selectors = $this->parseSelectors($cssExpr);
|
||||||
|
|
||||||
/** @var SelectorNode $selector */
|
/** @var SelectorNode $selector */
|
||||||
foreach ($selectors as $selector) {
|
foreach ($selectors as $index => $selector) {
|
||||||
if (null !== $selector->getPseudoElement()) {
|
if (null !== $selector->getPseudoElement()) {
|
||||||
throw new ExpressionErrorException('Pseudo-elements are not supported.');
|
throw new ExpressionErrorException('Pseudo-elements are not supported.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$selectors[$index] = $this->selectorToXPath($selector, $prefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
$translator = $this;
|
return implode(' | ', $selectors);
|
||||||
|
|
||||||
return implode(' | ', array_map(function (SelectorNode $selector) use ($translator, $prefix) {
|
|
||||||
return $translator->selectorToXPath($selector, $prefix);
|
|
||||||
}, $selectors));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -17,6 +17,7 @@ use Symfony\Component\HttpFoundation\Request;
|
|||||||
* Holds information about the current request.
|
* Holds information about the current request.
|
||||||
*
|
*
|
||||||
* @author Fabien Potencier <fabien@symfony.com>
|
* @author Fabien Potencier <fabien@symfony.com>
|
||||||
|
* @author Tobias Schultze <http://tobion.de>
|
||||||
*
|
*
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
@ -52,16 +53,21 @@ class RequestContext
|
|||||||
*/
|
*/
|
||||||
public function __construct($baseUrl = '', $method = 'GET', $host = 'localhost', $scheme = 'http', $httpPort = 80, $httpsPort = 443, $path = '/', $queryString = '')
|
public function __construct($baseUrl = '', $method = 'GET', $host = 'localhost', $scheme = 'http', $httpPort = 80, $httpsPort = 443, $path = '/', $queryString = '')
|
||||||
{
|
{
|
||||||
$this->baseUrl = $baseUrl;
|
$this->setBaseUrl($baseUrl);
|
||||||
$this->method = strtoupper($method);
|
$this->setMethod($method);
|
||||||
$this->host = $host;
|
$this->setHost($host);
|
||||||
$this->scheme = strtolower($scheme);
|
$this->setScheme($scheme);
|
||||||
$this->httpPort = $httpPort;
|
$this->setHttpPort($httpPort);
|
||||||
$this->httpsPort = $httpsPort;
|
$this->setHttpsPort($httpsPort);
|
||||||
$this->pathInfo = $path;
|
$this->setPathInfo($path);
|
||||||
$this->queryString = $queryString;
|
$this->setQueryString($queryString);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the RequestContext information based on a HttpFoundation Request.
|
||||||
|
*
|
||||||
|
* @param Request $request A Request instance
|
||||||
|
*/
|
||||||
public function fromRequest(Request $request)
|
public function fromRequest(Request $request)
|
||||||
{
|
{
|
||||||
$this->setBaseUrl($request->getBaseUrl());
|
$this->setBaseUrl($request->getBaseUrl());
|
||||||
@ -71,7 +77,7 @@ class RequestContext
|
|||||||
$this->setScheme($request->getScheme());
|
$this->setScheme($request->getScheme());
|
||||||
$this->setHttpPort($request->isSecure() ? $this->httpPort : $request->getPort());
|
$this->setHttpPort($request->isSecure() ? $this->httpPort : $request->getPort());
|
||||||
$this->setHttpsPort($request->isSecure() ? $request->getPort() : $this->httpsPort);
|
$this->setHttpsPort($request->isSecure() ? $request->getPort() : $this->httpsPort);
|
||||||
$this->setQueryString($request->server->get('QUERY_STRING'));
|
$this->setQueryString($request->server->get('QUERY_STRING', ''));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -143,6 +149,8 @@ class RequestContext
|
|||||||
/**
|
/**
|
||||||
* Gets the HTTP host.
|
* Gets the HTTP host.
|
||||||
*
|
*
|
||||||
|
* The host is always lowercased because it must be treated case-insensitive.
|
||||||
|
*
|
||||||
* @return string The HTTP host
|
* @return string The HTTP host
|
||||||
*/
|
*/
|
||||||
public function getHost()
|
public function getHost()
|
||||||
@ -159,7 +167,7 @@ class RequestContext
|
|||||||
*/
|
*/
|
||||||
public function setHost($host)
|
public function setHost($host)
|
||||||
{
|
{
|
||||||
$this->host = $host;
|
$this->host = strtolower($host);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -187,7 +195,7 @@ class RequestContext
|
|||||||
/**
|
/**
|
||||||
* Gets the HTTP port.
|
* Gets the HTTP port.
|
||||||
*
|
*
|
||||||
* @return string The HTTP port
|
* @return int The HTTP port
|
||||||
*/
|
*/
|
||||||
public function getHttpPort()
|
public function getHttpPort()
|
||||||
{
|
{
|
||||||
@ -197,19 +205,19 @@ class RequestContext
|
|||||||
/**
|
/**
|
||||||
* Sets the HTTP port.
|
* Sets the HTTP port.
|
||||||
*
|
*
|
||||||
* @param string $httpPort The HTTP port
|
* @param int $httpPort The HTTP port
|
||||||
*
|
*
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
public function setHttpPort($httpPort)
|
public function setHttpPort($httpPort)
|
||||||
{
|
{
|
||||||
$this->httpPort = $httpPort;
|
$this->httpPort = (int) $httpPort;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the HTTPS port.
|
* Gets the HTTPS port.
|
||||||
*
|
*
|
||||||
* @return string The HTTPS port
|
* @return int The HTTPS port
|
||||||
*/
|
*/
|
||||||
public function getHttpsPort()
|
public function getHttpsPort()
|
||||||
{
|
{
|
||||||
@ -219,19 +227,19 @@ class RequestContext
|
|||||||
/**
|
/**
|
||||||
* Sets the HTTPS port.
|
* Sets the HTTPS port.
|
||||||
*
|
*
|
||||||
* @param string $httpsPort The HTTPS port
|
* @param int $httpsPort The HTTPS port
|
||||||
*
|
*
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
public function setHttpsPort($httpsPort)
|
public function setHttpsPort($httpsPort)
|
||||||
{
|
{
|
||||||
$this->httpsPort = $httpsPort;
|
$this->httpsPort = (int) $httpsPort;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the query string.
|
* Gets the query string.
|
||||||
*
|
*
|
||||||
* @return string The query string
|
* @return string The query string without the "?"
|
||||||
*/
|
*/
|
||||||
public function getQueryString()
|
public function getQueryString()
|
||||||
{
|
{
|
||||||
@ -241,13 +249,14 @@ class RequestContext
|
|||||||
/**
|
/**
|
||||||
* Sets the query string.
|
* Sets the query string.
|
||||||
*
|
*
|
||||||
* @param string $queryString The query string
|
* @param string $queryString The query string (after "?")
|
||||||
*
|
*
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
public function setQueryString($queryString)
|
public function setQueryString($queryString)
|
||||||
{
|
{
|
||||||
$this->queryString = $queryString;
|
// string cast to be fault-tolerant, accepting null
|
||||||
|
$this->queryString = (string) $queryString;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -263,11 +272,9 @@ class RequestContext
|
|||||||
/**
|
/**
|
||||||
* Sets the parameters.
|
* Sets the parameters.
|
||||||
*
|
*
|
||||||
* This method implements a fluent interface.
|
|
||||||
*
|
|
||||||
* @param array $parameters The parameters
|
* @param array $parameters The parameters
|
||||||
*
|
*
|
||||||
* @return Route The current Route instance
|
* @return RequestContext The current instance, implementing a fluent interface
|
||||||
*/
|
*/
|
||||||
public function setParameters(array $parameters)
|
public function setParameters(array $parameters)
|
||||||
{
|
{
|
||||||
@ -281,7 +288,7 @@ class RequestContext
|
|||||||
*
|
*
|
||||||
* @param string $name A parameter name
|
* @param string $name A parameter name
|
||||||
*
|
*
|
||||||
* @return mixed The parameter value
|
* @return mixed The parameter value or null if nonexistent
|
||||||
*/
|
*/
|
||||||
public function getParameter($name)
|
public function getParameter($name)
|
||||||
{
|
{
|
||||||
@ -293,7 +300,7 @@ class RequestContext
|
|||||||
*
|
*
|
||||||
* @param string $name A parameter name
|
* @param string $name A parameter name
|
||||||
*
|
*
|
||||||
* @return bool true if the parameter value is set, false otherwise
|
* @return bool True if the parameter value is set, false otherwise
|
||||||
*/
|
*/
|
||||||
public function hasParameter($name)
|
public function hasParameter($name)
|
||||||
{
|
{
|
||||||
|
@ -98,4 +98,46 @@ class RequestContextTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$this->assertEquals('bar', $requestContext->getParameter('foo'));
|
$this->assertEquals('bar', $requestContext->getParameter('foo'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testMethod()
|
||||||
|
{
|
||||||
|
$requestContext = new RequestContext();
|
||||||
|
$requestContext->setMethod('post');
|
||||||
|
|
||||||
|
$this->assertSame('POST', $requestContext->getMethod());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testScheme()
|
||||||
|
{
|
||||||
|
$requestContext = new RequestContext();
|
||||||
|
$requestContext->setScheme('HTTPS');
|
||||||
|
|
||||||
|
$this->assertSame('https', $requestContext->getScheme());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testHost()
|
||||||
|
{
|
||||||
|
$requestContext = new RequestContext();
|
||||||
|
$requestContext->setHost('eXampLe.com');
|
||||||
|
|
||||||
|
$this->assertSame('example.com', $requestContext->getHost());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testQueryString()
|
||||||
|
{
|
||||||
|
$requestContext = new RequestContext();
|
||||||
|
$requestContext->setQueryString(null);
|
||||||
|
|
||||||
|
$this->assertSame('', $requestContext->getQueryString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testPort()
|
||||||
|
{
|
||||||
|
$requestContext = new RequestContext();
|
||||||
|
$requestContext->setHttpPort('123');
|
||||||
|
$requestContext->setHttpsPort('456');
|
||||||
|
|
||||||
|
$this->assertSame(123, $requestContext->getHttpPort());
|
||||||
|
$this->assertSame(456, $requestContext->getHttpsPort());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user