From 1a7dcaf319bd5b8a37f51d3329507f2c8fbfcb6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Fr=C3=A9mont?= Date: Tue, 21 Nov 2023 11:33:20 +0100 Subject: [PATCH] Phpunit tests for Symfony respond listener --- .../EventListener/RespondListenerTest.php | 122 ++++++++++++++++++ .../EventListener/RespondListenerSpec.php | 112 ---------------- 2 files changed, 122 insertions(+), 112 deletions(-) create mode 100644 src/Component/Tests/Symfony/EventListener/RespondListenerTest.php delete mode 100644 src/Component/tests/spec/Symfony/EventListener/RespondListenerSpec.php diff --git a/src/Component/Tests/Symfony/EventListener/RespondListenerTest.php b/src/Component/Tests/Symfony/EventListener/RespondListenerTest.php new file mode 100644 index 000000000..5aaa138a5 --- /dev/null +++ b/src/Component/Tests/Symfony/EventListener/RespondListenerTest.php @@ -0,0 +1,122 @@ +operationInitiator = $this->prophesize(HttpOperationInitiatorInterface::class); + $this->contextInitiator = $this->prophesize(RequestContextInitiatorInterface::class); + $this->responder = $this->prophesize(ResponderInterface::class); + + $this->respondListener = new RespondListener( + $this->operationInitiator->reveal(), + $this->contextInitiator->reveal(), + $this->responder->reveal(), + ); + } + + /** @test */ + public function it_sets_a_response_on_event(): void + { + $kernel = $this->prophesize(HttpKernelInterface::class); + $request = $this->prophesize(Request::class); + $response = $this->prophesize(Response::class); + $attributes = $this->prophesize(ParameterBag::class); + $operation = $this->prophesize(HttpOperation::class); + + $event = new ViewEvent( + $kernel->reveal(), + $request->reveal(), + HttpKernelInterface::MAIN_REQUEST, + ['foo' => 'fighters'], + ); + + $context = new Context(); + + $this->contextInitiator->initializeContext($request)->willReturn($context); + $this->operationInitiator->initializeOperation($request)->willReturn($operation); + + $request->attributes = $attributes; + $request->getMethod()->willReturn('POST'); + + $this->responder->respond(['foo' => 'fighters'], $operation, $context) + ->willReturn($response) + ->shouldBeCalled() + ; + + $this->respondListener->onKernelView($event); + + Assert::eq($event->getResponse(), $response->reveal()); + } + + /** @test */ + public function it_does_nothing_when_controller_result_is_a_response(): void + { + $kernel = $this->prophesize(HttpKernelInterface::class); + $request = $this->prophesize(Request::class); + $response = $this->prophesize(Response::class); + $attributes = $this->prophesize(ParameterBag::class); + $operation = $this->prophesize(HttpOperation::class); + + $event = new ViewEvent( + $kernel->reveal(), + $request->reveal(), + HttpKernelInterface::MAIN_REQUEST, + $response->reveal(), + ); + + $context = new Context(); + + $this->contextInitiator->initializeContext($request)->willReturn($context); + $this->operationInitiator->initializeOperation($request)->willReturn($operation); + + $request->attributes = $attributes; + $request->getMethod()->willReturn('POST'); + + $this->responder->respond($response, $operation, $context) + ->willReturn($response) + ->shouldNotBeCalled() + ; + + $this->respondListener->onKernelView($event); + } +} diff --git a/src/Component/tests/spec/Symfony/EventListener/RespondListenerSpec.php b/src/Component/tests/spec/Symfony/EventListener/RespondListenerSpec.php deleted file mode 100644 index b2f7278d5..000000000 --- a/src/Component/tests/spec/Symfony/EventListener/RespondListenerSpec.php +++ /dev/null @@ -1,112 +0,0 @@ -beConstructedWith($operationInitiator, $contextInitiator, $responder); - } - - function it_is_initializable(): void - { - $this->shouldHaveType(RespondListener::class); - } - - function it_sets_a_response_on_event( - HttpKernelInterface $kernel, - Request $request, - HttpOperationInitiatorInterface $operationInitiator, - RequestContextInitiatorInterface $contextInitiator, - ParameterBag $attributes, - HttpOperation $operation, - ResponderInterface $responder, - Response $response, - ): void { - $event = new ViewEvent( - $kernel->getWrappedObject(), - $request->getWrappedObject(), - HttpKernelInterface::MAIN_REQUEST, - ['foo' => 'fighters'], - ); - - $context = new Context(); - - $contextInitiator->initializeContext($request)->willReturn($context); - $operationInitiator->initializeOperation($request)->willReturn($operation); - - $request->attributes = $attributes; - $request->getMethod()->willReturn('POST'); - - $responder->respond(['foo' => 'fighters'], $operation, $context) - ->willReturn($response) - ->shouldBeCalled() - ; - - $this->onKernelView($event); - - Assert::eq($event->getResponse(), $response->getWrappedObject()); - } - - function it_does_nothing_when_controller_result_is_a_response( - HttpKernelInterface $kernel, - Request $request, - HttpOperationInitiatorInterface $operationInitiator, - RequestContextInitiatorInterface $contextInitiator, - ParameterBag $attributes, - HttpOperation $operation, - ResponderInterface $responder, - Response $response, - ): void { - $event = new ViewEvent( - $kernel->getWrappedObject(), - $request->getWrappedObject(), - HttpKernelInterface::MAIN_REQUEST, - $response->getWrappedObject(), - ); - - $context = new Context(); - - $contextInitiator->initializeContext($request)->willReturn($context); - $operationInitiator->initializeOperation($request)->willReturn($operation); - - $request->attributes = $attributes; - $request->getMethod()->willReturn('POST'); - - $responder->respond($response, $operation, $context) - ->willReturn($response) - ->shouldNotBeCalled() - ; - - $this->onKernelView($event); - } -}