1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Tests\ORM; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\EventManager; |
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\Mapping\Driver\MappingDriver; |
13
|
|
|
use Doctrine\ORM\NativeQuery; |
14
|
|
|
use Doctrine\ORM\ORMException; |
15
|
|
|
use Doctrine\ORM\ORMInvalidArgumentException; |
16
|
|
|
use Doctrine\ORM\Proxy\Factory\ProxyFactory; |
17
|
|
|
use Doctrine\ORM\Query; |
18
|
|
|
use Doctrine\ORM\Query\ResultSetMapping; |
19
|
|
|
use Doctrine\ORM\QueryBuilder; |
20
|
|
|
use Doctrine\ORM\UnitOfWork; |
21
|
|
|
use Doctrine\Tests\Models\CMS\CmsUser; |
22
|
|
|
use Doctrine\Tests\Models\GeoNames\Country; |
23
|
|
|
use Doctrine\Tests\Models\IdentityIsAssociation\SimpleId; |
24
|
|
|
use Doctrine\Tests\Models\IdentityIsAssociation\ToOneAssociationIdToSimpleId; |
25
|
|
|
use Doctrine\Tests\Models\IdentityIsAssociation\ToOneCompositeAssociationToMultipleSimpleId; |
26
|
|
|
use Doctrine\Tests\OrmTestCase; |
27
|
|
|
use ProxyManager\Proxy\GhostObjectInterface; |
28
|
|
|
|
29
|
|
|
class EntityManagerTest extends OrmTestCase |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var EntityManager |
33
|
|
|
*/ |
34
|
|
|
private $em; |
35
|
|
|
|
36
|
|
|
public function setUp() |
37
|
|
|
{ |
38
|
|
|
parent::setUp(); |
39
|
|
|
|
40
|
|
|
$this->em = $this->getTestEntityManager(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @group DDC-899 |
45
|
|
|
*/ |
46
|
|
|
public function testIsOpen() |
47
|
|
|
{ |
48
|
|
|
self::assertTrue($this->em->isOpen()); |
49
|
|
|
$this->em->close(); |
50
|
|
|
self::assertFalse($this->em->isOpen()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testGetConnection() |
54
|
|
|
{ |
55
|
|
|
self::assertInstanceOf(Connection::class, $this->em->getConnection()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testGetMetadataFactory() |
59
|
|
|
{ |
60
|
|
|
self::assertInstanceOf(ClassMetadataFactory::class, $this->em->getMetadataFactory()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testGetConfiguration() |
64
|
|
|
{ |
65
|
|
|
self::assertInstanceOf(Configuration::class, $this->em->getConfiguration()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testGetUnitOfWork() |
69
|
|
|
{ |
70
|
|
|
self::assertInstanceOf(UnitOfWork::class, $this->em->getUnitOfWork()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testGetProxyFactory() |
74
|
|
|
{ |
75
|
|
|
self::assertInstanceOf(ProxyFactory::class, $this->em->getProxyFactory()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testGetEventManager() |
79
|
|
|
{ |
80
|
|
|
self::assertInstanceOf(EventManager::class, $this->em->getEventManager()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testCreateNativeQuery() |
84
|
|
|
{ |
85
|
|
|
$rsm = new ResultSetMapping(); |
86
|
|
|
$query = $this->em->createNativeQuery('SELECT foo', $rsm); |
87
|
|
|
|
88
|
|
|
self::assertSame('SELECT foo', $query->getSql()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @covers \Doctrine\ORM\EntityManager::createNamedNativeQuery |
93
|
|
|
*/ |
94
|
|
|
public function testCreateNamedNativeQuery() |
95
|
|
|
{ |
96
|
|
|
$rsm = new ResultSetMapping(); |
97
|
|
|
$this->em->getConfiguration()->addNamedNativeQuery('foo', 'SELECT foo', $rsm); |
98
|
|
|
|
99
|
|
|
$query = $this->em->createNamedNativeQuery('foo'); |
100
|
|
|
|
101
|
|
|
self::assertInstanceOf(NativeQuery::class, $query); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testCreateQueryBuilder() |
105
|
|
|
{ |
106
|
|
|
self::assertInstanceOf(QueryBuilder::class, $this->em->createQueryBuilder()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testCreateQueryBuilderAliasValid() |
110
|
|
|
{ |
111
|
|
|
$q = $this->em->createQueryBuilder() |
112
|
|
|
->select('u')->from(CmsUser::class, 'u'); |
113
|
|
|
$q2 = clone $q; |
114
|
|
|
|
115
|
|
|
self::assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $q->getQuery()->getDql()); |
116
|
|
|
self::assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $q2->getQuery()->getDql()); |
117
|
|
|
|
118
|
|
|
$q3 = clone $q; |
119
|
|
|
|
120
|
|
|
self::assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $q3->getQuery()->getDql()); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function testCreateQuery_DqlIsOptional() |
124
|
|
|
{ |
125
|
|
|
self::assertInstanceOf(Query::class, $this->em->createQuery()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function testGetPartialReference() |
129
|
|
|
{ |
130
|
|
|
$user = $this->em->getPartialReference(CmsUser::class, 42); |
131
|
|
|
self::assertTrue($this->em->contains($user)); |
132
|
|
|
self::assertEquals(42, $user->id); |
133
|
|
|
self::assertNull($user->getName()); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function testCreateQuery() |
137
|
|
|
{ |
138
|
|
|
$q = $this->em->createQuery('SELECT 1'); |
139
|
|
|
self::assertInstanceOf(Query::class, $q); |
140
|
|
|
self::assertEquals('SELECT 1', $q->getDql()); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
static public function dataMethodsAffectedByNoObjectArguments() |
|
|
|
|
144
|
|
|
{ |
145
|
|
|
return [ |
146
|
|
|
['persist'], |
147
|
|
|
['remove'], |
148
|
|
|
['refresh'], |
149
|
|
|
]; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @dataProvider dataMethodsAffectedByNoObjectArguments |
154
|
|
|
*/ |
155
|
|
|
public function testThrowsExceptionOnNonObjectValues($methodName) |
156
|
|
|
{ |
157
|
|
|
$this->expectException(ORMInvalidArgumentException::class); |
158
|
|
|
$this->expectExceptionMessage('EntityManager#' . $methodName . '() expects parameter 1 to be an entity object, NULL given.'); |
159
|
|
|
|
160
|
|
|
$this->em->{$methodName}(null); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
static public function dataAffectedByErrorIfClosedException() |
|
|
|
|
164
|
|
|
{ |
165
|
|
|
return [ |
166
|
|
|
['flush'], |
167
|
|
|
['persist'], |
168
|
|
|
['remove'], |
169
|
|
|
['refresh'], |
170
|
|
|
]; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @dataProvider dataAffectedByErrorIfClosedException |
175
|
|
|
* @param string $methodName |
176
|
|
|
*/ |
177
|
|
|
public function testAffectedByErrorIfClosedException($methodName) |
178
|
|
|
{ |
179
|
|
|
$this->expectException(ORMException::class); |
180
|
|
|
$this->expectExceptionMessage('closed'); |
181
|
|
|
|
182
|
|
|
$this->em->close(); |
183
|
|
|
$this->em->{$methodName}(new \stdClass()); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function dataToBeReturnedByTransactional() |
187
|
|
|
{ |
188
|
|
|
return [ |
189
|
|
|
[null], |
190
|
|
|
[false], |
191
|
|
|
['foo'], |
192
|
|
|
]; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @dataProvider dataToBeReturnedByTransactional |
197
|
|
|
*/ |
198
|
|
|
public function testTransactionalAcceptsReturn($value) |
199
|
|
|
{ |
200
|
|
|
self::assertSame( |
201
|
|
|
$value, |
202
|
|
|
$this->em->transactional(function ($em) use ($value) { |
203
|
|
|
return $value; |
204
|
|
|
}) |
205
|
|
|
); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function testTransactionalAcceptsVariousCallables() |
209
|
|
|
{ |
210
|
|
|
self::assertSame('callback', $this->em->transactional([$this, 'transactionalCallback'])); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function transactionalCallback($em) |
214
|
|
|
{ |
215
|
|
|
self::assertSame($this->em, $em); |
216
|
|
|
return 'callback'; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function testCreateInvalidConnection() |
220
|
|
|
{ |
221
|
|
|
$this->expectException(\InvalidArgumentException::class); |
222
|
|
|
$this->expectExceptionMessage('Invalid $connection argument of type integer given: "1".'); |
223
|
|
|
|
224
|
|
|
$config = new Configuration(); |
225
|
|
|
$config->setMetadataDriverImpl($this->createMock(MappingDriver::class)); |
226
|
|
|
EntityManager::create(1, $config); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @group #5796 |
231
|
|
|
*/ |
232
|
|
|
public function testTransactionalReThrowsThrowables() |
233
|
|
|
{ |
234
|
|
|
try { |
235
|
|
|
$this->em->transactional(function () { |
236
|
|
|
(function (array $value) { |
237
|
|
|
// this only serves as an IIFE that throws a `TypeError` |
238
|
|
|
})(null); |
239
|
|
|
}); |
240
|
|
|
|
241
|
|
|
self::fail('TypeError expected to be thrown'); |
242
|
|
|
} catch (\TypeError $ignored) { |
243
|
|
|
self::assertFalse($this->em->isOpen()); |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @group 6017 |
249
|
|
|
*/ |
250
|
|
|
public function testClearManager() |
251
|
|
|
{ |
252
|
|
|
$entity = new Country(456, 'United Kingdom'); |
253
|
|
|
|
254
|
|
|
$this->em->persist($entity); |
255
|
|
|
|
256
|
|
|
self::assertTrue($this->em->contains($entity)); |
257
|
|
|
|
258
|
|
|
$this->em->clear(); |
259
|
|
|
|
260
|
|
|
self::assertFalse($this->em->contains($entity)); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
public function testGetReferenceRetrievesReferencesWithGivenProxiesAsIdentifiers() : void |
264
|
|
|
{ |
265
|
|
|
$simpleIdReference = $this->em->getReference( |
266
|
|
|
SimpleId::class, |
267
|
|
|
['id' => 123] |
268
|
|
|
); |
269
|
|
|
/* @var $nestedReference GhostObjectInterface|ToOneAssociationIdToSimpleId */ |
270
|
|
|
$nestedIdReference = $this->em->getReference( |
271
|
|
|
ToOneAssociationIdToSimpleId::class, |
272
|
|
|
['simpleId' => $simpleIdReference] |
273
|
|
|
); |
274
|
|
|
|
275
|
|
|
self::assertInstanceOf(ToOneAssociationIdToSimpleId::class, $nestedIdReference); |
276
|
|
|
self::assertSame($simpleIdReference, $nestedIdReference->simpleId); |
277
|
|
|
self::assertTrue($this->em->contains($simpleIdReference)); |
278
|
|
|
self::assertTrue($this->em->contains($nestedIdReference)); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
public function testGetReferenceRetrievesReferencesWithGivenProxiesAsIdentifiersEvenIfIdentifierOrderIsSwapped() : void |
282
|
|
|
{ |
283
|
|
|
$simpleIdReferenceA = $this->em->getReference( |
284
|
|
|
SimpleId::class, |
285
|
|
|
['id' => 123] |
286
|
|
|
); |
287
|
|
|
$simpleIdReferenceB = $this->em->getReference( |
288
|
|
|
SimpleId::class, |
289
|
|
|
['id' => 456] |
290
|
|
|
); |
291
|
|
|
/* @var $nestedIdReference GhostObjectInterface|ToOneCompositeAssociationToMultipleSimpleId */ |
292
|
|
|
$nestedIdReference = $this->em->getReference( |
293
|
|
|
ToOneCompositeAssociationToMultipleSimpleId::class, |
294
|
|
|
['simpleIdB' => $simpleIdReferenceB, 'simpleIdA' => $simpleIdReferenceA] |
295
|
|
|
); |
296
|
|
|
|
297
|
|
|
self::assertInstanceOf(ToOneCompositeAssociationToMultipleSimpleId::class, $nestedIdReference); |
298
|
|
|
self::assertSame($simpleIdReferenceA, $nestedIdReference->simpleIdA); |
299
|
|
|
self::assertSame($simpleIdReferenceB, $nestedIdReference->simpleIdB); |
300
|
|
|
self::assertTrue($this->em->contains($simpleIdReferenceA)); |
301
|
|
|
self::assertTrue($this->em->contains($simpleIdReferenceB)); |
302
|
|
|
self::assertTrue($this->em->contains($nestedIdReference)); |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
|