1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\ORM\Functional; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
6
|
|
|
use Doctrine\DBAL\LockMode; |
7
|
|
|
use Doctrine\ORM\OptimisticLockException; |
8
|
|
|
use Doctrine\ORM\ORMException; |
9
|
|
|
use Doctrine\ORM\TransactionRequiredException; |
10
|
|
|
use Doctrine\Tests\Models\CMS\CmsUser; |
11
|
|
|
use Doctrine\Tests\Models\CMS\CmsEmail; |
12
|
|
|
use Doctrine\Tests\Models\CMS\CmsAddress; |
13
|
|
|
use Doctrine\Common\Collections\Criteria; |
14
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
15
|
|
|
use Doctrine\Tests\OrmFunctionalTestCase; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author robo |
19
|
|
|
*/ |
20
|
|
|
class EntityRepositoryTest extends OrmFunctionalTestCase |
21
|
|
|
{ |
22
|
|
|
protected function setUp() |
23
|
|
|
{ |
24
|
|
|
$this->useModelSet('cms'); |
25
|
|
|
parent::setUp(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function tearDown() |
29
|
|
|
{ |
30
|
|
|
if ($this->_em) { |
31
|
|
|
$this->_em->getConfiguration()->setEntityNamespaces(array()); |
32
|
|
|
} |
33
|
|
|
parent::tearDown(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function loadFixture() |
37
|
|
|
{ |
38
|
|
|
$user = new CmsUser; |
39
|
|
|
$user->name = 'Roman'; |
40
|
|
|
$user->username = 'romanb'; |
41
|
|
|
$user->status = 'freak'; |
42
|
|
|
$this->_em->persist($user); |
43
|
|
|
|
44
|
|
|
$user2 = new CmsUser; |
45
|
|
|
$user2->name = 'Guilherme'; |
46
|
|
|
$user2->username = 'gblanco'; |
47
|
|
|
$user2->status = 'dev'; |
48
|
|
|
$this->_em->persist($user2); |
49
|
|
|
|
50
|
|
|
$user3 = new CmsUser; |
51
|
|
|
$user3->name = 'Benjamin'; |
52
|
|
|
$user3->username = 'beberlei'; |
53
|
|
|
$user3->status = null; |
54
|
|
|
$this->_em->persist($user3); |
55
|
|
|
|
56
|
|
|
$user4 = new CmsUser; |
57
|
|
|
$user4->name = 'Alexander'; |
58
|
|
|
$user4->username = 'asm89'; |
59
|
|
|
$user4->status = 'dev'; |
60
|
|
|
$this->_em->persist($user4); |
61
|
|
|
|
62
|
|
|
$this->_em->flush(); |
63
|
|
|
|
64
|
|
|
$user1Id = $user->getId(); |
65
|
|
|
|
66
|
|
|
unset($user); |
67
|
|
|
unset($user2); |
68
|
|
|
unset($user3); |
69
|
|
|
unset($user4); |
70
|
|
|
|
71
|
|
|
$this->_em->clear(); |
72
|
|
|
|
73
|
|
|
return $user1Id; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function loadAssociatedFixture() |
77
|
|
|
{ |
78
|
|
|
$address = new CmsAddress(); |
79
|
|
|
$address->city = "Berlin"; |
80
|
|
|
$address->country = "Germany"; |
81
|
|
|
$address->street = "Foostreet"; |
82
|
|
|
$address->zip = "12345"; |
83
|
|
|
|
84
|
|
|
$user = new CmsUser(); |
85
|
|
|
$user->name = 'Roman'; |
86
|
|
|
$user->username = 'romanb'; |
87
|
|
|
$user->status = 'freak'; |
88
|
|
|
$user->setAddress($address); |
89
|
|
|
|
90
|
|
|
$this->_em->persist($user); |
91
|
|
|
$this->_em->persist($address); |
92
|
|
|
$this->_em->flush(); |
93
|
|
|
$this->_em->clear(); |
94
|
|
|
|
95
|
|
|
return array($user->id, $address->id); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function loadFixtureUserEmail() |
99
|
|
|
{ |
100
|
|
|
$user1 = new CmsUser(); |
101
|
|
|
$user2 = new CmsUser(); |
102
|
|
|
$user3 = new CmsUser(); |
103
|
|
|
|
104
|
|
|
$email1 = new CmsEmail(); |
105
|
|
|
$email2 = new CmsEmail(); |
106
|
|
|
$email3 = new CmsEmail(); |
107
|
|
|
|
108
|
|
|
$user1->name = 'Test 1'; |
109
|
|
|
$user1->username = 'test1'; |
110
|
|
|
$user1->status = 'active'; |
111
|
|
|
|
112
|
|
|
$user2->name = 'Test 2'; |
113
|
|
|
$user2->username = 'test2'; |
114
|
|
|
$user2->status = 'active'; |
115
|
|
|
|
116
|
|
|
$user3->name = 'Test 3'; |
117
|
|
|
$user3->username = 'test3'; |
118
|
|
|
$user3->status = 'active'; |
119
|
|
|
|
120
|
|
|
$email1->email = '[email protected]'; |
121
|
|
|
$email2->email = '[email protected]'; |
122
|
|
|
$email3->email = '[email protected]'; |
123
|
|
|
|
124
|
|
|
$user1->setEmail($email1); |
125
|
|
|
$user2->setEmail($email2); |
126
|
|
|
$user3->setEmail($email3); |
127
|
|
|
|
128
|
|
|
$this->_em->persist($user1); |
129
|
|
|
$this->_em->persist($user2); |
130
|
|
|
$this->_em->persist($user3); |
131
|
|
|
|
132
|
|
|
$this->_em->persist($email1); |
133
|
|
|
$this->_em->persist($email2); |
134
|
|
|
$this->_em->persist($email3); |
135
|
|
|
|
136
|
|
|
$this->_em->flush(); |
137
|
|
|
$this->_em->clear(); |
138
|
|
|
|
139
|
|
|
return array($user1, $user2, $user3); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function buildUser($name, $username, $status, $address) |
143
|
|
|
{ |
144
|
|
|
$user = new CmsUser(); |
145
|
|
|
$user->name = $name; |
146
|
|
|
$user->username = $username; |
147
|
|
|
$user->status = $status; |
148
|
|
|
$user->setAddress($address); |
149
|
|
|
|
150
|
|
|
$this->_em->persist($user); |
151
|
|
|
$this->_em->flush(); |
152
|
|
|
|
153
|
|
|
return $user; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function buildAddress($country, $city, $street, $zip) |
157
|
|
|
{ |
158
|
|
|
$address = new CmsAddress(); |
159
|
|
|
$address->country = $country; |
160
|
|
|
$address->city = $city; |
161
|
|
|
$address->street = $street; |
162
|
|
|
$address->zip = $zip; |
163
|
|
|
|
164
|
|
|
$this->_em->persist($address); |
165
|
|
|
$this->_em->flush(); |
166
|
|
|
|
167
|
|
|
return $address; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function testBasicFind() |
171
|
|
|
{ |
172
|
|
|
$user1Id = $this->loadFixture(); |
173
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
174
|
|
|
|
175
|
|
|
$user = $repos->find($user1Id); |
176
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser',$user); |
177
|
|
|
$this->assertEquals('Roman', $user->name); |
178
|
|
|
$this->assertEquals('freak', $user->status); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function testFindByField() |
182
|
|
|
{ |
183
|
|
|
$user1Id = $this->loadFixture(); |
184
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
185
|
|
|
|
186
|
|
|
$users = $repos->findBy(array('status' => 'dev')); |
187
|
|
|
$this->assertEquals(2, count($users)); |
188
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser',$users[0]); |
189
|
|
|
$this->assertEquals('Guilherme', $users[0]->name); |
190
|
|
|
$this->assertEquals('dev', $users[0]->status); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function testFindByAssociationWithIntegerAsParameter() |
194
|
|
|
{ |
195
|
|
|
$address1 = $this->buildAddress('Germany', 'Berlim', 'Foo st.', '123456'); |
196
|
|
|
$user1 = $this->buildUser('Benjamin', 'beberlei', 'dev', $address1); |
197
|
|
|
|
198
|
|
|
$address2 = $this->buildAddress('Brazil', 'São Paulo', 'Bar st.', '654321'); |
199
|
|
|
$user2 = $this->buildUser('Guilherme', 'guilhermeblanco', 'freak', $address2); |
200
|
|
|
|
201
|
|
|
$address3 = $this->buildAddress('USA', 'Nashville', 'Woo st.', '321654'); |
202
|
|
|
$user3 = $this->buildUser('Jonathan', 'jwage', 'dev', $address3); |
203
|
|
|
|
204
|
|
|
unset($address1); |
205
|
|
|
unset($address2); |
206
|
|
|
unset($address3); |
207
|
|
|
|
208
|
|
|
$this->_em->clear(); |
209
|
|
|
|
210
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsAddress'); |
211
|
|
|
$addresses = $repository->findBy(array('user' => array($user1->getId(), $user2->getId()))); |
212
|
|
|
|
213
|
|
|
$this->assertEquals(2, count($addresses)); |
214
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsAddress',$addresses[0]); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public function testFindByAssociationWithObjectAsParameter() |
218
|
|
|
{ |
219
|
|
|
$address1 = $this->buildAddress('Germany', 'Berlim', 'Foo st.', '123456'); |
220
|
|
|
$user1 = $this->buildUser('Benjamin', 'beberlei', 'dev', $address1); |
221
|
|
|
|
222
|
|
|
$address2 = $this->buildAddress('Brazil', 'São Paulo', 'Bar st.', '654321'); |
223
|
|
|
$user2 = $this->buildUser('Guilherme', 'guilhermeblanco', 'freak', $address2); |
224
|
|
|
|
225
|
|
|
$address3 = $this->buildAddress('USA', 'Nashville', 'Woo st.', '321654'); |
226
|
|
|
$user3 = $this->buildUser('Jonathan', 'jwage', 'dev', $address3); |
227
|
|
|
|
228
|
|
|
unset($address1); |
229
|
|
|
unset($address2); |
230
|
|
|
unset($address3); |
231
|
|
|
|
232
|
|
|
$this->_em->clear(); |
233
|
|
|
|
234
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsAddress'); |
235
|
|
|
$addresses = $repository->findBy(array('user' => array($user1, $user2))); |
236
|
|
|
|
237
|
|
|
$this->assertEquals(2, count($addresses)); |
238
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsAddress',$addresses[0]); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
public function testFindFieldByMagicCall() |
242
|
|
|
{ |
243
|
|
|
$user1Id = $this->loadFixture(); |
244
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
245
|
|
|
|
246
|
|
|
$users = $repos->findByStatus('dev'); |
247
|
|
|
$this->assertEquals(2, count($users)); |
248
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser',$users[0]); |
249
|
|
|
$this->assertEquals('Guilherme', $users[0]->name); |
250
|
|
|
$this->assertEquals('dev', $users[0]->status); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
public function testFindAll() |
254
|
|
|
{ |
255
|
|
|
$user1Id = $this->loadFixture(); |
256
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
257
|
|
|
|
258
|
|
|
$users = $repos->findAll(); |
259
|
|
|
$this->assertEquals(4, count($users)); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
public function testFindByAlias() |
263
|
|
|
{ |
264
|
|
|
$user1Id = $this->loadFixture(); |
265
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
266
|
|
|
|
267
|
|
|
$this->_em->getConfiguration()->addEntityNamespace('CMS', 'Doctrine\Tests\Models\CMS'); |
268
|
|
|
|
269
|
|
|
$repos = $this->_em->getRepository('CMS:CmsUser'); |
270
|
|
|
|
271
|
|
|
$users = $repos->findAll(); |
272
|
|
|
$this->assertEquals(4, count($users)); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
public function testCount() |
276
|
|
|
{ |
277
|
|
|
$this->loadFixture(); |
278
|
|
|
$repos = $this->_em->getRepository(CmsUser::class); |
279
|
|
|
|
280
|
|
|
$userCount = $repos->count(array()); |
281
|
|
|
$this->assertSame(4, $userCount); |
282
|
|
|
|
283
|
|
|
$userCount = $repos->count(array('status' => 'dev')); |
284
|
|
|
$this->assertSame(2, $userCount); |
285
|
|
|
|
286
|
|
|
$userCount = $repos->count(array('status' => 'nonexistent')); |
287
|
|
|
$this->assertSame(0, $userCount); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
public function testCountBy() |
291
|
|
|
{ |
292
|
|
|
$this->loadFixture(); |
293
|
|
|
$repos = $this->_em->getRepository(CmsUser::class); |
294
|
|
|
|
295
|
|
|
$userCount = $repos->countByStatus('dev'); |
296
|
|
|
$this->assertSame(2, $userCount); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* @expectedException \Doctrine\ORM\ORMException |
301
|
|
|
*/ |
302
|
|
|
public function testExceptionIsThrownWhenCallingFindByWithoutParameter() { |
303
|
|
|
$this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser') |
304
|
|
|
->findByStatus(); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* @expectedException \Doctrine\ORM\ORMException |
309
|
|
|
*/ |
310
|
|
|
public function testExceptionIsThrownWhenUsingInvalidFieldName() { |
311
|
|
|
$this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser') |
312
|
|
|
->findByThisFieldDoesNotExist('testvalue'); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @group locking |
317
|
|
|
* @group DDC-178 |
318
|
|
|
*/ |
319
|
|
|
public function testPessimisticReadLockWithoutTransaction_ThrowsException() |
320
|
|
|
{ |
321
|
|
|
$this->expectException(TransactionRequiredException::class); |
322
|
|
|
|
323
|
|
|
$this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser') |
324
|
|
|
->find(1, LockMode::PESSIMISTIC_READ); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* @group locking |
329
|
|
|
* @group DDC-178 |
330
|
|
|
*/ |
331
|
|
|
public function testPessimisticWriteLockWithoutTransaction_ThrowsException() |
332
|
|
|
{ |
333
|
|
|
$this->expectException(TransactionRequiredException::class); |
334
|
|
|
|
335
|
|
|
$this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser') |
336
|
|
|
->find(1, LockMode::PESSIMISTIC_WRITE); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* @group locking |
341
|
|
|
* @group DDC-178 |
342
|
|
|
*/ |
343
|
|
|
public function testOptimisticLockUnversionedEntity_ThrowsException() |
344
|
|
|
{ |
345
|
|
|
$this->expectException(OptimisticLockException::class); |
346
|
|
|
|
347
|
|
|
$this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser') |
348
|
|
|
->find(1, LockMode::OPTIMISTIC); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* @group locking |
353
|
|
|
* @group DDC-178 |
354
|
|
|
*/ |
355
|
|
|
public function testIdentityMappedOptimisticLockUnversionedEntity_ThrowsException() |
356
|
|
|
{ |
357
|
|
|
$user = new CmsUser; |
358
|
|
|
$user->name = 'Roman'; |
359
|
|
|
$user->username = 'romanb'; |
360
|
|
|
$user->status = 'freak'; |
361
|
|
|
$this->_em->persist($user); |
362
|
|
|
$this->_em->flush(); |
363
|
|
|
|
364
|
|
|
$userId = $user->id; |
365
|
|
|
|
366
|
|
|
$this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $userId); |
367
|
|
|
|
368
|
|
|
$this->expectException(OptimisticLockException::class); |
369
|
|
|
|
370
|
|
|
$this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $userId, LockMode::OPTIMISTIC); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* @group DDC-819 |
375
|
|
|
*/ |
376
|
|
|
public function testFindMagicCallByNullValue() |
377
|
|
|
{ |
378
|
|
|
$this->loadFixture(); |
379
|
|
|
|
380
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
381
|
|
|
|
382
|
|
|
$users = $repos->findByStatus(null); |
383
|
|
|
$this->assertEquals(1, count($users)); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* @group DDC-819 |
388
|
|
|
*/ |
389
|
|
|
public function testInvalidMagicCall() |
390
|
|
|
{ |
391
|
|
|
$this->expectException(\BadMethodCallException::class); |
392
|
|
|
|
393
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
394
|
|
|
$repos->foo(); |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* @group DDC-817 |
399
|
|
|
*/ |
400
|
|
|
public function testFindByAssociationKey_ExceptionOnInverseSide() |
401
|
|
|
{ |
402
|
|
|
list($userId, $addressId) = $this->loadAssociatedFixture(); |
403
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
404
|
|
|
|
405
|
|
|
$this->expectException(ORMException::class); |
406
|
|
|
$this->expectExceptionMessage("You cannot search for the association field 'Doctrine\Tests\Models\CMS\CmsUser#address', because it is the inverse side of an association. Find methods only work on owning side associations."); |
407
|
|
|
|
408
|
|
|
$user = $repos->findBy(array('address' => $addressId)); |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* @group DDC-817 |
413
|
|
|
*/ |
414
|
|
|
public function testFindOneByAssociationKey() |
415
|
|
|
{ |
416
|
|
|
list($userId, $addressId) = $this->loadAssociatedFixture(); |
417
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsAddress'); |
418
|
|
|
$address = $repos->findOneBy(array('user' => $userId)); |
419
|
|
|
|
420
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsAddress', $address); |
421
|
|
|
$this->assertEquals($addressId, $address->id); |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
/** |
425
|
|
|
* @group DDC-1241 |
426
|
|
|
*/ |
427
|
|
|
public function testFindOneByOrderBy() |
428
|
|
|
{ |
429
|
|
|
$this->loadFixture(); |
430
|
|
|
|
431
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
432
|
|
|
$userAsc = $repos->findOneBy(array(), array("username" => "ASC")); |
433
|
|
|
$userDesc = $repos->findOneBy(array(), array("username" => "DESC")); |
434
|
|
|
|
435
|
|
|
$this->assertNotSame($userAsc, $userDesc); |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* @group DDC-817 |
440
|
|
|
*/ |
441
|
|
|
public function testFindByAssociationKey() |
442
|
|
|
{ |
443
|
|
|
list($userId, $addressId) = $this->loadAssociatedFixture(); |
444
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsAddress'); |
445
|
|
|
$addresses = $repos->findBy(array('user' => $userId)); |
446
|
|
|
|
447
|
|
|
$this->assertContainsOnly('Doctrine\Tests\Models\CMS\CmsAddress', $addresses); |
448
|
|
|
$this->assertEquals(1, count($addresses)); |
449
|
|
|
$this->assertEquals($addressId, $addresses[0]->id); |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
/** |
453
|
|
|
* @group DDC-817 |
454
|
|
|
*/ |
455
|
|
|
public function testFindAssociationByMagicCall() |
456
|
|
|
{ |
457
|
|
|
list($userId, $addressId) = $this->loadAssociatedFixture(); |
458
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsAddress'); |
459
|
|
|
$addresses = $repos->findByUser($userId); |
460
|
|
|
|
461
|
|
|
$this->assertContainsOnly('Doctrine\Tests\Models\CMS\CmsAddress', $addresses); |
462
|
|
|
$this->assertEquals(1, count($addresses)); |
463
|
|
|
$this->assertEquals($addressId, $addresses[0]->id); |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
/** |
467
|
|
|
* @group DDC-817 |
468
|
|
|
*/ |
469
|
|
|
public function testFindOneAssociationByMagicCall() |
470
|
|
|
{ |
471
|
|
|
list($userId, $addressId) = $this->loadAssociatedFixture(); |
472
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsAddress'); |
473
|
|
|
$address = $repos->findOneByUser($userId); |
474
|
|
|
|
475
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsAddress', $address); |
476
|
|
|
$this->assertEquals($addressId, $address->id); |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
public function testValidNamedQueryRetrieval() |
480
|
|
|
{ |
481
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
482
|
|
|
|
483
|
|
|
$query = $repos->createNamedQuery('all'); |
484
|
|
|
|
485
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Query', $query); |
486
|
|
|
$this->assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $query->getDQL()); |
487
|
|
|
} |
488
|
|
|
|
489
|
|
|
public function testInvalidNamedQueryRetrieval() |
490
|
|
|
{ |
491
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
492
|
|
|
|
493
|
|
|
$this->expectException(\Doctrine\ORM\Mapping\MappingException::class); |
494
|
|
|
|
495
|
|
|
$repos->createNamedQuery('invalidNamedQuery'); |
496
|
|
|
} |
497
|
|
|
|
498
|
|
|
/** |
499
|
|
|
* @group DDC-1087 |
500
|
|
|
*/ |
501
|
|
|
public function testIsNullCriteriaDoesNotGenerateAParameter() |
502
|
|
|
{ |
503
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
504
|
|
|
$users = $repos->findBy(array('status' => null, 'username' => 'romanb')); |
505
|
|
|
|
506
|
|
|
$params = $this->_sqlLoggerStack->queries[$this->_sqlLoggerStack->currentQuery]['params']; |
507
|
|
|
$this->assertEquals(1, count($params), "Should only execute with one parameter."); |
508
|
|
|
$this->assertEquals(array('romanb'), $params); |
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
public function testIsNullCriteria() |
512
|
|
|
{ |
513
|
|
|
$this->loadFixture(); |
514
|
|
|
|
515
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
516
|
|
|
|
517
|
|
|
$users = $repos->findBy(array('status' => null)); |
518
|
|
|
$this->assertEquals(1, count($users)); |
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
/** |
522
|
|
|
* @group DDC-1094 |
523
|
|
|
*/ |
524
|
|
|
public function testFindByLimitOffset() |
525
|
|
|
{ |
526
|
|
|
$this->loadFixture(); |
527
|
|
|
|
528
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
529
|
|
|
|
530
|
|
|
$users1 = $repos->findBy(array(), null, 1, 0); |
531
|
|
|
$users2 = $repos->findBy(array(), null, 1, 1); |
532
|
|
|
|
533
|
|
|
$this->assertEquals(4, count($repos->findBy(array()))); |
534
|
|
|
$this->assertEquals(1, count($users1)); |
535
|
|
|
$this->assertEquals(1, count($users2)); |
536
|
|
|
$this->assertNotSame($users1[0], $users2[0]); |
537
|
|
|
} |
538
|
|
|
|
539
|
|
|
/** |
540
|
|
|
* @group DDC-1094 |
541
|
|
|
*/ |
542
|
|
|
public function testFindByOrderBy() |
543
|
|
|
{ |
544
|
|
|
$this->loadFixture(); |
545
|
|
|
|
546
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
547
|
|
|
$usersAsc = $repos->findBy(array(), array("username" => "ASC")); |
548
|
|
|
$usersDesc = $repos->findBy(array(), array("username" => "DESC")); |
549
|
|
|
|
550
|
|
|
$this->assertEquals(4, count($usersAsc), "Pre-condition: only four users in fixture"); |
551
|
|
|
$this->assertEquals(4, count($usersDesc), "Pre-condition: only four users in fixture"); |
552
|
|
|
$this->assertSame($usersAsc[0], $usersDesc[3]); |
553
|
|
|
$this->assertSame($usersAsc[3], $usersDesc[0]); |
554
|
|
|
} |
555
|
|
|
|
556
|
|
|
/** |
557
|
|
|
* @group DDC-1376 |
558
|
|
|
*/ |
559
|
|
|
public function testFindByOrderByAssociation() |
560
|
|
|
{ |
561
|
|
|
$this->loadFixtureUserEmail(); |
562
|
|
|
|
563
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
564
|
|
|
$resultAsc = $repository->findBy(array(), array('email' => 'ASC')); |
565
|
|
|
$resultDesc = $repository->findBy(array(), array('email' => 'DESC')); |
566
|
|
|
|
567
|
|
|
$this->assertCount(3, $resultAsc); |
568
|
|
|
$this->assertCount(3, $resultDesc); |
569
|
|
|
|
570
|
|
|
$this->assertEquals($resultAsc[0]->getEmail()->getId(), $resultDesc[2]->getEmail()->getId()); |
571
|
|
|
$this->assertEquals($resultAsc[2]->getEmail()->getId(), $resultDesc[0]->getEmail()->getId()); |
572
|
|
|
} |
573
|
|
|
|
574
|
|
|
/** |
575
|
|
|
* @group DDC-1426 |
576
|
|
|
*/ |
577
|
|
|
public function testFindFieldByMagicCallOrderBy() |
578
|
|
|
{ |
579
|
|
|
$this->loadFixture(); |
580
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
581
|
|
|
|
582
|
|
|
$usersAsc = $repos->findByStatus('dev', array('username' => "ASC")); |
583
|
|
|
$usersDesc = $repos->findByStatus('dev', array('username' => "DESC")); |
584
|
|
|
|
585
|
|
|
$this->assertEquals(2, count($usersAsc)); |
586
|
|
|
$this->assertEquals(2, count($usersDesc)); |
587
|
|
|
|
588
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser',$usersAsc[0]); |
589
|
|
|
$this->assertEquals('Alexander', $usersAsc[0]->name); |
590
|
|
|
$this->assertEquals('dev', $usersAsc[0]->status); |
591
|
|
|
|
592
|
|
|
$this->assertSame($usersAsc[0], $usersDesc[1]); |
593
|
|
|
$this->assertSame($usersAsc[1], $usersDesc[0]); |
594
|
|
|
} |
595
|
|
|
|
596
|
|
|
/** |
597
|
|
|
* @group DDC-1426 |
598
|
|
|
*/ |
599
|
|
|
public function testFindFieldByMagicCallLimitOffset() |
600
|
|
|
{ |
601
|
|
|
$this->loadFixture(); |
602
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
603
|
|
|
|
604
|
|
|
$users1 = $repos->findByStatus('dev', array(), 1, 0); |
605
|
|
|
$users2 = $repos->findByStatus('dev', array(), 1, 1); |
606
|
|
|
|
607
|
|
|
$this->assertEquals(1, count($users1)); |
608
|
|
|
$this->assertEquals(1, count($users2)); |
609
|
|
|
$this->assertNotSame($users1[0], $users2[0]); |
610
|
|
|
} |
611
|
|
|
|
612
|
|
|
/** |
613
|
|
|
* @group DDC-753 |
614
|
|
|
*/ |
615
|
|
|
public function testDefaultRepositoryClassName() |
616
|
|
|
{ |
617
|
|
|
$this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), "Doctrine\ORM\EntityRepository"); |
618
|
|
|
$this->_em->getConfiguration()->setDefaultRepositoryClassName("Doctrine\Tests\Models\DDC753\DDC753DefaultRepository"); |
619
|
|
|
$this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), "Doctrine\Tests\Models\DDC753\DDC753DefaultRepository"); |
620
|
|
|
|
621
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\DDC753\DDC753EntityWithDefaultCustomRepository'); |
622
|
|
|
$this->assertInstanceOf("Doctrine\Tests\Models\DDC753\DDC753DefaultRepository", $repos); |
623
|
|
|
$this->assertTrue($repos->isDefaultRepository()); |
624
|
|
|
|
625
|
|
|
|
626
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\DDC753\DDC753EntityWithCustomRepository'); |
627
|
|
|
$this->assertInstanceOf("Doctrine\Tests\Models\DDC753\DDC753CustomRepository", $repos); |
628
|
|
|
$this->assertTrue($repos->isCustomRepository()); |
629
|
|
|
|
630
|
|
|
$this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), "Doctrine\Tests\Models\DDC753\DDC753DefaultRepository"); |
631
|
|
|
$this->_em->getConfiguration()->setDefaultRepositoryClassName("Doctrine\ORM\EntityRepository"); |
632
|
|
|
$this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), "Doctrine\ORM\EntityRepository"); |
633
|
|
|
|
634
|
|
|
} |
635
|
|
|
|
636
|
|
|
/** |
637
|
|
|
* @group DDC-753 |
638
|
|
|
* @expectedException Doctrine\ORM\ORMException |
639
|
|
|
* @expectedExceptionMessage Invalid repository class 'Doctrine\Tests\Models\DDC753\DDC753InvalidRepository'. It must be a Doctrine\Common\Persistence\ObjectRepository. |
640
|
|
|
*/ |
641
|
|
|
public function testSetDefaultRepositoryInvalidClassError() |
642
|
|
|
{ |
643
|
|
|
$this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), "Doctrine\ORM\EntityRepository"); |
644
|
|
|
$this->_em->getConfiguration()->setDefaultRepositoryClassName("Doctrine\Tests\Models\DDC753\DDC753InvalidRepository"); |
645
|
|
|
} |
646
|
|
|
|
647
|
|
|
/** |
648
|
|
|
* @group DDC-3257 |
649
|
|
|
*/ |
650
|
|
|
public function testSingleRepositoryInstanceForDifferentEntityAliases() |
651
|
|
|
{ |
652
|
|
|
$config = $this->_em->getConfiguration(); |
653
|
|
|
|
654
|
|
|
$config->addEntityNamespace('Aliased', 'Doctrine\Tests\Models\CMS'); |
655
|
|
|
$config->addEntityNamespace('AliasedAgain', 'Doctrine\Tests\Models\CMS'); |
656
|
|
|
|
657
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
658
|
|
|
|
659
|
|
|
$this->assertSame($repository, $this->_em->getRepository('Aliased:CmsUser')); |
660
|
|
|
$this->assertSame($repository, $this->_em->getRepository('AliasedAgain:CmsUser')); |
661
|
|
|
} |
662
|
|
|
|
663
|
|
|
/** |
664
|
|
|
* @group DDC-3257 |
665
|
|
|
*/ |
666
|
|
|
public function testCanRetrieveRepositoryFromClassNameWithLeadingBackslash() |
667
|
|
|
{ |
668
|
|
|
$this->assertSame( |
669
|
|
|
$this->_em->getRepository('\\Doctrine\\Tests\\Models\\CMS\\CmsUser'), |
670
|
|
|
$this->_em->getRepository('Doctrine\\Tests\\Models\\CMS\\CmsUser') |
671
|
|
|
); |
672
|
|
|
} |
673
|
|
|
|
674
|
|
|
/** |
675
|
|
|
* @group DDC-1376 |
676
|
|
|
* |
677
|
|
|
* @expectedException Doctrine\ORM\ORMException |
678
|
|
|
* @expectedExceptionMessage You cannot search for the association field 'Doctrine\Tests\Models\CMS\CmsUser#address', because it is the inverse side of an association. |
679
|
|
|
*/ |
680
|
|
|
public function testInvalidOrderByAssociation() |
681
|
|
|
{ |
682
|
|
|
$this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser') |
683
|
|
|
->findBy(array('status' => 'test'), array('address' => 'ASC')); |
684
|
|
|
} |
685
|
|
|
|
686
|
|
|
/** |
687
|
|
|
* @group DDC-1500 |
688
|
|
|
*/ |
689
|
|
|
public function testInvalidOrientation() |
690
|
|
|
{ |
691
|
|
|
$this->expectException(ORMException::class); |
692
|
|
|
$this->expectExceptionMessage('Invalid order by orientation specified for Doctrine\Tests\Models\CMS\CmsUser#username'); |
693
|
|
|
|
694
|
|
|
$repo = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
695
|
|
|
$repo->findBy(array('status' => 'test'), array('username' => 'INVALID')); |
696
|
|
|
} |
697
|
|
|
|
698
|
|
|
/** |
699
|
|
|
* @group DDC-1713 |
700
|
|
|
*/ |
701
|
|
|
public function testFindByAssociationArray() |
702
|
|
|
{ |
703
|
|
|
$repo = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsArticle'); |
704
|
|
|
$data = $repo->findBy(array('user' => array(1, 2, 3))); |
705
|
|
|
|
706
|
|
|
$query = array_pop($this->_sqlLoggerStack->queries); |
707
|
|
|
$this->assertEquals(array(1,2,3), $query['params'][0]); |
708
|
|
|
$this->assertEquals(Connection::PARAM_INT_ARRAY, $query['types'][0]); |
709
|
|
|
} |
710
|
|
|
|
711
|
|
|
/** |
712
|
|
|
* @group DDC-1637 |
713
|
|
|
*/ |
714
|
|
|
public function testMatchingEmptyCriteria() |
715
|
|
|
{ |
716
|
|
|
$this->loadFixture(); |
717
|
|
|
|
718
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
719
|
|
|
$users = $repository->matching(new Criteria()); |
720
|
|
|
|
721
|
|
|
$this->assertEquals(4, count($users)); |
722
|
|
|
} |
723
|
|
|
|
724
|
|
|
/** |
725
|
|
|
* @group DDC-1637 |
726
|
|
|
*/ |
727
|
|
|
public function testMatchingCriteriaEqComparison() |
728
|
|
|
{ |
729
|
|
|
$this->loadFixture(); |
730
|
|
|
|
731
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
732
|
|
|
$users = $repository->matching(new Criteria( |
733
|
|
|
Criteria::expr()->eq('username', 'beberlei') |
734
|
|
|
)); |
735
|
|
|
|
736
|
|
|
$this->assertEquals(1, count($users)); |
737
|
|
|
} |
738
|
|
|
|
739
|
|
|
/** |
740
|
|
|
* @group DDC-1637 |
741
|
|
|
*/ |
742
|
|
|
public function testMatchingCriteriaNeqComparison() |
743
|
|
|
{ |
744
|
|
|
$this->loadFixture(); |
745
|
|
|
|
746
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
747
|
|
|
$users = $repository->matching(new Criteria( |
748
|
|
|
Criteria::expr()->neq('username', 'beberlei') |
749
|
|
|
)); |
750
|
|
|
|
751
|
|
|
$this->assertEquals(3, count($users)); |
752
|
|
|
} |
753
|
|
|
|
754
|
|
|
/** |
755
|
|
|
* @group DDC-1637 |
756
|
|
|
*/ |
757
|
|
|
public function testMatchingCriteriaInComparison() |
758
|
|
|
{ |
759
|
|
|
$this->loadFixture(); |
760
|
|
|
|
761
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
762
|
|
|
$users = $repository->matching(new Criteria( |
763
|
|
|
Criteria::expr()->in('username', array('beberlei', 'gblanco')) |
764
|
|
|
)); |
765
|
|
|
|
766
|
|
|
$this->assertEquals(2, count($users)); |
767
|
|
|
} |
768
|
|
|
|
769
|
|
|
/** |
770
|
|
|
* @group DDC-1637 |
771
|
|
|
*/ |
772
|
|
|
public function testMatchingCriteriaNotInComparison() |
773
|
|
|
{ |
774
|
|
|
$this->loadFixture(); |
775
|
|
|
|
776
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
777
|
|
|
$users = $repository->matching(new Criteria( |
778
|
|
|
Criteria::expr()->notIn('username', array('beberlei', 'gblanco', 'asm89')) |
779
|
|
|
)); |
780
|
|
|
|
781
|
|
|
$this->assertEquals(1, count($users)); |
782
|
|
|
} |
783
|
|
|
|
784
|
|
|
/** |
785
|
|
|
* @group DDC-1637 |
786
|
|
|
*/ |
787
|
|
|
public function testMatchingCriteriaLtComparison() |
788
|
|
|
{ |
789
|
|
|
$firstUserId = $this->loadFixture(); |
790
|
|
|
|
791
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
792
|
|
|
$users = $repository->matching(new Criteria( |
793
|
|
|
Criteria::expr()->lt('id', $firstUserId + 1) |
794
|
|
|
)); |
795
|
|
|
|
796
|
|
|
$this->assertEquals(1, count($users)); |
797
|
|
|
} |
798
|
|
|
|
799
|
|
|
/** |
800
|
|
|
* @group DDC-1637 |
801
|
|
|
*/ |
802
|
|
|
public function testMatchingCriteriaLeComparison() |
803
|
|
|
{ |
804
|
|
|
$firstUserId = $this->loadFixture(); |
805
|
|
|
|
806
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
807
|
|
|
$users = $repository->matching(new Criteria( |
808
|
|
|
Criteria::expr()->lte('id', $firstUserId + 1) |
809
|
|
|
)); |
810
|
|
|
|
811
|
|
|
$this->assertEquals(2, count($users)); |
812
|
|
|
} |
813
|
|
|
|
814
|
|
|
/** |
815
|
|
|
* @group DDC-1637 |
816
|
|
|
*/ |
817
|
|
|
public function testMatchingCriteriaGtComparison() |
818
|
|
|
{ |
819
|
|
|
$firstUserId = $this->loadFixture(); |
820
|
|
|
|
821
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
822
|
|
|
$users = $repository->matching(new Criteria( |
823
|
|
|
Criteria::expr()->gt('id', $firstUserId) |
824
|
|
|
)); |
825
|
|
|
|
826
|
|
|
$this->assertEquals(3, count($users)); |
827
|
|
|
} |
828
|
|
|
|
829
|
|
|
/** |
830
|
|
|
* @group DDC-1637 |
831
|
|
|
*/ |
832
|
|
|
public function testMatchingCriteriaGteComparison() |
833
|
|
|
{ |
834
|
|
|
$firstUserId = $this->loadFixture(); |
835
|
|
|
|
836
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
837
|
|
|
$users = $repository->matching(new Criteria( |
838
|
|
|
Criteria::expr()->gte('id', $firstUserId) |
839
|
|
|
)); |
840
|
|
|
|
841
|
|
|
$this->assertEquals(4, count($users)); |
842
|
|
|
} |
843
|
|
|
|
844
|
|
|
/** |
845
|
|
|
* @group DDC-2430 |
846
|
|
|
*/ |
847
|
|
|
public function testMatchingCriteriaAssocationByObjectInMemory() |
848
|
|
|
{ |
849
|
|
|
list($userId, $addressId) = $this->loadAssociatedFixture(); |
850
|
|
|
|
851
|
|
|
$user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $userId); |
852
|
|
|
|
853
|
|
|
$criteria = new Criteria( |
854
|
|
|
Criteria::expr()->eq('user', $user) |
855
|
|
|
); |
856
|
|
|
|
857
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsAddress'); |
858
|
|
|
$addresses = $repository->matching($criteria); |
859
|
|
|
|
860
|
|
|
$this->assertEquals(1, count($addresses)); |
861
|
|
|
|
862
|
|
|
$addresses = new ArrayCollection($repository->findAll()); |
863
|
|
|
|
864
|
|
|
$this->assertEquals(1, count($addresses->matching($criteria))); |
865
|
|
|
} |
866
|
|
|
|
867
|
|
|
/** |
868
|
|
|
* @group DDC-2430 |
869
|
|
|
*/ |
870
|
|
|
public function testMatchingCriteriaAssocationInWithArray() |
871
|
|
|
{ |
872
|
|
|
list($userId, $addressId) = $this->loadAssociatedFixture(); |
873
|
|
|
|
874
|
|
|
$user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $userId); |
875
|
|
|
|
876
|
|
|
$criteria = new Criteria( |
877
|
|
|
Criteria::expr()->in('user', array($user)) |
878
|
|
|
); |
879
|
|
|
|
880
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsAddress'); |
881
|
|
|
$addresses = $repository->matching($criteria); |
882
|
|
|
|
883
|
|
|
$this->assertEquals(1, count($addresses)); |
884
|
|
|
|
885
|
|
|
$addresses = new ArrayCollection($repository->findAll()); |
886
|
|
|
|
887
|
|
|
$this->assertEquals(1, count($addresses->matching($criteria))); |
888
|
|
|
} |
889
|
|
|
|
890
|
|
|
public function testMatchingCriteriaContainsComparison() |
891
|
|
|
{ |
892
|
|
|
$this->loadFixture(); |
893
|
|
|
|
894
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
895
|
|
|
|
896
|
|
|
$users = $repository->matching(new Criteria(Criteria::expr()->contains('name', 'Foobar'))); |
897
|
|
|
$this->assertEquals(0, count($users)); |
898
|
|
|
|
899
|
|
|
$users = $repository->matching(new Criteria(Criteria::expr()->contains('name', 'Rom'))); |
900
|
|
|
$this->assertEquals(1, count($users)); |
901
|
|
|
|
902
|
|
|
$users = $repository->matching(new Criteria(Criteria::expr()->contains('status', 'dev'))); |
903
|
|
|
$this->assertEquals(2, count($users)); |
904
|
|
|
} |
905
|
|
|
|
906
|
|
|
/** |
907
|
|
|
* @group DDC-2478 |
908
|
|
|
*/ |
909
|
|
|
public function testMatchingCriteriaNullAssocComparison() |
910
|
|
|
{ |
911
|
|
|
$fixtures = $this->loadFixtureUserEmail(); |
912
|
|
|
$user = $this->_em->merge($fixtures[0]); |
913
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
914
|
|
|
$criteriaIsNull = Criteria::create()->where(Criteria::expr()->isNull('email')); |
915
|
|
|
$criteriaEqNull = Criteria::create()->where(Criteria::expr()->eq('email', null)); |
916
|
|
|
|
917
|
|
|
$user->setEmail(null); |
918
|
|
|
$this->_em->persist($user); |
919
|
|
|
$this->_em->flush(); |
920
|
|
|
$this->_em->clear(); |
921
|
|
|
|
922
|
|
|
$usersIsNull = $repository->matching($criteriaIsNull); |
923
|
|
|
$usersEqNull = $repository->matching($criteriaEqNull); |
924
|
|
|
|
925
|
|
|
$this->assertCount(1, $usersIsNull); |
926
|
|
|
$this->assertCount(1, $usersEqNull); |
927
|
|
|
|
928
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser', $usersIsNull[0]); |
929
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser', $usersEqNull[0]); |
930
|
|
|
|
931
|
|
|
$this->assertNull($usersIsNull[0]->getEmail()); |
932
|
|
|
$this->assertNull($usersEqNull[0]->getEmail()); |
933
|
|
|
} |
934
|
|
|
|
935
|
|
|
/** |
936
|
|
|
* @group DDC-2055 |
937
|
|
|
*/ |
938
|
|
|
public function testCreateResultSetMappingBuilder() |
939
|
|
|
{ |
940
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
941
|
|
|
$rsm = $repository->createResultSetMappingBuilder('u'); |
942
|
|
|
|
943
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Query\ResultSetMappingBuilder', $rsm); |
944
|
|
|
$this->assertEquals(array('u' => 'Doctrine\Tests\Models\CMS\CmsUser'), $rsm->aliasMap); |
945
|
|
|
} |
946
|
|
|
|
947
|
|
|
/** |
948
|
|
|
* @group DDC-3045 |
949
|
|
|
*/ |
950
|
|
|
public function testFindByFieldInjectionPrevented() |
951
|
|
|
{ |
952
|
|
|
$this->expectException(ORMException::class); |
953
|
|
|
$this->expectExceptionMessage('Unrecognized field: '); |
954
|
|
|
|
955
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
956
|
|
|
$repository->findBy(array('username = ?; DELETE FROM cms_users; SELECT 1 WHERE 1' => 'test')); |
957
|
|
|
} |
958
|
|
|
|
959
|
|
|
/** |
960
|
|
|
* @group DDC-3045 |
961
|
|
|
*/ |
962
|
|
|
public function testFindOneByFieldInjectionPrevented() |
963
|
|
|
{ |
964
|
|
|
$this->expectException(ORMException::class); |
965
|
|
|
$this->expectExceptionMessage('Unrecognized field: '); |
966
|
|
|
|
967
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
968
|
|
|
$repository->findOneBy(array('username = ?; DELETE FROM cms_users; SELECT 1 WHERE 1' => 'test')); |
969
|
|
|
} |
970
|
|
|
|
971
|
|
|
/** |
972
|
|
|
* @group DDC-3045 |
973
|
|
|
*/ |
974
|
|
|
public function testMatchingInjectionPrevented() |
975
|
|
|
{ |
976
|
|
|
$this->expectException(ORMException::class); |
977
|
|
|
$this->expectExceptionMessage('Unrecognized field: '); |
978
|
|
|
|
979
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
980
|
|
|
$result = $repository->matching(new Criteria( |
981
|
|
|
Criteria::expr()->eq('username = ?; DELETE FROM cms_users; SELECT 1 WHERE 1', 'beberlei') |
982
|
|
|
)); |
983
|
|
|
|
984
|
|
|
// Because repository returns a lazy collection, we call toArray to force initialization |
985
|
|
|
$result->toArray(); |
986
|
|
|
} |
987
|
|
|
|
988
|
|
|
/** |
989
|
|
|
* @group DDC-3045 |
990
|
|
|
*/ |
991
|
|
|
public function testFindInjectionPrevented() |
992
|
|
|
{ |
993
|
|
|
$this->expectException(ORMException::class); |
994
|
|
|
$this->expectExceptionMessage('Unrecognized identifier fields: '); |
995
|
|
|
|
996
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
997
|
|
|
$repository->find(array('username = ?; DELETE FROM cms_users; SELECT 1 WHERE 1' => 'test', 'id' => 1)); |
998
|
|
|
} |
999
|
|
|
|
1000
|
|
|
/** |
1001
|
|
|
* @group DDC-3056 |
1002
|
|
|
*/ |
1003
|
|
|
public function testFindByNullValueInInCondition() |
1004
|
|
|
{ |
1005
|
|
|
$user1 = new CmsUser(); |
1006
|
|
|
$user2 = new CmsUser(); |
1007
|
|
|
|
1008
|
|
|
$user1->username = 'ocramius'; |
1009
|
|
|
$user1->name = 'Marco'; |
1010
|
|
|
$user2->status = null; |
1011
|
|
|
$user2->username = 'deeky666'; |
1012
|
|
|
$user2->name = 'Steve'; |
1013
|
|
|
$user2->status = 'dbal maintainer'; |
1014
|
|
|
|
1015
|
|
|
$this->_em->persist($user1); |
1016
|
|
|
$this->_em->persist($user2); |
1017
|
|
|
$this->_em->flush(); |
1018
|
|
|
|
1019
|
|
|
$users = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser')->findBy(array('status' => array(null))); |
1020
|
|
|
|
1021
|
|
|
$this->assertCount(1, $users); |
1022
|
|
|
$this->assertSame($user1, reset($users)); |
1023
|
|
|
} |
1024
|
|
|
|
1025
|
|
|
/** |
1026
|
|
|
* @group DDC-3056 |
1027
|
|
|
*/ |
1028
|
|
|
public function testFindByNullValueInMultipleInCriteriaValues() |
1029
|
|
|
{ |
1030
|
|
|
$user1 = new CmsUser(); |
1031
|
|
|
$user2 = new CmsUser(); |
1032
|
|
|
|
1033
|
|
|
$user1->username = 'ocramius'; |
1034
|
|
|
$user1->name = 'Marco'; |
1035
|
|
|
$user2->status = null; |
1036
|
|
|
$user2->username = 'deeky666'; |
1037
|
|
|
$user2->name = 'Steve'; |
1038
|
|
|
$user2->status = 'dbal maintainer'; |
1039
|
|
|
|
1040
|
|
|
$this->_em->persist($user1); |
1041
|
|
|
$this->_em->persist($user2); |
1042
|
|
|
$this->_em->flush(); |
1043
|
|
|
|
1044
|
|
|
$users = $this |
1045
|
|
|
->_em |
1046
|
|
|
->getRepository('Doctrine\Tests\Models\CMS\CmsUser') |
1047
|
|
|
->findBy(array('status' => array('foo', null))); |
1048
|
|
|
|
1049
|
|
|
$this->assertCount(1, $users); |
1050
|
|
|
$this->assertSame($user1, reset($users)); |
1051
|
|
|
} |
1052
|
|
|
|
1053
|
|
|
/** |
1054
|
|
|
* @group DDC-3056 |
1055
|
|
|
*/ |
1056
|
|
|
public function testFindMultipleByNullValueInMultipleInCriteriaValues() |
1057
|
|
|
{ |
1058
|
|
|
$user1 = new CmsUser(); |
1059
|
|
|
$user2 = new CmsUser(); |
1060
|
|
|
|
1061
|
|
|
$user1->username = 'ocramius'; |
1062
|
|
|
$user1->name = 'Marco'; |
1063
|
|
|
$user2->status = null; |
1064
|
|
|
$user2->username = 'deeky666'; |
1065
|
|
|
$user2->name = 'Steve'; |
1066
|
|
|
$user2->status = 'dbal maintainer'; |
1067
|
|
|
|
1068
|
|
|
$this->_em->persist($user1); |
1069
|
|
|
$this->_em->persist($user2); |
1070
|
|
|
$this->_em->flush(); |
1071
|
|
|
|
1072
|
|
|
$users = $this |
1073
|
|
|
->_em |
1074
|
|
|
->getRepository('Doctrine\Tests\Models\CMS\CmsUser') |
1075
|
|
|
->findBy(array('status' => array('dbal maintainer', null))); |
1076
|
|
|
|
1077
|
|
|
$this->assertCount(2, $users); |
1078
|
|
|
|
1079
|
|
|
foreach ($users as $user) { |
1080
|
|
|
$this->assertTrue(in_array($user, array($user1, $user2))); |
1081
|
|
|
} |
1082
|
|
|
} |
1083
|
|
|
} |
1084
|
|
|
|
1085
|
|
|
|