Skip to content

Commit df4afbd

Browse files
committed
Added base test case and DBManager
- Introduces DBManager concept to access fixture loaders / dms from different doctrines.
1 parent 9181b1a commit df4afbd

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"doctrine/doctrine-bundle": "1.*",
1717
"doctrine/phpcr-odm": "1.0.*",
1818
"doctrine/phpcr-bundle": "1.0.*",
19+
"doctrine/data-fixtures": "1.0.*",
1920

2021
"jackalope/jackalope-doctrine-dbal": "1.0.*",
2122

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Component\Testing\DataFixtures\PHPCR;
4+
5+
use Doctrine\Common\DataFixtures\FixtureInterface;
6+
use Doctrine\Common\Persistence\ObjectManager;
7+
use Doctrine\ODM\PHPCR\Document\Generic;
8+
9+
class LoadBaseData implements FixtureInterface
10+
{
11+
public function load(ObjectManager $manager)
12+
{
13+
$root = $manager->find(null, '/');
14+
$base = new Generic;
15+
$base->setNodename('test');
16+
$base->setParent($root);
17+
$manager->persist($base);
18+
$manager->flush();
19+
}
20+
}
21+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Component\Testing\Functional;
4+
5+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6+
7+
abstract class BaseTestCase extends WebTestCase
8+
{
9+
protected $db;
10+
11+
public function getContainer()
12+
{
13+
$client = $this->createClient();
14+
15+
return $client->getContainer();
16+
}
17+
18+
public function db($type)
19+
{
20+
return $this->getDbManager($type);
21+
}
22+
23+
public function getDbManager($type)
24+
{
25+
$className = sprintf(
26+
'Symfony\Cmf\Component\Testing\Functional\DbManager\%s',
27+
$type
28+
);
29+
30+
if (!class_exists($className)) {
31+
throw new \InvalidArgumentException(sprintf(
32+
'Test DBManager "%s" does not exist.',
33+
$className
34+
));
35+
}
36+
37+
$dbManager = new $className($this->getContainer());
38+
39+
return $dbManager;
40+
}
41+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Component\Testing\Functional\DbManager;
4+
5+
use Doctrine\Common\DataFixtures\Purger\PHPCRPurger;
6+
use Doctrine\Common\DataFixtures\Executor\PHPCRExecutor;
7+
use Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader;
8+
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
9+
use Doctrine\ODM\PHPCR\DocumentManager;
10+
use Symfony\Component\DependencyInjection\ContainerInterface;
11+
use Doctrine\Common\DataFixtures\ProxyReferenceRepository;
12+
13+
class PHPCR
14+
{
15+
protected $container;
16+
17+
public function __construct(ContainerInterface $container)
18+
{
19+
$this->container = $container;
20+
}
21+
22+
public function getRegistry()
23+
{
24+
return $this->container->get('doctrine_phpcr');
25+
}
26+
27+
public function getOm()
28+
{
29+
$om = $this->getRegistry()->getManager();
30+
return $om;
31+
}
32+
33+
public function loadFixtures(array $classNames)
34+
{
35+
$loader = new ContainerAwareLoader($this->container);;
36+
$purger = new PHPCRPurger();
37+
$executor = new PHPCRExecutor($this->getOm(), $purger);
38+
39+
$referenceRepository = new ProxyReferenceRepository($this->getOm());
40+
41+
$executor->setReferenceRepository($referenceRepository);
42+
$executor->purge();
43+
44+
foreach ($classNames as $className) {
45+
$this->loadFixtureClass($loader, $className);
46+
}
47+
48+
$executor->execute($loader->getFixtures(), true);
49+
}
50+
51+
public function loadFixtureClass($loader, $className)
52+
{
53+
if (!class_exists($className)) {
54+
throw new \InvalidArgumentException(sprintf(
55+
'Fixture class "%s" does not exist.',
56+
$className
57+
));
58+
}
59+
60+
$fixture = new $className();
61+
62+
if ($loader->hasFixture($fixture)) {
63+
unset($fixture);
64+
return;
65+
}
66+
67+
$loader->addFixture($fixture);
68+
69+
if ($fixture instanceof DependentFixtureInterface) {
70+
foreach ($fixture->getDependencies() as $dependency) {
71+
$this->loadFixtureClass($loader, $dependency);
72+
}
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)