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

EntityRepositoryCriteriaTest::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\ORM\LazyCriteriaCollection;
8
use Doctrine\Tests\Models\Generic\DateTimeModel;
9
use Doctrine\Common\Collections\Criteria;
10
use Doctrine\Tests\Models\Tweet\Tweet;
11
use Doctrine\Tests\Models\Tweet\User;
12
use Doctrine\Tests\OrmFunctionalTestCase;
13
14
/**
15
 * @author Josiah <[email protected]>
16
 */
17
class EntityRepositoryCriteriaTest extends OrmFunctionalTestCase
18
{
19
    protected function setUp()
20
    {
21
        $this->useModelSet('generic');
22
        $this->useModelSet('tweet');
23
        parent::setUp();
24
    }
25
26
    public function loadFixture()
27
    {
28
        $today = new DateTimeModel();
29
        $today->datetime =
30
        $today->date =
31
        $today->time =
32
            new \DateTime('today');
33
        $this->em->persist($today);
34
35
        $tomorrow = new DateTimeModel();
36
        $tomorrow->datetime =
37
        $tomorrow->date =
38
        $tomorrow->time =
39
            new \DateTime('tomorrow');
40
        $this->em->persist($tomorrow);
41
42
        $yesterday = new DateTimeModel();
43
        $yesterday->datetime =
44
        $yesterday->date =
45
        $yesterday->time =
46
            new \DateTime('yesterday');
47
        $this->em->persist($yesterday);
48
49
        $this->em->flush();
50
51
        unset($today, $tomorrow, $yesterday);
52
53
        $this->em->clear();
54
    }
55
56
    public function testLteDateComparison()
57
    {
58
        $this->loadFixture();
59
60
        $repository = $this->em->getRepository(DateTimeModel::class);
61
        $dates = $repository->matching(new Criteria(
62
            Criteria::expr()->lte('datetime', new \DateTime('today'))
63
        ));
64
65
        self::assertCount(2, $dates);
66
    }
67
68
    private function loadNullFieldFixtures()
69
    {
70
        $today = new DateTimeModel();
71
        $today->datetime =
72
        $today->date =
73
            new \DateTime('today');
74
75
        $this->em->persist($today);
76
77
        $tomorrow = new DateTimeModel();
78
        $tomorrow->datetime =
79
        $tomorrow->date =
80
        $tomorrow->time =
81
            new \DateTime('tomorrow');
82
        $this->em->persist($tomorrow);
83
84
        $this->em->flush();
85
        $this->em->clear();
86
    }
87
88
    public function testIsNullComparison()
89
    {
90
        $this->loadNullFieldFixtures();
91
        $repository = $this->em->getRepository(DateTimeModel::class);
92
93
        $dates = $repository->matching(new Criteria(
94
            Criteria::expr()->isNull('time')
95
        ));
96
97
        self::assertCount(1, $dates);
98
    }
99
100
    public function testEqNullComparison()
101
    {
102
        $this->loadNullFieldFixtures();
103
        $repository = $this->em->getRepository(DateTimeModel::class);
104
105
        $dates = $repository->matching(new Criteria(
106
            Criteria::expr()->eq('time', null)
107
        ));
108
109
        self::assertCount(1, $dates);
110
    }
111
112
    public function testNotEqNullComparison()
113
    {
114
        $this->loadNullFieldFixtures();
115
        $repository = $this->em->getRepository(DateTimeModel::class);
116
117
        $dates = $repository->matching(new Criteria(
118
            Criteria::expr()->neq('time', null)
119
        ));
120
121
        self::assertCount(1, $dates);
122
    }
123
124
    public function testCanCountWithoutLoadingCollection()
125
    {
126
        $this->loadFixture();
127
        $repository = $this->em->getRepository(DateTimeModel::class);
128
129
        $dates = $repository->matching(new Criteria());
130
131
        self::assertFalse($dates->isInitialized());
132
        self::assertCount(3, $dates);
133
        self::assertFalse($dates->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

133
        self::assertFalse($dates->/** @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

133
        self::assertFalse($dates->/** @scrutinizer ignore-call */ isInitialized());
Loading history...
134
135
        // Test it can work even with a constraint
136
        $dates = $repository->matching(new Criteria(
137
            Criteria::expr()->lte('datetime', new \DateTime('today'))
138
        ));
139
140
        self::assertFalse($dates->isInitialized());
141
        self::assertCount(2, $dates);
142
        self::assertFalse($dates->isInitialized());
143
144
        // Trigger a loading, to make sure collection is initialized
145
        $date = $dates[0];
0 ignored issues
show
Unused Code introduced by
The assignment to $date is dead and can be removed.
Loading history...
146
        self::assertTrue($dates->isInitialized());
147
    }
148
149
    public function testCanContainsWithoutLoadingCollection()
150
    {
151
        $user = new User();
152
        $user->name = 'Marco';
153
        $this->em->persist($user);
154
        $this->em->flush();
155
156
        $tweet = new Tweet();
157
        $tweet->author = $user;
158
        $tweet->content = 'Criteria is awesome';
159
        $this->em->persist($tweet);
160
        $this->em->flush();
161
162
        $this->em->clear();
163
164
        $criteria = new Criteria();
165
        $criteria->andWhere($criteria->expr()->contains('content', 'Criteria'));
166
167
        $user   = $this->em->find(User::class, $user->id);
168
        $tweets = $user->tweets->matching($criteria);
169
170
        self::assertInstanceOf(LazyCriteriaCollection::class, $tweets);
171
        self::assertFalse($tweets->isInitialized());
0 ignored issues
show
Bug introduced by
The method isInitialized() does not exist on Doctrine\Common\Collections\ArrayCollection. ( Ignorable by Annotation )

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

171
        self::assertFalse($tweets->/** @scrutinizer ignore-call */ isInitialized());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
172
173
        $tweets->contains($tweet);
174
        self::assertTrue($tweets->contains($tweet));
175
176
        self::assertFalse($tweets->isInitialized());
177
    }
178
}
179