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