Repository: symfony/symfony
File: src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php (PHP)

Code before:
                    return new ClassMetadata(
                        AbstractDummy::class,
                        new ClassDiscriminatorMapping('type', [
                            'first' => AbstractDummyFirstChild::class,
                            'second' => AbstractDummySecondChild::class,
                        ])
                    );
                }

                throw new InvalidArgumentException();
            }

            public function hasMetadataFor($value): bool
            {
                return AbstractDummy::class === $value;
            }
        };

        $discriminatorResolver = new ClassDiscriminatorFromClassMetadata($loaderMock);
        $normalizer = new AbstractObjectNormalizerDummy($factory, null, new PhpDocExtractor(), $discriminatorResolver);
        $serializer = new Serializer([$normalizer]);
        $normalizer->setSerializer($serializer);
        $normalizedData = $normalizer->denormalize(['foo' => 'foo', 'baz' => 'baz', 'quux' => ['value' => 'quux'], 'type' => $typeValue], AbstractDummy::class);

        if ($shouldFail) {
            $this->fail('Expected exception not thrown.');
        }

        $this->assertInstanceOf(DummyFirstChildQuux::class, $normalizedData->quux);
    }

    public function provideInvalidDiscriminatorTypes()
    {
        $toStringObject = new class() {
            public function __toString()
            {
                return 'first';
            }
        };

        return [
            [[], true],
            [new StdClass(), true],
            [123, true],
            [false, true],
            ['first', false],
            [$toStringObject, false],
        ];
    }

    public function testDenormalizeWithDiscriminatorMapUsesCorrectClassname()

Review comment (suggestion):
This is not needed because of the `exceptException above`

Code after:
                        AbstractDummy::class,
                        new ClassDiscriminatorMapping('type', [
                            'first' => AbstractDummyFirstChild::class,
                            'second' => AbstractDummySecondChild::class,
                        ])
                    );
                }

                throw new InvalidArgumentException();
            }

            public function hasMetadataFor($value): bool
            {
                return AbstractDummy::class === $value;
            }
        };

        $discriminatorResolver = new ClassDiscriminatorFromClassMetadata($loaderMock);
        $normalizer = new AbstractObjectNormalizerDummy($factory, null, new ReflectionExtractor(), $discriminatorResolver);
        $serializer = new Serializer([$normalizer]);
        $normalizer->setSerializer($serializer);
        $normalizedData = $normalizer->denormalize(['foo' => 'foo', 'baz' => 'baz', 'quux' => ['value' => 'quux'], 'type' => $typeValue], AbstractDummy::class);

        $this->assertInstanceOf(DummyFirstChildQuux::class, $normalizedData->quux);
    }

    /**
     * @return iterable<array{0: mixed, 1: bool}>
     */
    public static function provideInvalidDiscriminatorTypes(): array
    {
        $toStringObject = new class {
            public function __toString()
            {
                return 'first';
            }
        };

        return [
            [[], true],
            [new \stdClass(), true],
            [123, true],
            [false, true],
            ['first', false],
            [$toStringObject, false],
        ];
    }

    public function testDenormalizeWithDiscriminatorMapUsesCorrectClassname()
    {
        $factory = new ClassMetadataFactory(new AttributeLoader());