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