Failed Conditions
Pull Request — develop (#6935)
by Michael
65:23
created

loadTweetFixture()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
c 0
b 0
f 0
rs 9.2
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Functional;
6
7
use Doctrine\Common\Collections\Criteria;
8
use Doctrine\ORM\LazyCriteriaCollection;
9
use Doctrine\Tests\Models\Quote\Group;
10
use Doctrine\Tests\Models\Quote\User as QuoteUser;
11
use Doctrine\Tests\Models\Tweet\Tweet;
12
use Doctrine\Tests\Models\Tweet\User;
13
use Doctrine\Tests\Models\Tweet\User as TweetUser;
14
use Doctrine\Tests\OrmFunctionalTestCase;
15
16
/**
17
 * @author Michaël Gallego <[email protected]>
18
 */
19
class PersistentCollectionCriteriaTest extends OrmFunctionalTestCase
20
{
21
    protected function setUp()
22
    {
23
        $this->useModelSet('tweet');
24
        $this->useModelSet('quote');
25
26
        parent::setUp();
27
    }
28
29
    public function loadTweetFixture()
30
    {
31
        $author = new TweetUser();
32
        $author->name = 'ngal';
33
        $this->em->persist($author);
34
35
        $tweet1 = new Tweet();
36
        $tweet1->content = 'Foo';
37
        $author->addTweet($tweet1);
38
39
        $tweet2 = new Tweet();
40
        $tweet2->content = 'Bar';
41
        $author->addTweet($tweet2);
42
43
        $this->em->flush();
44
45
        unset($author);
46
        unset($tweet1);
47
        unset($tweet2);
48
49
        $this->em->clear();
50
    }
51
52
    public function loadQuoteFixture()
53
    {
54
        $user = new QuoteUser();
55
        $user->name = 'mgal';
56
        $this->em->persist($user);
57
58
        $quote1 = new Group('quote1');
59
        $user->groups->add($quote1);
60
61
        $quote2 = new Group('quote2');
62
        $user->groups->add($quote2);
63
64
        $this->em->flush();
65
66
        $this->em->clear();
67
    }
68
69
    public function testCanCountWithoutLoadingPersistentCollection()
70
    {
71
        $this->loadTweetFixture();
72
73
        $repository = $this->em->getRepository(User::class);
74
75
        $user   = $repository->findOneBy(['name' => 'ngal']);
76
        $tweets = $user->tweets->matching(new Criteria());
77
78
        self::assertInstanceOf(LazyCriteriaCollection::class, $tweets);
79
        self::assertFalse($tweets->isInitialized());
80
        self::assertCount(2, $tweets);
81
        self::assertFalse($tweets->isInitialized());
82
83
        // Make sure it works with constraints
84
        $tweets = $user->tweets->matching(new Criteria(
85
            Criteria::expr()->eq('content', 'Foo')
86
        ));
87
88
        self::assertInstanceOf(LazyCriteriaCollection::class, $tweets);
89
        self::assertFalse($tweets->isInitialized());
90
        self::assertCount(1, $tweets);
91
        self::assertFalse($tweets->isInitialized());
92
    }
93
}
94