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