1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Stinger Entity Search package. |
5
|
|
|
* |
6
|
|
|
* (c) Oliver Kotte <[email protected]> |
7
|
|
|
* (c) Florian Meyer <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
namespace StingerSoft\EntitySearchBundle\Tests; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\EventManager; |
15
|
|
|
use Doctrine\ORM\Configuration; |
16
|
|
|
use Doctrine\ORM\EntityManager; |
17
|
|
|
use Doctrine\ORM\Mapping\DefaultNamingStrategy; |
18
|
|
|
use Doctrine\ORM\Mapping\DefaultQuoteStrategy; |
19
|
|
|
use Doctrine\ORM\Mapping\Driver\AnnotationDriver; |
20
|
|
|
use Doctrine\ORM\Repository\DefaultRepositoryFactory; |
21
|
|
|
use Doctrine\ORM\Tools\SchemaTool; |
22
|
|
|
|
23
|
|
|
abstract class AbstractORMTestCase extends AbstractTestCase { |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* |
27
|
|
|
* @var EntityManager |
28
|
|
|
*/ |
29
|
|
|
protected $em; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* EntityManager mock object together with |
33
|
|
|
* annotation mapping driver and pdo_sqlite |
34
|
|
|
* database in memory |
35
|
|
|
* |
36
|
|
|
* @param EventManager $evm |
37
|
|
|
* |
38
|
|
|
* @return EntityManager |
39
|
|
|
*/ |
40
|
|
|
protected function getMockSqliteEntityManager(EventManager $evm = null, Configuration $config = null) { |
41
|
|
|
$conn = array( |
42
|
|
|
'driver' => 'pdo_sqlite', |
43
|
|
|
'memory' => true |
44
|
|
|
); |
45
|
|
|
$config = null === $config ? $this->getMockAnnotatedConfig() : $config; |
46
|
|
|
$em = EntityManager::create($conn, $config, $evm ?: $this->getEventManager()); |
47
|
|
|
$schema = array_map(function ($class) use ($em) { |
48
|
|
|
return $em->getClassMetadata($class); |
49
|
|
|
}, (array)$this->getUsedEntityFixtures()); |
50
|
|
|
$schemaTool = new SchemaTool($em); |
51
|
|
|
$schemaTool->dropSchema(array()); |
52
|
|
|
$schemaTool->createSchema($schema); |
53
|
|
|
return $this->em = $em; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function getEventManager() { |
57
|
|
|
return new EventManager(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get annotation mapping configuration |
62
|
|
|
* |
63
|
|
|
* @return \Doctrine\ORM\Configuration |
64
|
|
|
*/ |
65
|
|
|
protected function getMockAnnotatedConfig() { |
66
|
|
|
// We need to mock every method except the ones which |
67
|
|
|
// handle the filters |
68
|
|
|
$configurationClass = 'Doctrine\ORM\Configuration'; |
69
|
|
|
$refl = new \ReflectionClass($configurationClass); |
70
|
|
|
$methods = $refl->getMethods(); |
71
|
|
|
$mockMethods = array(); |
72
|
|
|
foreach($methods as $method) { |
73
|
|
|
if($method->name !== 'addFilter' && $method->name !== 'getFilterClassName') { |
74
|
|
|
$mockMethods[] = $method->name; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
$config = $this->getMockBuilder($configurationClass)->setMethods($mockMethods)->getMock(); |
78
|
|
|
$config->expects($this->once())->method('getProxyDir')->will($this->returnValue(__DIR__ . '/../../temp')); |
79
|
|
|
$config->expects($this->once())->method('getProxyNamespace')->will($this->returnValue('Proxy')); |
80
|
|
|
$config->expects($this->any())->method('getDefaultQueryHints')->will($this->returnValue(array())); |
81
|
|
|
$config->expects($this->once())->method('getAutoGenerateProxyClasses')->will($this->returnValue(true)); |
82
|
|
|
$config->expects($this->once())->method('getClassMetadataFactoryName')->will($this->returnValue('Doctrine\\ORM\\Mapping\\ClassMetadataFactory')); |
83
|
|
|
$mappingDriver = $this->getMetadataDriverImplementation(); |
84
|
|
|
$config->expects($this->any())->method('getMetadataDriverImpl')->will($this->returnValue($mappingDriver)); |
85
|
|
|
$config->expects($this->any())->method('getDefaultRepositoryClassName')->will($this->returnValue('Doctrine\\ORM\\EntityRepository')); |
86
|
|
|
$config->expects($this->any())->method('getQuoteStrategy')->will($this->returnValue(new DefaultQuoteStrategy())); |
87
|
|
|
$config->expects($this->any())->method('getNamingStrategy')->will($this->returnValue(new DefaultNamingStrategy())); |
88
|
|
|
$config->expects($this->once())->method('getRepositoryFactory')->will($this->returnValue(new DefaultRepositoryFactory())); |
89
|
|
|
return $config; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Creates default mapping driver |
94
|
|
|
* |
95
|
|
|
* @return \Doctrine\ORM\Mapping\Driver\Driver |
96
|
|
|
*/ |
97
|
|
|
protected function getMetadataDriverImplementation() { |
|
|
|
|
98
|
|
|
return new AnnotationDriver($_ENV['annotation_reader'], $this->getPaths()); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
protected function getPaths() { |
102
|
|
|
return array(); |
103
|
|
|
} |
104
|
|
|
} |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: