Failed Conditions
Push — master ( 6744b4...2b8acb )
by Marco
60:45 queued 60:36
created

Tests/ORM/Functional/EntityRepositoryTest.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Doctrine\Tests\ORM\Functional;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Criteria;
7
use Doctrine\DBAL\Connection;
8
use Doctrine\DBAL\LockMode;
9
use Doctrine\ORM\EntityRepository;
10
use Doctrine\ORM\OptimisticLockException;
11
use Doctrine\ORM\ORMException;
12
use Doctrine\ORM\Query;
13
use Doctrine\ORM\TransactionRequiredException;
14
use Doctrine\Tests\Models\CMS\CmsAddress;
15
use Doctrine\Tests\Models\CMS\CmsEmail;
16
use Doctrine\Tests\Models\CMS\CmsUser;
17
use Doctrine\Tests\Models\DDC753\DDC753CustomRepository;
18
use Doctrine\Tests\Models\DDC753\DDC753DefaultRepository;
19
use Doctrine\Tests\Models\DDC753\DDC753EntityWithCustomRepository;
20
use Doctrine\Tests\Models\DDC753\DDC753EntityWithDefaultCustomRepository;
21
use Doctrine\Tests\Models\DDC753\DDC753InvalidRepository;
22
use Doctrine\Tests\OrmFunctionalTestCase;
23
24
/**
25
 * @author robo
26
 */
27
class EntityRepositoryTest extends OrmFunctionalTestCase
28
{
29
    protected function setUp()
30
    {
31
        $this->useModelSet('cms');
32
        parent::setUp();
33
    }
34
35
    public function tearDown()
36
    {
37
        if ($this->_em) {
38
            $this->_em->getConfiguration()->setEntityNamespaces([]);
39
        }
40
        parent::tearDown();
41
    }
42
43
    public function loadFixture()
44
    {
45
        $user = new CmsUser;
46
        $user->name = 'Roman';
47
        $user->username = 'romanb';
48
        $user->status = 'freak';
49
        $this->_em->persist($user);
50
51
        $user2 = new CmsUser;
52
        $user2->name = 'Guilherme';
53
        $user2->username = 'gblanco';
54
        $user2->status = 'dev';
55
        $this->_em->persist($user2);
56
57
        $user3 = new CmsUser;
58
        $user3->name = 'Benjamin';
59
        $user3->username = 'beberlei';
60
        $user3->status = null;
61
        $this->_em->persist($user3);
62
63
        $user4 = new CmsUser;
64
        $user4->name = 'Alexander';
65
        $user4->username = 'asm89';
66
        $user4->status = 'dev';
67
        $this->_em->persist($user4);
68
69
        $this->_em->flush();
70
71
        $user1Id = $user->getId();
72
73
        unset($user);
74
        unset($user2);
75
        unset($user3);
76
        unset($user4);
77
78
        $this->_em->clear();
79
80
        return $user1Id;
81
    }
82
83
    public function loadAssociatedFixture()
84
    {
85
        $address = new CmsAddress();
86
        $address->city = "Berlin";
87
        $address->country = "Germany";
88
        $address->street = "Foostreet";
89
        $address->zip = "12345";
90
91
        $user = new CmsUser();
92
        $user->name = 'Roman';
93
        $user->username = 'romanb';
94
        $user->status = 'freak';
95
        $user->setAddress($address);
96
97
        $this->_em->persist($user);
98
        $this->_em->persist($address);
99
        $this->_em->flush();
100
        $this->_em->clear();
101
102
        return [$user->id, $address->id];
103
    }
104
105
    public function loadFixtureUserEmail()
106
    {
107
        $user1 = new CmsUser();
108
        $user2 = new CmsUser();
109
        $user3 = new CmsUser();
110
111
        $email1 = new CmsEmail();
112
        $email2 = new CmsEmail();
113
        $email3 = new CmsEmail();
114
115
        $user1->name     = 'Test 1';
116
        $user1->username = 'test1';
117
        $user1->status   = 'active';
118
119
        $user2->name     = 'Test 2';
120
        $user2->username = 'test2';
121
        $user2->status   = 'active';
122
123
        $user3->name     = 'Test 3';
124
        $user3->username = 'test3';
125
        $user3->status   = 'active';
126
127
        $email1->email   = '[email protected]';
128
        $email2->email   = '[email protected]';
129
        $email3->email   = '[email protected]';
130
131
        $user1->setEmail($email1);
132
        $user2->setEmail($email2);
133
        $user3->setEmail($email3);
134
135
        $this->_em->persist($user1);
136
        $this->_em->persist($user2);
137
        $this->_em->persist($user3);
138
139
        $this->_em->persist($email1);
140
        $this->_em->persist($email2);
141
        $this->_em->persist($email3);
142
143
        $this->_em->flush();
144
        $this->_em->clear();
145
146
        return [$user1, $user2, $user3];
147
    }
148
149
    public function buildUser($name, $username, $status, $address)
150
    {
151
        $user = new CmsUser();
152
        $user->name     = $name;
153
        $user->username = $username;
154
        $user->status   = $status;
155
        $user->setAddress($address);
156
157
        $this->_em->persist($user);
158
        $this->_em->flush();
159
160
        return $user;
161
    }
162
163
    public function buildAddress($country, $city, $street, $zip)
164
    {
165
        $address = new CmsAddress();
166
        $address->country = $country;
167
        $address->city    = $city;
168
        $address->street  = $street;
169
        $address->zip     = $zip;
170
171
        $this->_em->persist($address);
172
        $this->_em->flush();
173
174
        return $address;
175
    }
176
177 View Code Duplication
    public function testBasicFind()
178
    {
179
        $user1Id = $this->loadFixture();
180
        $repos = $this->_em->getRepository(CmsUser::class);
181
182
        $user = $repos->find($user1Id);
183
        $this->assertInstanceOf(CmsUser::class,$user);
184
        $this->assertEquals('Roman', $user->name);
185
        $this->assertEquals('freak', $user->status);
186
    }
187
188 View Code Duplication
    public function testFindByField()
189
    {
190
        $user1Id = $this->loadFixture();
191
        $repos = $this->_em->getRepository(CmsUser::class);
192
193
        $users = $repos->findBy(['status' => 'dev']);
194
        $this->assertEquals(2, count($users));
195
        $this->assertInstanceOf(CmsUser::class,$users[0]);
196
        $this->assertEquals('Guilherme', $users[0]->name);
197
        $this->assertEquals('dev', $users[0]->status);
198
    }
199
200 View Code Duplication
    public function testFindByAssociationWithIntegerAsParameter()
201
    {
202
        $address1 = $this->buildAddress('Germany', 'Berlim', 'Foo st.', '123456');
203
        $user1    = $this->buildUser('Benjamin', 'beberlei', 'dev', $address1);
204
205
        $address2 = $this->buildAddress('Brazil', 'São Paulo', 'Bar st.', '654321');
206
        $user2    = $this->buildUser('Guilherme', 'guilhermeblanco', 'freak', $address2);
207
208
        $address3 = $this->buildAddress('USA', 'Nashville', 'Woo st.', '321654');
209
        $user3    = $this->buildUser('Jonathan', 'jwage', 'dev', $address3);
210
211
        unset($address1);
212
        unset($address2);
213
        unset($address3);
214
215
        $this->_em->clear();
216
217
        $repository = $this->_em->getRepository(CmsAddress::class);
218
        $addresses  = $repository->findBy(['user' => [$user1->getId(), $user2->getId()]]);
219
220
        $this->assertEquals(2, count($addresses));
221
        $this->assertInstanceOf(CmsAddress::class,$addresses[0]);
222
    }
223
224 View Code Duplication
    public function testFindByAssociationWithObjectAsParameter()
225
    {
226
        $address1 = $this->buildAddress('Germany', 'Berlim', 'Foo st.', '123456');
227
        $user1    = $this->buildUser('Benjamin', 'beberlei', 'dev', $address1);
228
229
        $address2 = $this->buildAddress('Brazil', 'São Paulo', 'Bar st.', '654321');
230
        $user2    = $this->buildUser('Guilherme', 'guilhermeblanco', 'freak', $address2);
231
232
        $address3 = $this->buildAddress('USA', 'Nashville', 'Woo st.', '321654');
233
        $user3    = $this->buildUser('Jonathan', 'jwage', 'dev', $address3);
234
235
        unset($address1);
236
        unset($address2);
237
        unset($address3);
238
239
        $this->_em->clear();
240
241
        $repository = $this->_em->getRepository(CmsAddress::class);
242
        $addresses  = $repository->findBy(['user' => [$user1, $user2]]);
243
244
        $this->assertEquals(2, count($addresses));
245
        $this->assertInstanceOf(CmsAddress::class,$addresses[0]);
246
    }
247
248 View Code Duplication
    public function testFindFieldByMagicCall()
249
    {
250
        $user1Id = $this->loadFixture();
251
        $repos = $this->_em->getRepository(CmsUser::class);
252
253
        $users = $repos->findByStatus('dev');
254
        $this->assertEquals(2, count($users));
255
        $this->assertInstanceOf(CmsUser::class,$users[0]);
256
        $this->assertEquals('Guilherme', $users[0]->name);
257
        $this->assertEquals('dev', $users[0]->status);
258
    }
259
260 View Code Duplication
    public function testFindAll()
261
    {
262
        $user1Id = $this->loadFixture();
263
        $repos = $this->_em->getRepository(CmsUser::class);
264
265
        $users = $repos->findAll();
266
        $this->assertEquals(4, count($users));
267
    }
268
269
    public function testFindByAlias()
270
    {
271
        $user1Id = $this->loadFixture();
272
        $repos = $this->_em->getRepository(CmsUser::class);
273
274
        $this->_em->getConfiguration()->addEntityNamespace('CMS', 'Doctrine\Tests\Models\CMS');
275
276
        $repos = $this->_em->getRepository('CMS:CmsUser');
277
278
        $users = $repos->findAll();
279
        $this->assertEquals(4, count($users));
280
    }
281
282
    public function testCount()
283
    {
284
        $this->loadFixture();
285
        $repos = $this->_em->getRepository(CmsUser::class);
286
287
        $userCount = $repos->count([]);
288
        $this->assertSame(4, $userCount);
289
290
        $userCount = $repos->count(['status' => 'dev']);
291
        $this->assertSame(2, $userCount);
292
293
        $userCount = $repos->count(['status' => 'nonexistent']);
294
        $this->assertSame(0, $userCount);
295
    }
296
297
    public function testCountBy()
298
    {
299
        $this->loadFixture();
300
        $repos = $this->_em->getRepository(CmsUser::class);
301
302
        $userCount = $repos->countByStatus('dev');
303
        $this->assertSame(2, $userCount);
304
    }
305
306
    /**
307
     * @expectedException \Doctrine\ORM\ORMException
308
     */
309
    public function testExceptionIsThrownWhenCallingFindByWithoutParameter() {
310
        $this->_em->getRepository(CmsUser::class)
311
                  ->findByStatus();
312
    }
313
314
    /**
315
     * @expectedException \Doctrine\ORM\ORMException
316
     */
317
    public function testExceptionIsThrownWhenUsingInvalidFieldName() {
318
        $this->_em->getRepository(CmsUser::class)
319
                  ->findByThisFieldDoesNotExist('testvalue');
320
    }
321
322
    /**
323
     * @group locking
324
     * @group DDC-178
325
     */
326
    public function testPessimisticReadLockWithoutTransaction_ThrowsException()
327
    {
328
        $this->expectException(TransactionRequiredException::class);
329
330
        $this->_em->getRepository(CmsUser::class)
331
                  ->find(1, LockMode::PESSIMISTIC_READ);
332
    }
333
334
    /**
335
     * @group locking
336
     * @group DDC-178
337
     */
338
    public function testPessimisticWriteLockWithoutTransaction_ThrowsException()
339
    {
340
        $this->expectException(TransactionRequiredException::class);
341
342
        $this->_em->getRepository(CmsUser::class)
343
                  ->find(1, LockMode::PESSIMISTIC_WRITE);
344
    }
345
346
    /**
347
     * @group locking
348
     * @group DDC-178
349
     */
350
    public function testOptimisticLockUnversionedEntity_ThrowsException()
351
    {
352
        $this->expectException(OptimisticLockException::class);
353
354
        $this->_em->getRepository(CmsUser::class)
355
                  ->find(1, LockMode::OPTIMISTIC);
356
    }
357
358
    /**
359
     * @group locking
360
     * @group DDC-178
361
     */
362
    public function testIdentityMappedOptimisticLockUnversionedEntity_ThrowsException()
363
    {
364
        $user = new CmsUser;
365
        $user->name = 'Roman';
366
        $user->username = 'romanb';
367
        $user->status = 'freak';
368
        $this->_em->persist($user);
369
        $this->_em->flush();
370
371
        $userId = $user->id;
372
373
        $this->_em->find(CmsUser::class, $userId);
374
375
        $this->expectException(OptimisticLockException::class);
376
377
        $this->_em->find(CmsUser::class, $userId, LockMode::OPTIMISTIC);
378
    }
379
380
    /**
381
     * @group DDC-819
382
     */
383 View Code Duplication
    public function testFindMagicCallByNullValue()
384
    {
385
        $this->loadFixture();
386
387
        $repos = $this->_em->getRepository(CmsUser::class);
388
389
        $users = $repos->findByStatus(null);
390
        $this->assertEquals(1, count($users));
391
    }
392
393
    /**
394
     * @group DDC-819
395
     */
396
    public function testInvalidMagicCall()
397
    {
398
        $this->expectException(\BadMethodCallException::class);
399
400
        $repos = $this->_em->getRepository(CmsUser::class);
401
        $repos->foo();
402
    }
403
404
    /**
405
     * @group DDC-817
406
     */
407
    public function testFindByAssociationKey_ExceptionOnInverseSide()
408
    {
409
        list($userId, $addressId) = $this->loadAssociatedFixture();
410
        $repos = $this->_em->getRepository(CmsUser::class);
411
412
        $this->expectException(ORMException::class);
413
        $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.");
414
415
        $user = $repos->findBy(['address' => $addressId]);
416
    }
417
418
    /**
419
     * @group DDC-817
420
     */
421 View Code Duplication
    public function testFindOneByAssociationKey()
422
    {
423
        list($userId, $addressId) = $this->loadAssociatedFixture();
424
        $repos = $this->_em->getRepository(CmsAddress::class);
425
        $address = $repos->findOneBy(['user' => $userId]);
426
427
        $this->assertInstanceOf(CmsAddress::class, $address);
428
        $this->assertEquals($addressId, $address->id);
429
    }
430
431
    /**
432
     * @group DDC-1241
433
     */
434
    public function testFindOneByOrderBy()
435
    {
436
    	$this->loadFixture();
437
438
    	$repos = $this->_em->getRepository(CmsUser::class);
439
    	$userAsc = $repos->findOneBy([], ["username" => "ASC"]);
440
    	$userDesc = $repos->findOneBy([], ["username" => "DESC"]);
441
442
    	$this->assertNotSame($userAsc, $userDesc);
443
    }
444
445
    /**
446
     * @group DDC-817
447
     */
448 View Code Duplication
    public function testFindByAssociationKey()
449
    {
450
        list($userId, $addressId) = $this->loadAssociatedFixture();
451
        $repos = $this->_em->getRepository(CmsAddress::class);
452
        $addresses = $repos->findBy(['user' => $userId]);
453
454
        $this->assertContainsOnly(CmsAddress::class, $addresses);
455
        $this->assertEquals(1, count($addresses));
456
        $this->assertEquals($addressId, $addresses[0]->id);
457
    }
458
459
    /**
460
     * @group DDC-817
461
     */
462 View Code Duplication
    public function testFindAssociationByMagicCall()
463
    {
464
        list($userId, $addressId) = $this->loadAssociatedFixture();
465
        $repos = $this->_em->getRepository(CmsAddress::class);
466
        $addresses = $repos->findByUser($userId);
467
468
        $this->assertContainsOnly(CmsAddress::class, $addresses);
469
        $this->assertEquals(1, count($addresses));
470
        $this->assertEquals($addressId, $addresses[0]->id);
471
    }
472
473
    /**
474
     * @group DDC-817
475
     */
476
    public function testFindOneAssociationByMagicCall()
477
    {
478
        list($userId, $addressId) = $this->loadAssociatedFixture();
479
        $repos = $this->_em->getRepository(CmsAddress::class);
480
        $address = $repos->findOneByUser($userId);
481
482
        $this->assertInstanceOf(CmsAddress::class, $address);
483
        $this->assertEquals($addressId, $address->id);
484
    }
485
486
    public function testValidNamedQueryRetrieval()
487
    {
488
        $repos = $this->_em->getRepository(CmsUser::class);
489
490
        $query = $repos->createNamedQuery('all');
491
492
        $this->assertInstanceOf(Query::class, $query);
493
        $this->assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $query->getDQL());
494
    }
495
496
    public function testInvalidNamedQueryRetrieval()
497
    {
498
        $repos = $this->_em->getRepository(CmsUser::class);
499
500
        $this->expectException(\Doctrine\ORM\Mapping\MappingException::class);
501
502
        $repos->createNamedQuery('invalidNamedQuery');
503
    }
504
505
    /**
506
     * @group DDC-1087
507
     */
508
    public function testIsNullCriteriaDoesNotGenerateAParameter()
509
    {
510
        $repos = $this->_em->getRepository(CmsUser::class);
511
        $users = $repos->findBy(['status' => null, 'username' => 'romanb']);
512
513
        $params = $this->_sqlLoggerStack->queries[$this->_sqlLoggerStack->currentQuery]['params'];
514
        $this->assertEquals(1, count($params), "Should only execute with one parameter.");
515
        $this->assertEquals(['romanb'], $params);
516
    }
517
518 View Code Duplication
    public function testIsNullCriteria()
519
    {
520
        $this->loadFixture();
521
522
        $repos = $this->_em->getRepository(CmsUser::class);
523
524
        $users = $repos->findBy(['status' => null]);
525
        $this->assertEquals(1, count($users));
526
    }
527
528
    /**
529
     * @group DDC-1094
530
     */
531
    public function testFindByLimitOffset()
532
    {
533
        $this->loadFixture();
534
535
        $repos = $this->_em->getRepository(CmsUser::class);
536
537
        $users1 = $repos->findBy([], null, 1, 0);
538
        $users2 = $repos->findBy([], null, 1, 1);
539
540
        $this->assertEquals(4, count($repos->findBy([])));
541
        $this->assertEquals(1, count($users1));
542
        $this->assertEquals(1, count($users2));
543
        $this->assertNotSame($users1[0], $users2[0]);
544
    }
545
546
    /**
547
     * @group DDC-1094
548
     */
549
    public function testFindByOrderBy()
550
    {
551
        $this->loadFixture();
552
553
        $repos = $this->_em->getRepository(CmsUser::class);
554
        $usersAsc = $repos->findBy([], ["username" => "ASC"]);
555
        $usersDesc = $repos->findBy([], ["username" => "DESC"]);
556
557
        $this->assertEquals(4, count($usersAsc), "Pre-condition: only four users in fixture");
558
        $this->assertEquals(4, count($usersDesc), "Pre-condition: only four users in fixture");
559
        $this->assertSame($usersAsc[0], $usersDesc[3]);
560
        $this->assertSame($usersAsc[3], $usersDesc[0]);
561
    }
562
563
    /**
564
     * @group DDC-1376
565
     */
566
    public function testFindByOrderByAssociation()
567
    {
568
        $this->loadFixtureUserEmail();
569
570
        $repository = $this->_em->getRepository(CmsUser::class);
571
        $resultAsc  = $repository->findBy([], ['email' => 'ASC']);
572
        $resultDesc = $repository->findBy([], ['email' => 'DESC']);
573
574
        $this->assertCount(3, $resultAsc);
575
        $this->assertCount(3, $resultDesc);
576
577
        $this->assertEquals($resultAsc[0]->getEmail()->getId(), $resultDesc[2]->getEmail()->getId());
578
        $this->assertEquals($resultAsc[2]->getEmail()->getId(), $resultDesc[0]->getEmail()->getId());
579
    }
580
581
    /**
582
     * @group DDC-1426
583
     */
584
    public function testFindFieldByMagicCallOrderBy()
585
    {
586
        $this->loadFixture();
587
        $repos = $this->_em->getRepository(CmsUser::class);
588
589
        $usersAsc = $repos->findByStatus('dev', ['username' => "ASC"]);
0 ignored issues
show
Documentation Bug introduced by
The method findByStatus does not exist on object<Doctrine\ORM\EntityRepository>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
590
        $usersDesc = $repos->findByStatus('dev', ['username' => "DESC"]);
0 ignored issues
show
Documentation Bug introduced by
The method findByStatus does not exist on object<Doctrine\ORM\EntityRepository>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
591
592
        $this->assertEquals(2, count($usersAsc));
593
        $this->assertEquals(2, count($usersDesc));
594
595
        $this->assertInstanceOf(CmsUser::class,$usersAsc[0]);
596
        $this->assertEquals('Alexander', $usersAsc[0]->name);
597
        $this->assertEquals('dev', $usersAsc[0]->status);
598
599
        $this->assertSame($usersAsc[0], $usersDesc[1]);
600
        $this->assertSame($usersAsc[1], $usersDesc[0]);
601
    }
602
603
    /**
604
     * @group DDC-1426
605
     */
606
    public function testFindFieldByMagicCallLimitOffset()
607
    {
608
        $this->loadFixture();
609
        $repos = $this->_em->getRepository(CmsUser::class);
610
611
        $users1 = $repos->findByStatus('dev', [], 1, 0);
612
        $users2 = $repos->findByStatus('dev', [], 1, 1);
613
614
        $this->assertEquals(1, count($users1));
615
        $this->assertEquals(1, count($users2));
616
        $this->assertNotSame($users1[0], $users2[0]);
617
    }
618
619
    /**
620
     * @group DDC-753
621
     */
622
    public function testDefaultRepositoryClassName()
623
    {
624
        $this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), EntityRepository::class);
625
        $this->_em->getConfiguration()->setDefaultRepositoryClassName(DDC753DefaultRepository::class);
626
        $this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), DDC753DefaultRepository::class);
627
628
        $repos = $this->_em->getRepository(DDC753EntityWithDefaultCustomRepository::class);
629
        $this->assertInstanceOf(DDC753DefaultRepository::class, $repos);
630
        $this->assertTrue($repos->isDefaultRepository());
631
632
633
        $repos = $this->_em->getRepository(DDC753EntityWithCustomRepository::class);
634
        $this->assertInstanceOf(DDC753CustomRepository::class, $repos);
635
        $this->assertTrue($repos->isCustomRepository());
636
637
        $this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), DDC753DefaultRepository::class);
638
        $this->_em->getConfiguration()->setDefaultRepositoryClassName(EntityRepository::class);
639
        $this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), EntityRepository::class);
640
641
    }
642
643
    /**
644
     * @group DDC-753
645
     * @expectedException Doctrine\ORM\ORMException
646
     * @expectedExceptionMessage Invalid repository class 'Doctrine\Tests\Models\DDC753\DDC753InvalidRepository'. It must be a Doctrine\Common\Persistence\ObjectRepository.
647
     */
648
    public function testSetDefaultRepositoryInvalidClassError()
649
    {
650
        $this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), EntityRepository::class);
651
        $this->_em->getConfiguration()->setDefaultRepositoryClassName(DDC753InvalidRepository::class);
652
    }
653
654
    /**
655
     * @group DDC-3257
656
     */
657
    public function testSingleRepositoryInstanceForDifferentEntityAliases()
658
    {
659
        $config = $this->_em->getConfiguration();
660
661
        $config->addEntityNamespace('Aliased', 'Doctrine\Tests\Models\CMS');
662
        $config->addEntityNamespace('AliasedAgain', 'Doctrine\Tests\Models\CMS');
663
664
        $repository = $this->_em->getRepository(CmsUser::class);
665
666
        $this->assertSame($repository, $this->_em->getRepository('Aliased:CmsUser'));
667
        $this->assertSame($repository, $this->_em->getRepository('AliasedAgain:CmsUser'));
668
    }
669
670
    /**
671
     * @group DDC-3257
672
     */
673
    public function testCanRetrieveRepositoryFromClassNameWithLeadingBackslash()
674
    {
675
        $this->assertSame(
676
            $this->_em->getRepository('\\' . CmsUser::class),
677
            $this->_em->getRepository(CmsUser::class)
678
        );
679
    }
680
681
    /**
682
     * @group DDC-1376
683
     *
684
     * @expectedException Doctrine\ORM\ORMException
685
     * @expectedExceptionMessage You cannot search for the association field 'Doctrine\Tests\Models\CMS\CmsUser#address', because it is the inverse side of an association.
686
     */
687
    public function testInvalidOrderByAssociation()
688
    {
689
        $this->_em->getRepository(CmsUser::class)
690
            ->findBy(['status' => 'test'], ['address' => 'ASC']);
691
    }
692
693
    /**
694
     * @group DDC-1500
695
     */
696 View Code Duplication
    public function testInvalidOrientation()
697
    {
698
        $this->expectException(ORMException::class);
699
        $this->expectExceptionMessage('Invalid order by orientation specified for Doctrine\Tests\Models\CMS\CmsUser#username');
700
701
        $repo = $this->_em->getRepository(CmsUser::class);
702
        $repo->findBy(['status' => 'test'], ['username' => 'INVALID']);
703
    }
704
705
    /**
706
     * @group DDC-1713
707
     */
708
    public function testFindByAssociationArray()
709
    {
710
        $repo = $this->_em->getRepository(CmsAddress::class);
711
        $data = $repo->findBy(['user' => [1, 2, 3]]);
712
713
        $query = array_pop($this->_sqlLoggerStack->queries);
714
        $this->assertEquals([1,2,3], $query['params'][0]);
715
        $this->assertEquals(Connection::PARAM_INT_ARRAY, $query['types'][0]);
716
    }
717
718
    /**
719
     * @group DDC-1637
720
     */
721 View Code Duplication
    public function testMatchingEmptyCriteria()
722
    {
723
        $this->loadFixture();
724
725
        $repository = $this->_em->getRepository(CmsUser::class);
726
        $users = $repository->matching(new Criteria());
727
728
        $this->assertEquals(4, count($users));
729
    }
730
731
    /**
732
     * @group DDC-1637
733
     */
734
    public function testMatchingCriteriaEqComparison()
735
    {
736
        $this->loadFixture();
737
738
        $repository = $this->_em->getRepository(CmsUser::class);
739
        $users = $repository->matching(new Criteria(
740
            Criteria::expr()->eq('username', 'beberlei')
741
        ));
742
743
        $this->assertEquals(1, count($users));
744
    }
745
746
    /**
747
     * @group DDC-1637
748
     */
749 View Code Duplication
    public function testMatchingCriteriaNeqComparison()
750
    {
751
        $this->loadFixture();
752
753
        $repository = $this->_em->getRepository(CmsUser::class);
754
        $users = $repository->matching(new Criteria(
755
            Criteria::expr()->neq('username', 'beberlei')
756
        ));
757
758
        $this->assertEquals(3, count($users));
759
    }
760
761
    /**
762
     * @group DDC-1637
763
     */
764 View Code Duplication
    public function testMatchingCriteriaInComparison()
765
    {
766
        $this->loadFixture();
767
768
        $repository = $this->_em->getRepository(CmsUser::class);
769
        $users = $repository->matching(new Criteria(
770
            Criteria::expr()->in('username', ['beberlei', 'gblanco'])
771
        ));
772
773
        $this->assertEquals(2, count($users));
774
    }
775
776
    /**
777
     * @group DDC-1637
778
     */
779 View Code Duplication
    public function testMatchingCriteriaNotInComparison()
780
    {
781
        $this->loadFixture();
782
783
        $repository = $this->_em->getRepository(CmsUser::class);
784
        $users = $repository->matching(new Criteria(
785
            Criteria::expr()->notIn('username', ['beberlei', 'gblanco', 'asm89'])
786
        ));
787
788
        $this->assertEquals(1, count($users));
789
    }
790
791
    /**
792
     * @group DDC-1637
793
     */
794
    public function testMatchingCriteriaLtComparison()
795
    {
796
        $firstUserId = $this->loadFixture();
797
798
        $repository = $this->_em->getRepository(CmsUser::class);
799
        $users = $repository->matching(new Criteria(
800
            Criteria::expr()->lt('id', $firstUserId + 1)
801
        ));
802
803
        $this->assertEquals(1, count($users));
804
    }
805
806
    /**
807
     * @group DDC-1637
808
     */
809
    public function testMatchingCriteriaLeComparison()
810
    {
811
        $firstUserId = $this->loadFixture();
812
813
        $repository = $this->_em->getRepository(CmsUser::class);
814
        $users = $repository->matching(new Criteria(
815
            Criteria::expr()->lte('id', $firstUserId + 1)
816
        ));
817
818
        $this->assertEquals(2, count($users));
819
    }
820
821
    /**
822
     * @group DDC-1637
823
     */
824
    public function testMatchingCriteriaGtComparison()
825
    {
826
        $firstUserId = $this->loadFixture();
827
828
        $repository = $this->_em->getRepository(CmsUser::class);
829
        $users = $repository->matching(new Criteria(
830
            Criteria::expr()->gt('id', $firstUserId)
831
        ));
832
833
        $this->assertEquals(3, count($users));
834
    }
835
836
    /**
837
     * @group DDC-1637
838
     */
839
    public function testMatchingCriteriaGteComparison()
840
    {
841
        $firstUserId = $this->loadFixture();
842
843
        $repository = $this->_em->getRepository(CmsUser::class);
844
        $users = $repository->matching(new Criteria(
845
            Criteria::expr()->gte('id', $firstUserId)
846
        ));
847
848
        $this->assertEquals(4, count($users));
849
    }
850
851
    /**
852
     * @group DDC-2430
853
     */
854 View Code Duplication
    public function testMatchingCriteriaAssocationByObjectInMemory()
855
    {
856
        list($userId, $addressId) = $this->loadAssociatedFixture();
857
858
        $user = $this->_em->find(CmsUser::class, $userId);
859
860
        $criteria = new Criteria(
861
            Criteria::expr()->eq('user', $user)
862
        );
863
864
        $repository = $this->_em->getRepository(CmsAddress::class);
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
    /**
875
     * @group DDC-2430
876
     */
877 View Code Duplication
    public function testMatchingCriteriaAssocationInWithArray()
878
    {
879
        list($userId, $addressId) = $this->loadAssociatedFixture();
880
881
        $user = $this->_em->find(CmsUser::class, $userId);
882
883
        $criteria = new Criteria(
884
            Criteria::expr()->in('user', [$user])
885
        );
886
887
        $repository = $this->_em->getRepository(CmsAddress::class);
888
        $addresses = $repository->matching($criteria);
889
890
        $this->assertEquals(1, count($addresses));
891
892
        $addresses = new ArrayCollection($repository->findAll());
893
894
        $this->assertEquals(1, count($addresses->matching($criteria)));
895
    }
896
897
    public function testMatchingCriteriaContainsComparison()
898
    {
899
        $this->loadFixture();
900
901
        $repository = $this->_em->getRepository(CmsUser::class);
902
903
        $users = $repository->matching(new Criteria(Criteria::expr()->contains('name', 'Foobar')));
904
        $this->assertEquals(0, count($users));
905
906
        $users = $repository->matching(new Criteria(Criteria::expr()->contains('name', 'Rom')));
907
        $this->assertEquals(1, count($users));
908
909
        $users = $repository->matching(new Criteria(Criteria::expr()->contains('status', 'dev')));
910
        $this->assertEquals(2, count($users));
911
    }
912
913 View Code Duplication
    public function testMatchingCriteriaStartsWithComparison()
914
    {
915
        $this->loadFixture();
916
917
        $repository = $this->_em->getRepository(CmsUser::class);
918
919
        $users = $repository->matching(new Criteria(Criteria::expr()->startsWith('name', 'Foo')));
920
        $this->assertCount(0, $users);
921
922
        $users = $repository->matching(new Criteria(Criteria::expr()->startsWith('name', 'R')));
923
        $this->assertCount(1, $users);
924
925
        $users = $repository->matching(new Criteria(Criteria::expr()->startsWith('status', 'de')));
926
        $this->assertCount(2, $users);
927
    }
928
929 View Code Duplication
    public function testMatchingCriteriaEndsWithComparison()
930
    {
931
        $this->loadFixture();
932
933
        $repository = $this->_em->getRepository(CmsUser::class);
934
935
        $users = $repository->matching(new Criteria(Criteria::expr()->endsWith('name', 'foo')));
936
        $this->assertCount(0, $users);
937
938
        $users = $repository->matching(new Criteria(Criteria::expr()->endsWith('name', 'oman')));
939
        $this->assertCount(1, $users);
940
941
        $users = $repository->matching(new Criteria(Criteria::expr()->endsWith('status', 'ev')));
942
        $this->assertCount(2, $users);
943
    }
944
945
    /**
946
     * @group DDC-2478
947
     */
948
    public function testMatchingCriteriaNullAssocComparison()
949
    {
950
        $fixtures       = $this->loadFixtureUserEmail();
951
        $user           = $this->_em->merge($fixtures[0]);
952
        $repository     = $this->_em->getRepository(CmsUser::class);
953
        $criteriaIsNull = Criteria::create()->where(Criteria::expr()->isNull('email'));
954
        $criteriaEqNull = Criteria::create()->where(Criteria::expr()->eq('email', null));
955
956
        $user->setEmail(null);
957
        $this->_em->persist($user);
958
        $this->_em->flush();
959
        $this->_em->clear();
960
961
        $usersIsNull = $repository->matching($criteriaIsNull);
962
        $usersEqNull = $repository->matching($criteriaEqNull);
963
964
        $this->assertCount(1, $usersIsNull);
965
        $this->assertCount(1, $usersEqNull);
966
967
        $this->assertInstanceOf(CmsUser::class, $usersIsNull[0]);
968
        $this->assertInstanceOf(CmsUser::class, $usersEqNull[0]);
969
970
        $this->assertNull($usersIsNull[0]->getEmail());
971
        $this->assertNull($usersEqNull[0]->getEmail());
972
    }
973
974
    /**
975
     * @group DDC-2055
976
     */
977
    public function testCreateResultSetMappingBuilder()
978
    {
979
        $repository = $this->_em->getRepository(CmsUser::class);
980
        $rsm = $repository->createResultSetMappingBuilder('u');
981
982
        $this->assertInstanceOf(Query\ResultSetMappingBuilder::class, $rsm);
983
        $this->assertEquals(['u' => CmsUser::class], $rsm->aliasMap);
984
    }
985
986
    /**
987
     * @group DDC-3045
988
     */
989 View Code Duplication
    public function testFindByFieldInjectionPrevented()
990
    {
991
        $this->expectException(ORMException::class);
992
        $this->expectExceptionMessage('Unrecognized field: ');
993
994
        $repository = $this->_em->getRepository(CmsUser::class);
995
        $repository->findBy(['username = ?; DELETE FROM cms_users; SELECT 1 WHERE 1' => 'test']);
996
    }
997
998
    /**
999
     * @group DDC-3045
1000
     */
1001 View Code Duplication
    public function testFindOneByFieldInjectionPrevented()
1002
    {
1003
        $this->expectException(ORMException::class);
1004
        $this->expectExceptionMessage('Unrecognized field: ');
1005
1006
        $repository = $this->_em->getRepository(CmsUser::class);
1007
        $repository->findOneBy(['username = ?; DELETE FROM cms_users; SELECT 1 WHERE 1' => 'test']);
1008
    }
1009
1010
    /**
1011
     * @group DDC-3045
1012
     */
1013
    public function testMatchingInjectionPrevented()
1014
    {
1015
        $this->expectException(ORMException::class);
1016
        $this->expectExceptionMessage('Unrecognized field: ');
1017
1018
        $repository = $this->_em->getRepository(CmsUser::class);
1019
        $result     = $repository->matching(new Criteria(
1020
            Criteria::expr()->eq('username = ?; DELETE FROM cms_users; SELECT 1 WHERE 1', 'beberlei')
1021
        ));
1022
1023
        // Because repository returns a lazy collection, we call toArray to force initialization
1024
        $result->toArray();
1025
    }
1026
1027
    /**
1028
     * @group DDC-3045
1029
     */
1030 View Code Duplication
    public function testFindInjectionPrevented()
1031
    {
1032
        $this->expectException(ORMException::class);
1033
        $this->expectExceptionMessage('Unrecognized identifier fields: ');
1034
1035
        $repository = $this->_em->getRepository(CmsUser::class);
1036
        $repository->find(['username = ?; DELETE FROM cms_users; SELECT 1 WHERE 1' => 'test', 'id' => 1]);
1037
    }
1038
1039
    /**
1040
     * @group DDC-3056
1041
     */
1042 View Code Duplication
    public function testFindByNullValueInInCondition()
1043
    {
1044
        $user1 = new CmsUser();
1045
        $user2 = new CmsUser();
1046
1047
        $user1->username = 'ocramius';
1048
        $user1->name = 'Marco';
1049
        $user2->status = null;
1050
        $user2->username = 'deeky666';
1051
        $user2->name = 'Steve';
1052
        $user2->status = 'dbal maintainer';
1053
1054
        $this->_em->persist($user1);
1055
        $this->_em->persist($user2);
1056
        $this->_em->flush();
1057
1058
        $users = $this->_em->getRepository(CmsUser::class)->findBy(['status' => [null]]);
1059
1060
        $this->assertCount(1, $users);
1061
        $this->assertSame($user1, reset($users));
1062
    }
1063
1064
    /**
1065
     * @group DDC-3056
1066
     */
1067 View Code Duplication
    public function testFindByNullValueInMultipleInCriteriaValues()
1068
    {
1069
        $user1 = new CmsUser();
1070
        $user2 = new CmsUser();
1071
1072
        $user1->username = 'ocramius';
1073
        $user1->name = 'Marco';
1074
        $user2->status = null;
1075
        $user2->username = 'deeky666';
1076
        $user2->name = 'Steve';
1077
        $user2->status = 'dbal maintainer';
1078
1079
        $this->_em->persist($user1);
1080
        $this->_em->persist($user2);
1081
        $this->_em->flush();
1082
1083
        $users = $this
1084
            ->_em
1085
            ->getRepository(CmsUser::class)
1086
            ->findBy(['status' => ['foo', null]]);
1087
1088
        $this->assertCount(1, $users);
1089
        $this->assertSame($user1, reset($users));
1090
    }
1091
1092
    /**
1093
     * @group DDC-3056
1094
     */
1095
    public function testFindMultipleByNullValueInMultipleInCriteriaValues()
1096
    {
1097
        $user1 = new CmsUser();
1098
        $user2 = new CmsUser();
1099
1100
        $user1->username = 'ocramius';
1101
        $user1->name = 'Marco';
1102
        $user2->status = null;
1103
        $user2->username = 'deeky666';
1104
        $user2->name = 'Steve';
1105
        $user2->status = 'dbal maintainer';
1106
1107
        $this->_em->persist($user1);
1108
        $this->_em->persist($user2);
1109
        $this->_em->flush();
1110
1111
        $users = $this
1112
            ->_em
1113
            ->getRepository(CmsUser::class)
1114
            ->findBy(['status' => ['dbal maintainer', null]]);
1115
1116
        $this->assertCount(2, $users);
1117
1118
        foreach ($users as $user) {
1119
            $this->assertTrue(in_array($user, [$user1, $user2]));
1120
        }
1121
    }
1122
}
1123
1124