Symfony\Bundle\FrameworkBundle\Tests\Controller\TestController::json PHP Method

json() public method

public json ( $data, $status = 200, $headers = [], $context = [] )
    public function json($data, $status = 200, $headers = array(), $context = array())
    {
        return parent::json($data, $status, $headers, $context);
    }

Usage Example

Example #1
0
 public function testJsonWithSerializerContextOverride()
 {
     $container = $this->getMockBuilder(ContainerInterface::class)->getMock();
     $container->expects($this->once())->method('has')->with('serializer')->will($this->returnValue(true));
     $serializer = $this->getMockBuilder(SerializerInterface::class)->getMock();
     $serializer->expects($this->once())->method('serialize')->with(array(), 'json', array('json_encode_options' => 0, 'other' => 'context'))->will($this->returnValue('[]'));
     $container->expects($this->once())->method('get')->with('serializer')->will($this->returnValue($serializer));
     $controller = new TestController();
     $controller->setContainer($container);
     $response = $controller->json(array(), 200, array(), array('json_encode_options' => 0, 'other' => 'context'));
     $this->assertInstanceOf(JsonResponse::class, $response);
     $this->assertEquals('[]', $response->getContent());
     $response->setEncodingOptions(JSON_FORCE_OBJECT);
     $this->assertEquals('{}', $response->getContent());
 }