Skip to content

Commit

Permalink
Merge pull request #222 from symfony-cmf/container-aware-fixtures
Browse files Browse the repository at this point in the history
fix handling container aware fixtures with symfony 7
  • Loading branch information
dbu authored Apr 6, 2024
2 parents 05a1992 + 52885f6 commit e623e12
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----

Expand Down
37 changes: 24 additions & 13 deletions src/Functional/DbManager/PHPCR.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<class-string|object> $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);
Expand Down

0 comments on commit e623e12

Please sign in to comment.