|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\ORM; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\EventManager; |
|
6
|
|
|
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver; |
|
7
|
|
|
use Doctrine\Common\Persistence\Mapping\MappingException; |
|
8
|
|
|
use Doctrine\DBAL\Connection; |
|
9
|
|
|
use Doctrine\ORM\Configuration; |
|
10
|
|
|
use Doctrine\ORM\EntityManager; |
|
11
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataFactory; |
|
12
|
|
|
use Doctrine\ORM\NativeQuery; |
|
13
|
|
|
use Doctrine\ORM\ORMException; |
|
14
|
|
|
use Doctrine\ORM\ORMInvalidArgumentException; |
|
15
|
|
|
use Doctrine\ORM\Proxy\ProxyFactory; |
|
16
|
|
|
use Doctrine\ORM\Query; |
|
17
|
|
|
use Doctrine\ORM\Query\ResultSetMapping; |
|
18
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
19
|
|
|
use Doctrine\ORM\UnitOfWork; |
|
20
|
|
|
use Doctrine\Tests\Models\CMS\CmsUser; |
|
21
|
|
|
use Doctrine\Tests\Models\GeoNames\Country; |
|
22
|
|
|
use Doctrine\Tests\OrmTestCase; |
|
23
|
|
|
use Doctrine\Tests\VerifyDeprecations; |
|
24
|
|
|
|
|
25
|
|
|
class EntityManagerTest extends OrmTestCase |
|
26
|
|
|
{ |
|
27
|
|
|
use VerifyDeprecations; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var EntityManager |
|
31
|
|
|
*/ |
|
32
|
|
|
private $_em; |
|
33
|
|
|
|
|
34
|
|
|
function setUp() |
|
35
|
|
|
{ |
|
36
|
|
|
parent::setUp(); |
|
37
|
|
|
$this->_em = $this->_getTestEntityManager(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @group DDC-899 |
|
42
|
|
|
*/ |
|
43
|
|
|
public function testIsOpen() |
|
44
|
|
|
{ |
|
45
|
|
|
$this->assertTrue($this->_em->isOpen()); |
|
46
|
|
|
$this->_em->close(); |
|
47
|
|
|
$this->assertFalse($this->_em->isOpen()); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testGetConnection() |
|
51
|
|
|
{ |
|
52
|
|
|
$this->assertInstanceOf(Connection::class, $this->_em->getConnection()); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function testGetMetadataFactory() |
|
56
|
|
|
{ |
|
57
|
|
|
$this->assertInstanceOf(ClassMetadataFactory::class, $this->_em->getMetadataFactory()); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function testGetConfiguration() |
|
61
|
|
|
{ |
|
62
|
|
|
$this->assertInstanceOf(Configuration::class, $this->_em->getConfiguration()); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function testGetUnitOfWork() |
|
66
|
|
|
{ |
|
67
|
|
|
$this->assertInstanceOf(UnitOfWork::class, $this->_em->getUnitOfWork()); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function testGetProxyFactory() |
|
71
|
|
|
{ |
|
72
|
|
|
$this->assertInstanceOf(ProxyFactory::class, $this->_em->getProxyFactory()); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function testGetEventManager() |
|
76
|
|
|
{ |
|
77
|
|
|
$this->assertInstanceOf(EventManager::class, $this->_em->getEventManager()); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function testCreateNativeQuery() |
|
81
|
|
|
{ |
|
82
|
|
|
$rsm = new ResultSetMapping(); |
|
83
|
|
|
$query = $this->_em->createNativeQuery('SELECT foo', $rsm); |
|
84
|
|
|
|
|
85
|
|
|
$this->assertSame('SELECT foo', $query->getSql()); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @covers \Doctrine\ORM\EntityManager::createNamedNativeQuery |
|
90
|
|
|
*/ |
|
91
|
|
|
public function testCreateNamedNativeQuery() |
|
92
|
|
|
{ |
|
93
|
|
|
$rsm = new ResultSetMapping(); |
|
94
|
|
|
$this->_em->getConfiguration()->addNamedNativeQuery('foo', 'SELECT foo', $rsm); |
|
95
|
|
|
|
|
96
|
|
|
$query = $this->_em->createNamedNativeQuery('foo'); |
|
97
|
|
|
|
|
98
|
|
|
$this->assertInstanceOf(NativeQuery::class, $query); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function testCreateQueryBuilder() |
|
102
|
|
|
{ |
|
103
|
|
|
$this->assertInstanceOf(QueryBuilder::class, $this->_em->createQueryBuilder()); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function testCreateQueryBuilderAliasValid() |
|
107
|
|
|
{ |
|
108
|
|
|
$q = $this->_em->createQueryBuilder() |
|
109
|
|
|
->select('u')->from(CmsUser::class, 'u'); |
|
110
|
|
|
$q2 = clone $q; |
|
111
|
|
|
|
|
112
|
|
|
$this->assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $q->getQuery()->getDql()); |
|
113
|
|
|
$this->assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $q2->getQuery()->getDql()); |
|
114
|
|
|
|
|
115
|
|
|
$q3 = clone $q; |
|
116
|
|
|
|
|
117
|
|
|
$this->assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $q3->getQuery()->getDql()); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function testCreateQuery_DqlIsOptional() |
|
121
|
|
|
{ |
|
122
|
|
|
$this->assertInstanceOf(Query::class, $this->_em->createQuery()); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function testGetPartialReference() |
|
126
|
|
|
{ |
|
127
|
|
|
$user = $this->_em->getPartialReference(CmsUser::class, 42); |
|
128
|
|
|
$this->assertTrue($this->_em->contains($user)); |
|
129
|
|
|
$this->assertEquals(42, $user->id); |
|
130
|
|
|
$this->assertNull($user->getName()); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function testCreateQuery() |
|
134
|
|
|
{ |
|
135
|
|
|
$q = $this->_em->createQuery('SELECT 1'); |
|
136
|
|
|
$this->assertInstanceOf(Query::class, $q); |
|
137
|
|
|
$this->assertEquals('SELECT 1', $q->getDql()); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @covers Doctrine\ORM\EntityManager::createNamedQuery |
|
142
|
|
|
*/ |
|
143
|
|
|
public function testCreateNamedQuery() |
|
144
|
|
|
{ |
|
145
|
|
|
$this->_em->getConfiguration()->addNamedQuery('foo', 'SELECT 1'); |
|
146
|
|
|
|
|
147
|
|
|
$query = $this->_em->createNamedQuery('foo'); |
|
148
|
|
|
$this->assertInstanceOf(Query::class, $query); |
|
149
|
|
|
$this->assertEquals('SELECT 1', $query->getDql()); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
static public function dataMethodsAffectedByNoObjectArguments() |
|
153
|
|
|
{ |
|
154
|
|
|
return [ |
|
155
|
|
|
['persist'], |
|
156
|
|
|
['remove'], |
|
157
|
|
|
['merge'], |
|
158
|
|
|
['refresh'], |
|
159
|
|
|
['detach'] |
|
160
|
|
|
]; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @dataProvider dataMethodsAffectedByNoObjectArguments |
|
165
|
|
|
*/ |
|
166
|
|
|
public function testThrowsExceptionOnNonObjectValues($methodName) { |
|
167
|
|
|
$this->expectException(ORMInvalidArgumentException::class); |
|
168
|
|
|
$this->expectExceptionMessage('EntityManager#' . $methodName . '() expects parameter 1 to be an entity object, NULL given.'); |
|
169
|
|
|
|
|
170
|
|
|
$this->_em->$methodName(null); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
static public function dataAffectedByErrorIfClosedException() |
|
174
|
|
|
{ |
|
175
|
|
|
return [ |
|
176
|
|
|
['flush'], |
|
177
|
|
|
['persist'], |
|
178
|
|
|
['remove'], |
|
179
|
|
|
['merge'], |
|
180
|
|
|
['refresh'], |
|
181
|
|
|
]; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @dataProvider dataAffectedByErrorIfClosedException |
|
186
|
|
|
* @param string $methodName |
|
187
|
|
|
*/ |
|
188
|
|
|
public function testAffectedByErrorIfClosedException($methodName) |
|
189
|
|
|
{ |
|
190
|
|
|
$this->expectException(ORMException::class); |
|
191
|
|
|
$this->expectExceptionMessage('closed'); |
|
192
|
|
|
|
|
193
|
|
|
$this->_em->close(); |
|
194
|
|
|
$this->_em->$methodName(new \stdClass()); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* @group DDC-1125 |
|
199
|
|
|
*/ |
|
200
|
|
|
public function testTransactionalAcceptsReturn() |
|
201
|
|
|
{ |
|
202
|
|
|
$return = $this->_em->transactional(function ($em) { |
|
203
|
|
|
return 'foo'; |
|
204
|
|
|
}); |
|
205
|
|
|
|
|
206
|
|
|
$this->assertEquals('foo', $return); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
public function testTransactionalAcceptsVariousCallables() |
|
210
|
|
|
{ |
|
211
|
|
|
$this->assertSame('callback', $this->_em->transactional([$this, 'transactionalCallback'])); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
public function testTransactionalThrowsInvalidArgumentExceptionIfNonCallablePassed() |
|
215
|
|
|
{ |
|
216
|
|
|
$this->expectException(\InvalidArgumentException::class); |
|
217
|
|
|
$this->expectExceptionMessage('Expected argument of type "callable", got "object"'); |
|
218
|
|
|
|
|
219
|
|
|
$this->_em->transactional($this); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
public function transactionalCallback($em) |
|
223
|
|
|
{ |
|
224
|
|
|
$this->assertSame($this->_em, $em); |
|
225
|
|
|
return 'callback'; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
public function testCreateInvalidConnection() |
|
229
|
|
|
{ |
|
230
|
|
|
$this->expectException(\InvalidArgumentException::class); |
|
231
|
|
|
$this->expectExceptionMessage('Invalid $connection argument of type integer given: "1".'); |
|
232
|
|
|
|
|
233
|
|
|
$config = new Configuration(); |
|
234
|
|
|
$config->setMetadataDriverImpl($this->createMock(MappingDriver::class)); |
|
235
|
|
|
EntityManager::create(1, $config); |
|
|
|
|
|
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* @group #5796 |
|
240
|
|
|
*/ |
|
241
|
|
|
public function testTransactionalReThrowsThrowables() |
|
242
|
|
|
{ |
|
243
|
|
|
try { |
|
244
|
|
|
$this->_em->transactional(function () { |
|
245
|
|
|
(function (array $value) { |
|
246
|
|
|
// this only serves as an IIFE that throws a `TypeError` |
|
247
|
|
|
})(null); |
|
248
|
|
|
}); |
|
249
|
|
|
|
|
250
|
|
|
self::fail('TypeError expected to be thrown'); |
|
251
|
|
|
} catch (\TypeError $ignored) { |
|
252
|
|
|
self::assertFalse($this->_em->isOpen()); |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* @group 6017 |
|
258
|
|
|
*/ |
|
259
|
|
|
public function testClearManagerWithObject() |
|
260
|
|
|
{ |
|
261
|
|
|
$entity = new Country(456, 'United Kingdom'); |
|
262
|
|
|
|
|
263
|
|
|
$this->expectException(ORMInvalidArgumentException::class); |
|
264
|
|
|
|
|
265
|
|
|
$this->_em->clear($entity); |
|
|
|
|
|
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* @group 6017 |
|
270
|
|
|
*/ |
|
271
|
|
|
public function testClearManagerWithUnknownEntityName() |
|
272
|
|
|
{ |
|
273
|
|
|
$this->expectException(MappingException::class); |
|
274
|
|
|
|
|
275
|
|
|
$this->_em->clear(uniqid('nonExisting', true)); |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* @group 6017 |
|
280
|
|
|
*/ |
|
281
|
|
|
public function testClearManagerWithProxyClassName() |
|
282
|
|
|
{ |
|
283
|
|
|
$proxy = $this->_em->getReference(Country::class, ['id' => random_int(457, 100000)]); |
|
284
|
|
|
|
|
285
|
|
|
$entity = new Country(456, 'United Kingdom'); |
|
286
|
|
|
|
|
287
|
|
|
$this->_em->persist($entity); |
|
288
|
|
|
|
|
289
|
|
|
$this->assertTrue($this->_em->contains($entity)); |
|
290
|
|
|
|
|
291
|
|
|
$this->_em->clear(get_class($proxy)); |
|
292
|
|
|
|
|
293
|
|
|
$this->assertFalse($this->_em->contains($entity)); |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* @group 6017 |
|
298
|
|
|
*/ |
|
299
|
|
|
public function testClearManagerWithNullValue() |
|
300
|
|
|
{ |
|
301
|
|
|
$entity = new Country(456, 'United Kingdom'); |
|
302
|
|
|
|
|
303
|
|
|
$this->_em->persist($entity); |
|
304
|
|
|
|
|
305
|
|
|
$this->assertTrue($this->_em->contains($entity)); |
|
306
|
|
|
|
|
307
|
|
|
$this->_em->clear(null); |
|
308
|
|
|
|
|
309
|
|
|
$this->assertFalse($this->_em->contains($entity)); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
public function testDeprecatedFlushWithArguments() : void |
|
313
|
|
|
{ |
|
314
|
|
|
$entity = new Country(456, 'United Kingdom'); |
|
315
|
|
|
$this->_em->persist($entity); |
|
316
|
|
|
|
|
317
|
|
|
$this->expectDeprecationMessage('Calling Doctrine\ORM\EntityManager::flush() with any arguments to flush specific entities is deprecated and will not be supported in Doctrine 3.0.'); |
|
318
|
|
|
$this->_em->flush($entity); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
public function testDeprecatedMerge() : void |
|
322
|
|
|
{ |
|
323
|
|
|
$entity = new Country(456, 'United Kingdom'); |
|
324
|
|
|
$this->_em->persist($entity); |
|
325
|
|
|
|
|
326
|
|
|
$this->expectDeprecationMessage('Method Doctrine\ORM\EntityManager::merge() is deprecated and will be removed in Doctrine 3.0.'); |
|
327
|
|
|
$this->_em->merge($entity); |
|
|
|
|
|
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
public function testDeprecatedDetach() : void |
|
331
|
|
|
{ |
|
332
|
|
|
$entity = new Country(456, 'United Kingdom'); |
|
333
|
|
|
$this->_em->persist($entity); |
|
334
|
|
|
|
|
335
|
|
|
$this->expectDeprecationMessage('Method Doctrine\ORM\EntityManager::detach() is deprecated and will be removed in Doctrine 3.0.'); |
|
336
|
|
|
$this->_em->detach($entity); |
|
|
|
|
|
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
public function testDeprecatedCopy() : void |
|
340
|
|
|
{ |
|
341
|
|
|
$entity = new Country(456, 'United Kingdom'); |
|
342
|
|
|
$this->_em->persist($entity); |
|
343
|
|
|
|
|
344
|
|
|
try { |
|
345
|
|
|
$this->expectDeprecationMessage('Method Doctrine\ORM\EntityManager::copy() is deprecated and will be removed in Doctrine 3.0.'); |
|
346
|
|
|
$this->_em->copy($entity); |
|
347
|
|
|
} catch (\BadMethodCallException $e) { |
|
348
|
|
|
// do nothing |
|
349
|
|
|
} |
|
350
|
|
|
} |
|
351
|
|
|
} |
|
352
|
|
|
|