Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DoctrineObjectConstructor Using array_key_exists() on objects is deprecated in php7.4 #1253

Merged
merged 1 commit into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Construction/DoctrineObjectConstructor.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ public function construct(DeserializationVisitorInterface $visitor, ClassMetadat
return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type, $context);
}

if (!array_key_exists($propertyMetadata->serializedName, $data)) {
if (is_array($data) && !array_key_exists($propertyMetadata->serializedName, $data)) {
return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type, $context);
} elseif (is_object($data) && !property_exists($data, $propertyMetadata->serializedName)) {
return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type, $context);
}

Expand Down
56 changes: 56 additions & 0 deletions tests/Serializer/Doctrine/ObjectConstructorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
use Doctrine\ORM\UnitOfWork;
use Doctrine\ORM\Version as ORMVersion;
use Doctrine\Persistence\AbstractManagerRegistry;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\Mapping\ClassMetadata as DoctrineClassMetadata;
use Doctrine\Persistence\Mapping\ClassMetadataFactory;
use Doctrine\Persistence\ObjectManager;
use Doctrine\Persistence\Proxy;
use JMS\Serializer\Builder\CallbackDriverFactory;
use JMS\Serializer\Builder\DefaultDriverFactory;
Expand All @@ -30,6 +34,7 @@
use JMS\Serializer\GraphNavigatorInterface;
use JMS\Serializer\Metadata\ClassMetadata;
use JMS\Serializer\Metadata\Driver\DoctrineTypeDriver;
use JMS\Serializer\Metadata\PropertyMetadata;
use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy;
use JMS\Serializer\Serializer;
use JMS\Serializer\SerializerBuilder;
Expand Down Expand Up @@ -328,6 +333,57 @@ static function ($id) use ($connection, $entityManager) {
$constructor->construct($this->visitor, $class, $data, $type, $this->context);
}

/**
* php7.4 Using array_key_exists() on objects is deprecated.
*/
public function testArrayKeyExistsOnObject(): void
{
$metadataFactory = $this->createMock(ClassMetadataFactory::class);

$ormClassMetadata = $this->createMock(DoctrineClassMetadata::class);
$ormClassMetadata
->expects(self::atLeastOnce())
->method('getIdentifierFieldNames')
->willReturn(['id']);

$om = $this->createMock(ObjectManager::class);
$om
->expects(self::atLeastOnce())
->method('getMetadataFactory')
->willReturn($metadataFactory);
$om
->expects(self::atLeastOnce())
->method('getClassMetadata')
->willReturnMap([
[BlogPostSeo::class, $ormClassMetadata],
]);

$registry = $this->createMock(ManagerRegistry::class);
$registry
->expects(self::atLeastOnce())
->method('getManagerForClass')
->willReturnMap([
[BlogPostSeo::class, $om],
]);

$fallback = $this->createMock(ObjectConstructorInterface::class);
$fallback
->expects(self::once())
->method('construct');

$type = ['name' => BlogPostSeo::class, 'params' => []];

$pm = $this->createMock(PropertyMetadata::class);
$pm->serializedName = 'id';

$classMetadata = new ClassMetadata(BlogPostSeo::class);
$classMetadata->propertyMetadata['id'] = $pm;

$data = new \SimpleXMLElement('<metaTitle>test</metaTitle>');
$constructor = new DoctrineObjectConstructor($registry, $fallback, DoctrineObjectConstructor::ON_MISSING_FALLBACK);
$constructor->construct($this->visitor, $classMetadata, $data, $type, $this->context);
}

protected function setUp(): void
{
$this->visitor = $this->getMockBuilder(DeserializationVisitorInterface::class)->getMock();
Expand Down