Failed Conditions
Pull Request — develop (#6935)
by Michael
167:08 queued 149:28
created

PersistentCollectionCriteriaTest::tearDown()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 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, $tweet1, $tweet2);
46
47
        $this->em->clear();
48
    }
49
50
    public function loadQuoteFixture()
51
    {
52
        $user = new QuoteUser();
53
        $user->name = 'mgal';
54
        $this->em->persist($user);
55
56
        $quote1 = new Group('quote1');
57
        $user->groups->add($quote1);
58
59
        $quote2 = new Group('quote2');
60
        $user->groups->add($quote2);
61
62
        $this->em->flush();
63
64
        $this->em->clear();
65
    }
66
67
    public function testCanCountWithoutLoadingPersistentCollection()
68
    {
69
        $this->loadTweetFixture();
70
71
        $repository = $this->em->getRepository(User::class);
72
73
        $user   = $repository->findOneBy(['name' => 'ngal']);
74
        $tweets = $user->tweets->matching(new Criteria());
75
76
        self::assertInstanceOf(LazyCriteriaCollection::class, $tweets);
77
        self::assertFalse($tweets->isInitialized());
78
        self::assertCount(2, $tweets);
79
        self::assertFalse($tweets->isInitialized());
0 ignored issues
show
Bug introduced by
The method isInitialized() does not exist on Countable. It seems like you code against a sub-type of Countable such as Doctrine\Common\Collections\AbstractLazyCollection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
        self::assertFalse($tweets->/** @scrutinizer ignore-call */ isInitialized());
Loading history...
Bug introduced by
The method isInitialized() does not exist on Traversable. It seems like you code against a sub-type of Traversable such as Doctrine\Common\Collections\AbstractLazyCollection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
        self::assertFalse($tweets->/** @scrutinizer ignore-call */ isInitialized());
Loading history...
80
81
        // Make sure it works with constraints
82
        $tweets = $user->tweets->matching(new Criteria(
83
            Criteria::expr()->eq('content', 'Foo')
84
        ));
85
86
        self::assertInstanceOf(LazyCriteriaCollection::class, $tweets);
87
        self::assertFalse($tweets->isInitialized());
88
        self::assertCount(1, $tweets);
89
        self::assertFalse($tweets->isInitialized());
90
    }
91
}
92