|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DoctrineORMModuleTest\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver; |
|
6
|
|
|
use Doctrine\DBAL\Driver\PDOSqlite\Driver as PDOSqliteDriver; |
|
7
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
|
8
|
|
|
use Doctrine\ORM\Configuration; |
|
9
|
|
|
use DoctrineORMModuleTest\Assets\Types\MoneyType; |
|
10
|
|
|
use PHPUnit\Framework\TestCase; |
|
11
|
|
|
use DoctrineORMModule\Service\DBALConnectionFactory; |
|
12
|
|
|
use Doctrine\DBAL\Types\Type; |
|
13
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
|
14
|
|
|
use Doctrine\Common\EventManager; |
|
15
|
|
|
use Laminas\ServiceManager\ServiceManager; |
|
16
|
|
|
use DoctrineORMModule\Service\ConfigurationFactory; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @covers \DoctrineORMModule\Service\DBALConnectionFactory |
|
20
|
|
|
*/ |
|
21
|
|
|
class DBALConnectionFactoryTest extends TestCase |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var ServiceManager |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $serviceManager; |
|
27
|
|
|
/** |
|
28
|
|
|
* @var DBALConnectionFactory |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $factory; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* {@inheritDoc} |
|
34
|
|
|
*/ |
|
35
|
|
|
public function setUp() : void |
|
36
|
|
|
{ |
|
37
|
|
|
$this->serviceManager = new ServiceManager(); |
|
38
|
|
|
$this->factory = new DBALConnectionFactory('orm_default'); |
|
39
|
|
|
$this->serviceManager->setService('doctrine.cache.array', new ArrayCache()); |
|
40
|
|
|
$this->serviceManager->setService('doctrine.eventmanager.orm_default', new EventManager()); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function testNoConnectWithoutCustomMappingsAndCommentedTypes() |
|
44
|
|
|
{ |
|
45
|
|
|
$config = [ |
|
46
|
|
|
'doctrine' => [ |
|
47
|
|
|
'connection' => [ |
|
48
|
|
|
'orm_default' => [ |
|
49
|
|
|
'driverClass' => PDOSqliteDriver::class, |
|
50
|
|
|
'params' => [ |
|
51
|
|
|
'memory' => true, |
|
52
|
|
|
], |
|
53
|
|
|
] |
|
54
|
|
|
], |
|
55
|
|
|
], |
|
56
|
|
|
]; |
|
57
|
|
|
$configurationMock = $this->getMockBuilder(Configuration::class) |
|
|
|
|
|
|
58
|
|
|
->disableOriginalConstructor() |
|
59
|
|
|
->getMock(); |
|
60
|
|
|
|
|
61
|
|
|
$this->serviceManager->setService('doctrine.configuration.orm_default', $configurationMock); |
|
62
|
|
|
$this->serviceManager->setService('config', $config); |
|
63
|
|
|
$this->serviceManager->setService('Configuration', $config); |
|
64
|
|
|
|
|
65
|
|
|
$dbal = $this->factory->createService($this->serviceManager); |
|
66
|
|
|
$this->assertFalse($dbal->isConnected()); |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function testDoctrineMappingTypeReturnCorrectParent() |
|
70
|
|
|
{ |
|
71
|
|
|
$config = [ |
|
72
|
|
|
'doctrine' => [ |
|
73
|
|
|
'connection' => [ |
|
74
|
|
|
'orm_default' => [ |
|
75
|
|
|
'driverClass' => PDOSqliteDriver::class, |
|
76
|
|
|
'params' => [ |
|
77
|
|
|
'memory' => true, |
|
78
|
|
|
], |
|
79
|
|
|
'doctrineTypeMappings' => [ |
|
80
|
|
|
'money' => 'string', |
|
81
|
|
|
], |
|
82
|
|
|
], |
|
83
|
|
|
], |
|
84
|
|
|
], |
|
85
|
|
|
]; |
|
86
|
|
|
$configurationMock = $this->getMockBuilder(Configuration::class) |
|
|
|
|
|
|
87
|
|
|
->disableOriginalConstructor() |
|
88
|
|
|
->getMock(); |
|
89
|
|
|
|
|
90
|
|
|
$this->serviceManager->setService('doctrine.configuration.orm_default', $configurationMock); |
|
91
|
|
|
$this->serviceManager->setService('config', $config); |
|
92
|
|
|
$this->serviceManager->setService('Configuration', $config); |
|
93
|
|
|
|
|
94
|
|
|
$dbal = $this->factory->createService($this->serviceManager); |
|
95
|
|
|
$platform = $dbal->getDatabasePlatform(); |
|
96
|
|
|
$this->assertSame('string', $platform->getDoctrineTypeMapping("money")); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function testDoctrineAddCustomCommentedType() |
|
100
|
|
|
{ |
|
101
|
|
|
$config = [ |
|
102
|
|
|
'doctrine' => [ |
|
103
|
|
|
'connection' => [ |
|
104
|
|
|
'orm_default' => [ |
|
105
|
|
|
'driverClass' => PDOSqliteDriver::class, |
|
106
|
|
|
'params' => [ |
|
107
|
|
|
'memory' => true, |
|
108
|
|
|
], |
|
109
|
|
|
'doctrineTypeMappings' => [ |
|
110
|
|
|
'money' => 'money', |
|
111
|
|
|
], |
|
112
|
|
|
'doctrineCommentedTypes' => [ |
|
113
|
|
|
'money', |
|
114
|
|
|
], |
|
115
|
|
|
], |
|
116
|
|
|
], |
|
117
|
|
|
'configuration' => [ |
|
118
|
|
|
'orm_default' => [ |
|
119
|
|
|
'types' => [ |
|
120
|
|
|
'money' => MoneyType::class, |
|
121
|
|
|
], |
|
122
|
|
|
], |
|
123
|
|
|
], |
|
124
|
|
|
], |
|
125
|
|
|
]; |
|
126
|
|
|
$this->serviceManager->setService('config', $config); |
|
127
|
|
|
$this->serviceManager->setService('Configuration', $config); |
|
128
|
|
|
$this->serviceManager->setService( |
|
129
|
|
|
'doctrine.driver.orm_default', |
|
130
|
|
|
$this->createMock(MappingDriver::class) |
|
|
|
|
|
|
131
|
|
|
); |
|
132
|
|
|
$configurationFactory = new ConfigurationFactory('orm_default'); |
|
133
|
|
|
$this->serviceManager->setService( |
|
134
|
|
|
'doctrine.configuration.orm_default', |
|
135
|
|
|
$configurationFactory->createService($this->serviceManager) |
|
136
|
|
|
); |
|
137
|
|
|
$dbal = $this->factory->createService($this->serviceManager); |
|
138
|
|
|
$platform = $dbal->getDatabasePlatform(); |
|
139
|
|
|
$type = Type::getType($platform->getDoctrineTypeMapping("money")); |
|
140
|
|
|
|
|
141
|
|
|
$this->assertInstanceOf(MoneyType::class, $type); |
|
|
|
|
|
|
142
|
|
|
$this->assertTrue($platform->isCommentedDoctrineType($type)); |
|
|
|
|
|
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function testGettingPlatformFromContainer() |
|
146
|
|
|
{ |
|
147
|
|
|
$config = [ |
|
148
|
|
|
'doctrine' => [ |
|
149
|
|
|
'connection' => [ |
|
150
|
|
|
'orm_default' => [ |
|
151
|
|
|
'driverClass' => PDOSqliteDriver::class, |
|
152
|
|
|
'params' => [ |
|
153
|
|
|
'platform' => 'platform_service', |
|
154
|
|
|
], |
|
155
|
|
|
] |
|
156
|
|
|
], |
|
157
|
|
|
], |
|
158
|
|
|
]; |
|
159
|
|
|
$configurationMock = $this->getMockBuilder(Configuration::class) |
|
|
|
|
|
|
160
|
|
|
->disableOriginalConstructor() |
|
161
|
|
|
->getMock(); |
|
162
|
|
|
|
|
163
|
|
|
$platformMock = $this->getMockBuilder(AbstractPlatform::class) |
|
|
|
|
|
|
164
|
|
|
->disableOriginalConstructor() |
|
165
|
|
|
->getMock(); |
|
166
|
|
|
|
|
167
|
|
|
$this->serviceManager->setService('doctrine.configuration.orm_default', $configurationMock); |
|
168
|
|
|
$this->serviceManager->setService('config', $config); |
|
169
|
|
|
$this->serviceManager->setService('Configuration', $config); |
|
170
|
|
|
$this->serviceManager->setService('platform_service', $platformMock); |
|
171
|
|
|
|
|
172
|
|
|
$dbal = $this->factory->createService($this->serviceManager); |
|
173
|
|
|
$platform = $dbal->getDatabasePlatform(); |
|
174
|
|
|
$this->assertSame($platformMock, $platform); |
|
|
|
|
|
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
public function testWithoutUseSavepoints() |
|
178
|
|
|
{ |
|
179
|
|
|
$config = [ |
|
180
|
|
|
'doctrine' => [ |
|
181
|
|
|
'connection' => [ |
|
182
|
|
|
'orm_default' => [ |
|
183
|
|
|
'driverClass' => PDOSqliteDriver::class, |
|
184
|
|
|
'params' => [ |
|
185
|
|
|
'memory' => true, |
|
186
|
|
|
], |
|
187
|
|
|
] |
|
188
|
|
|
], |
|
189
|
|
|
], |
|
190
|
|
|
]; |
|
191
|
|
|
$configurationMock = $this->getMockBuilder(Configuration::class) |
|
|
|
|
|
|
192
|
|
|
->disableOriginalConstructor() |
|
193
|
|
|
->getMock(); |
|
194
|
|
|
|
|
195
|
|
|
$this->serviceManager->setService('doctrine.configuration.orm_default', $configurationMock); |
|
196
|
|
|
$this->serviceManager->setService('config', $config); |
|
197
|
|
|
$this->serviceManager->setService('Configuration', $config); |
|
198
|
|
|
|
|
199
|
|
|
$dbal = $this->factory->createService($this->serviceManager); |
|
200
|
|
|
$this->assertFalse($dbal->getNestTransactionsWithSavepoints()); |
|
|
|
|
|
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
public function testWithUseSavepoints() |
|
204
|
|
|
{ |
|
205
|
|
|
$config = [ |
|
206
|
|
|
'doctrine' => [ |
|
207
|
|
|
'connection' => [ |
|
208
|
|
|
'orm_default' => [ |
|
209
|
|
|
'driverClass' => PDOSqliteDriver::class, |
|
210
|
|
|
'use_savepoints' => true, |
|
211
|
|
|
'params' => [ |
|
212
|
|
|
'memory' => true, |
|
213
|
|
|
], |
|
214
|
|
|
] |
|
215
|
|
|
], |
|
216
|
|
|
], |
|
217
|
|
|
]; |
|
218
|
|
|
$configurationMock = $this->getMockBuilder(Configuration::class) |
|
|
|
|
|
|
219
|
|
|
->disableOriginalConstructor() |
|
220
|
|
|
->getMock(); |
|
221
|
|
|
|
|
222
|
|
|
$this->serviceManager->setService('doctrine.configuration.orm_default', $configurationMock); |
|
223
|
|
|
$this->serviceManager->setService('config', $config); |
|
224
|
|
|
$this->serviceManager->setService('Configuration', $config); |
|
225
|
|
|
|
|
226
|
|
|
$dbal = $this->factory->createService($this->serviceManager); |
|
227
|
|
|
$this->assertTrue($dbal->getNestTransactionsWithSavepoints()); |
|
|
|
|
|
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.