diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/RequestListenerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/RequestListenerTest.php new file mode 100644 index 0000000000..8f7de63d4a --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/RequestListenerTest.php @@ -0,0 +1,75 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\FrameworkBundle\Tests; + +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpKernel\HttpKernelInterface; +use Symfony\Component\HttpKernel\Event\GetResponseEvent; +use Symfony\Component\Routing\RequestContext; +use Symfony\Bundle\FrameworkBundle\RequestListener; + +class RequestListenerTest extends \PHPUnit_Framework_TestCase +{ + private $container; + private $router; + + protected function setUp() + { + $this->container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); + // TODO: Change to Symfony\Component\Routing\RouterInterface once has setContext method + $this->router = $this->getMockBuilder('Symfony\Component\Routing\Router') + ->disableOriginalConstructor() + ->getMock(); + } + + public function testConstructPortGetsPassedInRouterSetContext() + { + $listener = new RequestListener($this->container, $this->router, 99); + + $expectedContext = new RequestContext(); + $expectedContext->setHttpPort(99); + $this->router->expects($this->once()) + ->method('setContext') + ->with($expectedContext); + + $event = $this->createGetResponseEventForUri('http://localhost:99/'); + $listener->onCoreRequest($event); + } + + public function testRequestPortGetsPassedInRouterSetContextIfNoConstructorPort() + { + $listener = new RequestListener($this->container, $this->router); + + $expectedContext = new RequestContext(); + $expectedContext->setHttpPort(99); + $this->router->expects($this->once()) + ->method('setContext') + ->with($expectedContext); + + $event = $this->createGetResponseEventForUri('http://localhost:99/'); + $listener->onCoreRequest($event); + } + + /** + * @param string $uri + * @return GetResponseEvent + */ + private function createGetResponseEventForUri($uri) + { + $kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'); + $request = Request::create($uri); + $request->attributes->set('_controller', null); // Prevents going in to routing process + $event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST); + + return $event; + } +} \ No newline at end of file