fix tests mocking final events
This commit is contained in:
parent
5765539a3c
commit
637461fd51
@ -16,7 +16,8 @@ use Symfony\Bridge\Monolog\Processor\RouteProcessor;
|
|||||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
|
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
|
||||||
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
|
use Symfony\Component\HttpKernel\Event\RequestEvent;
|
||||||
|
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||||
|
|
||||||
class RouteProcessorTest extends TestCase
|
class RouteProcessorTest extends TestCase
|
||||||
{
|
{
|
||||||
@ -28,7 +29,7 @@ class RouteProcessorTest extends TestCase
|
|||||||
{
|
{
|
||||||
$request = $this->mockFilledRequest();
|
$request = $this->mockFilledRequest();
|
||||||
$processor = new RouteProcessor();
|
$processor = new RouteProcessor();
|
||||||
$processor->addRouteData($this->mockGetResponseEvent($request));
|
$processor->addRouteData($this->getRequestEvent($request));
|
||||||
|
|
||||||
$record = $processor(['extra' => []]);
|
$record = $processor(['extra' => []]);
|
||||||
|
|
||||||
@ -44,7 +45,7 @@ class RouteProcessorTest extends TestCase
|
|||||||
{
|
{
|
||||||
$request = $this->mockFilledRequest();
|
$request = $this->mockFilledRequest();
|
||||||
$processor = new RouteProcessor(false);
|
$processor = new RouteProcessor(false);
|
||||||
$processor->addRouteData($this->mockGetResponseEvent($request));
|
$processor->addRouteData($this->getRequestEvent($request));
|
||||||
|
|
||||||
$record = $processor(['extra' => []]);
|
$record = $processor(['extra' => []]);
|
||||||
|
|
||||||
@ -63,8 +64,8 @@ class RouteProcessorTest extends TestCase
|
|||||||
$subRequest = $this->mockFilledRequest($controllerFromSubRequest);
|
$subRequest = $this->mockFilledRequest($controllerFromSubRequest);
|
||||||
|
|
||||||
$processor = new RouteProcessor(false);
|
$processor = new RouteProcessor(false);
|
||||||
$processor->addRouteData($this->mockGetResponseEvent($mainRequest));
|
$processor->addRouteData($this->getRequestEvent($mainRequest));
|
||||||
$processor->addRouteData($this->mockGetResponseEvent($subRequest));
|
$processor->addRouteData($this->getRequestEvent($subRequest, HttpKernelInterface::SUB_REQUEST));
|
||||||
|
|
||||||
$record = $processor(['extra' => []]);
|
$record = $processor(['extra' => []]);
|
||||||
|
|
||||||
@ -86,9 +87,9 @@ class RouteProcessorTest extends TestCase
|
|||||||
$subRequest = $this->mockFilledRequest('OtherController::otherMethod');
|
$subRequest = $this->mockFilledRequest('OtherController::otherMethod');
|
||||||
|
|
||||||
$processor = new RouteProcessor(false);
|
$processor = new RouteProcessor(false);
|
||||||
$processor->addRouteData($this->mockGetResponseEvent($mainRequest));
|
$processor->addRouteData($this->getRequestEvent($mainRequest));
|
||||||
$processor->addRouteData($this->mockGetResponseEvent($subRequest));
|
$processor->addRouteData($this->getRequestEvent($subRequest, HttpKernelInterface::SUB_REQUEST));
|
||||||
$processor->removeRouteData($this->mockFinishRequestEvent($subRequest));
|
$processor->removeRouteData($this->getFinishRequestEvent($subRequest));
|
||||||
$record = $processor(['extra' => []]);
|
$record = $processor(['extra' => []]);
|
||||||
|
|
||||||
$this->assertArrayHasKey('requests', $record['extra']);
|
$this->assertArrayHasKey('requests', $record['extra']);
|
||||||
@ -98,7 +99,7 @@ class RouteProcessorTest extends TestCase
|
|||||||
$record['extra']['requests'][0]
|
$record['extra']['requests'][0]
|
||||||
);
|
);
|
||||||
|
|
||||||
$processor->removeRouteData($this->mockFinishRequestEvent($mainRequest));
|
$processor->removeRouteData($this->getFinishRequestEvent($mainRequest));
|
||||||
$record = $processor(['extra' => []]);
|
$record = $processor(['extra' => []]);
|
||||||
|
|
||||||
$this->assertArrayNotHasKey('requests', $record['extra']);
|
$this->assertArrayNotHasKey('requests', $record['extra']);
|
||||||
@ -108,7 +109,7 @@ class RouteProcessorTest extends TestCase
|
|||||||
{
|
{
|
||||||
$request = $this->mockEmptyRequest();
|
$request = $this->mockEmptyRequest();
|
||||||
$processor = new RouteProcessor();
|
$processor = new RouteProcessor();
|
||||||
$processor->addRouteData($this->mockGetResponseEvent($request));
|
$processor->addRouteData($this->getRequestEvent($request));
|
||||||
|
|
||||||
$record = $processor(['extra' => []]);
|
$record = $processor(['extra' => []]);
|
||||||
$this->assertEquals(['extra' => []], $record);
|
$this->assertEquals(['extra' => []], $record);
|
||||||
@ -122,20 +123,14 @@ class RouteProcessorTest extends TestCase
|
|||||||
$this->assertEquals(['extra' => []], $record);
|
$this->assertEquals(['extra' => []], $record);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function mockGetResponseEvent(Request $request): GetResponseEvent
|
private function getRequestEvent(Request $request, int $requestType = HttpKernelInterface::MASTER_REQUEST): RequestEvent
|
||||||
{
|
{
|
||||||
$event = $this->getMockBuilder(GetResponseEvent::class)->disableOriginalConstructor()->getMock();
|
return new RequestEvent($this->createMock(HttpKernelInterface::class), $request, $requestType);
|
||||||
$event->method('getRequest')->willReturn($request);
|
|
||||||
|
|
||||||
return $event;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function mockFinishRequestEvent(Request $request): FinishRequestEvent
|
private function getFinishRequestEvent(Request $request): FinishRequestEvent
|
||||||
{
|
{
|
||||||
$event = $this->getMockBuilder(FinishRequestEvent::class)->disableOriginalConstructor()->getMock();
|
return new FinishRequestEvent($this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MASTER_REQUEST);
|
||||||
$event->method('getRequest')->willReturn($request);
|
|
||||||
|
|
||||||
return $event;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function mockEmptyRequest(): Request
|
private function mockEmptyRequest(): Request
|
||||||
|
@ -16,6 +16,7 @@ use PHPUnit\Framework\TestCase;
|
|||||||
use Symfony\Bridge\Monolog\Processor\WebProcessor;
|
use Symfony\Bridge\Monolog\Processor\WebProcessor;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\HttpKernel\Event\RequestEvent;
|
use Symfony\Component\HttpKernel\Event\RequestEvent;
|
||||||
|
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||||
|
|
||||||
class WebProcessorTest extends TestCase
|
class WebProcessorTest extends TestCase
|
||||||
{
|
{
|
||||||
@ -71,7 +72,7 @@ class WebProcessorTest extends TestCase
|
|||||||
$this->assertEquals($server['HTTP_REFERER'], $record['extra']['referrer']);
|
$this->assertEquals($server['HTTP_REFERER'], $record['extra']['referrer']);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function createRequestEvent($additionalServerParameters = []): array
|
private function createRequestEvent(array $additionalServerParameters = []): array
|
||||||
{
|
{
|
||||||
$server = array_merge(
|
$server = array_merge(
|
||||||
[
|
[
|
||||||
@ -88,15 +89,7 @@ class WebProcessorTest extends TestCase
|
|||||||
$request->server->replace($server);
|
$request->server->replace($server);
|
||||||
$request->headers->replace($server);
|
$request->headers->replace($server);
|
||||||
|
|
||||||
$event = $this->getMockBuilder(RequestEvent::class)
|
$event = new RequestEvent($this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MASTER_REQUEST);
|
||||||
->disableOriginalConstructor()
|
|
||||||
->getMock();
|
|
||||||
$event->expects($this->any())
|
|
||||||
->method('isMasterRequest')
|
|
||||||
->willReturn(true);
|
|
||||||
$event->expects($this->any())
|
|
||||||
->method('getRequest')
|
|
||||||
->willReturn($request);
|
|
||||||
|
|
||||||
return [$event, $server];
|
return [$event, $server];
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,7 @@ namespace Symfony\Component\HttpKernel\Tests\EventListener;
|
|||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
|
||||||
use Symfony\Component\HttpKernel\Event\RequestEvent;
|
use Symfony\Component\HttpKernel\Event\RequestEvent;
|
||||||
use Symfony\Component\HttpKernel\EventListener\LocaleListener;
|
use Symfony\Component\HttpKernel\EventListener\LocaleListener;
|
||||||
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||||
@ -96,7 +97,7 @@ class LocaleListenerTest extends TestCase
|
|||||||
|
|
||||||
$this->requestStack->expects($this->once())->method('getParentRequest')->willReturn($parentRequest);
|
$this->requestStack->expects($this->once())->method('getParentRequest')->willReturn($parentRequest);
|
||||||
|
|
||||||
$event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\FinishRequestEvent')->disableOriginalConstructor()->getMock();
|
$event = new FinishRequestEvent($this->createMock(HttpKernelInterface::class), new Request(), HttpKernelInterface::MASTER_REQUEST);
|
||||||
|
|
||||||
$listener = new LocaleListener($this->requestStack, 'fr', $router);
|
$listener = new LocaleListener($this->requestStack, 'fr', $router);
|
||||||
$listener->onKernelFinishRequest($event);
|
$listener->onKernelFinishRequest($event);
|
||||||
|
@ -207,9 +207,6 @@ class ContextListenerTest extends TestCase
|
|||||||
{
|
{
|
||||||
$tokenStorage = $this->getMockBuilder(TokenStorageInterface::class)->getMock();
|
$tokenStorage = $this->getMockBuilder(TokenStorageInterface::class)->getMock();
|
||||||
$dispatcher = $this->getMockBuilder(EventDispatcherInterface::class)->getMock();
|
$dispatcher = $this->getMockBuilder(EventDispatcherInterface::class)->getMock();
|
||||||
$event = $this->getMockBuilder(ResponseEvent::class)
|
|
||||||
->disableOriginalConstructor()
|
|
||||||
->getMock();
|
|
||||||
|
|
||||||
$listener = new ContextListener($tokenStorage, [], 'key123', null, $dispatcher);
|
$listener = new ContextListener($tokenStorage, [], 'key123', null, $dispatcher);
|
||||||
|
|
||||||
@ -218,12 +215,7 @@ class ContextListenerTest extends TestCase
|
|||||||
->method('hasSession')
|
->method('hasSession')
|
||||||
->willReturn(true);
|
->willReturn(true);
|
||||||
|
|
||||||
$event->expects($this->any())
|
$event = new ResponseEvent($this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MASTER_REQUEST, new Response());
|
||||||
->method('isMasterRequest')
|
|
||||||
->willReturn(true);
|
|
||||||
$event->expects($this->any())
|
|
||||||
->method('getRequest')
|
|
||||||
->willReturn($request);
|
|
||||||
|
|
||||||
$dispatcher->expects($this->once())
|
$dispatcher->expects($this->once())
|
||||||
->method('removeListener')
|
->method('removeListener')
|
||||||
|
@ -90,16 +90,8 @@ class ResponseListenerTest extends TestCase
|
|||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getEvent($request, $response, $type = HttpKernelInterface::MASTER_REQUEST)
|
private function getEvent(Request $request, Response $response, int $type = HttpKernelInterface::MASTER_REQUEST): ResponseEvent
|
||||||
{
|
{
|
||||||
$event = $this->getMockBuilder(ResponseEvent::class)
|
return new ResponseEvent($this->createMock(HttpKernelInterface::class), $request, $type, $response);
|
||||||
->disableOriginalConstructor()
|
|
||||||
->getMock();
|
|
||||||
|
|
||||||
$event->expects($this->any())->method('getRequest')->willReturn($request);
|
|
||||||
$event->expects($this->any())->method('isMasterRequest')->willReturn(HttpKernelInterface::MASTER_REQUEST === $type);
|
|
||||||
$event->expects($this->any())->method('getResponse')->willReturn($response);
|
|
||||||
|
|
||||||
return $event;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
|||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Symfony\Component\HttpKernel\Event\ResponseEvent;
|
use Symfony\Component\HttpKernel\Event\ResponseEvent;
|
||||||
|
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||||
use Symfony\Component\HttpKernel\KernelEvents;
|
use Symfony\Component\HttpKernel\KernelEvents;
|
||||||
use Symfony\Component\WebLink\EventListener\AddLinkHeaderListener;
|
use Symfony\Component\WebLink\EventListener\AddLinkHeaderListener;
|
||||||
use Symfony\Component\WebLink\GenericLinkProvider;
|
use Symfony\Component\WebLink\GenericLinkProvider;
|
||||||
@ -33,10 +34,7 @@ class AddLinkHeaderListenerTest extends TestCase
|
|||||||
|
|
||||||
$subscriber = new AddLinkHeaderListener();
|
$subscriber = new AddLinkHeaderListener();
|
||||||
|
|
||||||
$event = $this->getMockBuilder(ResponseEvent::class)->disableOriginalConstructor()->getMock();
|
$event = new ResponseEvent($this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MASTER_REQUEST, $response);
|
||||||
$event->method('isMasterRequest')->willReturn(true);
|
|
||||||
$event->method('getRequest')->willReturn($request);
|
|
||||||
$event->method('getResponse')->willReturn($response);
|
|
||||||
|
|
||||||
$subscriber->onKernelResponse($event);
|
$subscriber->onKernelResponse($event);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user