Skip to content

Commit

Permalink
fixed issue in JsonSerializeResponseHandler, since serialized content…
Browse files Browse the repository at this point in the history
… is already json.
  • Loading branch information
Basster committed Mar 1, 2021
1 parent 5094146 commit 1b67362
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
1 change: 1 addition & 0 deletions src/Response/Handler/JsonSerializeResponseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ protected function generateResponse(LazyResponseInterface $controllerResult): Re
$this->serializer->serialize($controllerResult->getData(), 'json'),
$controllerResult->getStatusCode(),
$controllerResult->getHeaders(),
true
);
}

Expand Down
17 changes: 12 additions & 5 deletions tests/Response/Handler/JsonSerializeResponseHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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();
}

Expand All @@ -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()
);
}

/**
Expand Down Expand Up @@ -75,6 +82,6 @@ protected function getHandlerClassName(): string

protected function createHandlerSubject(): AbstractLazyResponseHandler
{
return new JsonSerializeResponseHandler($this->serializer->reveal());
return new JsonSerializeResponseHandler($this->serializer);
}
}

0 comments on commit 1b67362

Please # to comment.