Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ protected function json(mixed $data, int $status = 200, array $headers = [], arr
return new JsonResponse($json, $status, $headers, true);
}

if (null === $data) {
return new JsonResponse('null', $status, $headers, true);
}

return new JsonResponse($data, $status, $headers);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,37 @@ public function testJsonWithSerializerContextOverride()
$this->assertEquals('{}', $response->getContent());
}

public function testJsonNull()
{
$controller = $this->createController();
$controller->setContainer(new Container());

$response = $controller->json(null);
$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertSame('null', $response->getContent());
}

public function testJsonNullWithSerializer()
{
$container = new Container();

$serializer = $this->createMock(SerializerInterface::class);
$serializer
->expects($this->once())
->method('serialize')
->with(null, 'json', ['json_encode_options' => JsonResponse::DEFAULT_ENCODING_OPTIONS])
->willReturn('null');

$container->set('serializer', $serializer);

$controller = $this->createController();
$controller->setContainer($container);

$response = $controller->json(null);
$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertSame('null', $response->getContent());
}

public function testFile()
{
$container = new Container();
Expand Down
Loading