浏览代码

[FrameworkBundle] refactored tests

Fabien Potencier 14 年之前
父节点
当前提交
bb7b7e851d
共有 1 个文件被更改,包括 75 次插入75 次删除
  1. 75 75
      src/Symfony/Bundle/FrameworkBundle/Tests/RequestListenerTest.php

+ 75 - 75
src/Symfony/Bundle/FrameworkBundle/Tests/RequestListenerTest.php

@@ -1,75 +1,75 @@
-<?php
-
-/*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * 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;
-    }
-}
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * 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');
+        $this->router = $this->getMockBuilder('Symfony\Component\Routing\RouterInterface')
+                             ->disableOriginalConstructor()
+                             ->getMock();
+    }
+
+    /**
+     * @dataProvider getPortData
+     */
+    public function testPort($defaultHttpPort, $defaultHttpsPort, $uri, $expectedHttpPort, $expectedHttpsPort)
+    {
+        $listener = new RequestListener($this->container, $this->router, $defaultHttpPort, $defaultHttpsPort);
+
+        $expectedContext = new RequestContext();
+        $expectedContext->setHttpPort($expectedHttpPort);
+        $expectedContext->setHttpsPort($expectedHttpsPort);
+        $expectedContext->setScheme(0 === strpos($uri, 'https') ? 'https' : 'http');
+        $this->router->expects($this->once())
+                     ->method('setContext')
+                     ->with($expectedContext);
+
+        $event = $this->createGetResponseEventForUri($uri);
+        $listener->onCoreRequest($event);
+    }
+
+    public function getPortData()
+    {
+        return array(
+            array(80, 443, 'http://localhost/', 80, 443),
+            array(80, 443, 'http://localhost:90/', 90, 443),
+            array(80, 443, 'https://localhost/', 80, 443),
+            array(80, 443, 'https://localhost:90/', 80, 90),
+        );
+    }
+
+    /**
+     * @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
+
+        return new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
+    }
+}