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); |
52
|
|
|
unset($tomorrow); |
53
|
|
|
unset($yesterday); |
54
|
|
|
|
55
|
|
|
$this->em->clear(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testLteDateComparison() |
59
|
|
|
{ |
60
|
|
|
$this->loadFixture(); |
61
|
|
|
|
62
|
|
|
$repository = $this->em->getRepository(DateTimeModel::class); |
63
|
|
|
$dates = $repository->matching(new Criteria( |
64
|
|
|
Criteria::expr()->lte('datetime', new \DateTime('today')) |
65
|
|
|
)); |
66
|
|
|
|
67
|
|
|
self::assertCount(2, $dates); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
private function loadNullFieldFixtures() |
71
|
|
|
{ |
72
|
|
|
$today = new DateTimeModel(); |
73
|
|
|
$today->datetime = |
74
|
|
|
$today->date = |
75
|
|
|
new \DateTime('today'); |
76
|
|
|
|
77
|
|
|
$this->em->persist($today); |
78
|
|
|
|
79
|
|
|
$tomorrow = new DateTimeModel(); |
80
|
|
|
$tomorrow->datetime = |
81
|
|
|
$tomorrow->date = |
82
|
|
|
$tomorrow->time = |
83
|
|
|
new \DateTime('tomorrow'); |
84
|
|
|
$this->em->persist($tomorrow); |
85
|
|
|
|
86
|
|
|
$this->em->flush(); |
87
|
|
|
$this->em->clear(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testIsNullComparison() |
91
|
|
|
{ |
92
|
|
|
$this->loadNullFieldFixtures(); |
93
|
|
|
$repository = $this->em->getRepository(DateTimeModel::class); |
94
|
|
|
|
95
|
|
|
$dates = $repository->matching(new Criteria( |
96
|
|
|
Criteria::expr()->isNull('time') |
97
|
|
|
)); |
98
|
|
|
|
99
|
|
|
self::assertCount(1, $dates); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function testEqNullComparison() |
103
|
|
|
{ |
104
|
|
|
$this->loadNullFieldFixtures(); |
105
|
|
|
$repository = $this->em->getRepository(DateTimeModel::class); |
106
|
|
|
|
107
|
|
|
$dates = $repository->matching(new Criteria( |
108
|
|
|
Criteria::expr()->eq('time', null) |
109
|
|
|
)); |
110
|
|
|
|
111
|
|
|
self::assertCount(1, $dates); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function testNotEqNullComparison() |
115
|
|
|
{ |
116
|
|
|
$this->loadNullFieldFixtures(); |
117
|
|
|
$repository = $this->em->getRepository(DateTimeModel::class); |
118
|
|
|
|
119
|
|
|
$dates = $repository->matching(new Criteria( |
120
|
|
|
Criteria::expr()->neq('time', null) |
121
|
|
|
)); |
122
|
|
|
|
123
|
|
|
self::assertCount(1, $dates); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function testCanCountWithoutLoadingCollection() |
127
|
|
|
{ |
128
|
|
|
$this->loadFixture(); |
129
|
|
|
$repository = $this->em->getRepository(DateTimeModel::class); |
130
|
|
|
|
131
|
|
|
$dates = $repository->matching(new Criteria()); |
132
|
|
|
|
133
|
|
|
self::assertFalse($dates->isInitialized()); |
134
|
|
|
self::assertCount(3, $dates); |
135
|
|
|
self::assertFalse($dates->isInitialized()); |
136
|
|
|
|
137
|
|
|
// Test it can work even with a constraint |
138
|
|
|
$dates = $repository->matching(new Criteria( |
139
|
|
|
Criteria::expr()->lte('datetime', new \DateTime('today')) |
140
|
|
|
)); |
141
|
|
|
|
142
|
|
|
self::assertFalse($dates->isInitialized()); |
143
|
|
|
self::assertCount(2, $dates); |
144
|
|
|
self::assertFalse($dates->isInitialized()); |
145
|
|
|
|
146
|
|
|
// Trigger a loading, to make sure collection is initialized |
147
|
|
|
$date = $dates[0]; |
|
|
|
|
148
|
|
|
self::assertTrue($dates->isInitialized()); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function testCanContainsWithoutLoadingCollection() |
152
|
|
|
{ |
153
|
|
|
$user = new User(); |
154
|
|
|
$user->name = 'Marco'; |
155
|
|
|
$this->em->persist($user); |
156
|
|
|
$this->em->flush(); |
157
|
|
|
|
158
|
|
|
$tweet = new Tweet(); |
159
|
|
|
$tweet->author = $user; |
160
|
|
|
$tweet->content = 'Criteria is awesome'; |
161
|
|
|
$this->em->persist($tweet); |
162
|
|
|
$this->em->flush(); |
163
|
|
|
|
164
|
|
|
$this->em->clear(); |
165
|
|
|
|
166
|
|
|
$criteria = new Criteria(); |
167
|
|
|
$criteria->andWhere($criteria->expr()->contains('content', 'Criteria')); |
168
|
|
|
|
169
|
|
|
$user = $this->em->find(User::class, $user->id); |
170
|
|
|
$tweets = $user->tweets->matching($criteria); |
171
|
|
|
|
172
|
|
|
self::assertInstanceOf(LazyCriteriaCollection::class, $tweets); |
173
|
|
|
self::assertFalse($tweets->isInitialized()); |
174
|
|
|
|
175
|
|
|
$tweets->contains($tweet); |
176
|
|
|
self::assertTrue($tweets->contains($tweet)); |
177
|
|
|
|
178
|
|
|
self::assertFalse($tweets->isInitialized()); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.