ソースを参照

[WebProfilerBundle] Add some tests for the listener

Antoine Hérault 14 年 前
コミット
237d7fd327

+ 162 - 7
src/Symfony/Bundle/WebProfilerBundle/Tests/WebDebugToolbarListenerTest.php

@@ -13,6 +13,7 @@ namespace Symfony\Bundle\WebProfilerBundle\Tests;
 
 use Symfony\Bundle\WebProfilerBundle\WebDebugToolbarListener;
 use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
 
 class WebDebugToolbarListenerTest extends \PHPUnit_Framework_TestCase
 {
@@ -21,13 +22,7 @@ class WebDebugToolbarListenerTest extends \PHPUnit_Framework_TestCase
      */
     public function testInjectToolbar($content, $expected)
     {
-        $templating = $this->getMock('Symfony\Bundle\TwigBundle\TwigEngine', array(), array(), '', false);
-        $templating->expects($this->any())
-                 ->method('render')
-                 ->will($this->returnValue('WDT'));
-        ;
-        $request = $this->getMock('Symfony\Component\HttpFoundation\Request');
-        $listener = new WebDebugToolbarListener($templating);
+        $listener = new WebDebugToolbarListener($this->getTemplatingMock());
         $m = new \ReflectionMethod($listener, 'injectToolbar');
         $m->setAccessible(true);
 
@@ -54,4 +49,164 @@ class WebDebugToolbarListenerTest extends \PHPUnit_Framework_TestCase
             </html>"),
         );
     }
+
+    public function testRedirectionIsIntercepted()
+    {
+        foreach (array(301, 302) as $statusCode) {
+            $response = new Response('Some content', $statusCode);
+            $response->headers->set('X-Debug-Token', 'xxxxxxxx');
+            $event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(), Kernel::MASTER_REQUEST, $response);
+
+            $listener = new WebDebugToolbarListener($this->getTemplatingMock(), true);
+            $listener->onCoreResponse($event);
+
+            $this->assertEquals(200, $response->getStatusCode());
+            $this->assertRegExp('/The redirect was intercepted/', $response->getContent());
+        }
+    }
+
+    public function testToolbarIsInjected()
+    {
+        $response = new Response('<html><head></head><body></body></html>');
+        $response->headers->set('X-Debug-Token', 'xxxxxxxx');
+
+        $event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(), Kernel::MASTER_REQUEST, $response);
+
+        $listener = new WebDebugToolbarListener($this->getTemplatingMock());
+        $listener->onCoreResponse($event);
+
+        $this->assertEquals("<html><head></head><body>\nWDT\n</body></html>", $response->getContent());
+    }
+
+    /**
+     * @depends testToolbarIsInjected
+     */
+    public function testToolbarIsNotInjectedOnRedirection()
+    {
+        foreach (array(301, 302) as $statusCode) {
+            $response = new Response('<html><head></head><body></body></html>', $statusCode);
+            $response->headers->set('X-Debug-Token', 'xxxxxxxx');
+            $event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(), Kernel::MASTER_REQUEST, $response);
+
+            $listener = new WebDebugToolbarListener($this->getTemplatingMock());
+            $listener->onCoreResponse($event);
+
+            $this->assertEquals('<html><head></head><body></body></html>', $response->getContent());
+        }
+    }
+
+    /**
+     * @depends testToolbarIsInjected
+     */
+    public function testToolbarIsNotInjectedWhenThereIsNoNoXDebugTokenResponseHeader()
+    {
+        $response = new Response('<html><head></head><body></body></html>');
+
+        $event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(), Kernel::MASTER_REQUEST, $response);
+
+        $listener = new WebDebugToolbarListener($this->getTemplatingMock());
+        $listener->onCoreResponse($event);
+
+        $this->assertEquals('<html><head></head><body></body></html>', $response->getContent());
+    }
+
+    /**
+     * @depends testToolbarIsInjected
+     */
+    public function testToolbarIsNotInjectedWhenOnSubRequest()
+    {
+        $response = new Response('<html><head></head><body></body></html>');
+        $response->headers->set('X-Debug-Token', 'xxxxxxxx');
+
+        $event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(), Kernel::SUB_REQUEST, $response);
+
+        $listener = new WebDebugToolbarListener($this->getTemplatingMock());
+        $listener->onCoreResponse($event);
+
+        $this->assertEquals('<html><head></head><body></body></html>', $response->getContent());
+    }
+
+    /**
+     * @depends testToolbarIsInjected
+     */
+    public function testToolbarIsNotInjectedOnUncompleteHtmlResponses()
+    {
+        $response = new Response('<div>Some content</div>');
+        $response->headers->set('X-Debug-Token', 'xxxxxxxx');
+
+        $event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(), Kernel::MASTER_REQUEST, $response);
+
+        $listener = new WebDebugToolbarListener($this->getTemplatingMock());
+        $listener->onCoreResponse($event);
+
+        $this->assertEquals('<div>Some content</div>', $response->getContent());
+    }
+
+    /**
+     * @depends testToolbarIsInjected
+     */
+    public function testToolbarIsNotInjectedOnXmlHttpRequests()
+    {
+        $response = new Response('<html><head></head><body></body></html>');
+        $response->headers->set('X-Debug-Token', 'xxxxxxxx');
+
+        $event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(true), Kernel::MASTER_REQUEST, $response);
+
+        $listener = new WebDebugToolbarListener($this->getTemplatingMock());
+        $listener->onCoreResponse($event);
+
+        $this->assertEquals('<html><head></head><body></body></html>', $response->getContent());
+    }
+
+    /**
+     * @depends testToolbarIsInjected
+     */
+    public function testToolbarIsNotInjectedOnNonHtmlRequests()
+    {
+        $response = new Response('<html><head></head><body></body></html>');
+        $response->headers->set('X-Debug-Token', 'xxxxxxxx');
+
+        $event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(false, 'json'), Kernel::MASTER_REQUEST, $response);
+
+        $listener = new WebDebugToolbarListener($this->getTemplatingMock());
+        $listener->onCoreResponse($event);
+
+        $this->assertEquals('<html><head></head><body></body></html>', $response->getContent());
+    }
+
+    protected function getRequestMock($isXmlHttpRequest = false, $requestFormat = 'html')
+    {
+        $session = $this->getMock('Symfony\Component\HttpFoundation\Session', array(), array(), '', false);
+        $request = $this->getMock(
+            'Symfony\Component\HttpFoundation\Request',
+            array('getSession', 'isXmlHttpRequest', 'getRequestFormat'),
+            array(), '', false
+        );
+        $request->expects($this->any())
+            ->method('isXmlHttpRequest')
+            ->will($this->returnValue($isXmlHttpRequest));
+        $request->expects($this->any())
+            ->method('getRequestFormat')
+            ->will($this->returnValue($requestFormat));
+        $request->expects($this->any())
+            ->method('getSession')
+            ->will($this->returnValue($session));
+
+        return $request;
+    }
+
+    protected function getTemplatingMock($render = 'WDT')
+    {
+        $templating = $this->getMock('Symfony\Bundle\TwigBundle\TwigEngine', array(), array(), '', false);
+        $templating->expects($this->any())
+            ->method('render')
+            ->will($this->returnValue($render));
+
+        return $templating;
+    }
+
+    protected function getKernelMock()
+    {
+        return $this->getMock('Symfony\Component\HttpKernel\Kernel', array(), array(), '', false);
+    }
 }