Skip to content

Commit

Permalink
Merge pull request #16 from goodwix/Normalization_empty_typed_map
Browse files Browse the repository at this point in the history
Normalization_empty_typed_map
  • Loading branch information
strider2038 authored Jul 21, 2020
2 parents e466ad7 + 71cf70b commit 61dbaca
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 21 deletions.
10 changes: 7 additions & 3 deletions src/Serialization/RamseyCollection/TypedMapNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
use Ramsey\Collection\Exception\InvalidArgumentException as RamseyInvalidArgumentException;
use Ramsey\Collection\Map\MapInterface;
use Ramsey\Collection\Map\TypedMapInterface;
use Symfony\Component\Serializer\Encoder\NormalizationAwareInterface;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

class TypedMapNormalizer implements DenormalizerInterface, DenormalizerAwareInterface, NormalizerInterface, NormalizationAwareInterface
class TypedMapNormalizer implements DenormalizerInterface, DenormalizerAwareInterface, NormalizerInterface, NormalizerAwareInterface
{
use DenormalizerAwareTrait;
use NormalizerAwareTrait;
Expand Down Expand Up @@ -65,7 +65,11 @@ public function normalize($object, $format = null, array $context = [])
$normalizedMap = null;

if (count($object) > 0) {
$normalizedMap = $object->toArray();
$normalizedMap = [];

foreach ($object as $key => $value) {
$normalizedMap[$key] = $this->normalizer->normalize($value, $format, $context);
}
} else {
$normalizedMap = new \ArrayObject();
}
Expand Down
11 changes: 10 additions & 1 deletion tests/Functional/MapSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Goodwix\DoctrineJsonOdm\Tests\Resources\DummyEntity;
use Goodwix\DoctrineJsonOdm\Tests\Resources\DummyEntityMap;
use Goodwix\DoctrineJsonOdm\Tests\Resources\DummyMap;
use Goodwix\DoctrineJsonOdm\Tests\Resources\DummyPrimitiveMap;
use Goodwix\DoctrineJsonOdm\Tests\TestCase\SerializerTestCase;
use Ramsey\Collection\Map\TypedMapInterface;
Expand All @@ -24,7 +25,15 @@ public function serialize_map_encodedToJson(TypedMapInterface $map, string $expe
public function mapAndExpectedJsonProvider(): \Iterator
{
yield 'primitive map' => [new DummyPrimitiveMap(['key' => 'value']), '{"key":"value"}'];
yield 'entity map' => [new DummyEntityMap(['key' => new DummyEntity()]), '{"key":{}}'];
yield 'entity map' => [new DummyEntityMap(['key' => new DummyEntity()]), '{"key":{"field":null}}'];
yield 'empty map' => [new DummyPrimitiveMap(), '{}'];
yield 'map with inner entity map' => [
new DummyMap([
'key' => new DummyEntityMap([
'inner' => new DummyEntity(),
]),
]),
'{"key":{"inner":{"field":null}}}',
];
}
}
1 change: 1 addition & 0 deletions tests/Resources/DummyEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@

class DummyEntity
{
public $field;
}
19 changes: 19 additions & 0 deletions tests/Resources/DummyMap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Goodwix\DoctrineJsonOdm\Tests\Resources;

use Ramsey\Collection\Map\AbstractTypedMap;
use Ramsey\Collection\Map\MapInterface;

class DummyMap extends AbstractTypedMap
{
public function getKeyType(): string
{
return 'string';
}

public function getValueType(): string
{
return MapInterface::class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,24 @@
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

class TypedMapNormalizerTest extends TestCase
{
protected const JSON_FORMAT = 'json';
protected const KEY = 'key';
protected const VALUE = 'value';

/** @var DenormalizerInterface */
private $denormalizer;

/** @var NormalizerInterface */
private $normalizer;

protected function setUp(): void
{
$this->denormalizer = \Phake::mock(DenormalizerInterface::class);
$this->normalizer = \Phake::mock(NormalizerInterface::class);
}

/** @test */
Expand Down Expand Up @@ -66,7 +73,7 @@ public function denormalize_arrayOfClassMap_classMapReturned(): void
{
$normalizer = $this->createMapNormalizer();
$data = [
'key' => [
self::KEY => [
'id',
],
];
Expand All @@ -75,16 +82,16 @@ public function denormalize_arrayOfClassMap_classMapReturned(): void
$map = $normalizer->denormalize($data, DummyEntityMap::class, self::JSON_FORMAT);

$this->assertCount(1, $map);
$this->assertInstanceOf(DummyEntity::class, $map->get('key'));
$this->assertDenormalizer_denormalize_wasCalledOnceWithDataAndType($data['key'], DummyEntity::class);
$this->assertInstanceOf(DummyEntity::class, $map->get(self::KEY));
$this->assertDenormalizer_denormalize_wasCalledOnceWithDataAndType($data[self::KEY], DummyEntity::class);
}

/** @test */
public function denormalize_arrayOfInterfaceMap_interfaceMapReturned(): void
{
$normalizer = $this->createMapNormalizer();
$data = [
'key' => [
self::KEY => [
'id',
],
];
Expand All @@ -93,8 +100,8 @@ public function denormalize_arrayOfInterfaceMap_interfaceMapReturned(): void
$map = $normalizer->denormalize($data, DummyEntityInterfaceMap::class, self::JSON_FORMAT);

$this->assertCount(1, $map);
$this->assertInstanceOf(DummyEntityInterface::class, $map->get('key'));
$this->assertDenormalizer_denormalize_wasCalledOnceWithDataAndType($data['key'], DummyEntityInterface::class);
$this->assertInstanceOf(DummyEntityInterface::class, $map->get(self::KEY));
$this->assertDenormalizer_denormalize_wasCalledOnceWithDataAndType($data[self::KEY], DummyEntityInterface::class);
}

/** @test */
Expand Down Expand Up @@ -171,19 +178,31 @@ public function normalize_emptyMap_arrayObjectReturned(): void
$map = $normalizer->normalize(new DummyPrimitiveMap());

$this->assertInstanceOf(\ArrayObject::class, $map);
$this->assertNormalizer_normalize_wasNeverCalled();
}

/** @test */
public function normalize_map_arrayReturned(): void
{
$normalizer = $this->createMapNormalizer();
$expectedArray = ['key' => 'value'];
$expectedArray = [self::KEY => self::VALUE];
$map = new DummyPrimitiveMap($expectedArray);
$this->givenNormalizer_normalize_returnData(self::VALUE);

$normalizedMap = $normalizer->normalize($map, self::JSON_FORMAT);

$this->assertIsArray($normalizedMap);
$this->assertSame($expectedArray, $normalizedMap);
$this->assertNormalizer_normalize_wasCalledOnceWithObject(self::VALUE);
}

$map = $normalizer->normalize($map, self::JSON_FORMAT);
private function createMapNormalizer(): TypedMapNormalizer
{
$normalizer = new TypedMapNormalizer();
$normalizer->setDenormalizer($this->denormalizer);
$normalizer->setNormalizer($this->normalizer);

$this->assertIsArray($map);
$this->assertSame($expectedArray, $map);
return $normalizer;
}

private function givenDenormalizer_denormalize_returnItem($item): void
Expand All @@ -199,17 +218,28 @@ private function assertDenormalizer_denormalize_wasCalledOnceWithDataAndType(arr
->denormalize($data, $type, self::JSON_FORMAT, []);
}

private function createMapNormalizer(): TypedMapNormalizer
private function assertDenormalizer_denormalize_wasNeverCalled(): void
{
$normalizer = new TypedMapNormalizer();
$normalizer->setDenormalizer($this->denormalizer);
\Phake::verify($this->denormalizer, \Phake::never())
->denormalize(\Phake::anyParameters());
}

return $normalizer;
private function givenNormalizer_normalize_returnData($data): void
{
\Phake::when($this->normalizer)
->normalize(\Phake::anyParameters())
->thenReturn($data);
}

private function assertDenormalizer_denormalize_wasNeverCalled(): void
private function assertNormalizer_normalize_wasCalledOnceWithObject($object): void
{
\Phake::verify($this->denormalizer, \Phake::never())
->denormalize(\Phake::anyParameters());
\Phake::verify($this->normalizer)
->normalize($object, self::JSON_FORMAT, []);
}

private function assertNormalizer_normalize_wasNeverCalled(): void
{
\Phake::verify($this->normalizer, \Phake::never())
->normalize(\Phake::anyParameters());
}
}

0 comments on commit 61dbaca

Please sign in to comment.