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
|
|
|
/** |
291
|
|
|
* @expectedException \Doctrine\ORM\ORMException |
292
|
|
|
*/ |
293
|
|
|
public function testExceptionIsThrownWhenCallingFindByWithoutParameter() { |
294
|
|
|
$this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser') |
295
|
|
|
->findByStatus(); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* @expectedException \Doctrine\ORM\ORMException |
300
|
|
|
*/ |
301
|
|
|
public function testExceptionIsThrownWhenUsingInvalidFieldName() { |
302
|
|
|
$this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser') |
303
|
|
|
->findByThisFieldDoesNotExist('testvalue'); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* @group locking |
308
|
|
|
* @group DDC-178 |
309
|
|
|
*/ |
310
|
|
|
public function testPessimisticReadLockWithoutTransaction_ThrowsException() |
311
|
|
|
{ |
312
|
|
|
$this->expectException(TransactionRequiredException::class); |
313
|
|
|
|
314
|
|
|
$this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser') |
315
|
|
|
->find(1, LockMode::PESSIMISTIC_READ); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* @group locking |
320
|
|
|
* @group DDC-178 |
321
|
|
|
*/ |
322
|
|
|
public function testPessimisticWriteLockWithoutTransaction_ThrowsException() |
323
|
|
|
{ |
324
|
|
|
$this->expectException(TransactionRequiredException::class); |
325
|
|
|
|
326
|
|
|
$this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser') |
327
|
|
|
->find(1, LockMode::PESSIMISTIC_WRITE); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* @group locking |
332
|
|
|
* @group DDC-178 |
333
|
|
|
*/ |
334
|
|
|
public function testOptimisticLockUnversionedEntity_ThrowsException() |
335
|
|
|
{ |
336
|
|
|
$this->expectException(OptimisticLockException::class); |
337
|
|
|
|
338
|
|
|
$this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser') |
339
|
|
|
->find(1, LockMode::OPTIMISTIC); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* @group locking |
344
|
|
|
* @group DDC-178 |
345
|
|
|
*/ |
346
|
|
|
public function testIdentityMappedOptimisticLockUnversionedEntity_ThrowsException() |
347
|
|
|
{ |
348
|
|
|
$user = new CmsUser; |
349
|
|
|
$user->name = 'Roman'; |
350
|
|
|
$user->username = 'romanb'; |
351
|
|
|
$user->status = 'freak'; |
352
|
|
|
$this->_em->persist($user); |
353
|
|
|
$this->_em->flush(); |
354
|
|
|
|
355
|
|
|
$userId = $user->id; |
356
|
|
|
|
357
|
|
|
$this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $userId); |
358
|
|
|
|
359
|
|
|
$this->expectException(OptimisticLockException::class); |
360
|
|
|
|
361
|
|
|
$this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $userId, LockMode::OPTIMISTIC); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* @group DDC-819 |
366
|
|
|
*/ |
367
|
|
|
public function testFindMagicCallByNullValue() |
368
|
|
|
{ |
369
|
|
|
$this->loadFixture(); |
370
|
|
|
|
371
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
372
|
|
|
|
373
|
|
|
$users = $repos->findByStatus(null); |
374
|
|
|
$this->assertEquals(1, count($users)); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* @group DDC-819 |
379
|
|
|
*/ |
380
|
|
|
public function testInvalidMagicCall() |
381
|
|
|
{ |
382
|
|
|
$this->expectException(\BadMethodCallException::class); |
383
|
|
|
|
384
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
385
|
|
|
$repos->foo(); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* @group DDC-817 |
390
|
|
|
*/ |
391
|
|
|
public function testFindByAssociationKey_ExceptionOnInverseSide() |
392
|
|
|
{ |
393
|
|
|
list($userId, $addressId) = $this->loadAssociatedFixture(); |
394
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
395
|
|
|
|
396
|
|
|
$this->expectException(ORMException::class); |
397
|
|
|
$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."); |
398
|
|
|
|
399
|
|
|
$user = $repos->findBy(array('address' => $addressId)); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* @group DDC-817 |
404
|
|
|
*/ |
405
|
|
|
public function testFindOneByAssociationKey() |
406
|
|
|
{ |
407
|
|
|
list($userId, $addressId) = $this->loadAssociatedFixture(); |
408
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsAddress'); |
409
|
|
|
$address = $repos->findOneBy(array('user' => $userId)); |
410
|
|
|
|
411
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsAddress', $address); |
412
|
|
|
$this->assertEquals($addressId, $address->id); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
/** |
416
|
|
|
* @group DDC-1241 |
417
|
|
|
*/ |
418
|
|
|
public function testFindOneByOrderBy() |
419
|
|
|
{ |
420
|
|
|
$this->loadFixture(); |
421
|
|
|
|
422
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
423
|
|
|
$userAsc = $repos->findOneBy(array(), array("username" => "ASC")); |
424
|
|
|
$userDesc = $repos->findOneBy(array(), array("username" => "DESC")); |
425
|
|
|
|
426
|
|
|
$this->assertNotSame($userAsc, $userDesc); |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
/** |
430
|
|
|
* @group DDC-817 |
431
|
|
|
*/ |
432
|
|
|
public function testFindByAssociationKey() |
433
|
|
|
{ |
434
|
|
|
list($userId, $addressId) = $this->loadAssociatedFixture(); |
435
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsAddress'); |
436
|
|
|
$addresses = $repos->findBy(array('user' => $userId)); |
437
|
|
|
|
438
|
|
|
$this->assertContainsOnly('Doctrine\Tests\Models\CMS\CmsAddress', $addresses); |
439
|
|
|
$this->assertEquals(1, count($addresses)); |
440
|
|
|
$this->assertEquals($addressId, $addresses[0]->id); |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
/** |
444
|
|
|
* @group DDC-817 |
445
|
|
|
*/ |
446
|
|
|
public function testFindAssociationByMagicCall() |
447
|
|
|
{ |
448
|
|
|
list($userId, $addressId) = $this->loadAssociatedFixture(); |
449
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsAddress'); |
450
|
|
|
$addresses = $repos->findByUser($userId); |
451
|
|
|
|
452
|
|
|
$this->assertContainsOnly('Doctrine\Tests\Models\CMS\CmsAddress', $addresses); |
453
|
|
|
$this->assertEquals(1, count($addresses)); |
454
|
|
|
$this->assertEquals($addressId, $addresses[0]->id); |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
/** |
458
|
|
|
* @group DDC-817 |
459
|
|
|
*/ |
460
|
|
|
public function testFindOneAssociationByMagicCall() |
461
|
|
|
{ |
462
|
|
|
list($userId, $addressId) = $this->loadAssociatedFixture(); |
463
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsAddress'); |
464
|
|
|
$address = $repos->findOneByUser($userId); |
465
|
|
|
|
466
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsAddress', $address); |
467
|
|
|
$this->assertEquals($addressId, $address->id); |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
public function testValidNamedQueryRetrieval() |
471
|
|
|
{ |
472
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
473
|
|
|
|
474
|
|
|
$query = $repos->createNamedQuery('all'); |
475
|
|
|
|
476
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Query', $query); |
477
|
|
|
$this->assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $query->getDQL()); |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
public function testInvalidNamedQueryRetrieval() |
481
|
|
|
{ |
482
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
483
|
|
|
|
484
|
|
|
$this->expectException(\Doctrine\ORM\Mapping\MappingException::class); |
485
|
|
|
|
486
|
|
|
$repos->createNamedQuery('invalidNamedQuery'); |
487
|
|
|
} |
488
|
|
|
|
489
|
|
|
/** |
490
|
|
|
* @group DDC-1087 |
491
|
|
|
*/ |
492
|
|
|
public function testIsNullCriteriaDoesNotGenerateAParameter() |
493
|
|
|
{ |
494
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
495
|
|
|
$users = $repos->findBy(array('status' => null, 'username' => 'romanb')); |
496
|
|
|
|
497
|
|
|
$params = $this->_sqlLoggerStack->queries[$this->_sqlLoggerStack->currentQuery]['params']; |
498
|
|
|
$this->assertEquals(1, count($params), "Should only execute with one parameter."); |
499
|
|
|
$this->assertEquals(array('romanb'), $params); |
500
|
|
|
} |
501
|
|
|
|
502
|
|
|
public function testIsNullCriteria() |
503
|
|
|
{ |
504
|
|
|
$this->loadFixture(); |
505
|
|
|
|
506
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
507
|
|
|
|
508
|
|
|
$users = $repos->findBy(array('status' => null)); |
509
|
|
|
$this->assertEquals(1, count($users)); |
510
|
|
|
} |
511
|
|
|
|
512
|
|
|
/** |
513
|
|
|
* @group DDC-1094 |
514
|
|
|
*/ |
515
|
|
|
public function testFindByLimitOffset() |
516
|
|
|
{ |
517
|
|
|
$this->loadFixture(); |
518
|
|
|
|
519
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
520
|
|
|
|
521
|
|
|
$users1 = $repos->findBy(array(), null, 1, 0); |
522
|
|
|
$users2 = $repos->findBy(array(), null, 1, 1); |
523
|
|
|
|
524
|
|
|
$this->assertEquals(4, count($repos->findBy(array()))); |
525
|
|
|
$this->assertEquals(1, count($users1)); |
526
|
|
|
$this->assertEquals(1, count($users2)); |
527
|
|
|
$this->assertNotSame($users1[0], $users2[0]); |
528
|
|
|
} |
529
|
|
|
|
530
|
|
|
/** |
531
|
|
|
* @group DDC-1094 |
532
|
|
|
*/ |
533
|
|
|
public function testFindByOrderBy() |
534
|
|
|
{ |
535
|
|
|
$this->loadFixture(); |
536
|
|
|
|
537
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
538
|
|
|
$usersAsc = $repos->findBy(array(), array("username" => "ASC")); |
539
|
|
|
$usersDesc = $repos->findBy(array(), array("username" => "DESC")); |
540
|
|
|
|
541
|
|
|
$this->assertEquals(4, count($usersAsc), "Pre-condition: only four users in fixture"); |
542
|
|
|
$this->assertEquals(4, count($usersDesc), "Pre-condition: only four users in fixture"); |
543
|
|
|
$this->assertSame($usersAsc[0], $usersDesc[3]); |
544
|
|
|
$this->assertSame($usersAsc[3], $usersDesc[0]); |
545
|
|
|
} |
546
|
|
|
|
547
|
|
|
/** |
548
|
|
|
* @group DDC-1376 |
549
|
|
|
*/ |
550
|
|
|
public function testFindByOrderByAssociation() |
551
|
|
|
{ |
552
|
|
|
$this->loadFixtureUserEmail(); |
553
|
|
|
|
554
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
555
|
|
|
$resultAsc = $repository->findBy(array(), array('email' => 'ASC')); |
556
|
|
|
$resultDesc = $repository->findBy(array(), array('email' => 'DESC')); |
557
|
|
|
|
558
|
|
|
$this->assertCount(3, $resultAsc); |
559
|
|
|
$this->assertCount(3, $resultDesc); |
560
|
|
|
|
561
|
|
|
$this->assertEquals($resultAsc[0]->getEmail()->getId(), $resultDesc[2]->getEmail()->getId()); |
562
|
|
|
$this->assertEquals($resultAsc[2]->getEmail()->getId(), $resultDesc[0]->getEmail()->getId()); |
563
|
|
|
} |
564
|
|
|
|
565
|
|
|
/** |
566
|
|
|
* @group DDC-1426 |
567
|
|
|
*/ |
568
|
|
|
public function testFindFieldByMagicCallOrderBy() |
569
|
|
|
{ |
570
|
|
|
$this->loadFixture(); |
571
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
572
|
|
|
|
573
|
|
|
$usersAsc = $repos->findByStatus('dev', array('username' => "ASC")); |
574
|
|
|
$usersDesc = $repos->findByStatus('dev', array('username' => "DESC")); |
575
|
|
|
|
576
|
|
|
$this->assertEquals(2, count($usersAsc)); |
577
|
|
|
$this->assertEquals(2, count($usersDesc)); |
578
|
|
|
|
579
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser',$usersAsc[0]); |
580
|
|
|
$this->assertEquals('Alexander', $usersAsc[0]->name); |
581
|
|
|
$this->assertEquals('dev', $usersAsc[0]->status); |
582
|
|
|
|
583
|
|
|
$this->assertSame($usersAsc[0], $usersDesc[1]); |
584
|
|
|
$this->assertSame($usersAsc[1], $usersDesc[0]); |
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
/** |
588
|
|
|
* @group DDC-1426 |
589
|
|
|
*/ |
590
|
|
|
public function testFindFieldByMagicCallLimitOffset() |
591
|
|
|
{ |
592
|
|
|
$this->loadFixture(); |
593
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
594
|
|
|
|
595
|
|
|
$users1 = $repos->findByStatus('dev', array(), 1, 0); |
596
|
|
|
$users2 = $repos->findByStatus('dev', array(), 1, 1); |
597
|
|
|
|
598
|
|
|
$this->assertEquals(1, count($users1)); |
599
|
|
|
$this->assertEquals(1, count($users2)); |
600
|
|
|
$this->assertNotSame($users1[0], $users2[0]); |
601
|
|
|
} |
602
|
|
|
|
603
|
|
|
/** |
604
|
|
|
* @group DDC-753 |
605
|
|
|
*/ |
606
|
|
|
public function testDefaultRepositoryClassName() |
607
|
|
|
{ |
608
|
|
|
$this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), "Doctrine\ORM\EntityRepository"); |
609
|
|
|
$this->_em->getConfiguration()->setDefaultRepositoryClassName("Doctrine\Tests\Models\DDC753\DDC753DefaultRepository"); |
610
|
|
|
$this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), "Doctrine\Tests\Models\DDC753\DDC753DefaultRepository"); |
611
|
|
|
|
612
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\DDC753\DDC753EntityWithDefaultCustomRepository'); |
613
|
|
|
$this->assertInstanceOf("Doctrine\Tests\Models\DDC753\DDC753DefaultRepository", $repos); |
614
|
|
|
$this->assertTrue($repos->isDefaultRepository()); |
615
|
|
|
|
616
|
|
|
|
617
|
|
|
$repos = $this->_em->getRepository('Doctrine\Tests\Models\DDC753\DDC753EntityWithCustomRepository'); |
618
|
|
|
$this->assertInstanceOf("Doctrine\Tests\Models\DDC753\DDC753CustomRepository", $repos); |
619
|
|
|
$this->assertTrue($repos->isCustomRepository()); |
620
|
|
|
|
621
|
|
|
$this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), "Doctrine\Tests\Models\DDC753\DDC753DefaultRepository"); |
622
|
|
|
$this->_em->getConfiguration()->setDefaultRepositoryClassName("Doctrine\ORM\EntityRepository"); |
623
|
|
|
$this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), "Doctrine\ORM\EntityRepository"); |
624
|
|
|
|
625
|
|
|
} |
626
|
|
|
|
627
|
|
|
/** |
628
|
|
|
* @group DDC-753 |
629
|
|
|
* @expectedException Doctrine\ORM\ORMException |
630
|
|
|
* @expectedExceptionMessage Invalid repository class 'Doctrine\Tests\Models\DDC753\DDC753InvalidRepository'. It must be a Doctrine\Common\Persistence\ObjectRepository. |
631
|
|
|
*/ |
632
|
|
|
public function testSetDefaultRepositoryInvalidClassError() |
633
|
|
|
{ |
634
|
|
|
$this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), "Doctrine\ORM\EntityRepository"); |
635
|
|
|
$this->_em->getConfiguration()->setDefaultRepositoryClassName("Doctrine\Tests\Models\DDC753\DDC753InvalidRepository"); |
636
|
|
|
} |
637
|
|
|
|
638
|
|
|
/** |
639
|
|
|
* @group DDC-3257 |
640
|
|
|
*/ |
641
|
|
|
public function testSingleRepositoryInstanceForDifferentEntityAliases() |
642
|
|
|
{ |
643
|
|
|
$config = $this->_em->getConfiguration(); |
644
|
|
|
|
645
|
|
|
$config->addEntityNamespace('Aliased', 'Doctrine\Tests\Models\CMS'); |
646
|
|
|
$config->addEntityNamespace('AliasedAgain', 'Doctrine\Tests\Models\CMS'); |
647
|
|
|
|
648
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
649
|
|
|
|
650
|
|
|
$this->assertSame($repository, $this->_em->getRepository('Aliased:CmsUser')); |
651
|
|
|
$this->assertSame($repository, $this->_em->getRepository('AliasedAgain:CmsUser')); |
652
|
|
|
} |
653
|
|
|
|
654
|
|
|
/** |
655
|
|
|
* @group DDC-3257 |
656
|
|
|
*/ |
657
|
|
|
public function testCanRetrieveRepositoryFromClassNameWithLeadingBackslash() |
658
|
|
|
{ |
659
|
|
|
$this->assertSame( |
660
|
|
|
$this->_em->getRepository('\\Doctrine\\Tests\\Models\\CMS\\CmsUser'), |
661
|
|
|
$this->_em->getRepository('Doctrine\\Tests\\Models\\CMS\\CmsUser') |
662
|
|
|
); |
663
|
|
|
} |
664
|
|
|
|
665
|
|
|
/** |
666
|
|
|
* @group DDC-1376 |
667
|
|
|
* |
668
|
|
|
* @expectedException Doctrine\ORM\ORMException |
669
|
|
|
* @expectedExceptionMessage You cannot search for the association field 'Doctrine\Tests\Models\CMS\CmsUser#address', because it is the inverse side of an association. |
670
|
|
|
*/ |
671
|
|
|
public function testInvalidOrderByAssociation() |
672
|
|
|
{ |
673
|
|
|
$this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser') |
674
|
|
|
->findBy(array('status' => 'test'), array('address' => 'ASC')); |
675
|
|
|
} |
676
|
|
|
|
677
|
|
|
/** |
678
|
|
|
* @group DDC-1500 |
679
|
|
|
*/ |
680
|
|
|
public function testInvalidOrientation() |
681
|
|
|
{ |
682
|
|
|
$this->expectException(ORMException::class); |
683
|
|
|
$this->expectExceptionMessage('Invalid order by orientation specified for Doctrine\Tests\Models\CMS\CmsUser#username'); |
684
|
|
|
|
685
|
|
|
$repo = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
686
|
|
|
$repo->findBy(array('status' => 'test'), array('username' => 'INVALID')); |
687
|
|
|
} |
688
|
|
|
|
689
|
|
|
/** |
690
|
|
|
* @group DDC-1713 |
691
|
|
|
*/ |
692
|
|
|
public function testFindByAssociationArray() |
693
|
|
|
{ |
694
|
|
|
$repo = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsArticle'); |
695
|
|
|
$data = $repo->findBy(array('user' => array(1, 2, 3))); |
696
|
|
|
|
697
|
|
|
$query = array_pop($this->_sqlLoggerStack->queries); |
698
|
|
|
$this->assertEquals(array(1,2,3), $query['params'][0]); |
699
|
|
|
$this->assertEquals(Connection::PARAM_INT_ARRAY, $query['types'][0]); |
700
|
|
|
} |
701
|
|
|
|
702
|
|
|
/** |
703
|
|
|
* @group DDC-1637 |
704
|
|
|
*/ |
705
|
|
|
public function testMatchingEmptyCriteria() |
706
|
|
|
{ |
707
|
|
|
$this->loadFixture(); |
708
|
|
|
|
709
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
710
|
|
|
$users = $repository->matching(new Criteria()); |
711
|
|
|
|
712
|
|
|
$this->assertEquals(4, count($users)); |
713
|
|
|
} |
714
|
|
|
|
715
|
|
|
/** |
716
|
|
|
* @group DDC-1637 |
717
|
|
|
*/ |
718
|
|
|
public function testMatchingCriteriaEqComparison() |
719
|
|
|
{ |
720
|
|
|
$this->loadFixture(); |
721
|
|
|
|
722
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
723
|
|
|
$users = $repository->matching(new Criteria( |
724
|
|
|
Criteria::expr()->eq('username', 'beberlei') |
725
|
|
|
)); |
726
|
|
|
|
727
|
|
|
$this->assertEquals(1, count($users)); |
728
|
|
|
} |
729
|
|
|
|
730
|
|
|
/** |
731
|
|
|
* @group DDC-1637 |
732
|
|
|
*/ |
733
|
|
|
public function testMatchingCriteriaNeqComparison() |
734
|
|
|
{ |
735
|
|
|
$this->loadFixture(); |
736
|
|
|
|
737
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
738
|
|
|
$users = $repository->matching(new Criteria( |
739
|
|
|
Criteria::expr()->neq('username', 'beberlei') |
740
|
|
|
)); |
741
|
|
|
|
742
|
|
|
$this->assertEquals(3, count($users)); |
743
|
|
|
} |
744
|
|
|
|
745
|
|
|
/** |
746
|
|
|
* @group DDC-1637 |
747
|
|
|
*/ |
748
|
|
|
public function testMatchingCriteriaInComparison() |
749
|
|
|
{ |
750
|
|
|
$this->loadFixture(); |
751
|
|
|
|
752
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
753
|
|
|
$users = $repository->matching(new Criteria( |
754
|
|
|
Criteria::expr()->in('username', array('beberlei', 'gblanco')) |
755
|
|
|
)); |
756
|
|
|
|
757
|
|
|
$this->assertEquals(2, count($users)); |
758
|
|
|
} |
759
|
|
|
|
760
|
|
|
/** |
761
|
|
|
* @group DDC-1637 |
762
|
|
|
*/ |
763
|
|
|
public function testMatchingCriteriaNotInComparison() |
764
|
|
|
{ |
765
|
|
|
$this->loadFixture(); |
766
|
|
|
|
767
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
768
|
|
|
$users = $repository->matching(new Criteria( |
769
|
|
|
Criteria::expr()->notIn('username', array('beberlei', 'gblanco', 'asm89')) |
770
|
|
|
)); |
771
|
|
|
|
772
|
|
|
$this->assertEquals(1, count($users)); |
773
|
|
|
} |
774
|
|
|
|
775
|
|
|
/** |
776
|
|
|
* @group DDC-1637 |
777
|
|
|
*/ |
778
|
|
|
public function testMatchingCriteriaLtComparison() |
779
|
|
|
{ |
780
|
|
|
$firstUserId = $this->loadFixture(); |
781
|
|
|
|
782
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
783
|
|
|
$users = $repository->matching(new Criteria( |
784
|
|
|
Criteria::expr()->lt('id', $firstUserId + 1) |
785
|
|
|
)); |
786
|
|
|
|
787
|
|
|
$this->assertEquals(1, count($users)); |
788
|
|
|
} |
789
|
|
|
|
790
|
|
|
/** |
791
|
|
|
* @group DDC-1637 |
792
|
|
|
*/ |
793
|
|
|
public function testMatchingCriteriaLeComparison() |
794
|
|
|
{ |
795
|
|
|
$firstUserId = $this->loadFixture(); |
796
|
|
|
|
797
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
798
|
|
|
$users = $repository->matching(new Criteria( |
799
|
|
|
Criteria::expr()->lte('id', $firstUserId + 1) |
800
|
|
|
)); |
801
|
|
|
|
802
|
|
|
$this->assertEquals(2, count($users)); |
803
|
|
|
} |
804
|
|
|
|
805
|
|
|
/** |
806
|
|
|
* @group DDC-1637 |
807
|
|
|
*/ |
808
|
|
|
public function testMatchingCriteriaGtComparison() |
809
|
|
|
{ |
810
|
|
|
$firstUserId = $this->loadFixture(); |
811
|
|
|
|
812
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
813
|
|
|
$users = $repository->matching(new Criteria( |
814
|
|
|
Criteria::expr()->gt('id', $firstUserId) |
815
|
|
|
)); |
816
|
|
|
|
817
|
|
|
$this->assertEquals(3, count($users)); |
818
|
|
|
} |
819
|
|
|
|
820
|
|
|
/** |
821
|
|
|
* @group DDC-1637 |
822
|
|
|
*/ |
823
|
|
|
public function testMatchingCriteriaGteComparison() |
824
|
|
|
{ |
825
|
|
|
$firstUserId = $this->loadFixture(); |
826
|
|
|
|
827
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
828
|
|
|
$users = $repository->matching(new Criteria( |
829
|
|
|
Criteria::expr()->gte('id', $firstUserId) |
830
|
|
|
)); |
831
|
|
|
|
832
|
|
|
$this->assertEquals(4, count($users)); |
833
|
|
|
} |
834
|
|
|
|
835
|
|
|
/** |
836
|
|
|
* @group DDC-2430 |
837
|
|
|
*/ |
838
|
|
|
public function testMatchingCriteriaAssocationByObjectInMemory() |
839
|
|
|
{ |
840
|
|
|
list($userId, $addressId) = $this->loadAssociatedFixture(); |
841
|
|
|
|
842
|
|
|
$user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $userId); |
843
|
|
|
|
844
|
|
|
$criteria = new Criteria( |
845
|
|
|
Criteria::expr()->eq('user', $user) |
846
|
|
|
); |
847
|
|
|
|
848
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsAddress'); |
849
|
|
|
$addresses = $repository->matching($criteria); |
850
|
|
|
|
851
|
|
|
$this->assertEquals(1, count($addresses)); |
852
|
|
|
|
853
|
|
|
$addresses = new ArrayCollection($repository->findAll()); |
854
|
|
|
|
855
|
|
|
$this->assertEquals(1, count($addresses->matching($criteria))); |
856
|
|
|
} |
857
|
|
|
|
858
|
|
|
/** |
859
|
|
|
* @group DDC-2430 |
860
|
|
|
*/ |
861
|
|
|
public function testMatchingCriteriaAssocationInWithArray() |
862
|
|
|
{ |
863
|
|
|
list($userId, $addressId) = $this->loadAssociatedFixture(); |
864
|
|
|
|
865
|
|
|
$user = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $userId); |
866
|
|
|
|
867
|
|
|
$criteria = new Criteria( |
868
|
|
|
Criteria::expr()->in('user', array($user)) |
869
|
|
|
); |
870
|
|
|
|
871
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsAddress'); |
872
|
|
|
$addresses = $repository->matching($criteria); |
873
|
|
|
|
874
|
|
|
$this->assertEquals(1, count($addresses)); |
875
|
|
|
|
876
|
|
|
$addresses = new ArrayCollection($repository->findAll()); |
877
|
|
|
|
878
|
|
|
$this->assertEquals(1, count($addresses->matching($criteria))); |
879
|
|
|
} |
880
|
|
|
|
881
|
|
|
public function testMatchingCriteriaContainsComparison() |
882
|
|
|
{ |
883
|
|
|
$this->loadFixture(); |
884
|
|
|
|
885
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
886
|
|
|
|
887
|
|
|
$users = $repository->matching(new Criteria(Criteria::expr()->contains('name', 'Foobar'))); |
888
|
|
|
$this->assertEquals(0, count($users)); |
889
|
|
|
|
890
|
|
|
$users = $repository->matching(new Criteria(Criteria::expr()->contains('name', 'Rom'))); |
891
|
|
|
$this->assertEquals(1, count($users)); |
892
|
|
|
|
893
|
|
|
$users = $repository->matching(new Criteria(Criteria::expr()->contains('status', 'dev'))); |
894
|
|
|
$this->assertEquals(2, count($users)); |
895
|
|
|
} |
896
|
|
|
|
897
|
|
|
/** |
898
|
|
|
* @group DDC-2478 |
899
|
|
|
*/ |
900
|
|
|
public function testMatchingCriteriaNullAssocComparison() |
901
|
|
|
{ |
902
|
|
|
$fixtures = $this->loadFixtureUserEmail(); |
903
|
|
|
$user = $this->_em->merge($fixtures[0]); |
904
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
905
|
|
|
$criteriaIsNull = Criteria::create()->where(Criteria::expr()->isNull('email')); |
906
|
|
|
$criteriaEqNull = Criteria::create()->where(Criteria::expr()->eq('email', null)); |
907
|
|
|
|
908
|
|
|
$user->setEmail(null); |
909
|
|
|
$this->_em->persist($user); |
910
|
|
|
$this->_em->flush(); |
911
|
|
|
$this->_em->clear(); |
912
|
|
|
|
913
|
|
|
$usersIsNull = $repository->matching($criteriaIsNull); |
914
|
|
|
$usersEqNull = $repository->matching($criteriaEqNull); |
915
|
|
|
|
916
|
|
|
$this->assertCount(1, $usersIsNull); |
917
|
|
|
$this->assertCount(1, $usersEqNull); |
918
|
|
|
|
919
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser', $usersIsNull[0]); |
920
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser', $usersEqNull[0]); |
921
|
|
|
|
922
|
|
|
$this->assertNull($usersIsNull[0]->getEmail()); |
923
|
|
|
$this->assertNull($usersEqNull[0]->getEmail()); |
924
|
|
|
} |
925
|
|
|
|
926
|
|
|
/** |
927
|
|
|
* @group DDC-2055 |
928
|
|
|
*/ |
929
|
|
|
public function testCreateResultSetMappingBuilder() |
930
|
|
|
{ |
931
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
932
|
|
|
$rsm = $repository->createResultSetMappingBuilder('u'); |
933
|
|
|
|
934
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Query\ResultSetMappingBuilder', $rsm); |
935
|
|
|
$this->assertEquals(array('u' => 'Doctrine\Tests\Models\CMS\CmsUser'), $rsm->aliasMap); |
936
|
|
|
} |
937
|
|
|
|
938
|
|
|
/** |
939
|
|
|
* @group DDC-3045 |
940
|
|
|
*/ |
941
|
|
|
public function testFindByFieldInjectionPrevented() |
942
|
|
|
{ |
943
|
|
|
$this->expectException(ORMException::class); |
944
|
|
|
$this->expectExceptionMessage('Unrecognized field: '); |
945
|
|
|
|
946
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
947
|
|
|
$repository->findBy(array('username = ?; DELETE FROM cms_users; SELECT 1 WHERE 1' => 'test')); |
948
|
|
|
} |
949
|
|
|
|
950
|
|
|
/** |
951
|
|
|
* @group DDC-3045 |
952
|
|
|
*/ |
953
|
|
|
public function testFindOneByFieldInjectionPrevented() |
954
|
|
|
{ |
955
|
|
|
$this->expectException(ORMException::class); |
956
|
|
|
$this->expectExceptionMessage('Unrecognized field: '); |
957
|
|
|
|
958
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
959
|
|
|
$repository->findOneBy(array('username = ?; DELETE FROM cms_users; SELECT 1 WHERE 1' => 'test')); |
960
|
|
|
} |
961
|
|
|
|
962
|
|
|
/** |
963
|
|
|
* @group DDC-3045 |
964
|
|
|
*/ |
965
|
|
|
public function testMatchingInjectionPrevented() |
966
|
|
|
{ |
967
|
|
|
$this->expectException(ORMException::class); |
968
|
|
|
$this->expectExceptionMessage('Unrecognized field: '); |
969
|
|
|
|
970
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
971
|
|
|
$result = $repository->matching(new Criteria( |
972
|
|
|
Criteria::expr()->eq('username = ?; DELETE FROM cms_users; SELECT 1 WHERE 1', 'beberlei') |
973
|
|
|
)); |
974
|
|
|
|
975
|
|
|
// Because repository returns a lazy collection, we call toArray to force initialization |
976
|
|
|
$result->toArray(); |
977
|
|
|
} |
978
|
|
|
|
979
|
|
|
/** |
980
|
|
|
* @group DDC-3045 |
981
|
|
|
*/ |
982
|
|
|
public function testFindInjectionPrevented() |
983
|
|
|
{ |
984
|
|
|
$this->expectException(ORMException::class); |
985
|
|
|
$this->expectExceptionMessage('Unrecognized identifier fields: '); |
986
|
|
|
|
987
|
|
|
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); |
988
|
|
|
$repository->find(array('username = ?; DELETE FROM cms_users; SELECT 1 WHERE 1' => 'test', 'id' => 1)); |
989
|
|
|
} |
990
|
|
|
|
991
|
|
|
/** |
992
|
|
|
* @group DDC-3056 |
993
|
|
|
*/ |
994
|
|
|
public function testFindByNullValueInInCondition() |
995
|
|
|
{ |
996
|
|
|
$user1 = new CmsUser(); |
997
|
|
|
$user2 = new CmsUser(); |
998
|
|
|
|
999
|
|
|
$user1->username = 'ocramius'; |
1000
|
|
|
$user1->name = 'Marco'; |
1001
|
|
|
$user2->status = null; |
1002
|
|
|
$user2->username = 'deeky666'; |
1003
|
|
|
$user2->name = 'Steve'; |
1004
|
|
|
$user2->status = 'dbal maintainer'; |
1005
|
|
|
|
1006
|
|
|
$this->_em->persist($user1); |
1007
|
|
|
$this->_em->persist($user2); |
1008
|
|
|
$this->_em->flush(); |
1009
|
|
|
|
1010
|
|
|
$users = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser')->findBy(array('status' => array(null))); |
1011
|
|
|
|
1012
|
|
|
$this->assertCount(1, $users); |
1013
|
|
|
$this->assertSame($user1, reset($users)); |
1014
|
|
|
} |
1015
|
|
|
|
1016
|
|
|
/** |
1017
|
|
|
* @group DDC-3056 |
1018
|
|
|
*/ |
1019
|
|
|
public function testFindByNullValueInMultipleInCriteriaValues() |
1020
|
|
|
{ |
1021
|
|
|
$user1 = new CmsUser(); |
1022
|
|
|
$user2 = new CmsUser(); |
1023
|
|
|
|
1024
|
|
|
$user1->username = 'ocramius'; |
1025
|
|
|
$user1->name = 'Marco'; |
1026
|
|
|
$user2->status = null; |
1027
|
|
|
$user2->username = 'deeky666'; |
1028
|
|
|
$user2->name = 'Steve'; |
1029
|
|
|
$user2->status = 'dbal maintainer'; |
1030
|
|
|
|
1031
|
|
|
$this->_em->persist($user1); |
1032
|
|
|
$this->_em->persist($user2); |
1033
|
|
|
$this->_em->flush(); |
1034
|
|
|
|
1035
|
|
|
$users = $this |
1036
|
|
|
->_em |
1037
|
|
|
->getRepository('Doctrine\Tests\Models\CMS\CmsUser') |
1038
|
|
|
->findBy(array('status' => array('foo', null))); |
1039
|
|
|
|
1040
|
|
|
$this->assertCount(1, $users); |
1041
|
|
|
$this->assertSame($user1, reset($users)); |
1042
|
|
|
} |
1043
|
|
|
|
1044
|
|
|
/** |
1045
|
|
|
* @group DDC-3056 |
1046
|
|
|
*/ |
1047
|
|
|
public function testFindMultipleByNullValueInMultipleInCriteriaValues() |
1048
|
|
|
{ |
1049
|
|
|
$user1 = new CmsUser(); |
1050
|
|
|
$user2 = new CmsUser(); |
1051
|
|
|
|
1052
|
|
|
$user1->username = 'ocramius'; |
1053
|
|
|
$user1->name = 'Marco'; |
1054
|
|
|
$user2->status = null; |
1055
|
|
|
$user2->username = 'deeky666'; |
1056
|
|
|
$user2->name = 'Steve'; |
1057
|
|
|
$user2->status = 'dbal maintainer'; |
1058
|
|
|
|
1059
|
|
|
$this->_em->persist($user1); |
1060
|
|
|
$this->_em->persist($user2); |
1061
|
|
|
$this->_em->flush(); |
1062
|
|
|
|
1063
|
|
|
$users = $this |
1064
|
|
|
->_em |
1065
|
|
|
->getRepository('Doctrine\Tests\Models\CMS\CmsUser') |
1066
|
|
|
->findBy(array('status' => array('dbal maintainer', null))); |
1067
|
|
|
|
1068
|
|
|
$this->assertCount(2, $users); |
1069
|
|
|
|
1070
|
|
|
foreach ($users as $user) { |
1071
|
|
|
$this->assertTrue(in_array($user, array($user1, $user2))); |
1072
|
|
|
} |
1073
|
|
|
} |
1074
|
|
|
} |
1075
|
|
|
|
1076
|
|
|
|