From ad8289369128fc1e72f9f8f11b738679ed35a6de Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 22 Jan 2013 09:30:21 +0100 Subject: [PATCH] removed the need for a proxy route for rendering strategies --- .../Resources/config/content_generator.xml | 2 - .../FrameworkBundle/Resources/config/esi.xml | 1 - .../Resources/config/routing/proxy.xml | 10 -- .../EventListener/RouterProxyListener.php | 4 +- .../DefaultRenderingStrategy.php | 2 +- .../EsiRenderingStrategy.php | 2 +- .../GeneratorAwareRenderingStrategy.php | 82 -------------- .../HIncludeRenderingStrategy.php | 2 +- .../ProxyAwareRenderingStrategy.php | 44 ++++++++ .../EventListener/RouterProxyListenerTest.php | 38 ++----- .../AbstractRenderingStrategyTest.php | 29 ----- .../DefaultRenderingStrategyTest.php | 3 +- .../EsiRenderingStrategyTest.php | 9 +- .../GeneratorAwareRenderingStrategyTest.php | 101 ------------------ .../HIncludeRenderingStrategyTest.php | 10 +- .../ProxyAwareRenderingStrategyTest.php | 63 +++++++++++ 16 files changed, 128 insertions(+), 274 deletions(-) delete mode 100644 src/Symfony/Bundle/FrameworkBundle/Resources/config/routing/proxy.xml delete mode 100644 src/Symfony/Component/HttpKernel/RenderingStrategy/GeneratorAwareRenderingStrategy.php create mode 100644 src/Symfony/Component/HttpKernel/RenderingStrategy/ProxyAwareRenderingStrategy.php delete mode 100644 src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/AbstractRenderingStrategyTest.php delete mode 100644 src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/GeneratorAwareRenderingStrategyTest.php create mode 100644 src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/ProxyAwareRenderingStrategyTest.php diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/content_generator.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/content_generator.xml index 332cf0de38..5bc6f2e360 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/content_generator.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/content_generator.xml @@ -22,7 +22,6 @@ - @@ -30,7 +29,6 @@ %http_content_renderer.strategy.hinclude.global_template% - diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/esi.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/esi.xml index 0c4a271863..8b390da4f1 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/esi.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/esi.xml @@ -22,7 +22,6 @@ - diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/routing/proxy.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/routing/proxy.xml deleted file mode 100644 index ab792c29cc..0000000000 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/routing/proxy.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - .+ - - diff --git a/src/Symfony/Component/HttpKernel/EventListener/RouterProxyListener.php b/src/Symfony/Component/HttpKernel/EventListener/RouterProxyListener.php index b88350c20f..572d0a1456 100644 --- a/src/Symfony/Component/HttpKernel/EventListener/RouterProxyListener.php +++ b/src/Symfony/Component/HttpKernel/EventListener/RouterProxyListener.php @@ -47,7 +47,7 @@ class RouterProxyListener implements EventSubscriberInterface { $request = $event->getRequest(); - if ('_proxy' !== $request->attributes->get('_route')) { + if ('/_proxy' !== rawurldecode($request->getPathInfo())) { return; } @@ -92,7 +92,7 @@ class RouterProxyListener implements EventSubscriberInterface public static function getSubscribedEvents() { return array( - KernelEvents::REQUEST => array(array('onKernelRequest', 16)), + KernelEvents::REQUEST => array(array('onKernelRequest', 48)), ); } } diff --git a/src/Symfony/Component/HttpKernel/RenderingStrategy/DefaultRenderingStrategy.php b/src/Symfony/Component/HttpKernel/RenderingStrategy/DefaultRenderingStrategy.php index 058c82fe62..5a3427e05e 100644 --- a/src/Symfony/Component/HttpKernel/RenderingStrategy/DefaultRenderingStrategy.php +++ b/src/Symfony/Component/HttpKernel/RenderingStrategy/DefaultRenderingStrategy.php @@ -21,7 +21,7 @@ use Symfony\Component\HttpKernel\Controller\ControllerReference; * * @author Fabien Potencier */ -class DefaultRenderingStrategy extends GeneratorAwareRenderingStrategy +class DefaultRenderingStrategy extends ProxyAwareRenderingStrategy { private $kernel; diff --git a/src/Symfony/Component/HttpKernel/RenderingStrategy/EsiRenderingStrategy.php b/src/Symfony/Component/HttpKernel/RenderingStrategy/EsiRenderingStrategy.php index 945a388ac7..195066d004 100644 --- a/src/Symfony/Component/HttpKernel/RenderingStrategy/EsiRenderingStrategy.php +++ b/src/Symfony/Component/HttpKernel/RenderingStrategy/EsiRenderingStrategy.php @@ -21,7 +21,7 @@ use Symfony\Component\HttpKernel\HttpCache\Esi; * * @author Fabien Potencier */ -class EsiRenderingStrategy extends GeneratorAwareRenderingStrategy +class EsiRenderingStrategy extends ProxyAwareRenderingStrategy { private $esi; private $defaultStrategy; diff --git a/src/Symfony/Component/HttpKernel/RenderingStrategy/GeneratorAwareRenderingStrategy.php b/src/Symfony/Component/HttpKernel/RenderingStrategy/GeneratorAwareRenderingStrategy.php deleted file mode 100644 index 57e0d55407..0000000000 --- a/src/Symfony/Component/HttpKernel/RenderingStrategy/GeneratorAwareRenderingStrategy.php +++ /dev/null @@ -1,82 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\HttpKernel\RenderingStrategy; - -use Symfony\Component\HttpKernel\Controller\ControllerReference; -use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\Routing\Generator\UrlGeneratorInterface; -use Symfony\Component\Routing\Exception\RouteNotFoundException; - -/** - * Adds the possibility to generate a proxy URI for a given Controller. - * - * @author Fabien Potencier - */ -abstract class GeneratorAwareRenderingStrategy implements RenderingStrategyInterface -{ - protected $generator; - - /** - * Sets a URL generator to use for proxy URIs generation. - * - * @param UrlGeneratorInterface $generator An UrlGeneratorInterface instance - */ - public function setUrlGenerator(UrlGeneratorInterface $generator) - { - $this->generator = $generator; - } - - /** - * Generates a proxy URI for a given controller. - * - * This method only works when using the Symfony Routing component and - * if a "_proxy" route is defined with a {_controller} and {_format} - * placeholders. - * - * @param ControllerReference $reference A ControllerReference instance - * @param Request $request A Request instance - * - * @return string A proxy URI - * - * @throws \LogicException when the _proxy route is not available - * @throws \LogicException when there is no registered route generator - */ - protected function generateProxyUri(ControllerReference $reference, Request $request) - { - if (null === $this->generator) { - throw new \LogicException('Unable to generate a proxy URL as there is no registered route generator.'); - } - - if (isset($reference->attributes['_format'])) { - $format = $reference->attributes['_format']; - unset($reference->attributes['_format']); - } else { - $format = $request->getRequestFormat(); - } - - try { - $uri = $this->generator->generate('_proxy', array('_controller' => $reference->controller, '_format' => $format), UrlGeneratorInterface::ABSOLUTE_URL); - } catch (RouteNotFoundException $e) { - throw new \LogicException('Unable to generate a proxy URL as the "_proxy" route is not registered.', 0, $e); - } - - if ($path = http_build_query($reference->attributes, '', '&')) { - $reference->query['path'] = $path; - } - - if ($qs = http_build_query($reference->query, '', '&')) { - $uri .= '?'.$qs; - } - - return $uri; - } -} diff --git a/src/Symfony/Component/HttpKernel/RenderingStrategy/HIncludeRenderingStrategy.php b/src/Symfony/Component/HttpKernel/RenderingStrategy/HIncludeRenderingStrategy.php index fa13179180..07a695635b 100644 --- a/src/Symfony/Component/HttpKernel/RenderingStrategy/HIncludeRenderingStrategy.php +++ b/src/Symfony/Component/HttpKernel/RenderingStrategy/HIncludeRenderingStrategy.php @@ -22,7 +22,7 @@ use Symfony\Component\HttpKernel\UriSigner; * * @author Fabien Potencier */ -class HIncludeRenderingStrategy extends GeneratorAwareRenderingStrategy +class HIncludeRenderingStrategy extends ProxyAwareRenderingStrategy { private $templating; private $globalDefaultTemplate; diff --git a/src/Symfony/Component/HttpKernel/RenderingStrategy/ProxyAwareRenderingStrategy.php b/src/Symfony/Component/HttpKernel/RenderingStrategy/ProxyAwareRenderingStrategy.php new file mode 100644 index 0000000000..1067c30d13 --- /dev/null +++ b/src/Symfony/Component/HttpKernel/RenderingStrategy/ProxyAwareRenderingStrategy.php @@ -0,0 +1,44 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpKernel\RenderingStrategy; + +use Symfony\Component\HttpKernel\Controller\ControllerReference; +use Symfony\Component\HttpFoundation\Request; + +/** + * Adds the possibility to generate a proxy URI for a given Controller. + * + * @author Fabien Potencier + */ +abstract class ProxyAwareRenderingStrategy implements RenderingStrategyInterface +{ + /** + * Generates a proxy URI for a given controller. + * + * @param ControllerReference $reference A ControllerReference instance + * @param Request $request A Request instance + * + * @return string A proxy URI + */ + protected function generateProxyUri(ControllerReference $reference, Request $request) + { + if (!isset($reference->attributes['_format'])) { + $reference->attributes['_format'] = $request->getRequestFormat(); + } + + $reference->attributes['_controller'] = $reference->controller; + + $reference->query['path'] = http_build_query($reference->attributes, '', '&'); + + return $request->getUriForPath('/_proxy?'.http_build_query($reference->query, '', '&')); + } +} diff --git a/src/Symfony/Component/HttpKernel/Tests/EventListener/RouterProxyListenerTest.php b/src/Symfony/Component/HttpKernel/Tests/EventListener/RouterProxyListenerTest.php index b0e091d504..352e0ab1e0 100644 --- a/src/Symfony/Component/HttpKernel/Tests/EventListener/RouterProxyListenerTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/EventListener/RouterProxyListenerTest.php @@ -28,10 +28,10 @@ class RouterProxyListenerTest extends \PHPUnit_Framework_TestCase public function testOnlyTriggeredOnProxyRoute() { - $request = Request::create('http://example.com/foo?path=foo%3D=bar'); + $request = Request::create('http://example.com/foo?path=foo%3Dbar%26_controller%3Dfoo'); $listener = new RouterProxyListener(new UriSigner('foo')); - $event = $this->createGetResponseEvent($request, 'foobar'); + $event = $this->createGetResponseEvent($request); $expected = $request->attributes->all(); @@ -46,7 +46,7 @@ class RouterProxyListenerTest extends \PHPUnit_Framework_TestCase */ public function testAccessDeniedWithNonSafeMethods() { - $request = Request::create('http://example.com/foo', 'POST'); + $request = Request::create('http://example.com/_proxy', 'POST'); $listener = new RouterProxyListener(new UriSigner('foo')); $event = $this->createGetResponseEvent($request); @@ -59,7 +59,7 @@ class RouterProxyListenerTest extends \PHPUnit_Framework_TestCase */ public function testAccessDeniedWithNonLocalIps() { - $request = Request::create('http://example.com/foo', 'GET', array(), array(), array(), array('REMOTE_ADDR' => '10.0.0.1')); + $request = Request::create('http://example.com/_proxy', 'GET', array(), array(), array(), array('REMOTE_ADDR' => '10.0.0.1')); $listener = new RouterProxyListener(new UriSigner('foo')); $event = $this->createGetResponseEvent($request); @@ -72,7 +72,7 @@ class RouterProxyListenerTest extends \PHPUnit_Framework_TestCase */ public function testAccessDeniedWithWrongSignature() { - $request = Request::create('http://example.com/foo', 'GET', array(), array(), array(), array('REMOTE_ADDR' => '10.0.0.1')); + $request = Request::create('http://example.com/_proxy', 'GET', array(), array(), array(), array('REMOTE_ADDR' => '10.0.0.1')); $listener = new RouterProxyListener(new UriSigner('foo')); $event = $this->createGetResponseEvent($request); @@ -80,40 +80,22 @@ class RouterProxyListenerTest extends \PHPUnit_Framework_TestCase $listener->onKernelRequest($event); } - public function testWithSignatureAndNoPath() + public function testWithSignature() { $signer = new UriSigner('foo'); - $request = Request::create($signer->sign('http://example.com/foo'), 'GET', array(), array(), array(), array('REMOTE_ADDR' => '10.0.0.1')); + $request = Request::create($signer->sign('http://example.com/_proxy?path=foo%3Dbar%26_controller%3Dfoo'), 'GET', array(), array(), array(), array('REMOTE_ADDR' => '10.0.0.1')); $listener = new RouterProxyListener($signer); $event = $this->createGetResponseEvent($request); $listener->onKernelRequest($event); - $this->assertEquals(array('foo' => 'foo'), $request->attributes->get('_route_params')); + $this->assertEquals(array('foo' => 'bar', '_controller' => 'foo'), $request->attributes->get('_route_params')); $this->assertFalse($request->query->has('path')); } - public function testWithSignatureAndPath() + private function createGetResponseEvent(Request $request) { - $signer = new UriSigner('foo'); - $request = Request::create($signer->sign('http://example.com/foo?path=bar%3Dbar'), 'GET', array(), array(), array(), array('REMOTE_ADDR' => '10.0.0.1')); - - $listener = new RouterProxyListener($signer); - $event = $this->createGetResponseEvent($request); - - $listener->onKernelRequest($event); - - $this->assertEquals(array('foo' => 'foo', 'bar' => 'bar'), $request->attributes->get('_route_params')); - $this->assertFalse($request->query->has('path')); - } - - private function createGetResponseEvent(Request $request, $route = '_proxy') - { - $kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'); - $request->attributes->set('_route', $route); - $request->attributes->set('_route_params', array('foo' => 'foo')); - - return new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST); + return new GetResponseEvent($this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'), $request, HttpKernelInterface::MASTER_REQUEST); } } diff --git a/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/AbstractRenderingStrategyTest.php b/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/AbstractRenderingStrategyTest.php deleted file mode 100644 index ae3a07f2cc..0000000000 --- a/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/AbstractRenderingStrategyTest.php +++ /dev/null @@ -1,29 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\HttpKernel\Tests\RenderingStrategy; - -abstract class AbstractRenderingStrategyTest extends \PHPUnit_Framework_TestCase -{ - protected function getUrlGenerator() - { - $generator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface'); - $generator - ->expects($this->any()) - ->method('generate') - ->will($this->returnCallback(function ($name, $parameters, $referenceType) { - return '/'.$parameters['_controller'].'.'.$parameters['_format']; - })) - ; - - return $generator; - } -} diff --git a/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/DefaultRenderingStrategyTest.php b/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/DefaultRenderingStrategyTest.php index 2a6d9ab7b3..5d1d4c92d6 100644 --- a/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/DefaultRenderingStrategyTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/DefaultRenderingStrategyTest.php @@ -18,7 +18,7 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\EventDispatcher\EventDispatcher; -class DefaultRenderingStrategyTest extends AbstractRenderingStrategyTest +class DefaultRenderingStrategyTest extends \PHPUnit_Framework_TestCase { protected function setUp() { @@ -41,7 +41,6 @@ class DefaultRenderingStrategyTest extends AbstractRenderingStrategyTest public function testRenderWithControllerReference() { $strategy = new DefaultRenderingStrategy($this->getKernel($this->returnValue(new Response('foo')))); - $strategy->setUrlGenerator($this->getUrlGenerator()); $this->assertEquals('foo', $strategy->render(new ControllerReference('main_controller', array(), array()), Request::create('/'))->getContent()); } diff --git a/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/EsiRenderingStrategyTest.php b/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/EsiRenderingStrategyTest.php index 90241a7616..bace5d6f98 100644 --- a/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/EsiRenderingStrategyTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/EsiRenderingStrategyTest.php @@ -16,17 +16,13 @@ use Symfony\Component\HttpKernel\RenderingStrategy\EsiRenderingStrategy; use Symfony\Component\HttpKernel\HttpCache\Esi; use Symfony\Component\HttpFoundation\Request; -class EsiRenderingStrategyTest extends AbstractRenderingStrategyTest +class EsiRenderingStrategyTest extends \PHPUnit_Framework_TestCase { protected function setUp() { if (!class_exists('Symfony\Component\HttpFoundation\Request')) { $this->markTestSkipped('The "HttpFoundation" component is not available'); } - - if (!interface_exists('Symfony\Component\Routing\Generator\UrlGeneratorInterface')) { - $this->markTestSkipped('The "Routing" component is not available'); - } } public function testRenderFallbackToDefaultStrategyIfNoRequest() @@ -44,7 +40,6 @@ class EsiRenderingStrategyTest extends AbstractRenderingStrategyTest public function testRender() { $strategy = new EsiRenderingStrategy(new Esi(), $this->getDefaultStrategy()); - $strategy->setUrlGenerator($this->getUrlGenerator()); $request = Request::create('/'); $request->headers->set('Surrogate-Capability', 'ESI/1.0'); @@ -52,7 +47,7 @@ class EsiRenderingStrategyTest extends AbstractRenderingStrategyTest $this->assertEquals('', $strategy->render('/', $request)->getContent()); $this->assertEquals("\n", $strategy->render('/', $request, array('comment' => 'This is a comment'))->getContent()); $this->assertEquals('', $strategy->render('/', $request, array('alt' => 'foo'))->getContent()); - $this->assertEquals('', $strategy->render(new ControllerReference('main_controller', array(), array()), $request, array('alt' => new ControllerReference('alt_controller', array(), array())))->getContent()); + $this->assertEquals('', $strategy->render(new ControllerReference('main_controller', array(), array()), $request, array('alt' => new ControllerReference('alt_controller', array(), array())))->getContent()); } private function getDefaultStrategy($called = false) diff --git a/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/GeneratorAwareRenderingStrategyTest.php b/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/GeneratorAwareRenderingStrategyTest.php deleted file mode 100644 index 8d7a0b54b1..0000000000 --- a/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/GeneratorAwareRenderingStrategyTest.php +++ /dev/null @@ -1,101 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\HttpKernel\Tests\RenderingStrategy; - -use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpKernel\Controller\ControllerReference; -use Symfony\Component\HttpKernel\RenderingStrategy\GeneratorAwareRenderingStrategy; -use Symfony\Component\Routing\Generator\UrlGeneratorInterface; -use Symfony\Component\Routing\Exception\RouteNotFoundException; - -class GeneratorAwareRenderingStrategyTest extends AbstractRenderingStrategyTest -{ - protected function setUp() - { - if (!interface_exists('Symfony\Component\Routing\Generator\UrlGeneratorInterface')) { - $this->markTestSkipped('The "Routing" component is not available'); - } - } - - /** - * @expectedException \LogicException - */ - public function testGenerateProxyUriWithNoGenerator() - { - $strategy = new Strategy(); - $strategy->doGenerateProxyUri(new ControllerReference('controller', array(), array()), Request::create('/')); - } - - /** - * @expectedException \LogicException - */ - public function testGenerateProxyUriWhenRouteNotFound() - { - $generator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface'); - $generator - ->expects($this->once()) - ->method('generate') - ->will($this->throwException(new RouteNotFoundException())) - ; - - $strategy = new Strategy(); - $strategy->setUrlGenerator($generator); - $strategy->doGenerateProxyUri(new ControllerReference('controller', array(), array()), Request::create('/')); - } - - /** - * @dataProvider getGeneratorProxyUriData - */ - public function testGenerateProxyUri($uri, $controller) - { - $this->assertEquals($uri, $this->getStrategy()->doGenerateProxyUri($controller, Request::create('/'))); - } - - public function getGeneratorProxyUriData() - { - return array( - array('/controller.html', new ControllerReference('controller', array(), array())), - array('/controller.xml', new ControllerReference('controller', array('_format' => 'xml'), array())), - array('/controller.json?path=foo%3Dfoo', new ControllerReference('controller', array('foo' => 'foo', '_format' => 'json'), array())), - array('/controller.html?bar=bar&path=foo%3Dfoo', new ControllerReference('controller', array('foo' => 'foo'), array('bar' => 'bar'))), - array('/controller.html?foo=foo', new ControllerReference('controller', array(), array('foo' => 'foo'))), - ); - } - - public function testGenerateProxyUriWithARequest() - { - $request = Request::create('/'); - $request->attributes->set('_format', 'json'); - $controller = new ControllerReference('controller', array(), array()); - - $this->assertEquals('/controller.json', $this->getStrategy()->doGenerateProxyUri($controller, $request)); - } - - private function getStrategy() - { - $strategy = new Strategy(); - $strategy->setUrlGenerator($this->getUrlGenerator()); - - return $strategy; - } -} - -class Strategy extends GeneratorAwareRenderingStrategy -{ - public function render($uri, Request $request, array $options = array()) {} - public function getName() {} - - public function doGenerateProxyUri(ControllerReference $reference, Request $request) - { - return parent::generateProxyUri($reference, $request); - } -} diff --git a/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/HIncludeRenderingStrategyTest.php b/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/HIncludeRenderingStrategyTest.php index c78f2d153b..b00d16eb5c 100644 --- a/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/HIncludeRenderingStrategyTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/HIncludeRenderingStrategyTest.php @@ -16,17 +16,13 @@ use Symfony\Component\HttpKernel\RenderingStrategy\HIncludeRenderingStrategy; use Symfony\Component\HttpKernel\UriSigner; use Symfony\Component\HttpFoundation\Request; -class HIncludeRenderingStrategyTest extends AbstractRenderingStrategyTest +class HIncludeRenderingStrategyTest extends \PHPUnit_Framework_TestCase { protected function setUp() { if (!class_exists('Symfony\Component\HttpFoundation\Request')) { $this->markTestSkipped('The "HttpFoundation" component is not available'); } - - if (!interface_exists('Symfony\Component\Routing\Generator\UrlGeneratorInterface')) { - $this->markTestSkipped('The "Routing" component is not available'); - } } /** @@ -41,8 +37,8 @@ class HIncludeRenderingStrategyTest extends AbstractRenderingStrategyTest public function testRenderWithControllerAndSigner() { $strategy = new HIncludeRenderingStrategy(null, new UriSigner('foo')); - $strategy->setUrlGenerator($this->getUrlGenerator()); - $this->assertEquals('', $strategy->render(new ControllerReference('main_controller', array(), array()), Request::create('/'))->getContent()); + + $this->assertEquals('', $strategy->render(new ControllerReference('main_controller', array(), array()), Request::create('/'))->getContent()); } public function testRenderWithUri() diff --git a/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/ProxyAwareRenderingStrategyTest.php b/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/ProxyAwareRenderingStrategyTest.php new file mode 100644 index 0000000000..f1e1dc9044 --- /dev/null +++ b/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/ProxyAwareRenderingStrategyTest.php @@ -0,0 +1,63 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpKernel\Tests\RenderingStrategy; + +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpKernel\Controller\ControllerReference; +use Symfony\Component\HttpKernel\RenderingStrategy\ProxyAwareRenderingStrategy; + +class ProxyAwareRenderingStrategyTest extends \PHPUnit_Framework_TestCase +{ + /** + * @dataProvider getGenerateProxyUriData + */ + public function testGenerateProxyUri($uri, $controller) + { + $this->assertEquals($uri, $this->getStrategy()->doGenerateProxyUri($controller, Request::create('/'))); + } + + public function getGenerateProxyUriData() + { + return array( + array('http://localhost/_proxy?path=_format%3Dhtml%26_controller%3Dcontroller', new ControllerReference('controller', array(), array())), + array('http://localhost/_proxy?path=_format%3Dxml%26_controller%3Dcontroller', new ControllerReference('controller', array('_format' => 'xml'), array())), + array('http://localhost/_proxy?path=foo%3Dfoo%26_format%3Djson%26_controller%3Dcontroller', new ControllerReference('controller', array('foo' => 'foo', '_format' => 'json'), array())), + array('http://localhost/_proxy?bar=bar&path=foo%3Dfoo%26_format%3Dhtml%26_controller%3Dcontroller', new ControllerReference('controller', array('foo' => 'foo'), array('bar' => 'bar'))), + array('http://localhost/_proxy?foo=foo&path=_format%3Dhtml%26_controller%3Dcontroller', new ControllerReference('controller', array(), array('foo' => 'foo'))), + ); + } + + public function testGenerateProxyUriWithARequest() + { + $request = Request::create('/'); + $request->attributes->set('_format', 'json'); + $controller = new ControllerReference('controller', array(), array()); + + $this->assertEquals('http://localhost/_proxy?path=_format%3Djson%26_controller%3Dcontroller', $this->getStrategy()->doGenerateProxyUri($controller, $request)); + } + + private function getStrategy() + { + return new Strategy(); + } +} + +class Strategy extends ProxyAwareRenderingStrategy +{ + public function render($uri, Request $request, array $options = array()) {} + public function getName() {} + + public function doGenerateProxyUri(ControllerReference $reference, Request $request) + { + return parent::generateProxyUri($reference, $request); + } +}