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\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 dataAffectedByErrorIfClosedException() |
126
|
|
|
{ |
127
|
|
|
return [ |
128
|
|
|
['flush'], |
129
|
|
|
['persist'], |
130
|
|
|
['remove'], |
131
|
|
|
['refresh'], |
132
|
|
|
]; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @dataProvider dataAffectedByErrorIfClosedException |
137
|
|
|
* @param string $methodName |
138
|
|
|
*/ |
139
|
|
|
public function testAffectedByErrorIfClosedException($methodName) : void |
140
|
|
|
{ |
141
|
|
|
$this->expectException(ORMException::class); |
142
|
|
|
$this->expectExceptionMessage('closed'); |
143
|
|
|
|
144
|
|
|
$this->em->close(); |
145
|
|
|
$this->em->{$methodName}(new \stdClass()); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function dataToBeReturnedByTransactional() |
149
|
|
|
{ |
150
|
|
|
return [ |
151
|
|
|
[null], |
152
|
|
|
[false], |
153
|
|
|
['foo'], |
154
|
|
|
]; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @dataProvider dataToBeReturnedByTransactional |
159
|
|
|
*/ |
160
|
|
|
public function testTransactionalAcceptsReturn($value) : void |
161
|
|
|
{ |
162
|
|
|
self::assertSame( |
163
|
|
|
$value, |
164
|
|
|
$this->em->transactional(function ($em) use ($value) { |
165
|
|
|
return $value; |
166
|
|
|
}) |
167
|
|
|
); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function testTransactionalAcceptsVariousCallables() : void |
171
|
|
|
{ |
172
|
|
|
self::assertSame('callback', $this->em->transactional([$this, 'transactionalCallback'])); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function transactionalCallback($em) |
176
|
|
|
{ |
177
|
|
|
self::assertSame($this->em, $em); |
178
|
|
|
return 'callback'; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function testCreateInvalidConnection() : void |
182
|
|
|
{ |
183
|
|
|
$this->expectException(\InvalidArgumentException::class); |
184
|
|
|
$this->expectExceptionMessage('Invalid $connection argument of type integer given: "1".'); |
185
|
|
|
|
186
|
|
|
$config = new Configuration(); |
187
|
|
|
$config->setMetadataDriverImpl($this->createMock(MappingDriver::class)); |
188
|
|
|
EntityManager::create(1, $config); |
|
|
|
|
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @group #5796 |
193
|
|
|
*/ |
194
|
|
|
public function testTransactionalReThrowsThrowables() : void |
195
|
|
|
{ |
196
|
|
|
try { |
197
|
|
|
$this->em->transactional(function () { |
198
|
|
|
(function (array $value) { |
199
|
|
|
// this only serves as an IIFE that throws a `TypeError` |
200
|
|
|
})(null); |
201
|
|
|
}); |
202
|
|
|
|
203
|
|
|
self::fail('TypeError expected to be thrown'); |
204
|
|
|
} catch (\TypeError $ignored) { |
205
|
|
|
self::assertFalse($this->em->isOpen()); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @group 6017 |
211
|
|
|
*/ |
212
|
|
|
public function testClearManager() : void |
213
|
|
|
{ |
214
|
|
|
$entity = new Country(456, 'United Kingdom'); |
215
|
|
|
|
216
|
|
|
$this->em->persist($entity); |
217
|
|
|
|
218
|
|
|
self::assertTrue($this->em->contains($entity)); |
219
|
|
|
|
220
|
|
|
$this->em->clear(); |
221
|
|
|
|
222
|
|
|
self::assertFalse($this->em->contains($entity)); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function testGetReferenceRetrievesReferencesWithGivenProxiesAsIdentifiers() : void |
226
|
|
|
{ |
227
|
|
|
$simpleIdReference = $this->em->getReference( |
228
|
|
|
SimpleId::class, |
229
|
|
|
['id' => 123] |
230
|
|
|
); |
231
|
|
|
/** @var GhostObjectInterface|ToOneAssociationIdToSimpleId $nestedReference */ |
232
|
|
|
$nestedIdReference = $this->em->getReference( |
233
|
|
|
ToOneAssociationIdToSimpleId::class, |
234
|
|
|
['simpleId' => $simpleIdReference] |
235
|
|
|
); |
236
|
|
|
|
237
|
|
|
self::assertInstanceOf(ToOneAssociationIdToSimpleId::class, $nestedIdReference); |
238
|
|
|
self::assertSame($simpleIdReference, $nestedIdReference->simpleId); |
239
|
|
|
self::assertTrue($this->em->contains($simpleIdReference)); |
240
|
|
|
self::assertTrue($this->em->contains($nestedIdReference)); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
public function testGetReferenceRetrievesReferencesWithGivenProxiesAsIdentifiersEvenIfIdentifierOrderIsSwapped() : void |
244
|
|
|
{ |
245
|
|
|
$simpleIdReferenceA = $this->em->getReference( |
246
|
|
|
SimpleId::class, |
247
|
|
|
['id' => 123] |
248
|
|
|
); |
249
|
|
|
$simpleIdReferenceB = $this->em->getReference( |
250
|
|
|
SimpleId::class, |
251
|
|
|
['id' => 456] |
252
|
|
|
); |
253
|
|
|
/** @var GhostObjectInterface|ToOneCompositeAssociationToMultipleSimpleId $nestedIdReference */ |
254
|
|
|
$nestedIdReference = $this->em->getReference( |
255
|
|
|
ToOneCompositeAssociationToMultipleSimpleId::class, |
256
|
|
|
['simpleIdB' => $simpleIdReferenceB, 'simpleIdA' => $simpleIdReferenceA] |
257
|
|
|
); |
258
|
|
|
|
259
|
|
|
self::assertInstanceOf(ToOneCompositeAssociationToMultipleSimpleId::class, $nestedIdReference); |
260
|
|
|
self::assertSame($simpleIdReferenceA, $nestedIdReference->simpleIdA); |
261
|
|
|
self::assertSame($simpleIdReferenceB, $nestedIdReference->simpleIdB); |
262
|
|
|
self::assertTrue($this->em->contains($simpleIdReferenceA)); |
263
|
|
|
self::assertTrue($this->em->contains($simpleIdReferenceB)); |
264
|
|
|
self::assertTrue($this->em->contains($nestedIdReference)); |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|