diff --git a/src/ResponseFactory.php b/src/ResponseFactory.php index d721d6c..cbf7fa2 100644 --- a/src/ResponseFactory.php +++ b/src/ResponseFactory.php @@ -5,6 +5,7 @@ use ArrayIterator; use Dropelikeit\LaravelJmsSerializer\Config\ConfigInterface; +use Dropelikeit\LaravelJmsSerializer\Exception\SerializeType; use JMS\Serializer\SerializationContext; use JMS\Serializer\SerializerInterface; use Symfony\Component\HttpFoundation\JsonResponse; @@ -34,10 +35,22 @@ final class ResponseFactory */ private $context; + /** + * @var string + */ + private $serializeType; + + /** + * @var string + */ + private $cacheDir; + public function __construct(SerializerInterface $serializer, ConfigInterface $config) { - $this->serializer = $serializer; $this->config = $config; + $this->serializer = $serializer; + $this->serializeType = $config->getSerializeType(); + $this->cacheDir = $config->getCacheDir(); } public function withStatusCode(int $code): void @@ -50,13 +63,25 @@ public function withContext(SerializationContext $context): void $this->context = $context; } + public function withSerializeType(string $serializeType): self + { + if (!in_array($serializeType, [Config::SERIALIZE_TYPE_JSON, Config::SERIALIZE_TYPE_XML], true)) { + throw SerializeType::fromUnsupportedSerializeType($serializeType); + } + + $instance = new self($this->serializer, $this->config); + $instance->serializeType = $serializeType; + + return $instance; + } + public function create(object $jmsResponse): JsonResponse { $initialType = $this->getInitialType($jmsResponse); $content = $this->serializer->serialize( $jmsResponse, - $this->config->getSerializeType(), + $this->serializeType, $this->context, $initialType ); @@ -71,7 +96,7 @@ public function create(object $jmsResponse): JsonResponse */ public function createFromArray(array $jmsResponse): JsonResponse { - $content = $this->serializer->serialize($jmsResponse, $this->config->getSerializeType(), $this->context); + $content = $this->serializer->serialize($jmsResponse, $this->serializeType, $this->context); return new JsonResponse($content, $this->status, ['application/json'], true); } diff --git a/tests/ResponseFactoryTest.php b/tests/ResponseFactoryTest.php index dc3f395..770c768 100644 --- a/tests/ResponseFactoryTest.php +++ b/tests/ResponseFactoryTest.php @@ -6,6 +6,7 @@ use Doctrine\Common\Annotations\AnnotationRegistry; use Dropelikeit\LaravelJmsSerializer\Config\Config; use Dropelikeit\LaravelJmsSerializer\Config\ConfigInterface; +use Dropelikeit\LaravelJmsSerializer\Exception\SerializeType; use Dropelikeit\LaravelJmsSerializer\ResponseFactory; use Dropelikeit\LaravelJmsSerializer\Serializer\Factory; use Dropelikeit\LaravelJmsSerializer\Tests\ResponseFactory\Dummy; @@ -39,7 +40,7 @@ public function setUp(): void public function canCreateResponse(): void { $this->config - ->expects(self::once()) + ->expects(self::exactly(2)) ->method('getCacheDir') ->willReturn(__DIR__); @@ -67,7 +68,7 @@ public function canCreateResponse(): void public function canCreateFromArrayIterator(): void { $this->config - ->expects(self::once()) + ->expects(self::exactly(2)) ->method('getCacheDir') ->willReturn(__DIR__); @@ -95,17 +96,17 @@ public function canCreateFromArrayIterator(): void public function canCreateResponseFromArray(): void { $this->config - ->expects($this->once()) + ->expects(self::exactly(2)) ->method('getCacheDir') ->willReturn(__DIR__); $this->config - ->expects($this->once()) + ->expects(self::once()) ->method('debug') ->willReturn(true); $this->config - ->expects($this->once()) + ->expects(self::once()) ->method('getSerializeType') ->willReturn(Config::SERIALIZE_TYPE_JSON); @@ -113,8 +114,8 @@ public function canCreateResponseFromArray(): void $response = $responseFactory->createFromArray(require __DIR__ . '/ResponseFactory/dummy_array.php'); - $this->assertEquals(200, $response->getStatusCode()); - $this->assertEquals( + self::assertEquals(200, $response->getStatusCode()); + self::assertEquals( '{"some_objects":{"person":{"first_name":"Max","last_name":"Mustermann","birthdate":"01.01.1976","birth_place":"Berlin","nationality":"german"}}}', $response->getContent() ); @@ -126,7 +127,7 @@ public function canCreateResponseFromArray(): void public function canChangeStatusCode(): void { $this->config - ->expects(self::once()) + ->expects(self::exactly(2)) ->method('getCacheDir') ->willReturn(__DIR__); @@ -156,7 +157,7 @@ public function canChangeStatusCode(): void public function canUseGivenContext(): void { $this->config - ->expects(self::once()) + ->expects(self::exactly(2)) ->method('getCacheDir') ->willReturn(__DIR__); @@ -177,4 +178,68 @@ public function canUseGivenContext(): void self::assertEquals('{"amount":12,"text":"Hello World!","items":null}', $response->getContent()); } + + /** + * @test + */ + public function canWithSerializeType(): void + { + $this->config + ->expects(self::exactly(3)) + ->method('getCacheDir') + ->willReturn(__DIR__); + + $this->config + ->expects(self::once()) + ->method('debug') + ->willReturn(true); + + $this->config + ->expects(self::exactly(2)) + ->method('getSerializeType') + ->willReturn(Config::SERIALIZE_TYPE_JSON); + + $responseFactory = new ResponseFactory((new Factory())->getSerializer($this->config), $this->config); + $responseFactory->withContext(SerializationContext::create()->setSerializeNull(true)); + $responseFactory = $responseFactory->withSerializeType(Config::SERIALIZE_TYPE_XML); + + $response = $responseFactory->create(new Dummy()); + + self::assertEquals( + ' + + 12 + + +', + $response->getContent() + ); + } + + /** + * @test + */ + public function canNotCreateWithUnknownSerializeType(): void + { + $this->expectException(SerializeType::class); + + $this->config + ->expects(self::exactly(2)) + ->method('getCacheDir') + ->willReturn(__DIR__); + + $this->config + ->expects(self::once()) + ->method('debug') + ->willReturn(true); + + $this->config + ->expects(self::once()) + ->method('getSerializeType') + ->willReturn(Config::SERIALIZE_TYPE_JSON); + + $responseFactory = new ResponseFactory((new Factory())->getSerializer($this->config), $this->config); + $responseFactory->withContext(SerializationContext::create()->setSerializeNull(true)); + $responseFactory->withSerializeType('array'); + } }