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