Skip to content

Commit e623e12

Browse files
authored
Merge pull request #222 from symfony-cmf/container-aware-fixtures
fix handling container aware fixtures with symfony 7
2 parents 05a1992 + 52885f6 commit e623e12

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ Changelog
44
4.x
55
===
66

7+
4.5.1
8+
-----
9+
10+
* Use regular fixture loader with Symfony 7 rather than the dropped `ContainerAwareLoader`.
11+
For fixtures with services, instantiate the fixture and pass the instance to `PHPCR::loadFixture`
12+
instead of passing the class string.
13+
714
4.5.0
815
-----
916

src/Functional/DbManager/PHPCR.php

+24-13
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Doctrine\Bundle\PHPCRBundle\DataFixtures\PHPCRExecutor;
1515
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
16+
use Doctrine\Common\DataFixtures\FixtureInterface;
1617
use Doctrine\Common\DataFixtures\Loader;
1718
use Doctrine\Common\DataFixtures\ProxyReferenceRepository;
1819
use Doctrine\Common\DataFixtures\Purger\PHPCRPurger;
@@ -64,30 +65,40 @@ public function purgeRepository(bool $initialize = false): void
6465
}
6566

6667
/**
67-
* @param string[] $classNames Fixture classes to load
68-
* @param bool $initialize if the ODM repository initializers should be executed
68+
* @param array<class-string|object> $classes Fixture classes or class names to load
69+
* @param bool $initialize Whether the ODM repository initializers should be executed
6970
*/
70-
public function loadFixtures(array $classNames, bool $initialize = false): void
71+
public function loadFixtures(array $classes, bool $initialize = false): void
7172
{
72-
$loader = new ContainerAwareLoader($this->container);
73+
$loader = class_exists(ContainerAwareLoader::class)
74+
? new ContainerAwareLoader($this->container)
75+
: new Loader()
76+
;
7377

74-
foreach ($classNames as $className) {
78+
foreach ($classes as $className) {
7579
$this->loadFixtureClass($loader, $className);
7680
}
7781

7882
$this->getExecutor($initialize)->execute($loader->getFixtures(), false);
7983
}
8084

81-
public function loadFixtureClass(Loader $loader, string $className)
85+
/**
86+
* @param class-string|FixtureInterface $class
87+
*/
88+
public function loadFixtureClass(Loader $loader, $class)
8289
{
83-
if (!class_exists($className)) {
84-
throw new \InvalidArgumentException(sprintf(
85-
'Fixture class "%s" does not exist.',
86-
$className
87-
));
88-
}
90+
if (\is_object($class)) {
91+
$fixture = $class;
92+
} else {
93+
if (!class_exists($class)) {
94+
throw new \InvalidArgumentException(sprintf(
95+
'Fixture class "%s" does not exist.',
96+
$class
97+
));
98+
}
8999

90-
$fixture = new $className();
100+
$fixture = new $class();
101+
}
91102

92103
if ($loader->hasFixture($fixture)) {
93104
unset($fixture);

0 commit comments

Comments
 (0)