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

testFindAssociationByMagicCall()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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