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