doctrine / doctrine-laminas-hydrator

Doctrine hydrators for Laminas applications
https://www.doctrine-project.org/projects/doctrine-laminas-hydrator.html
MIT License
33 stars 19 forks source link

Fixed mock in unit tests to return appropriate reflection classes #45

Closed driehle closed 2 years ago

driehle commented 2 years ago

In this test the reflection classes have not been mocked correctly. Though the current unit tests to not have any issues with it, the mocks should use the correct reflection classes, as otherwise updating variables via reflection will not work. See #44.

When adding the following debug code, one can see what is actually going wrong:

    protected function hydrateByReference(array $data, $object)
    {
        $tryObject = $this->tryConvertArrayToObject($data, $object);
        $metadata  = $this->metadata;
        $refl      = $metadata->getReflectionClass();

        if (is_object($tryObject)) {
            $object = $tryObject;
        }

        // ADDITIONAL DEBUG CHECK START
        if (get_class($object) !== $refl->getName()) { 
            echo "\nObject:     " . get_class($object) . "\n";
            echo "Reflection: " . $refl->getName() . "\n";
        }
        // ADDITIONAL DEBUG CHECK END

        foreach ($data as $field => $value) {
            // [...]
        }

        return $object;
    }

Obviously, get_class($object) and $refl->getName() should always be identitcal, but in testHydrateOneToManyAssociationByReferenceUsingIdentifiersArrayForRelations they aren't:

Object:     DoctrineTest\Laminas\Hydrator\Assets\ByValueDifferentiatorEntity
Reflection: DoctrineTest\Laminas\Hydrator\Assets\OneToManyEntity

This PR fixes the test case to ensure that ClassMetadata is mocked correctly. Actually, in other test cases this is already implemented correctly, see for example in testHydrateOneToManyAssociationByReferenceWithArrayCausingDataModifications:

https://github.com/doctrine/doctrine-laminas-hydrator/blob/94e6cadcaa04166777a52eac577827f318a12ffa/tests/DoctrineObjectTest.php#L2242-L2281

This is simply transferred to testHydrateOneToManyAssociationByReferenceUsingIdentifiersArrayForRelations.