From 1b67362a820720a59b8e35f9916c7b7c26c1e826 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20R=C3=B6=C3=9Fner?= Date: Mon, 1 Mar 2021 17:01:23 +0100 Subject: [PATCH] fixed issue in JsonSerializeResponseHandler, since serialized content is already json. --- composer.json | 3 ++- .../Handler/JsonSerializeResponseHandler.php | 1 + .../JsonSerializeResponseHandlerTest.php | 17 ++++++++++++----- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index d550ac5..0279d39 100644 --- a/composer.json +++ b/composer.json @@ -33,7 +33,8 @@ "friendsofphp/php-cs-fixer": "^2.18", "vimeo/psalm": "^4.6", "phpunit/phpunit": "^9.5", - "phpspec/prophecy-phpunit": "^2.0" + "phpspec/prophecy-phpunit": "^2.0", + "symfony/property-access": "^5.2" }, "suggest": { "symfony/event-dispatcher": "Required when using any of the event handlers.", diff --git a/src/Response/Handler/JsonSerializeResponseHandler.php b/src/Response/Handler/JsonSerializeResponseHandler.php index 6ca4360..2d0fd48 100644 --- a/src/Response/Handler/JsonSerializeResponseHandler.php +++ b/src/Response/Handler/JsonSerializeResponseHandler.php @@ -29,6 +29,7 @@ protected function generateResponse(LazyResponseInterface $controllerResult): Re $this->serializer->serialize($controllerResult->getData(), 'json'), $controllerResult->getStatusCode(), $controllerResult->getHeaders(), + true ); } diff --git a/tests/Response/Handler/JsonSerializeResponseHandlerTest.php b/tests/Response/Handler/JsonSerializeResponseHandlerTest.php index 40b3c34..5141d90 100644 --- a/tests/Response/Handler/JsonSerializeResponseHandlerTest.php +++ b/tests/Response/Handler/JsonSerializeResponseHandlerTest.php @@ -7,7 +7,9 @@ use Basster\LazyResponseBundle\Response\Handler\JsonSerializeResponseHandler; use Basster\LazyResponseBundle\Response\JsonSerializeResponse; use Prophecy\PhpUnit\ProphecyTrait; -use Prophecy\Prophecy\ObjectProphecy; +use Symfony\Component\Serializer\Encoder\JsonEncoder; +use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; +use Symfony\Component\Serializer\Serializer; use Symfony\Component\Serializer\SerializerInterface; /** @@ -19,11 +21,11 @@ final class JsonSerializeResponseHandlerTest extends AbstractLazyResponseHandler { use ProphecyTrait; - private SerializerInterface | ObjectProphecy $serializer; + private SerializerInterface $serializer; protected function setUp(): void { - $this->serializer = $this->prophesize(SerializerInterface::class); + $this->serializer = new Serializer([new ObjectNormalizer()], [new JsonEncoder()]); parent::setUp(); } @@ -33,11 +35,16 @@ protected function setUp(): void public function serializeControllerResultDataWhenSupported(): void { $data = new class() { + public string $foo = 'bar'; }; $controllerResult = new JsonSerializeResponse($data); $event = $this->createViewEvent($controllerResult); $this->handler->handleLazyResponse($event); - $this->serializer->serialize($data, 'json')->shouldHaveBeenCalled(); + + self::assertJsonStringEqualsJsonString( + '{"foo":"bar"}', + $event->getResponse()->getContent() + ); } /** @@ -75,6 +82,6 @@ protected function getHandlerClassName(): string protected function createHandlerSubject(): AbstractLazyResponseHandler { - return new JsonSerializeResponseHandler($this->serializer->reveal()); + return new JsonSerializeResponseHandler($this->serializer); } }