Skip to content

Commit

Permalink
Merge pull request #1 from Dropelikeit/bugfix/can_not_handle_array_it…
Browse files Browse the repository at this point in the history
…erator_object

Bugfix/can not handle array iterator object
  • Loading branch information
Dropelikeit authored Feb 5, 2020
2 parents fe659d5 + 5cc517a commit 91e8170
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/Exception/MissingRequiredItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public static function fromConfig(string $fields): self
{
return new self(
sprintf(
'Missing required fields, please check your serializer-config. Missing fields "%s"',
$fields
),
'Missing required fields, please check your serializer-config. Missing fields "%s"',
$fields
),
400
);
}
Expand Down
19 changes: 18 additions & 1 deletion src/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Dropelikeit\LaravelJmsSerializer;

use ArrayIterator;
use Dropelikeit\LaravelJmsSerializer\Config\Config;
use JMS\Serializer\SerializationContext;
use JMS\Serializer\SerializerInterface;
Expand Down Expand Up @@ -52,7 +53,14 @@ public function withContext(SerializationContext $context): void

public function create(object $jmsResponse): JsonResponse
{
$content = $this->serializer->serialize($jmsResponse, $this->config->getSerializeType(), $this->context);
$initialType = $this->getInitialType($jmsResponse);

$content = $this->serializer->serialize(
$jmsResponse,
$this->config->getSerializeType(),
$this->context,
$initialType
);

return new JsonResponse($content, $this->status, ['application/json'], true);
}
Expand All @@ -63,4 +71,13 @@ public function createFromArray(array $jmsResponse): JsonResponse

return new JsonResponse($content, $this->status, ['application/json'], true);
}

private function getInitialType(object $jmsResponse): ?string
{
if ($jmsResponse instanceof ArrayIterator) {
return 'array';
}

return null;
}
}
4 changes: 2 additions & 2 deletions src/Serializer/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
namespace Dropelikeit\LaravelJmsSerializer\Serializer;

use Dropelikeit\LaravelJmsSerializer\Config\Config;
use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy;
use JMS\Serializer\Naming\SerializedNameAnnotationStrategy;
use JMS\Serializer\SerializationContext;
use JMS\Serializer\SerializerBuilder;
use JMS\Serializer\SerializerInterface;
use JMS\Serializer\Naming\SerializedNameAnnotationStrategy;
use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy;

/**
* @author Marcel Strahl <[email protected]>
Expand Down
28 changes: 28 additions & 0 deletions tests/ResponseFactory/Response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Dropelikeit\LaravelJmsSerializer\Tests\ResponseFactory;

use ArrayIterator;
use Dropelikeit\LaravelJmsSerializer\Tests\ResponseFactory\Response\Item;
use Webmozart\Assert\Assert;

/**
* @author Marcel Strahl <[email protected]>
*/
class Response extends ArrayIterator
{
private function __construct(array $items)
{
Assert::allIsInstanceOf($items, Item::class);
Assert::isList($items);

parent::__construct($items);
}

public static function create(array $items): self
{
return new self($items);
}
}
25 changes: 25 additions & 0 deletions tests/ResponseFactory/Response/Item.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Dropelikeit\LaravelJmsSerializer\Tests\ResponseFactory\Response;

use JMS\Serializer\Annotation as Serializer;

/**
* @author Marcel Strahl <[email protected]>
*/
class Item
{
/**
* @Serializer\Type("string")
* @var string
*/
private $key = 'magic_number';

/**
* @Serializer\Type("integer")
* @var int
*/
private $value = 12;
}
15 changes: 15 additions & 0 deletions tests/ResponseFactory/dummy_array.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

return [
'some_objects' => [
'person' => [
'first_name' => 'Max',
'last_name' => 'Mustermann',
'birthdate' => '01.01.1976',
'birth_place' => 'Berlin',
'nationality' => 'german',
],
],
];
60 changes: 60 additions & 0 deletions tests/ResponseFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Dropelikeit\LaravelJmsSerializer\ResponseFactory;
use Dropelikeit\LaravelJmsSerializer\Serializer\Factory;
use Dropelikeit\LaravelJmsSerializer\Tests\ResponseFactory\Dummy;
use Dropelikeit\LaravelJmsSerializer\Tests\ResponseFactory\Response;
use JMS\Serializer\SerializationContext;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -60,6 +61,65 @@ public function canCreateResponse(): void
$this->assertEquals('{"amount":12,"text":"Hello World!"}', $response->getContent());
}

/**
* @test
*/
public function canCreateFromArrayIterator(): void
{
$this->config
->expects($this->once())
->method('getCacheDir')
->willReturn(__DIR__);

$this->config
->expects($this->once())
->method('debug')
->willReturn(true);

$this->config
->expects($this->once())
->method('getSerializeType')
->willReturn(Config::SERIALIZE_TYPE_JSON);

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

$response = $responseFactory->create(Response::create([new Response\Item()]));

$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('[{"key":"magic_number","value":12}]', $response->getContent());
}

/**
* @test
*/
public function canCreateResponseFromArray(): void
{
$this->config
->expects($this->once())
->method('getCacheDir')
->willReturn(__DIR__);

$this->config
->expects($this->once())
->method('debug')
->willReturn(true);

$this->config
->expects($this->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(
'{"some_objects":{"person":{"first_name":"Max","last_name":"Mustermann","birthdate":"01.01.1976","birth_place":"Berlin","nationality":"german"}}}',
$response->getContent()
);
}

/**
* @test
*/
Expand Down

0 comments on commit 91e8170

Please sign in to comment.