Passed
Pull Request — 2.7 (#7874)
by Thiago
07:27
created

EntityRepositoryCriteriaTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
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 testIsNullComparisonQuery()
109
    {
110
        $this->loadNullFieldFixtures();
111
        $repository = $this->_em->getRepository(DateTimeModel::class);
112
113
        $repository->matching(new Criteria(
114
            Criteria::expr()->isNull('time')
115
        ))->first();
116
117
        $this->assertContains(
118
            'col_time IS NULL',
119
            $this->_sqlLoggerStack->queries[$this->_sqlLoggerStack->currentQuery]['sql']
120
        );
121
    }
122
123
    public function testEqNullComparison()
124
    {
125
        $this->loadNullFieldFixtures();
126
        $repository = $this->_em->getRepository(DateTimeModel::class);
127
128
        $dates = $repository->matching(new Criteria(
129
            Criteria::expr()->eq('time', null)
130
        ));
131
132
        $this->assertEquals(1, count($dates));
133
    }
134
135
    public function testNotEqNullComparison()
136
    {
137
        $this->loadNullFieldFixtures();
138
        $repository = $this->_em->getRepository(DateTimeModel::class);
139
140
        $dates = $repository->matching(new Criteria(
141
            Criteria::expr()->neq('time', null)
142
        ));
143
144
        $this->assertEquals(1, count($dates));
145
    }
146
147
    public function testCanCountWithoutLoadingCollection()
148
    {
149
        $this->loadFixture();
150
        $repository = $this->_em->getRepository(DateTimeModel::class);
151
152
        $dates = $repository->matching(new Criteria());
153
154
        $this->assertFalse($dates->isInitialized());
155
        $this->assertCount(3, $dates);
156
        $this->assertFalse($dates->isInitialized());
157
158
        // Test it can work even with a constraint
159
        $dates = $repository->matching(new Criteria(
160
            Criteria::expr()->lte('datetime', new \DateTime('today'))
161
        ));
162
163
        $this->assertFalse($dates->isInitialized());
164
        $this->assertCount(2, $dates);
165
        $this->assertFalse($dates->isInitialized());
166
167
        // Trigger a loading, to make sure collection is initialized
168
        $date = $dates[0];
0 ignored issues
show
Unused Code introduced by
The assignment to $date is dead and can be removed.
Loading history...
169
        $this->assertTrue($dates->isInitialized());
170
    }
171
172
    public function testCanContainsWithoutLoadingCollection()
173
    {
174
        $user = new User();
175
        $user->name = 'Marco';
176
        $this->_em->persist($user);
177
        $this->_em->flush();
178
179
        $tweet = new Tweet();
180
        $tweet->author = $user;
181
        $tweet->content = 'Criteria is awesome';
182
        $this->_em->persist($tweet);
183
        $this->_em->flush();
184
185
        $this->_em->clear();
186
187
        $criteria = new Criteria();
188
        $criteria->andWhere($criteria->expr()->contains('content', 'Criteria'));
189
190
        $user   = $this->_em->find(User::class, $user->id);
191
        $tweets = $user->tweets->matching($criteria);
192
193
        $this->assertInstanceOf(LazyCriteriaCollection::class, $tweets);
194
        $this->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

194
        $this->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...
195
196
        $tweets->contains($tweet);
197
        $this->assertTrue($tweets->contains($tweet));
198
199
        $this->assertFalse($tweets->isInitialized());
200
    }
201
}
202