Completed
Pull Request — master (#6003)
by Javier
09:53
created

EntityRepositoryTest::testIsNullCriteria()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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