Skip to content

Commit

Permalink
Merge pull request #223 from symfony-cmf/4-to-5
Browse files Browse the repository at this point in the history
4 to 5
  • Loading branch information
dbu authored Apr 6, 2024
2 parents c85c4cd + d447691 commit 3ea0140
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,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
39 changes: 25 additions & 14 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 @@ -54,30 +55,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): void
/**
* @param class-string|FixtureInterface $class
*/
public function loadFixtureClass(Loader $loader, $class): void
{
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 Expand Up @@ -110,7 +121,7 @@ public function createTestNode(): void
$session->save();
}

private function getExecutor($initialize = false): PHPCRExecutor
private function getExecutor(bool $initialize = false): PHPCRExecutor
{
static $lastInitialize = null;

Expand Down

0 comments on commit 3ea0140

Please sign in to comment.