/** * Set up mocks */ protected function setUp() { $this->jsonDecoderMock = $this->getMockBuilder('Symfony\\Component\\Serializer\\Encoder\\DecoderInterface')->getMockForAbstractClass(); $this->jsonDecoderMock->expects($this->any())->method('decode')->willReturnCallback(function ($string) { $data = json_decode($string); if (is_null($data)) { throw new \Exception(); } return $data; }); $this->jsonDecoderMock->expects($this->any())->method('supportsDecoding')->willReturn(true); $this->serializer = new SerializerAdapter(SymfonySerializerFactory::factory($this->jsonDecoderMock)); $this->contentDecoder = new ContentDecoder($this->serializer, new SerializationTypeResolver()); }
/** * @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()); }