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