Merge branch '2.3' into 2.4
* 2.3: added missing unit test fixed too greedy replacements fixed protocol-relative URLs added override power to server parameters provided on request method made parsing controllers more robust Fixed YamlFileLoader imports path Conflicts: src/Symfony/Component/HttpKernel/Tests/Controller/ControllerResolverTest.php
This commit is contained in:
commit
82cbf698b7
@ -297,6 +297,15 @@ abstract class Client
|
|||||||
}
|
}
|
||||||
|
|
||||||
$uri = $this->getAbsoluteUri($uri);
|
$uri = $this->getAbsoluteUri($uri);
|
||||||
|
|
||||||
|
if (isset($server['HTTP_HOST'])) {
|
||||||
|
$uri = preg_replace('{^(https?\://)'.parse_url($uri, PHP_URL_HOST).'}', '\\1'.$server['HTTP_HOST'], $uri);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($server['HTTPS'])) {
|
||||||
|
$uri = preg_replace('{^'.parse_url($uri, PHP_URL_SCHEME).'}', $server['HTTPS'] ? 'https' : 'http', $uri);
|
||||||
|
}
|
||||||
|
|
||||||
$server = array_merge($this->server, $server);
|
$server = array_merge($this->server, $server);
|
||||||
|
|
||||||
if (!$this->history->isEmpty()) {
|
if (!$this->history->isEmpty()) {
|
||||||
@ -513,7 +522,7 @@ abstract class Client
|
|||||||
}
|
}
|
||||||
|
|
||||||
$server = $request->getServer();
|
$server = $request->getServer();
|
||||||
unset($server['HTTP_IF_NONE_MATCH'], $server['HTTP_IF_MODIFIED_SINCE']);
|
$server = $this->updateServerFromUri($server, $this->redirect);
|
||||||
|
|
||||||
$this->isMainRequest = false;
|
$this->isMainRequest = false;
|
||||||
|
|
||||||
@ -595,4 +604,14 @@ abstract class Client
|
|||||||
{
|
{
|
||||||
return $this->request($request->getMethod(), $request->getUri(), $request->getParameters(), $request->getFiles(), $request->getServer(), $request->getContent(), $changeHistory);
|
return $this->request($request->getMethod(), $request->getUri(), $request->getParameters(), $request->getFiles(), $request->getServer(), $request->getContent(), $changeHistory);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function updateServerFromUri($server, $uri)
|
||||||
|
{
|
||||||
|
$server['HTTP_HOST'] = parse_url($uri, PHP_URL_HOST);
|
||||||
|
$scheme = parse_url($uri, PHP_URL_SCHEME);
|
||||||
|
$server['HTTPS'] = null === $scheme ? $server['HTTPS'] : 'https' == $scheme;
|
||||||
|
unset($server['HTTP_IF_NONE_MATCH'], $server['HTTP_IF_MODIFIED_SINCE']);
|
||||||
|
|
||||||
|
return $server;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -549,4 +549,38 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||||||
$client->setServerParameter('HTTP_USER_AGENT', 'testua');
|
$client->setServerParameter('HTTP_USER_AGENT', 'testua');
|
||||||
$this->assertEquals('testua', $client->getServerParameter('HTTP_USER_AGENT'));
|
$this->assertEquals('testua', $client->getServerParameter('HTTP_USER_AGENT'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSetServerParameterInRequest()
|
||||||
|
{
|
||||||
|
$client = new TestClient();
|
||||||
|
|
||||||
|
$this->assertEquals('localhost', $client->getServerParameter('HTTP_HOST'));
|
||||||
|
$this->assertEquals('Symfony2 BrowserKit', $client->getServerParameter('HTTP_USER_AGENT'));
|
||||||
|
|
||||||
|
$client->request('GET', 'https://www.example.com/https/www.example.com', array(), array(), array(
|
||||||
|
'HTTP_HOST' => 'testhost',
|
||||||
|
'HTTP_USER_AGENT' => 'testua',
|
||||||
|
'HTTPS' => false,
|
||||||
|
'NEW_SERVER_KEY' => 'new-server-key-value'
|
||||||
|
));
|
||||||
|
|
||||||
|
$this->assertEquals('localhost', $client->getServerParameter('HTTP_HOST'));
|
||||||
|
$this->assertEquals('Symfony2 BrowserKit', $client->getServerParameter('HTTP_USER_AGENT'));
|
||||||
|
|
||||||
|
$this->assertEquals('http://testhost/https/www.example.com', $client->getRequest()->getUri());
|
||||||
|
|
||||||
|
$server = $client->getRequest()->getServer();
|
||||||
|
|
||||||
|
$this->assertArrayHasKey('HTTP_USER_AGENT', $server);
|
||||||
|
$this->assertEquals('testua', $server['HTTP_USER_AGENT']);
|
||||||
|
|
||||||
|
$this->assertArrayHasKey('HTTP_HOST', $server);
|
||||||
|
$this->assertEquals('testhost', $server['HTTP_HOST']);
|
||||||
|
|
||||||
|
$this->assertArrayHasKey('NEW_SERVER_KEY', $server);
|
||||||
|
$this->assertEquals('new-server-key-value', $server['NEW_SERVER_KEY']);
|
||||||
|
|
||||||
|
$this->assertArrayHasKey('HTTPS', $server);
|
||||||
|
$this->assertFalse($server['HTTPS']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -52,7 +52,7 @@ class YamlFileLoader extends FileLoader
|
|||||||
}
|
}
|
||||||
|
|
||||||
// imports
|
// imports
|
||||||
$this->parseImports($content, $file);
|
$this->parseImports($content, $path);
|
||||||
|
|
||||||
// parameters
|
// parameters
|
||||||
if (isset($content['parameters'])) {
|
if (isset($content['parameters'])) {
|
||||||
|
@ -21,6 +21,7 @@ use Symfony\Component\DependencyInjection\ContainerBuilder;
|
|||||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||||
use Symfony\Component\DependencyInjection\Definition;
|
use Symfony\Component\DependencyInjection\Definition;
|
||||||
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
|
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
|
||||||
|
use Symfony\Component\DependencyInjection\Exception\InactiveScopeException;
|
||||||
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
|
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
|
||||||
use Symfony\Component\DependencyInjection\Reference;
|
use Symfony\Component\DependencyInjection\Reference;
|
||||||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
|
||||||
@ -152,6 +153,16 @@ class ContainerBuilderTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertNull($builder->get('foo', ContainerInterface::NULL_ON_INVALID_REFERENCE));
|
$this->assertNull($builder->get('foo', ContainerInterface::NULL_ON_INVALID_REFERENCE));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers Symfony\Component\DependencyInjection\ContainerBuilder::get
|
||||||
|
*/
|
||||||
|
public function testGetReturnsNullOnInactiveScopeWhenServiceIsCreatedByAMethod()
|
||||||
|
{
|
||||||
|
$builder = new ProjectContainer();
|
||||||
|
|
||||||
|
$this->assertNull($builder->get('foobaz', ContainerInterface::NULL_ON_INVALID_REFERENCE));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers Symfony\Component\DependencyInjection\ContainerBuilder::getServiceIds
|
* @covers Symfony\Component\DependencyInjection\ContainerBuilder::getServiceIds
|
||||||
*/
|
*/
|
||||||
@ -790,8 +801,16 @@ class ContainerBuilderTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->assertEquals(true, $classInList);
|
$this->assertTrue($classInList);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class FooClass {}
|
class FooClass {}
|
||||||
|
|
||||||
|
class ProjectContainer extends ContainerBuilder
|
||||||
|
{
|
||||||
|
public function getFoobazService()
|
||||||
|
{
|
||||||
|
throw new InactiveScopeException('foo', 'request');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
imports:
|
imports:
|
||||||
- { resource: services2.yml }
|
- { resource: services2.yml }
|
||||||
- { resource: services3.yml }
|
- { resource: services3.yml }
|
||||||
|
- { resource: "../php/simple.php" }
|
||||||
- { resource: "../ini/parameters.ini", class: Symfony\Component\DependencyInjection\Loader\IniFileLoader }
|
- { resource: "../ini/parameters.ini", class: Symfony\Component\DependencyInjection\Loader\IniFileLoader }
|
||||||
- { resource: "../ini/parameters2.ini" }
|
- { resource: "../ini/parameters2.ini" }
|
||||||
- { resource: "../xml/services13.xml" }
|
- { resource: "../xml/services13.xml" }
|
||||||
|
@ -18,6 +18,7 @@ use Symfony\Component\Config\Loader\Loader;
|
|||||||
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
|
||||||
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
|
||||||
use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
|
use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
|
||||||
|
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
|
||||||
use Symfony\Component\Config\Loader\LoaderResolver;
|
use Symfony\Component\Config\Loader\LoaderResolver;
|
||||||
use Symfony\Component\Config\FileLocator;
|
use Symfony\Component\Config\FileLocator;
|
||||||
use Symfony\Component\ExpressionLanguage\Expression;
|
use Symfony\Component\ExpressionLanguage\Expression;
|
||||||
@ -83,6 +84,7 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
|
|||||||
$resolver = new LoaderResolver(array(
|
$resolver = new LoaderResolver(array(
|
||||||
new IniFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml')),
|
new IniFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml')),
|
||||||
new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml')),
|
new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml')),
|
||||||
|
new PhpFileLoader($container, new FileLocator(self::$fixturesPath.'/php')),
|
||||||
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml')),
|
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml')),
|
||||||
));
|
));
|
||||||
$loader->setResolver($resolver);
|
$loader->setResolver($resolver);
|
||||||
|
@ -57,10 +57,18 @@ class ControllerResolver implements ControllerResolverInterface
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_array($controller) || (is_object($controller) && method_exists($controller, '__invoke'))) {
|
if (is_array($controller)) {
|
||||||
return $controller;
|
return $controller;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (is_object($controller)) {
|
||||||
|
if (method_exists($controller, '__invoke')) {
|
||||||
|
return $controller;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new \InvalidArgumentException(sprintf('Controller "%s" for URI "%s" is not callable.', get_class($controller), $request->getPathInfo()));
|
||||||
|
}
|
||||||
|
|
||||||
if (false === strpos($controller, ':')) {
|
if (false === strpos($controller, ':')) {
|
||||||
if (method_exists($controller, '__invoke')) {
|
if (method_exists($controller, '__invoke')) {
|
||||||
return new $controller();
|
return new $controller();
|
||||||
@ -72,7 +80,7 @@ class ControllerResolver implements ControllerResolverInterface
|
|||||||
$callable = $this->createController($controller);
|
$callable = $this->createController($controller);
|
||||||
|
|
||||||
if (!is_callable($callable)) {
|
if (!is_callable($callable)) {
|
||||||
throw new \InvalidArgumentException(sprintf('The controller for URI "%s" is not callable.', $request->getPathInfo()));
|
throw new \InvalidArgumentException(sprintf('Controller "%s" for URI "%s" is not callable.', $controller, $request->getPathInfo()));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $callable;
|
return $callable;
|
||||||
|
@ -17,66 +17,119 @@ use Symfony\Component\HttpFoundation\Request;
|
|||||||
|
|
||||||
class ControllerResolverTest extends \PHPUnit_Framework_TestCase
|
class ControllerResolverTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
public function testGetController()
|
public function testGetControllerWithoutControllerParameter()
|
||||||
{
|
{
|
||||||
$logger = new Logger();
|
$logger = $this->getMock('Psr\Log\LoggerInterface');
|
||||||
|
$logger->expects($this->once())->method('warning')->with('Unable to look for the controller as the "_controller" parameter is missing');
|
||||||
$resolver = new ControllerResolver($logger);
|
$resolver = new ControllerResolver($logger);
|
||||||
|
|
||||||
$request = Request::create('/');
|
$request = Request::create('/');
|
||||||
$this->assertFalse($resolver->getController($request), '->getController() returns false when the request has no _controller attribute');
|
$this->assertFalse($resolver->getController($request), '->getController() returns false when the request has no _controller attribute');
|
||||||
$this->assertEquals(array('Unable to look for the controller as the "_controller" parameter is missing'), $logger->getLogs('warning'));
|
}
|
||||||
|
|
||||||
$request->attributes->set('_controller', 'Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest::testGetController');
|
public function testGetControllerWithLambda()
|
||||||
$controller = $resolver->getController($request);
|
{
|
||||||
$this->assertInstanceOf('Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest', $controller[0], '->getController() returns a PHP callable');
|
$resolver = new ControllerResolver();
|
||||||
|
|
||||||
|
$request = Request::create('/');
|
||||||
$request->attributes->set('_controller', $lambda = function () {});
|
$request->attributes->set('_controller', $lambda = function () {});
|
||||||
$controller = $resolver->getController($request);
|
$controller = $resolver->getController($request);
|
||||||
$this->assertSame($lambda, $controller);
|
$this->assertSame($lambda, $controller);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetControllerWithObjectAndInvokeMethod()
|
||||||
|
{
|
||||||
|
$resolver = new ControllerResolver();
|
||||||
|
|
||||||
|
$request = Request::create('/');
|
||||||
$request->attributes->set('_controller', $this);
|
$request->attributes->set('_controller', $this);
|
||||||
$controller = $resolver->getController($request);
|
$controller = $resolver->getController($request);
|
||||||
$this->assertSame($this, $controller);
|
$this->assertSame($this, $controller);
|
||||||
|
}
|
||||||
|
|
||||||
$request->attributes->set('_controller', 'Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest');
|
public function testGetControllerWithObjectAndMethod()
|
||||||
$controller = $resolver->getController($request);
|
{
|
||||||
$this->assertInstanceOf('Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest', $controller);
|
$resolver = new ControllerResolver();
|
||||||
|
|
||||||
|
$request = Request::create('/');
|
||||||
$request->attributes->set('_controller', array($this, 'controllerMethod1'));
|
$request->attributes->set('_controller', array($this, 'controllerMethod1'));
|
||||||
$controller = $resolver->getController($request);
|
$controller = $resolver->getController($request);
|
||||||
$this->assertSame(array($this, 'controllerMethod1'), $controller);
|
$this->assertSame(array($this, 'controllerMethod1'), $controller);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetControllerWithClassAndMethod()
|
||||||
|
{
|
||||||
|
$resolver = new ControllerResolver();
|
||||||
|
|
||||||
|
$request = Request::create('/');
|
||||||
$request->attributes->set('_controller', array('Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest', 'controllerMethod4'));
|
$request->attributes->set('_controller', array('Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest', 'controllerMethod4'));
|
||||||
$controller = $resolver->getController($request);
|
$controller = $resolver->getController($request);
|
||||||
$this->assertSame(array('Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest', 'controllerMethod4'), $controller);
|
$this->assertSame(array('Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest', 'controllerMethod4'), $controller);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetControllerWithObjectAndMethodAsString()
|
||||||
|
{
|
||||||
|
$resolver = new ControllerResolver();
|
||||||
|
|
||||||
|
$request = Request::create('/');
|
||||||
|
$request->attributes->set('_controller', 'Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest::controllerMethod1');
|
||||||
|
$controller = $resolver->getController($request);
|
||||||
|
$this->assertInstanceOf('Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest', $controller[0], '->getController() returns a PHP callable');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetControllerWithClassAndInvokeMethod()
|
||||||
|
{
|
||||||
|
$resolver = new ControllerResolver();
|
||||||
|
|
||||||
|
$request = Request::create('/');
|
||||||
|
$request->attributes->set('_controller', 'Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest');
|
||||||
|
$controller = $resolver->getController($request);
|
||||||
|
$this->assertInstanceOf('Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest', $controller);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException \InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function testGetControllerOnObjectWithoutInvokeMethod()
|
||||||
|
{
|
||||||
|
$resolver = new ControllerResolver();
|
||||||
|
|
||||||
|
$request = Request::create('/');
|
||||||
|
$request->attributes->set('_controller', new \stdClass());
|
||||||
|
$resolver->getController($request);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetControllerWithFunction()
|
||||||
|
{
|
||||||
|
$resolver = new ControllerResolver();
|
||||||
|
|
||||||
|
$request = Request::create('/');
|
||||||
$request->attributes->set('_controller', 'Symfony\Component\HttpKernel\Tests\Controller\some_controller_function');
|
$request->attributes->set('_controller', 'Symfony\Component\HttpKernel\Tests\Controller\some_controller_function');
|
||||||
$controller = $resolver->getController($request);
|
$controller = $resolver->getController($request);
|
||||||
$this->assertSame('Symfony\Component\HttpKernel\Tests\Controller\some_controller_function', $controller);
|
$this->assertSame('Symfony\Component\HttpKernel\Tests\Controller\some_controller_function', $controller);
|
||||||
|
}
|
||||||
|
|
||||||
$request->attributes->set('_controller', 'foo');
|
/**
|
||||||
try {
|
* @dataProvider getUndefinedControllers
|
||||||
$resolver->getController($request);
|
* @expectedException \InvalidArgumentException
|
||||||
$this->fail('->getController() throws an \InvalidArgumentException if the _controller attribute is not well-formatted');
|
*/
|
||||||
} catch (\Exception $e) {
|
public function testGetControllerOnNonUndefinedFunction($controller)
|
||||||
$this->assertInstanceOf('\InvalidArgumentException', $e, '->getController() throws an \InvalidArgumentException if the _controller attribute is not well-formatted');
|
{
|
||||||
}
|
$resolver = new ControllerResolver();
|
||||||
|
|
||||||
$request->attributes->set('_controller', 'foo::bar');
|
$request = Request::create('/');
|
||||||
try {
|
$request->attributes->set('_controller', $controller);
|
||||||
$resolver->getController($request);
|
$resolver->getController($request);
|
||||||
$this->fail('->getController() throws an \InvalidArgumentException if the _controller attribute contains a non-existent class');
|
}
|
||||||
} catch (\Exception $e) {
|
|
||||||
$this->assertInstanceOf('\InvalidArgumentException', $e, '->getController() throws an \InvalidArgumentException if the _controller attribute contains a non-existent class');
|
|
||||||
}
|
|
||||||
|
|
||||||
$request->attributes->set('_controller', 'Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest::bar');
|
public function getUndefinedControllers()
|
||||||
try {
|
{
|
||||||
$resolver->getController($request);
|
return array(
|
||||||
$this->fail('->getController() throws an \InvalidArgumentException if the _controller attribute contains a non-existent method');
|
array('foo'),
|
||||||
} catch (\Exception $e) {
|
array('foo::bar'),
|
||||||
$this->assertInstanceOf('\InvalidArgumentException', $e, '->getController() throws an \InvalidArgumentException if the _controller attribute contains a non-existent method');
|
array('stdClass'),
|
||||||
}
|
array('Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest::bar'),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetArguments()
|
public function testGetArguments()
|
||||||
@ -158,7 +211,7 @@ class ControllerResolverTest extends \PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function controllerMethod1($foo)
|
public function controllerMethod1($foo)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user