1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Khepin\Utils; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
6
|
|
|
use \Mockery as m; |
7
|
|
|
use Doctrine\ORM\Tools\SchemaTool; |
8
|
|
|
use Doctrine\ORM\Mapping\Driver\AnnotationDriver; |
9
|
|
|
use Doctrine\ORM\Mapping\DefaultQuoteStrategy; |
10
|
|
|
use Doctrine\ORM\Repository\DefaultRepositoryFactory; |
11
|
|
|
|
12
|
|
|
class BaseTestCaseOrm extends \PHPUnit_Framework_TestCase |
13
|
|
|
{ |
14
|
|
|
protected $doctrine; |
15
|
|
|
|
16
|
|
|
private function getMockAnnotatedConfig() |
17
|
|
|
{ |
18
|
|
|
$config = $this->createMock('Doctrine\ORM\Configuration'); |
19
|
|
|
$config |
20
|
|
|
->expects($this->once()) |
21
|
|
|
->method('getProxyDir') |
22
|
|
|
->will($this->returnValue(__DIR__ . '/temp')) |
23
|
|
|
; |
24
|
|
|
|
25
|
|
|
$config |
26
|
|
|
->expects($this->once()) |
27
|
|
|
->method('getProxyNamespace') |
28
|
|
|
->will($this->returnValue('Proxy')) |
29
|
|
|
; |
30
|
|
|
|
31
|
|
|
$config |
32
|
|
|
->expects($this->once()) |
33
|
|
|
->method('getAutoGenerateProxyClasses') |
34
|
|
|
->will($this->returnValue(true)) |
35
|
|
|
; |
36
|
|
|
|
37
|
|
|
$config |
38
|
|
|
->expects($this->once()) |
39
|
|
|
->method('getClassMetadataFactoryName') |
40
|
|
|
->will($this->returnValue('Doctrine\\ORM\\Mapping\\ClassMetadataFactory')) |
41
|
|
|
; |
42
|
|
|
|
43
|
|
|
$mappingDriver = $this->getMetadataDriverImplementation(); |
44
|
|
|
|
45
|
|
|
$config |
46
|
|
|
->expects($this->any()) |
47
|
|
|
->method('getMetadataDriverImpl') |
48
|
|
|
->will($this->returnValue($mappingDriver)) |
49
|
|
|
; |
50
|
|
|
|
51
|
|
|
$config |
52
|
|
|
->expects($this->any()) |
53
|
|
|
->method('getDefaultRepositoryClassName') |
54
|
|
|
->will($this->returnValue('Doctrine\\ORM\\EntityRepository')) |
55
|
|
|
; |
56
|
|
|
|
57
|
|
|
$quoteStrategy = new DefaultQuoteStrategy(); |
58
|
|
|
|
59
|
|
|
$config |
60
|
|
|
->expects($this->any()) |
61
|
|
|
->method('getQuoteStrategy') |
62
|
|
|
->will($this->returnValue($quoteStrategy)) |
63
|
|
|
; |
64
|
|
|
|
65
|
|
|
$repositoryFactory = new DefaultRepositoryFactory(); |
66
|
|
|
|
67
|
|
|
$config |
68
|
|
|
->expects($this->any()) |
69
|
|
|
->method('getRepositoryFactory') |
70
|
|
|
->will($this->returnValue($repositoryFactory)) |
71
|
|
|
; |
72
|
|
|
|
73
|
|
|
return $config; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Creates default mapping driver |
78
|
|
|
* |
79
|
|
|
* @return \Doctrine\ORM\Mapping\Driver\Driver |
80
|
|
|
*/ |
81
|
|
|
protected function getMetadataDriverImplementation() |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
return new AnnotationDriver( |
84
|
|
|
$_ENV['annotation_reader'], |
85
|
|
|
array(__DIR__.'/../Fixture/Entity') |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* EntityManager mock object together with |
91
|
|
|
* annotation mapping driver and pdo_sqlite |
92
|
|
|
* database in memory |
93
|
|
|
* |
94
|
|
|
* @param EventManager $evm |
|
|
|
|
95
|
|
|
* @return EntityManager |
96
|
|
|
*/ |
97
|
|
|
protected function getDoctrine() |
98
|
|
|
{ |
99
|
|
|
$conn = array( |
100
|
|
|
'driver' => 'pdo_sqlite', |
101
|
|
|
'memory' => true, |
102
|
|
|
//'path' => __DIR__.'/../db.sqlite', |
|
|
|
|
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
$config = $this->getMockAnnotatedConfig(); |
106
|
|
|
$em = EntityManager::create($conn, $config); |
107
|
|
|
|
108
|
|
|
$entities = array( |
109
|
|
|
'Khepin\\Fixture\\Entity\\Car', |
110
|
|
|
'Khepin\\Fixture\\Entity\\Driver', |
111
|
|
|
'Khepin\\Fixture\\Entity\\Owner' |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
$schema = array_map(function ($class) use ($em) { |
115
|
|
|
return $em->getClassMetadata($class); |
116
|
|
|
}, $entities); |
117
|
|
|
|
118
|
|
|
$schemaTool = new SchemaTool($em); |
119
|
|
|
$schemaTool->dropSchema(array()); |
120
|
|
|
$schemaTool->createSchema($schema); |
121
|
|
|
|
122
|
|
|
return $this->doctrine = m::mock( |
123
|
|
|
array( |
124
|
|
|
'getEntityManager' => $em, |
125
|
|
|
'getManager' => $em, |
126
|
|
|
'getManagers' => array($em), |
127
|
|
|
'getManagerForClass' => $em |
128
|
|
|
) |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
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: