From 6c2017282bceb74ad4feb604646c6b56dc19b1d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Tue, 21 Jan 2025 22:36:59 +0100 Subject: [PATCH] Add support for doctrine/persistence 4 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. --- composer.json | 2 +- .../ArgumentResolver/EntityValueResolverTest.php | 10 +++++++--- .../Constraints/UniqueEntityValidatorTest.php | 14 ++++++++++---- .../Constraints/UniqueEntityValidator.php | 8 ++++---- src/Symfony/Bridge/Doctrine/composer.json | 2 +- 5 files changed, 23 insertions(+), 13 deletions(-) diff --git a/composer.json b/composer.json index d4e6370e216e9..53f24f502f0d4 100644 --- a/composer.json +++ b/composer.json @@ -39,7 +39,7 @@ "ext-xml": "*", "friendsofphp/proxy-manager-lts": "^1.0.2", "doctrine/event-manager": "^1.2|^2", - "doctrine/persistence": "^2.5|^3.1", + "doctrine/persistence": "^2.5|^3.1|^4", "twig/twig": "^2.13|^3.0.4", "psr/cache": "^2.0|^3.0", "psr/clock": "^1.0", diff --git a/src/Symfony/Bridge/Doctrine/Tests/ArgumentResolver/EntityValueResolverTest.php b/src/Symfony/Bridge/Doctrine/Tests/ArgumentResolver/EntityValueResolverTest.php index 471ab56aef337..022af885002ee 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/ArgumentResolver/EntityValueResolverTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/ArgumentResolver/EntityValueResolverTest.php @@ -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; } diff --git a/src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueEntityValidatorTest.php b/src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueEntityValidatorTest.php index 82c122c4ef486..efb28dbdff66c 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueEntityValidatorTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueEntityValidatorTest.php @@ -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; } diff --git a/src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php b/src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php index 821815d055906..b356f8068c15d 100644 --- a/src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php +++ b/src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php @@ -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); diff --git a/src/Symfony/Bridge/Doctrine/composer.json b/src/Symfony/Bridge/Doctrine/composer.json index 3379073eb9192..17828cabe6d66 100644 --- a/src/Symfony/Bridge/Doctrine/composer.json +++ b/src/Symfony/Bridge/Doctrine/composer.json @@ -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",