/**
  * @test
  */
 public function willCreateJsonResponseFromObject()
 {
     $serializer = new SerializerAdapter(JmsSerializerFactory::factory());
     $factory = new ResponseFactory(new DocumentRepository(), $serializer);
     $response = $factory->createResponse(new Request(), (new JmsAnnotatedResourceStub())->setFoo('bar'));
     $expected = json_encode(['foo' => 'bar']);
     $this->assertEquals($expected, $response->getContent());
 }
 /**
  * @test
  * @SuppressWarnings(PHPMD.EvalExpression)
  */
 public function willCreateJsonResponseFromObject()
 {
     $className = 'CreateJsonResponseFromObject';
     $number = 0;
     while (class_exists($className)) {
         $className .= ++$number;
     }
     eval("\n            class {$className} {\n                public function setFoo(\$foo){ \$this->foo = \$foo; return \$this;}\n                public function getFoo(){ return \$this->foo; }\n            }\n        ");
     $jsonEncoderMock = $this->getMockBuilder('Symfony\\Component\\Serializer\\Encoder\\EncoderInterface')->getMockForAbstractClass();
     $jsonEncoderMock->expects($this->once())->method('encode')->willReturnCallback(function ($string) {
         $data = json_encode($string);
         if (is_null($data)) {
             throw new \Exception();
         }
         return $data;
     });
     $jsonEncoderMock->expects($this->any())->method('supportsEncoding')->willReturn(true);
     $serializer = new SerializerAdapter(SymfonySerializerFactory::factory($jsonEncoderMock));
     $factory = new ResponseFactory(new DocumentRepository(), $serializer);
     $response = $factory->createResponse(new Request(), (new $className())->setFoo('bar'));
     $expected = json_encode(['foo' => 'bar']);
     $this->assertEquals($expected, $response->getContent());
 }
Example #3
0
 /**
  * @param GetResponseForControllerResultEvent $event
  *
  * @throws MalformedContentException
  * @throws UnsupportedContentTypeException
  */
 public function onKernelView(GetResponseForControllerResultEvent $event)
 {
     $result = $event->getControllerResult();
     $response = $this->responseFactory->createResponse($event->getRequest(), $result);
     $event->setResponse($response);
 }