From 994e30ed624aff6f4a9f1505235cd2b1acdd30f8 Mon Sep 17 00:00:00 2001 From: Marcel Strahl Date: Wed, 16 Sep 2020 23:09:16 +0200 Subject: [PATCH 1/3] Added setter method for SerializeType in ResponseFactory to dynamically change the serialization --- src/ResponseFactory.php | 31 ++++++++++++++++++++++++++++--- tests/ResponseFactoryTest.php | 10 +++++----- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/ResponseFactory.php b/src/ResponseFactory.php index b2ca6a8..c3e231d 100644 --- a/src/ResponseFactory.php +++ b/src/ResponseFactory.php @@ -6,6 +6,7 @@ use ArrayIterator; use Dropelikeit\LaravelJmsSerializer\Config\Config; +use Dropelikeit\LaravelJmsSerializer\Exception\SerializeType; use JMS\Serializer\SerializationContext; use JMS\Serializer\SerializerInterface; use Symfony\Component\HttpFoundation\JsonResponse; @@ -35,10 +36,22 @@ class ResponseFactory */ private $context; + /** + * @var string + */ + private $serializeType; + + /** + * @var string + */ + private $cacheDir; + public function __construct(SerializerInterface $serializer, Config $config) { - $this->serializer = $serializer; $this->config = $config; + $this->serializer = $serializer; + $this->serializeType = $config->getSerializeType(); + $this->cacheDir = $config->getCacheDir(); } public function withStatusCode(int $code): void @@ -51,13 +64,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 ); @@ -67,7 +92,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 7f254cc..6463f58 100644 --- a/tests/ResponseFactoryTest.php +++ b/tests/ResponseFactoryTest.php @@ -39,7 +39,7 @@ public function setUp(): void public function canCreateResponse(): void { $this->config - ->expects($this->once()) + ->expects($this->exactly(2)) ->method('getCacheDir') ->willReturn(__DIR__); @@ -67,7 +67,7 @@ public function canCreateResponse(): void public function canCreateFromArrayIterator(): void { $this->config - ->expects($this->once()) + ->expects($this->exactly(2)) ->method('getCacheDir') ->willReturn(__DIR__); @@ -95,7 +95,7 @@ public function canCreateFromArrayIterator(): void public function canCreateResponseFromArray(): void { $this->config - ->expects($this->once()) + ->expects($this->exactly(2)) ->method('getCacheDir') ->willReturn(__DIR__); @@ -126,7 +126,7 @@ public function canCreateResponseFromArray(): void public function canChangeStatusCode(): void { $this->config - ->expects($this->once()) + ->expects($this->exactly(2)) ->method('getCacheDir') ->willReturn(__DIR__); @@ -156,7 +156,7 @@ public function canChangeStatusCode(): void public function canUseGivenContext(): void { $this->config - ->expects($this->once()) + ->expects($this->exactly(2)) ->method('getCacheDir') ->willReturn(__DIR__); From b97d17750309bbd173e270de6d45a8cbae107a53 Mon Sep 17 00:00:00 2001 From: Marcel Strahl Date: Thu, 17 Sep 2020 10:18:53 +0200 Subject: [PATCH 2/3] add serialize type tests --- phpunit.xml | 26 -------- phpunit.xml.dist | 19 ++++++ tests/ResponseFactoryTest.php | 113 ++++++++++++++++++++++++++-------- 3 files changed, 108 insertions(+), 50 deletions(-) delete mode 100755 phpunit.xml create mode 100755 phpunit.xml.dist diff --git a/phpunit.xml b/phpunit.xml deleted file mode 100755 index 51e51e3..0000000 --- a/phpunit.xml +++ /dev/null @@ -1,26 +0,0 @@ - - - - tests - - - - src - - ./tests - ./vendor - - - - - - - diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100755 index 0000000..477377d --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,19 @@ + + + + + src + + + ./tests + ./vendor + + + + + + + tests + + + diff --git a/tests/ResponseFactoryTest.php b/tests/ResponseFactoryTest.php index 6463f58..d2a1634 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\Exception\SerializeType; use Dropelikeit\LaravelJmsSerializer\ResponseFactory; use Dropelikeit\LaravelJmsSerializer\Serializer\Factory; use Dropelikeit\LaravelJmsSerializer\Tests\ResponseFactory\Dummy; @@ -39,17 +40,17 @@ public function setUp(): void public function canCreateResponse(): void { $this->config - ->expects($this->exactly(2)) + ->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); @@ -57,8 +58,8 @@ public function canCreateResponse(): void $response = $responseFactory->create(new Dummy()); - $this->assertEquals(200, $response->getStatusCode()); - $this->assertEquals('{"amount":12,"text":"Hello World!"}', $response->getContent()); + self::assertEquals(200, $response->getStatusCode()); + self::assertEquals('{"amount":12,"text":"Hello World!"}', $response->getContent()); } /** @@ -67,17 +68,17 @@ public function canCreateResponse(): void public function canCreateFromArrayIterator(): void { $this->config - ->expects($this->exactly(2)) + ->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); @@ -85,8 +86,8 @@ public function canCreateFromArrayIterator(): void $response = $responseFactory->create(Response::create([new Response\Item()])); - $this->assertEquals(200, $response->getStatusCode()); - $this->assertEquals('[{"key":"magic_number","value":12}]', $response->getContent()); + self::assertEquals(200, $response->getStatusCode()); + self::assertEquals('[{"key":"magic_number","value":12}]', $response->getContent()); } /** @@ -95,17 +96,17 @@ public function canCreateFromArrayIterator(): void public function canCreateResponseFromArray(): void { $this->config - ->expects($this->exactly(2)) + ->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,17 +127,17 @@ public function canCreateResponseFromArray(): void public function canChangeStatusCode(): void { $this->config - ->expects($this->exactly(2)) + ->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); @@ -146,8 +147,8 @@ public function canChangeStatusCode(): void $response = $responseFactory->create(new Dummy()); - $this->assertEquals(404, $response->getStatusCode()); - $this->assertEquals('{"amount":12,"text":"Hello World!"}', $response->getContent()); + self::assertEquals(404, $response->getStatusCode()); + self::assertEquals('{"amount":12,"text":"Hello World!"}', $response->getContent()); } /** @@ -156,17 +157,17 @@ public function canChangeStatusCode(): void public function canUseGivenContext(): void { $this->config - ->expects($this->exactly(2)) + ->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); @@ -175,6 +176,70 @@ public function canUseGivenContext(): void $response = $responseFactory->create(new Dummy()); - $this->assertEquals('{"amount":12,"text":"Hello World!","items":null}', $response->getContent()); + 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'); } } From 7e1bbdadee2766fb7db5ef31b559b99d91658611 Mon Sep 17 00:00:00 2001 From: Marcel Strahl Date: Thu, 17 Sep 2020 10:26:23 +0200 Subject: [PATCH 3/3] resolve merge conflicts --- tests/ResponseFactoryTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/ResponseFactoryTest.php b/tests/ResponseFactoryTest.php index d9f60a9..770c768 100644 --- a/tests/ResponseFactoryTest.php +++ b/tests/ResponseFactoryTest.php @@ -4,6 +4,7 @@ namespace Dropelikeit\LaravelJmsSerializer\Tests; use Doctrine\Common\Annotations\AnnotationRegistry; +use Dropelikeit\LaravelJmsSerializer\Config\Config; use Dropelikeit\LaravelJmsSerializer\Config\ConfigInterface; use Dropelikeit\LaravelJmsSerializer\Exception\SerializeType; use Dropelikeit\LaravelJmsSerializer\ResponseFactory;