From 0adca4c528f6382aabc06a2ac1af574f9fa02811 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Wed, 22 May 2024 21:30:50 +0200 Subject: [PATCH] Implement isUninitializedObject() in ObjectManagerDecorator That method will be part of the ObjectManager interface in 4.0.x. --- composer.json | 2 +- phpstan-baseline.neon | 5 +++ src/Persistence/ObjectManagerDecorator.php | 28 ++++++++++++++++ .../ObjectManagerDecoratorTest.php | 32 +++++++++++++++++++ 4 files changed, 66 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 03d832fe..49ebaf57 100644 --- a/composer.json +++ b/composer.json @@ -30,7 +30,7 @@ "phpstan/phpstan-strict-rules": "^1.1", "doctrine/coding-standard": "^12", "doctrine/common": "^3.0", - "phpunit/phpunit": "^8.5 || ^9.5", + "phpunit/phpunit": "^8.5.38 || ^9.5", "symfony/cache": "^4.4 || ^5.4 || ^6.0", "vimeo/psalm": "4.30.0 || 5.24.0" }, diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index fed7f899..2978128f 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -5,6 +5,11 @@ parameters: count: 1 path: src/Persistence/Mapping/Driver/StaticPHPDriver.php + - + message: "#^Call to function method_exists\\(\\) with TObjectManager of Doctrine\\\\Persistence\\\\ObjectManager and 'isUninitializedObje…' will always evaluate to true\\.$#" + count: 1 + path: src/Persistence/ObjectManagerDecorator.php + - message: "#^Doctrine\\\\Persistence\\\\Reflection\\\\EnumReflectionProperty\\:\\:__construct\\(\\) does not call parent constructor from ReflectionProperty\\.$#" count: 1 diff --git a/src/Persistence/ObjectManagerDecorator.php b/src/Persistence/ObjectManagerDecorator.php index 8c038b8e..5fbe8de9 100644 --- a/src/Persistence/ObjectManagerDecorator.php +++ b/src/Persistence/ObjectManagerDecorator.php @@ -4,9 +4,14 @@ namespace Doctrine\Persistence; +use BadMethodCallException; use Doctrine\Persistence\Mapping\ClassMetadata; use Doctrine\Persistence\Mapping\ClassMetadataFactory; +use function get_class; +use function method_exists; +use function sprintf; + /** * Base class to simplify ObjectManager decorators * @@ -82,6 +87,29 @@ public function initializeObject(object $obj) $this->wrapped->initializeObject($obj); } + /** @param mixed $value */ + public function isUninitializedObject($value): bool + { + if (! method_exists($this->wrapped, 'isUninitializedObject')) { + $wrappedClass = get_class($this->wrapped); + + throw new BadMethodCallException(sprintf( + <<<'EXCEPTION' +Context: Trying to call %s +Problem: The wrapped ObjectManager, an instance of %s does not implement this method. +Solution: Implement %s::isUninitializedObject() with a signature compatible with this one: + public function isUninitializedObject(mixed $value): bool +EXCEPTION + , + __METHOD__, + $wrappedClass, + $wrappedClass + )); + } + + return $this->wrapped->isUninitializedObject($value); + } + /** * {@inheritDoc} */ diff --git a/tests/Persistence/ObjectManagerDecoratorTest.php b/tests/Persistence/ObjectManagerDecoratorTest.php index b2ccb1d2..6c27ea82 100644 --- a/tests/Persistence/ObjectManagerDecoratorTest.php +++ b/tests/Persistence/ObjectManagerDecoratorTest.php @@ -4,6 +4,7 @@ namespace Doctrine\Tests\Persistence; +use BadMethodCallException; use Doctrine\Persistence\Mapping\ClassMetadata; use Doctrine\Persistence\Mapping\ClassMetadataFactory; use Doctrine\Persistence\ObjectManager; @@ -155,6 +156,37 @@ public function testContains(): void self::assertTrue($this->decorated->contains($object)); } + + /** @requires PHP 8.0 */ + public function testIsUninitializedObject(): void + { + $object = new TestObject(); + + $wrapped = $this->createMock(ObjectManagerV4::class); + $decorated = new NullObjectManagerDecorator($wrapped); + $wrapped->expects(self::once()) + ->method('isUninitializedObject') + ->with($object) + ->willReturn(false); + + self::assertFalse($decorated->isUninitializedObject($object)); + } + + /** @requires PHP 8.0 */ + public function testIsThrowsWhenTheWrappedObjectManagerDoesNotImplementObjectManagerV4(): void + { + $object = new TestObject(); + + $this->expectException(BadMethodCallException::class); + $decorated = new NullObjectManagerDecorator($this->createMock(ObjectManager::class)); + + self::assertFalse($decorated->isUninitializedObject($object)); + } +} + +interface ObjectManagerV4 extends ObjectManager +{ + public function isUninitializedObject(mixed $object): bool; } /** @extends ObjectManagerDecorator */