Skip to content

Commit

Permalink
Merge pull request #7 from Dropelikeit/feature/add-set-serialize-type…
Browse files Browse the repository at this point in the history
…-method

Added setter method for SerializeType in ResponseFactory to dynamical…
  • Loading branch information
Dropelikeit authored Sep 17, 2020
2 parents 24cc934 + 7e1bbda commit 6238d2f
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 12 deletions.
31 changes: 28 additions & 3 deletions src/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
);
Expand All @@ -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);
}
Expand Down
83 changes: 74 additions & 9 deletions tests/ResponseFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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__);

Expand Down Expand Up @@ -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__);

Expand Down Expand Up @@ -95,26 +96,26 @@ 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);

$responseFactory = new ResponseFactory((new Factory())->getSerializer($this->config), $this->config);

$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()
);
Expand All @@ -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__);

Expand Down Expand Up @@ -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__);

Expand All @@ -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(
'<?xml version="1.0" encoding="UTF-8"?>
<result>
<amount>12</amount>
<text><![CDATA[Hello World!]]></text>
</result>
',
$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');
}
}

0 comments on commit 6238d2f

Please sign in to comment.