mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-02 13:03:50 +00:00
Fix serializing collection with intersection types
This commit is contained in:
@@ -13,6 +13,10 @@ namespace Chill\DocGeneratorBundle\tests\Serializer\Normalizer;
|
||||
|
||||
use Chill\MainBundle\Entity\Scope;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\Common\Collections\ReadableCollection;
|
||||
use Doctrine\Common\Collections\Selectable;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Symfony\Component\Serializer\Annotation as Serializer;
|
||||
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
||||
@@ -28,7 +32,6 @@ final class DocGenObjectNormalizerTest extends KernelTestCase
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
self::bootKernel();
|
||||
|
||||
$this->normalizer = self::$container->get(NormalizerInterface::class);
|
||||
@@ -171,6 +174,64 @@ final class DocGenObjectNormalizerTest extends KernelTestCase
|
||||
|
||||
$this->assertEquals($expected, $normalized, 'test normalization fo an user with null center');
|
||||
}
|
||||
|
||||
public function testIntersectionTypeForReadableCollection(): void
|
||||
{
|
||||
$value = new TestableWithIntersectionReadableCollection();
|
||||
|
||||
$normalized = $this->normalizer->normalize($value, 'docgen', [AbstractNormalizer::GROUPS => ['docgen:read'], 'docgen:expects' => TestableWithIntersectionReadableCollection::class]);
|
||||
|
||||
self::assertIsArray($normalized);
|
||||
self::assertIsArray($normalized['collection']);
|
||||
self::assertCount(2, $normalized['collection']);
|
||||
|
||||
self::assertEquals(
|
||||
['baz' => 'bloup', 'isNull' => false],
|
||||
$normalized['collection'][0]
|
||||
);
|
||||
self::assertEquals(
|
||||
['baz' => 'bloup', 'isNull' => false],
|
||||
$normalized['collection'][1]
|
||||
);
|
||||
}
|
||||
|
||||
public function testIntersectionTypeForReadableCollectionWithNullValue(): void
|
||||
{
|
||||
$normalized = $this->normalizer->normalize(null, 'docgen', [AbstractNormalizer::GROUPS => ['docgen:read'], 'docgen:expects' => TestableWithIntersectionReadableCollection::class]);
|
||||
|
||||
self::assertIsArray($normalized);
|
||||
self::assertIsArray($normalized['collection']);
|
||||
self::assertCount(0, $normalized['collection']);
|
||||
}
|
||||
|
||||
public function testIntersectionTypeForCollection(): void
|
||||
{
|
||||
$value = new TestableWithCollection();
|
||||
|
||||
$normalized = $this->normalizer->normalize($value, 'docgen', [AbstractNormalizer::GROUPS => ['docgen:read'], 'docgen:expects' => TestableWithCollection::class]);
|
||||
|
||||
self::assertIsArray($normalized);
|
||||
self::assertIsArray($normalized['collection']);
|
||||
self::assertCount(2, $normalized['collection']);
|
||||
|
||||
self::assertEquals(
|
||||
['baz' => 'bloup', 'isNull' => false],
|
||||
$normalized['collection'][0]
|
||||
);
|
||||
self::assertEquals(
|
||||
['baz' => 'bloup', 'isNull' => false],
|
||||
$normalized['collection'][1]
|
||||
);
|
||||
}
|
||||
|
||||
public function testIntersectionTypeForCollectionWithNullValue(): void
|
||||
{
|
||||
$normalized = $this->normalizer->normalize(null, 'docgen', [AbstractNormalizer::GROUPS => ['docgen:read'], 'docgen:expects' => TestableWithCollection::class]);
|
||||
|
||||
self::assertIsArray($normalized);
|
||||
self::assertIsArray($normalized['collection']);
|
||||
self::assertCount(0, $normalized['collection']);
|
||||
}
|
||||
}
|
||||
|
||||
class TestableParentClass
|
||||
@@ -215,3 +276,29 @@ class TestableClassWithBool
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
class TestableWithIntersectionReadableCollection
|
||||
{
|
||||
/**
|
||||
* @Serializer\Groups("docgen:read")
|
||||
*/
|
||||
public ReadableCollection&Selectable $collection;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->collection = new ArrayCollection([new TestableChildClass(), new TestableChildClass()]);
|
||||
}
|
||||
}
|
||||
|
||||
class TestableWithCollection
|
||||
{
|
||||
/**
|
||||
* @Serializer\Groups("docgen:read")
|
||||
*/
|
||||
public Collection&Selectable $collection;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->collection = new ArrayCollection([new TestableChildClass(), new TestableChildClass()]);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user