From 8bb98451f1b41db93792d3027cc4825f8c98de79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Fr=C3=A9mont?= Date: Fri, 17 Nov 2023 17:09:14 +0100 Subject: [PATCH] Init respond processor --- .../State/Processor/RespondProcessorTest.php | 88 +++++++++++++++++++ .../src/State/Processor/RespondProcessor.php | 44 ++++++++++ 2 files changed, 132 insertions(+) create mode 100644 src/Component/Tests/State/Processor/RespondProcessorTest.php create mode 100644 src/Component/src/State/Processor/RespondProcessor.php diff --git a/src/Component/Tests/State/Processor/RespondProcessorTest.php b/src/Component/Tests/State/Processor/RespondProcessorTest.php new file mode 100644 index 000000000..3ea1c3cc2 --- /dev/null +++ b/src/Component/Tests/State/Processor/RespondProcessorTest.php @@ -0,0 +1,88 @@ +decorated = $this->prophesize(ProcessorInterface::class); + $this->responder = $this->prophesize(ResponderInterface::class); + + $this->respondProcessor = new RespondProcessor( + $this->decorated->reveal(), + $this->responder->reveal(), + ); + } + + /** @test */ + public function it_returns_a_response(): void + { + $response = $this->prophesize(Response::class); + $operation = $this->prophesize(HttpOperation::class); + + $context = new Context(); + + $this->responder->respond(['foo' => 'fighters'], $operation, $context) + ->willReturn($response) + ->shouldBeCalled() + ; + + $this->decorated->process(['foo' => 'fighters'], $operation, $context)->willReturn(['foo' => 'fighters'])->shouldBeCalled(); + + $data = $this->respondProcessor->process(['foo' => 'fighters'], $operation->reveal(), $context); + Assert::eq($data, $response->reveal()); + } + + /** @test */ + public function it_does_nothing_when_data_is_a_response(): void + { + $response = $this->prophesize(Response::class); + $operation = $this->prophesize(HttpOperation::class); + + $context = new Context(); + + $this->decorated->process($response, $operation, $context)->willReturn($response)->shouldBeCalled(); + + $this->responder->respond($response, $operation, $context) + ->willReturn($response) + ->shouldNotBeCalled() + ; + + $data = $this->respondProcessor->process($response, $operation->reveal(), $context); + Assert::eq($data, $response->reveal()); + } +} diff --git a/src/Component/src/State/Processor/RespondProcessor.php b/src/Component/src/State/Processor/RespondProcessor.php new file mode 100644 index 000000000..0b8e59b11 --- /dev/null +++ b/src/Component/src/State/Processor/RespondProcessor.php @@ -0,0 +1,44 @@ +decorated->process($data, $operation, $context); + + if ($data instanceof Response) { + return $data; + } + + $response = $this->responder->respond($data, $operation, $context); + Assert::isInstanceOf($response, Response::class); + + return $response; + } +}