1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\ORM\Query; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
6
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
7
|
|
|
|
8
|
|
|
use Doctrine\ORM\EntityManager; |
9
|
|
|
use Doctrine\ORM\Query\Parameter; |
10
|
|
|
use Doctrine\Tests\Mocks\DriverConnectionMock; |
11
|
|
|
use Doctrine\Tests\Mocks\StatementArrayMock; |
12
|
|
|
use Doctrine\Tests\Models\CMS\CmsAddress; |
13
|
|
|
use Doctrine\Tests\OrmTestCase; |
14
|
|
|
|
15
|
|
|
class QueryTest extends OrmTestCase |
16
|
|
|
{ |
17
|
|
|
/** @var EntityManager */ |
18
|
|
|
protected $_em = null; |
19
|
|
|
|
20
|
|
|
protected function setUp() |
21
|
|
|
{ |
22
|
|
|
$this->_em = $this->_getTestEntityManager(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testGetParameters() |
26
|
|
|
{ |
27
|
|
|
$query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1"); |
28
|
|
|
|
29
|
|
|
$parameters = new ArrayCollection(); |
30
|
|
|
|
31
|
|
|
$this->assertEquals($parameters, $query->getParameters()); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testGetParameters_HasSomeAlready() |
35
|
|
|
{ |
36
|
|
|
$query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1"); |
37
|
|
|
$query->setParameter(2, 84); |
38
|
|
|
|
39
|
|
|
$parameters = new ArrayCollection(); |
40
|
|
|
$parameters->add(new Parameter(2, 84)); |
41
|
|
|
|
42
|
|
|
$this->assertEquals($parameters, $query->getParameters()); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testSetParameters() |
46
|
|
|
{ |
47
|
|
|
$query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1"); |
48
|
|
|
|
49
|
|
|
$parameters = new ArrayCollection(); |
50
|
|
|
$parameters->add(new Parameter(1, 'foo')); |
51
|
|
|
$parameters->add(new Parameter(2, 'bar')); |
52
|
|
|
|
53
|
|
|
$query->setParameters($parameters); |
54
|
|
|
|
55
|
|
|
$this->assertEquals($parameters, $query->getParameters()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testFree() |
59
|
|
|
{ |
60
|
|
|
$query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1"); |
61
|
|
|
$query->setParameter(2, 84, \PDO::PARAM_INT); |
62
|
|
|
|
63
|
|
|
$query->free(); |
64
|
|
|
|
65
|
|
|
$this->assertEquals(0, count($query->getParameters())); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testClone() |
69
|
|
|
{ |
70
|
|
|
$dql = "select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1"; |
71
|
|
|
|
72
|
|
|
$query = $this->_em->createQuery($dql); |
73
|
|
|
$query->setParameter(2, 84, \PDO::PARAM_INT); |
74
|
|
|
$query->setHint('foo', 'bar'); |
75
|
|
|
|
76
|
|
|
$cloned = clone $query; |
77
|
|
|
|
78
|
|
|
$this->assertEquals($dql, $cloned->getDQL()); |
79
|
|
|
$this->assertEquals(0, count($cloned->getParameters())); |
80
|
|
|
$this->assertFalse($cloned->getHint('foo')); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testFluentQueryInterface() |
84
|
|
|
{ |
85
|
|
|
$q = $this->_em->createQuery("select a from Doctrine\Tests\Models\CMS\CmsArticle a"); |
86
|
|
|
$q2 = $q->expireQueryCache(true) |
87
|
|
|
->setQueryCacheLifetime(3600) |
88
|
|
|
->setQueryCacheDriver(null) |
89
|
|
|
->expireResultCache(true) |
90
|
|
|
->setHint('foo', 'bar') |
91
|
|
|
->setHint('bar', 'baz') |
92
|
|
|
->setParameter(1, 'bar') |
93
|
|
|
->setParameters(new ArrayCollection([new Parameter(2, 'baz')])) |
94
|
|
|
->setResultCacheDriver(null) |
95
|
|
|
->setResultCacheId('foo') |
96
|
|
|
->setDQL('foo') |
97
|
|
|
->setFirstResult(10) |
98
|
|
|
->setMaxResults(10); |
99
|
|
|
|
100
|
|
|
$this->assertSame($q2, $q); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @group DDC-968 |
105
|
|
|
*/ |
106
|
|
|
public function testHints() |
107
|
|
|
{ |
108
|
|
|
$q = $this->_em->createQuery("select a from Doctrine\Tests\Models\CMS\CmsArticle a"); |
109
|
|
|
$q->setHint('foo', 'bar')->setHint('bar', 'baz'); |
110
|
|
|
|
111
|
|
|
$this->assertEquals('bar', $q->getHint('foo')); |
112
|
|
|
$this->assertEquals('baz', $q->getHint('bar')); |
113
|
|
|
$this->assertEquals(['foo' => 'bar', 'bar' => 'baz'], $q->getHints()); |
114
|
|
|
$this->assertTrue($q->hasHint('foo')); |
115
|
|
|
$this->assertFalse($q->hasHint('barFooBaz')); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @group DDC-1588 |
120
|
|
|
*/ |
121
|
|
|
public function testQueryDefaultResultCache() |
122
|
|
|
{ |
123
|
|
|
$this->_em->getConfiguration()->setResultCacheImpl(new ArrayCache()); |
124
|
|
|
$q = $this->_em->createQuery("select a from Doctrine\Tests\Models\CMS\CmsArticle a"); |
125
|
|
|
$q->useResultCache(true); |
126
|
|
|
$this->assertSame($this->_em->getConfiguration()->getResultCacheImpl(), $q->getQueryCacheProfile()->getResultCacheDriver()); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @expectedException Doctrine\ORM\Query\QueryException |
131
|
|
|
**/ |
132
|
|
|
public function testIterateWithNoDistinctAndWrongSelectClause() |
133
|
|
|
{ |
134
|
|
|
$q = $this->_em->createQuery("select u, a from Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a"); |
135
|
|
|
$q->iterate(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @expectedException Doctrine\ORM\Query\QueryException |
140
|
|
|
**/ |
141
|
|
|
public function testIterateWithNoDistinctAndWithValidSelectClause() |
142
|
|
|
{ |
143
|
|
|
$q = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a"); |
144
|
|
|
$q->iterate(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function testIterateWithDistinct() |
148
|
|
|
{ |
149
|
|
|
$q = $this->_em->createQuery("SELECT DISTINCT u from Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a"); |
150
|
|
|
$q->iterate(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @group DDC-1697 |
155
|
|
|
*/ |
156
|
|
|
public function testCollectionParameters() |
157
|
|
|
{ |
158
|
|
|
$cities = [ |
159
|
|
|
0 => "Paris", |
160
|
|
|
3 => "Canne", |
161
|
|
|
9 => "St Julien" |
162
|
|
|
]; |
163
|
|
|
|
164
|
|
|
$query = $this->_em |
165
|
|
|
->createQuery("SELECT a FROM Doctrine\Tests\Models\CMS\CmsAddress a WHERE a.city IN (:cities)") |
166
|
|
|
->setParameter('cities', $cities); |
167
|
|
|
|
168
|
|
|
$parameters = $query->getParameters(); |
169
|
|
|
$parameter = $parameters->first(); |
170
|
|
|
|
171
|
|
|
$this->assertEquals('cities', $parameter->getName()); |
172
|
|
|
$this->assertEquals($cities, $parameter->getValue()); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @group DDC-2224 |
177
|
|
|
*/ |
178
|
|
|
public function testProcessParameterValueClassMetadata() |
179
|
|
|
{ |
180
|
|
|
$query = $this->_em->createQuery("SELECT a FROM Doctrine\Tests\Models\CMS\CmsAddress a WHERE a.city IN (:cities)"); |
181
|
|
|
$this->assertEquals( |
182
|
|
|
CmsAddress::class, |
183
|
|
|
$query->processParameterValue($this->_em->getClassMetadata(CmsAddress::class)) |
184
|
|
|
); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function testDefaultQueryHints() |
188
|
|
|
{ |
189
|
|
|
$config = $this->_em->getConfiguration(); |
190
|
|
|
$defaultHints = [ |
191
|
|
|
'hint_name_1' => 'hint_value_1', |
192
|
|
|
'hint_name_2' => 'hint_value_2', |
193
|
|
|
'hint_name_3' => 'hint_value_3', |
194
|
|
|
]; |
195
|
|
|
|
196
|
|
|
$config->setDefaultQueryHints($defaultHints); |
197
|
|
|
$query = $this->_em->createQuery(); |
198
|
|
|
$this->assertSame($config->getDefaultQueryHints(), $query->getHints()); |
199
|
|
|
$this->_em->getConfiguration()->setDefaultQueryHint('hint_name_1', 'hint_another_value_1'); |
200
|
|
|
$this->assertNotSame($config->getDefaultQueryHints(), $query->getHints()); |
201
|
|
|
$q2 = clone $query; |
202
|
|
|
$this->assertSame($config->getDefaultQueryHints(), $q2->getHints()); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @group DDC-3714 |
207
|
|
|
*/ |
208
|
|
|
public function testResultCacheCaching() |
209
|
|
|
{ |
210
|
|
|
$this->_em->getConfiguration()->setResultCacheImpl(new ArrayCache()); |
211
|
|
|
$this->_em->getConfiguration()->setQueryCacheImpl(new ArrayCache()); |
212
|
|
|
/** @var DriverConnectionMock $driverConnectionMock */ |
213
|
|
|
$driverConnectionMock = $this->_em->getConnection()->getWrappedConnection(); |
214
|
|
|
$stmt = new StatementArrayMock([ |
215
|
|
|
[ |
216
|
|
|
'id_0' => 1, |
217
|
|
|
] |
218
|
|
|
]); |
219
|
|
|
$driverConnectionMock->setStatementMock($stmt); |
220
|
|
|
$res = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u") |
221
|
|
|
->useQueryCache(true) |
222
|
|
|
->useResultCache(true, 60) |
223
|
|
|
//let it cache |
224
|
|
|
->getResult(); |
225
|
|
|
|
226
|
|
|
$this->assertCount(1, $res); |
227
|
|
|
|
228
|
|
|
$driverConnectionMock->setStatementMock(null); |
229
|
|
|
|
230
|
|
|
$res = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u") |
231
|
|
|
->useQueryCache(true) |
232
|
|
|
->useResultCache(false) |
233
|
|
|
->getResult(); |
234
|
|
|
$this->assertCount(0, $res); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @group DDC-3741 |
239
|
|
|
*/ |
240
|
|
|
public function testSetHydrationCacheProfileNull() |
241
|
|
|
{ |
242
|
|
|
$query = $this->_em->createQuery(); |
243
|
|
|
$query->setHydrationCacheProfile(null); |
244
|
|
|
$this->assertNull($query->getHydrationCacheProfile()); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @group 2947 |
249
|
|
|
*/ |
250
|
|
|
public function testResultCacheEviction() |
251
|
|
|
{ |
252
|
|
|
$this->_em->getConfiguration()->setResultCacheImpl(new ArrayCache()); |
253
|
|
|
|
254
|
|
|
$query = $this->_em->createQuery("SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u") |
255
|
|
|
->useResultCache(true); |
256
|
|
|
|
257
|
|
|
/** @var DriverConnectionMock $driverConnectionMock */ |
258
|
|
|
$driverConnectionMock = $this->_em->getConnection() |
259
|
|
|
->getWrappedConnection(); |
260
|
|
|
|
261
|
|
|
$driverConnectionMock->setStatementMock(new StatementArrayMock([['id_0' => 1]])); |
262
|
|
|
|
263
|
|
|
// Performs the query and sets up the initial cache |
264
|
|
|
self::assertCount(1, $query->getResult()); |
265
|
|
|
|
266
|
|
|
$driverConnectionMock->setStatementMock(new StatementArrayMock([['id_0' => 1], ['id_0' => 2]])); |
267
|
|
|
|
268
|
|
|
// Retrieves cached data since expire flag is false and we have a cached result set |
269
|
|
|
self::assertCount(1, $query->getResult()); |
270
|
|
|
|
271
|
|
|
// Performs the query and caches the result set since expire flag is true |
272
|
|
|
self::assertCount(2, $query->expireResultCache(true)->getResult()); |
273
|
|
|
|
274
|
|
|
$driverConnectionMock->setStatementMock(new StatementArrayMock([['id_0' => 1]])); |
275
|
|
|
|
276
|
|
|
// Retrieves cached data since expire flag is false and we have a cached result set |
277
|
|
|
self::assertCount(2, $query->expireResultCache(false)->getResult()); |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|