Failed Conditions
Push — master ( cfa1df...e1825e )
by Michael
198:41 queued 92:58
created

ORM/Functional/EntityRepositoryCriteriaTest.php (1 issue)

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

141
        $this->assertFalse($dates->/** @scrutinizer ignore-call */ isInitialized());
Loading history...
142
143
        // Test it can work even with a constraint
144
        $dates = $repository->matching(new Criteria(
145
            Criteria::expr()->lte('datetime', new \DateTime('today'))
146
        ));
147
148
        $this->assertFalse($dates->isInitialized());
149
        $this->assertCount(2, $dates);
150
        $this->assertFalse($dates->isInitialized());
151
152
        // Trigger a loading, to make sure collection is initialized
153
        $date = $dates[0];
154
        $this->assertTrue($dates->isInitialized());
155
    }
156
157
    public function testCanContainsWithoutLoadingCollection()
158
    {
159
        $user = new User();
160
        $user->name = 'Marco';
161
        $this->_em->persist($user);
162
        $this->_em->flush();
163
164
        $tweet = new Tweet();
165
        $tweet->author = $user;
166
        $tweet->content = 'Criteria is awesome';
167
        $this->_em->persist($tweet);
168
        $this->_em->flush();
169
170
        $this->_em->clear();
171
172
        $criteria = new Criteria();
173
        $criteria->andWhere($criteria->expr()->contains('content', 'Criteria'));
174
175
        $user   = $this->_em->find(User::class, $user->id);
176
        $tweets = $user->tweets->matching($criteria);
177
178
        $this->assertInstanceOf(LazyCriteriaCollection::class, $tweets);
179
        $this->assertFalse($tweets->isInitialized());
180
181
        $tweets->contains($tweet);
182
        $this->assertTrue($tweets->contains($tweet));
183
184
        $this->assertFalse($tweets->isInitialized());
185
    }
186
}
187