Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/3.4.x' into 4.0.x
Browse files Browse the repository at this point in the history
  • Loading branch information
greg0ire committed May 24, 2024
2 parents add5021 + e66e37b commit cbfd23a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions src/Persistence/ObjectManagerDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -76,6 +81,28 @@ public function initializeObject(object $obj): void
$this->wrapped->initializeObject($obj);
}

public function isUninitializedObject(mixed $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);
}

public function contains(object $object): bool
{
return $this->wrapped->contains($object);
Expand Down
30 changes: 30 additions & 0 deletions tests/Persistence/ObjectManagerDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\Tests\Persistence;

use BadMethodCallException;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Persistence\Mapping\ClassMetadataFactory;
use Doctrine\Persistence\ObjectManager;
Expand Down Expand Up @@ -153,6 +154,35 @@ public function testContains(): void

self::assertTrue($this->decorated->contains($object));
}

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));
}

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<ObjectManager&MockObject> */
Expand Down

0 comments on commit cbfd23a

Please sign in to comment.