Skip to content

Commit af8e5f1

Browse files
author
Codeliner
committed
Add doctrine support to base TestCase
1 parent a667ed7 commit af8e5f1

File tree

1 file changed

+64
-0
lines changed
  • module/Application/tests/PHPUnit/ApplicationTest

1 file changed

+64
-0
lines changed

module/Application/tests/PHPUnit/ApplicationTest/TestCase.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,76 @@
99
namespace ApplicationTest;
1010

1111
use PHPUnit_Framework_TestCase;
12+
use Doctrine\ORM\EntityManager;
1213
/**
1314
* TestCase
1415
*
1516
* @author Alexander Miertsch <[email protected]>
1617
*/
1718
class TestCase extends PHPUnit_Framework_TestCase
1819
{
20+
protected $entityManager;
21+
protected $schemaTool;
1922

23+
/**
24+
*
25+
* @return EntityManager
26+
*/
27+
public function getTestEntityManager()
28+
{
29+
if (null === $this->entityManager) {
30+
$conn = \Doctrine\DBAL\DriverManager::getConnection(array(
31+
'driver' => 'pdo_sqlite',
32+
'memory' => true
33+
));
34+
35+
$config = new \Doctrine\ORM\Configuration();
36+
37+
$config->setAutoGenerateProxyClasses(true);
38+
$config->setProxyDir(\sys_get_temp_dir());
39+
$config->setProxyNamespace(get_class($this) . '\Entities');
40+
$config->setMetadataDriverImpl(
41+
new \Doctrine\ORM\Mapping\Driver\AnnotationDriver(
42+
new \Doctrine\Common\Annotations\IndexedReader(
43+
new \Doctrine\Common\Annotations\AnnotationReader()
44+
)
45+
)
46+
);
47+
48+
$config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache());
49+
$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache());
50+
51+
$this->entityManager = \Doctrine\ORM\EntityManager::create(array(
52+
'driver' => 'pdo_sqlite',
53+
'memory' => true
54+
), $config);
55+
}
56+
57+
return $this->entityManager;
58+
}
59+
60+
public function createEntitySchema($entityNameOrNamespace, $pathToEntityDir = null)
61+
{
62+
if (!is_null($pathToEntityDir)) {
63+
$dir = opendir($pathToEntityDir);
64+
65+
$entityNameOrNamespace = trim($entityNameOrNamespace, '\\');
66+
67+
while($file = readdir($dir)) {
68+
if (0 !== strpos($file, '.')) {
69+
$entityClass = $entityNameOrNamespace . '\\' . str_replace('.php', '', $file);
70+
$this->createEntitySchema($entityClass);
71+
}
72+
}
73+
74+
return;
75+
}
76+
77+
if (null === $this->schemaTool) {
78+
$this->schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->getTestEntityManager());
79+
}
80+
$schema = $this->getTestEntityManager()->getClassMetadata($entityNameOrNamespace);
81+
$this->schemaTool->dropSchema(array($schema));
82+
$this->schemaTool->createSchema(array($schema));
83+
}
2084
}

0 commit comments

Comments
 (0)