From 52885f6f330e18d79522af9ef189da6ae5cd4cf6 Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Sat, 6 Apr 2024 11:20:47 +0200 Subject: [PATCH] fix handling container aware fixtures with symfony 7 --- CHANGELOG.md | 7 ++++++ src/Functional/DbManager/PHPCR.php | 37 +++++++++++++++++++----------- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a33c22c..ef12e28 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,13 @@ Changelog 4.x === +4.5.1 +----- + +* Use regular fixture loader with Symfony 7 rather than the dropped `ContainerAwareLoader`. + For fixtures with services, instantiate the fixture and pass the instance to `PHPCR::loadFixture` + instead of passing the class string. + 4.5.0 ----- diff --git a/src/Functional/DbManager/PHPCR.php b/src/Functional/DbManager/PHPCR.php index 5e3c4c6..0ae6622 100644 --- a/src/Functional/DbManager/PHPCR.php +++ b/src/Functional/DbManager/PHPCR.php @@ -13,6 +13,7 @@ use Doctrine\Bundle\PHPCRBundle\DataFixtures\PHPCRExecutor; use Doctrine\Common\DataFixtures\DependentFixtureInterface; +use Doctrine\Common\DataFixtures\FixtureInterface; use Doctrine\Common\DataFixtures\Loader; use Doctrine\Common\DataFixtures\ProxyReferenceRepository; use Doctrine\Common\DataFixtures\Purger\PHPCRPurger; @@ -64,30 +65,40 @@ public function purgeRepository(bool $initialize = false): void } /** - * @param string[] $classNames Fixture classes to load - * @param bool $initialize if the ODM repository initializers should be executed + * @param array $classes Fixture classes or class names to load + * @param bool $initialize Whether the ODM repository initializers should be executed */ - public function loadFixtures(array $classNames, bool $initialize = false): void + public function loadFixtures(array $classes, bool $initialize = false): void { - $loader = new ContainerAwareLoader($this->container); + $loader = class_exists(ContainerAwareLoader::class) + ? new ContainerAwareLoader($this->container) + : new Loader() + ; - foreach ($classNames as $className) { + foreach ($classes as $className) { $this->loadFixtureClass($loader, $className); } $this->getExecutor($initialize)->execute($loader->getFixtures(), false); } - public function loadFixtureClass(Loader $loader, string $className) + /** + * @param class-string|FixtureInterface $class + */ + public function loadFixtureClass(Loader $loader, $class) { - if (!class_exists($className)) { - throw new \InvalidArgumentException(sprintf( - 'Fixture class "%s" does not exist.', - $className - )); - } + if (\is_object($class)) { + $fixture = $class; + } else { + if (!class_exists($class)) { + throw new \InvalidArgumentException(sprintf( + 'Fixture class "%s" does not exist.', + $class + )); + } - $fixture = new $className(); + $fixture = new $class(); + } if ($loader->hasFixture($fixture)) { unset($fixture);