|
9 | 9 | namespace ApplicationTest; |
10 | 10 |
|
11 | 11 | use PHPUnit_Framework_TestCase; |
| 12 | +use Doctrine\ORM\EntityManager; |
12 | 13 | /** |
13 | 14 | * TestCase |
14 | 15 | * |
15 | 16 | * @author Alexander Miertsch <[email protected]> |
16 | 17 | */ |
17 | 18 | class TestCase extends PHPUnit_Framework_TestCase |
18 | 19 | { |
| 20 | + protected $entityManager; |
| 21 | + protected $schemaTool; |
19 | 22 |
|
| 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 | + } |
20 | 84 | } |
0 commit comments