Skip to content

Commit 12412e2

Browse files
committed
Merge pull request #53 from symfony-cmf/finish-orm-manager
Added Fixture loading to ORM Manager
2 parents 85d4cb6 + fb388f0 commit 12412e2

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Changelog
44
1.2.0-RC1
55
---------
66

7+
* **2014-07-27**: Added DataFixture support to the ORM DbManager
78
* **2014-06-16**: Initializer for phpcr fixture loading
89
* **2014-06-06**: Updated to PSR-4 autoloading
910

src/Functional/DbManager/ORM.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,14 @@
1212

1313
namespace Symfony\Cmf\Component\Testing\Functional\DbManager;
1414

15+
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
16+
use Doctrine\Common\DataFixtures\Loader;
17+
use Doctrine\Common\DataFixtures\ProxyReferenceRepository;
18+
use Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader;
1519
use Symfony\Component\DependencyInjection\ContainerInterface;
1620

21+
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
22+
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
1723
use Doctrine\Common\Persistence\ManagerRegistry;
1824
use Doctrine\Common\Persistence\ObjectManager;
1925

@@ -70,4 +76,58 @@ public function getOm($managerName = null)
7076

7177
return $this->om;
7278
}
79+
80+
/**
81+
* Loads fixture classes.
82+
*
83+
* @param string[] $classNames
84+
*/
85+
public function loadFixtures(array $classNames)
86+
{
87+
$loader = new ContainerAwareLoader($this->container);;
88+
$purger = new ORMPurger();
89+
$executor = new ORMExecutor($this->getOm(), $purger);
90+
91+
$referenceRepository = new ProxyReferenceRepository($this->getOm());
92+
93+
$executor->setReferenceRepository($referenceRepository);
94+
$executor->purge();
95+
96+
foreach ($classNames as $className) {
97+
$this->loadFixtureClass($loader, $className);
98+
}
99+
100+
$executor->execute($loader->getFixtures(), true);
101+
}
102+
103+
/**
104+
* Loads a single fixture.
105+
*
106+
* @param Loader $loader
107+
* @param string $className
108+
*/
109+
protected function loadFixtureClass(Loader $loader, $className)
110+
{
111+
if (!class_exists($className)) {
112+
throw new \InvalidArgumentException(sprintf(
113+
'Fixture class "%s" does not exist.',
114+
$className
115+
));
116+
}
117+
118+
$fixture = new $className();
119+
120+
if ($loader->hasFixture($fixture)) {
121+
unset($fixture);
122+
return;
123+
}
124+
125+
$loader->addFixture($fixture);
126+
127+
if ($fixture instanceof DependentFixtureInterface) {
128+
foreach ($fixture->getDependencies() as $dependency) {
129+
$this->loadFixtureClass($loader, $dependency);
130+
}
131+
}
132+
}
73133
}

0 commit comments

Comments
 (0)