Skip to content

Commit

Permalink
Add support for doctrine/persistence 4
Browse files Browse the repository at this point in the history
v4 provides a guarantee that ManagerRegistry::getManager() returns an entity
manager (as opposed to null).

The tests need to be adjusted to reflect the behavior of the mocked dependency
more accurately, as it is throwing an exception in case of a null manager for
all three supported versions of the library.
  • Loading branch information
greg0ire authored and fabpot committed Jan 25, 2025
1 parent c922465 commit fd0094d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
10 changes: 7 additions & 3 deletions Tests/ArgumentResolver/EntityValueResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,13 @@ private function createRegistry(?ObjectManager $manager = null): ManagerRegistry
->method('getManagerForClass')
->willReturn($manager);

$registry->expects($this->any())
->method('getManager')
->willReturn($manager);
if (null === $manager) {
$registry->method('getManager')
->willThrowException(new \InvalidArgumentException());
} else {
$registry->method('getManager')->willReturn($manager);
}


return $registry;
}
Expand Down
14 changes: 10 additions & 4 deletions Tests/Validator/Constraints/UniqueEntityValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,16 @@ protected function setUp(): void
protected function createRegistryMock($em = null)
{
$registry = $this->createMock(ManagerRegistry::class);
$registry->expects($this->any())
->method('getManager')
->with($this->equalTo(self::EM_NAME))
->willReturn($em);

if (null === $em) {
$registry->method('getManager')
->with($this->equalTo(self::EM_NAME))
->willThrowException(new \InvalidArgumentException());
} else {
$registry->method('getManager')
->with($this->equalTo(self::EM_NAME))
->willReturn($em);
}

return $registry;
}
Expand Down
8 changes: 4 additions & 4 deletions Validator/Constraints/UniqueEntityValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ public function validate(mixed $entity, Constraint $constraint)
}

if ($constraint->em) {
$em = $this->registry->getManager($constraint->em);

if (!$em) {
throw new ConstraintDefinitionException(sprintf('Object manager "%s" does not exist.', $constraint->em));
try {
$em = $this->registry->getManager($constraint->em);
} catch (\InvalidArgumentException $e) {
throw new ConstraintDefinitionException(sprintf('Object manager "%s" does not exist.', $constraint->em), 0, $e);
}
} else {
$em = $this->registry->getManagerForClass($entity::class);
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"require": {
"php": ">=8.1",
"doctrine/event-manager": "^1.2|^2",
"doctrine/persistence": "^2.5|^3.1",
"doctrine/persistence": "^2.5|^3.1|^4",
"symfony/deprecation-contracts": "^2.5|^3",
"symfony/polyfill-ctype": "~1.8",
"symfony/polyfill-mbstring": "~1.0",
Expand Down

0 comments on commit fd0094d

Please sign in to comment.