do not mock the Request class
This commit is contained in:
parent
068429ed3c
commit
968809c2f1
@ -13,10 +13,8 @@ namespace Symfony\Bundle\WebProfilerBundle\Tests\EventListener;
|
|||||||
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Symfony\Bundle\WebProfilerBundle\EventListener\WebDebugToolbarListener;
|
use Symfony\Bundle\WebProfilerBundle\EventListener\WebDebugToolbarListener;
|
||||||
use Symfony\Component\HttpFoundation\HeaderBag;
|
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Symfony\Component\HttpFoundation\Session\Session;
|
|
||||||
use Symfony\Component\HttpKernel\Event\ResponseEvent;
|
use Symfony\Component\HttpKernel\Event\ResponseEvent;
|
||||||
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||||
use Symfony\Component\HttpKernel\Kernel;
|
use Symfony\Component\HttpKernel\Kernel;
|
||||||
@ -61,11 +59,11 @@ class WebDebugToolbarListenerTest extends TestCase
|
|||||||
/**
|
/**
|
||||||
* @dataProvider provideRedirects
|
* @dataProvider provideRedirects
|
||||||
*/
|
*/
|
||||||
public function testHtmlRedirectionIsIntercepted($statusCode, $hasSession)
|
public function testHtmlRedirectionIsIntercepted($statusCode)
|
||||||
{
|
{
|
||||||
$response = new Response('Some content', $statusCode);
|
$response = new Response('Some content', $statusCode);
|
||||||
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
|
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
|
||||||
$event = new ResponseEvent($this->createMock(Kernel::class), $this->getRequestMock(false, 'html', $hasSession), HttpKernelInterface::MASTER_REQUEST, $response);
|
$event = new ResponseEvent($this->createMock(Kernel::class), new Request(), HttpKernelInterface::MASTER_REQUEST, $response);
|
||||||
|
|
||||||
$listener = new WebDebugToolbarListener($this->getTwigMock('Redirection'), true);
|
$listener = new WebDebugToolbarListener($this->getTwigMock('Redirection'), true);
|
||||||
$listener->onKernelResponse($event);
|
$listener->onKernelResponse($event);
|
||||||
@ -78,7 +76,7 @@ class WebDebugToolbarListenerTest extends TestCase
|
|||||||
{
|
{
|
||||||
$response = new Response('Some content', '301');
|
$response = new Response('Some content', '301');
|
||||||
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
|
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
|
||||||
$event = new ResponseEvent($this->createMock(Kernel::class), $this->getRequestMock(false, 'json', true), HttpKernelInterface::MASTER_REQUEST, $response);
|
$event = new ResponseEvent($this->createMock(Kernel::class), new Request([], [], ['_format' => 'json']), HttpKernelInterface::MASTER_REQUEST, $response);
|
||||||
|
|
||||||
$listener = new WebDebugToolbarListener($this->getTwigMock('Redirection'), true);
|
$listener = new WebDebugToolbarListener($this->getTwigMock('Redirection'), true);
|
||||||
$listener->onKernelResponse($event);
|
$listener->onKernelResponse($event);
|
||||||
@ -92,7 +90,7 @@ class WebDebugToolbarListenerTest extends TestCase
|
|||||||
$response = new Response('<html><head></head><body></body></html>');
|
$response = new Response('<html><head></head><body></body></html>');
|
||||||
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
|
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
|
||||||
|
|
||||||
$event = new ResponseEvent($this->createMock(Kernel::class), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response);
|
$event = new ResponseEvent($this->createMock(Kernel::class), new Request(), HttpKernelInterface::MASTER_REQUEST, $response);
|
||||||
|
|
||||||
$listener = new WebDebugToolbarListener($this->getTwigMock());
|
$listener = new WebDebugToolbarListener($this->getTwigMock());
|
||||||
$listener->onKernelResponse($event);
|
$listener->onKernelResponse($event);
|
||||||
@ -108,7 +106,7 @@ class WebDebugToolbarListenerTest extends TestCase
|
|||||||
$response = new Response('<html><head></head><body></body></html>');
|
$response = new Response('<html><head></head><body></body></html>');
|
||||||
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
|
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
|
||||||
$response->headers->set('Content-Type', 'text/xml');
|
$response->headers->set('Content-Type', 'text/xml');
|
||||||
$event = new ResponseEvent($this->createMock(Kernel::class), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response);
|
$event = new ResponseEvent($this->createMock(Kernel::class), new Request(), HttpKernelInterface::MASTER_REQUEST, $response);
|
||||||
|
|
||||||
$listener = new WebDebugToolbarListener($this->getTwigMock());
|
$listener = new WebDebugToolbarListener($this->getTwigMock());
|
||||||
$listener->onKernelResponse($event);
|
$listener->onKernelResponse($event);
|
||||||
@ -124,7 +122,7 @@ class WebDebugToolbarListenerTest extends TestCase
|
|||||||
$response = new Response('<html><head></head><body></body></html>');
|
$response = new Response('<html><head></head><body></body></html>');
|
||||||
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
|
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
|
||||||
$response->headers->set('Content-Disposition', 'attachment; filename=test.html');
|
$response->headers->set('Content-Disposition', 'attachment; filename=test.html');
|
||||||
$event = new ResponseEvent($this->createMock(Kernel::class), $this->getRequestMock(false, 'html'), HttpKernelInterface::MASTER_REQUEST, $response);
|
$event = new ResponseEvent($this->createMock(Kernel::class), new Request(), HttpKernelInterface::MASTER_REQUEST, $response);
|
||||||
|
|
||||||
$listener = new WebDebugToolbarListener($this->getTwigMock());
|
$listener = new WebDebugToolbarListener($this->getTwigMock());
|
||||||
$listener->onKernelResponse($event);
|
$listener->onKernelResponse($event);
|
||||||
@ -136,11 +134,11 @@ class WebDebugToolbarListenerTest extends TestCase
|
|||||||
* @depends testToolbarIsInjected
|
* @depends testToolbarIsInjected
|
||||||
* @dataProvider provideRedirects
|
* @dataProvider provideRedirects
|
||||||
*/
|
*/
|
||||||
public function testToolbarIsNotInjectedOnRedirection($statusCode, $hasSession)
|
public function testToolbarIsNotInjectedOnRedirection($statusCode)
|
||||||
{
|
{
|
||||||
$response = new Response('<html><head></head><body></body></html>', $statusCode);
|
$response = new Response('<html><head></head><body></body></html>', $statusCode);
|
||||||
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
|
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
|
||||||
$event = new ResponseEvent($this->createMock(Kernel::class), $this->getRequestMock(false, 'html', $hasSession), HttpKernelInterface::MASTER_REQUEST, $response);
|
$event = new ResponseEvent($this->createMock(Kernel::class), new Request(), HttpKernelInterface::MASTER_REQUEST, $response);
|
||||||
|
|
||||||
$listener = new WebDebugToolbarListener($this->getTwigMock());
|
$listener = new WebDebugToolbarListener($this->getTwigMock());
|
||||||
$listener->onKernelResponse($event);
|
$listener->onKernelResponse($event);
|
||||||
@ -151,10 +149,8 @@ class WebDebugToolbarListenerTest extends TestCase
|
|||||||
public function provideRedirects()
|
public function provideRedirects()
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
[301, true],
|
[301],
|
||||||
[302, true],
|
[302],
|
||||||
[301, false],
|
|
||||||
[302, false],
|
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -165,7 +161,7 @@ class WebDebugToolbarListenerTest extends TestCase
|
|||||||
{
|
{
|
||||||
$response = new Response('<html><head></head><body></body></html>');
|
$response = new Response('<html><head></head><body></body></html>');
|
||||||
|
|
||||||
$event = new ResponseEvent($this->createMock(Kernel::class), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response);
|
$event = new ResponseEvent($this->createMock(Kernel::class), new Request(), HttpKernelInterface::MASTER_REQUEST, $response);
|
||||||
|
|
||||||
$listener = new WebDebugToolbarListener($this->getTwigMock());
|
$listener = new WebDebugToolbarListener($this->getTwigMock());
|
||||||
$listener->onKernelResponse($event);
|
$listener->onKernelResponse($event);
|
||||||
@ -181,7 +177,7 @@ class WebDebugToolbarListenerTest extends TestCase
|
|||||||
$response = new Response('<html><head></head><body></body></html>');
|
$response = new Response('<html><head></head><body></body></html>');
|
||||||
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
|
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
|
||||||
|
|
||||||
$event = new ResponseEvent($this->createMock(Kernel::class), $this->getRequestMock(), HttpKernelInterface::SUB_REQUEST, $response);
|
$event = new ResponseEvent($this->createMock(Kernel::class), new Request(), HttpKernelInterface::SUB_REQUEST, $response);
|
||||||
|
|
||||||
$listener = new WebDebugToolbarListener($this->getTwigMock());
|
$listener = new WebDebugToolbarListener($this->getTwigMock());
|
||||||
$listener->onKernelResponse($event);
|
$listener->onKernelResponse($event);
|
||||||
@ -197,7 +193,7 @@ class WebDebugToolbarListenerTest extends TestCase
|
|||||||
$response = new Response('<div>Some content</div>');
|
$response = new Response('<div>Some content</div>');
|
||||||
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
|
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
|
||||||
|
|
||||||
$event = new ResponseEvent($this->createMock(Kernel::class), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response);
|
$event = new ResponseEvent($this->createMock(Kernel::class), new Request(), HttpKernelInterface::MASTER_REQUEST, $response);
|
||||||
|
|
||||||
$listener = new WebDebugToolbarListener($this->getTwigMock());
|
$listener = new WebDebugToolbarListener($this->getTwigMock());
|
||||||
$listener->onKernelResponse($event);
|
$listener->onKernelResponse($event);
|
||||||
@ -213,7 +209,10 @@ class WebDebugToolbarListenerTest extends TestCase
|
|||||||
$response = new Response('<html><head></head><body></body></html>');
|
$response = new Response('<html><head></head><body></body></html>');
|
||||||
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
|
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
|
||||||
|
|
||||||
$event = new ResponseEvent($this->createMock(Kernel::class), $this->getRequestMock(true), HttpKernelInterface::MASTER_REQUEST, $response);
|
$request = new Request();
|
||||||
|
$request->headers->set('X-Requested-With', 'XMLHttpRequest');
|
||||||
|
|
||||||
|
$event = new ResponseEvent($this->createMock(Kernel::class), $request, HttpKernelInterface::MASTER_REQUEST, $response);
|
||||||
|
|
||||||
$listener = new WebDebugToolbarListener($this->getTwigMock());
|
$listener = new WebDebugToolbarListener($this->getTwigMock());
|
||||||
$listener->onKernelResponse($event);
|
$listener->onKernelResponse($event);
|
||||||
@ -229,7 +228,7 @@ class WebDebugToolbarListenerTest extends TestCase
|
|||||||
$response = new Response('<html><head></head><body></body></html>');
|
$response = new Response('<html><head></head><body></body></html>');
|
||||||
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
|
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
|
||||||
|
|
||||||
$event = new ResponseEvent($this->createMock(Kernel::class), $this->getRequestMock(false, 'json'), HttpKernelInterface::MASTER_REQUEST, $response);
|
$event = new ResponseEvent($this->createMock(Kernel::class), new Request([], [], ['_format' => 'json']), HttpKernelInterface::MASTER_REQUEST, $response);
|
||||||
|
|
||||||
$listener = new WebDebugToolbarListener($this->getTwigMock());
|
$listener = new WebDebugToolbarListener($this->getTwigMock());
|
||||||
$listener->onKernelResponse($event);
|
$listener->onKernelResponse($event);
|
||||||
@ -250,7 +249,7 @@ class WebDebugToolbarListenerTest extends TestCase
|
|||||||
->willReturn('http://mydomain.com/_profiler/xxxxxxxx')
|
->willReturn('http://mydomain.com/_profiler/xxxxxxxx')
|
||||||
;
|
;
|
||||||
|
|
||||||
$event = new ResponseEvent($this->createMock(Kernel::class), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response);
|
$event = new ResponseEvent($this->createMock(Kernel::class), new Request(), HttpKernelInterface::MASTER_REQUEST, $response);
|
||||||
|
|
||||||
$listener = new WebDebugToolbarListener($this->getTwigMock(), false, WebDebugToolbarListener::ENABLED, $urlGenerator);
|
$listener = new WebDebugToolbarListener($this->getTwigMock(), false, WebDebugToolbarListener::ENABLED, $urlGenerator);
|
||||||
$listener->onKernelResponse($event);
|
$listener->onKernelResponse($event);
|
||||||
@ -271,7 +270,7 @@ class WebDebugToolbarListenerTest extends TestCase
|
|||||||
->willThrowException(new \Exception('foo'))
|
->willThrowException(new \Exception('foo'))
|
||||||
;
|
;
|
||||||
|
|
||||||
$event = new ResponseEvent($this->createMock(Kernel::class), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response);
|
$event = new ResponseEvent($this->createMock(Kernel::class), new Request(), HttpKernelInterface::MASTER_REQUEST, $response);
|
||||||
|
|
||||||
$listener = new WebDebugToolbarListener($this->getTwigMock(), false, WebDebugToolbarListener::ENABLED, $urlGenerator);
|
$listener = new WebDebugToolbarListener($this->getTwigMock(), false, WebDebugToolbarListener::ENABLED, $urlGenerator);
|
||||||
$listener->onKernelResponse($event);
|
$listener->onKernelResponse($event);
|
||||||
@ -292,7 +291,7 @@ class WebDebugToolbarListenerTest extends TestCase
|
|||||||
->willThrowException(new \Exception("This\nmultiline\r\ntabbed text should\tcome out\r on\n \ta single plain\r\nline"))
|
->willThrowException(new \Exception("This\nmultiline\r\ntabbed text should\tcome out\r on\n \ta single plain\r\nline"))
|
||||||
;
|
;
|
||||||
|
|
||||||
$event = new ResponseEvent($this->createMock(Kernel::class), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response);
|
$event = new ResponseEvent($this->createMock(Kernel::class), new Request(), HttpKernelInterface::MASTER_REQUEST, $response);
|
||||||
|
|
||||||
$listener = new WebDebugToolbarListener($this->getTwigMock(), false, WebDebugToolbarListener::ENABLED, $urlGenerator);
|
$listener = new WebDebugToolbarListener($this->getTwigMock(), false, WebDebugToolbarListener::ENABLED, $urlGenerator);
|
||||||
$listener->onKernelResponse($event);
|
$listener->onKernelResponse($event);
|
||||||
@ -300,28 +299,6 @@ class WebDebugToolbarListenerTest extends TestCase
|
|||||||
$this->assertEquals('Exception: This multiline tabbed text should come out on a single plain line', $response->headers->get('X-Debug-Error'));
|
$this->assertEquals('Exception: This multiline tabbed text should come out on a single plain line', $response->headers->get('X-Debug-Error'));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getRequestMock($isXmlHttpRequest = false, $requestFormat = 'html', $hasSession = true)
|
|
||||||
{
|
|
||||||
$request = $this->getMockBuilder(Request::class)->setMethods(['getSession', 'isXmlHttpRequest', 'getRequestFormat'])->disableOriginalConstructor()->getMock();
|
|
||||||
$request->expects($this->any())
|
|
||||||
->method('isXmlHttpRequest')
|
|
||||||
->willReturn($isXmlHttpRequest);
|
|
||||||
$request->expects($this->any())
|
|
||||||
->method('getRequestFormat')
|
|
||||||
->willReturn($requestFormat);
|
|
||||||
|
|
||||||
$request->headers = new HeaderBag();
|
|
||||||
|
|
||||||
if ($hasSession) {
|
|
||||||
$session = $this->createMock(Session::class);
|
|
||||||
$request->expects($this->any())
|
|
||||||
->method('getSession')
|
|
||||||
->willReturn($session);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $request;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function getTwigMock($render = 'WDT')
|
protected function getTwigMock($render = 'WDT')
|
||||||
{
|
{
|
||||||
$templating = $this->createMock(Environment::class);
|
$templating = $this->createMock(Environment::class);
|
||||||
|
@ -29,7 +29,7 @@ class DisallowRobotsIndexingListenerTest extends TestCase
|
|||||||
$response = new Response(...$responseArgs);
|
$response = new Response(...$responseArgs);
|
||||||
$listener = new DisallowRobotsIndexingListener();
|
$listener = new DisallowRobotsIndexingListener();
|
||||||
|
|
||||||
$event = new ResponseEvent($this->createMock(HttpKernelInterface::class), $this->createMock(Request::class), KernelInterface::MASTER_REQUEST, $response);
|
$event = new ResponseEvent($this->createMock(HttpKernelInterface::class), new Request(), KernelInterface::MASTER_REQUEST, $response);
|
||||||
|
|
||||||
$listener->onResponse($event);
|
$listener->onResponse($event);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user