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

Code before:

        $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()
    {
        $factory = new ClassMetadataFactory(new AttributeLoader());

        $loaderMock = new class implements ClassMetadataFactoryInterface {
            public function getMetadataFor($value): ClassMetadataInterface
            {
                if (AbstractDummy::class === $value) {
                    return new ClassMetadata(
                        AbstractDummy::class,
                        new ClassDiscriminatorMapping('type', [
                            'first' => AbstractDummyFirstChild::class,
                            'second' => AbstractDummySecondChild::class,
                        ])
                    );
                }

                throw new InvalidArgumentException();

Review comment (suggestion):
```suggestion
            [new \stdClass(), true],
```


Code after:
        $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());

        $loaderMock = new class implements ClassMetadataFactoryInterface {
            public function getMetadataFor($value): ClassMetadataInterface
            {
                if (AbstractDummy::class === $value) {
                    return new ClassMetadata(
                        AbstractDummy::class,
                        new ClassDiscriminatorMapping('type', [
                            'first' => AbstractDummyFirstChild::class,
                            'second' => AbstractDummySecondChild::class,
                        ])
                    );
                }

                throw new InvalidArgumentException();
            }