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