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