Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ee61a55

Browse files
committed
test: discriminator withou matching property
1 parent 380e709 commit ee61a55

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
/*
15+
* This file is part of the Symfony package.
16+
*
17+
* (c) Fabien Potencier <[email protected]>
18+
*
19+
* For the full copyright and license information, please view the LICENSE
20+
* file that was distributed with this source code.
21+
*/
22+
23+
namespace Symfony\Component\Serializer\Tests\Fixtures;
24+
25+
use Symfony\Component\Serializer\Annotation\DiscriminatorMap;
26+
27+
#[DiscriminatorMap(typeProperty: 'discr', mapping: ['concrete' => ConcreteWithDiscriminator::class])]
28+
abstract class AbstractWithDiscriminator
29+
{
30+
/**
31+
* @var int The id
32+
*/
33+
public ?int $id = null;
34+
35+
/**
36+
* @var string The dummy name
37+
*/
38+
public string $name;
39+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Fixtures;
13+
14+
class ConcreteWithDiscriminator extends AbstractWithDiscriminator
15+
{
16+
/**
17+
* @var string a concrete thing
18+
*/
19+
public string $instance;
20+
}

src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@
4141
use Symfony\Component\Serializer\Serializer;
4242
use Symfony\Component\Serializer\SerializerAwareInterface;
4343
use Symfony\Component\Serializer\SerializerInterface;
44+
use Symfony\Component\Serializer\Tests\Fixtures\AbstractWithDiscriminator;
4445
use Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummy;
4546
use Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummyFirstChild;
4647
use Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummySecondChild;
48+
use Symfony\Component\Serializer\Tests\Fixtures\ConcreteWithDiscriminator;
4749
use Symfony\Component\Serializer\Tests\Fixtures\DummySecondChildQuux;
4850
use Symfony\Component\Serializer\Tests\Normalizer\Features\ObjectDummyWithContextAttribute;
4951

@@ -302,6 +304,39 @@ public function testNormalizeWithNestedAttributesInConstructor()
302304
], $test);
303305
}
304306

307+
public function testDenormalizeWithDiscriminatorMapNotMappedToAProperty()
308+
{
309+
$factory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
310+
311+
$loaderMock = new class() implements ClassMetadataFactoryInterface {
312+
public function getMetadataFor($value): ClassMetadataInterface
313+
{
314+
if (ConcreteWithDiscriminator::class === $value) {
315+
return new ClassMetadata(
316+
AbstractWithDiscriminator::class,
317+
new ClassDiscriminatorMapping('discr', [
318+
'first' => ConcreteWithDiscriminator::class,
319+
])
320+
);
321+
}
322+
323+
throw new InvalidArgumentException();
324+
}
325+
326+
public function hasMetadataFor($value): bool
327+
{
328+
return ConcreteWithDiscriminator::class === $value;
329+
}
330+
};
331+
332+
$discriminatorResolver = new ClassDiscriminatorFromClassMetadata($loaderMock);
333+
$normalizer = new AbstractObjectNormalizerDummy($factory, null, new PhpDocExtractor(), $discriminatorResolver);
334+
$serializer = new Serializer([$normalizer]);
335+
$normalizer->setSerializer($serializer);
336+
337+
$this->assertInstanceOf(AbstractWithDiscriminator::class, $normalizer->denormalize(['id' => 0, 'instance' => 'test', 'name' => 'test'], ConcreteWithDiscriminator::class, 'any'));
338+
}
339+
305340
public function testNormalizeWithNestedAttributesInConstructorAndDiscriminatorMap()
306341
{
307342
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));

0 commit comments

Comments
 (0)