|
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\Internal\Hydration\IterableResult; |
|
10
|
|
|
use Doctrine\ORM\Query\Parameter; |
|
11
|
|
|
use Doctrine\ORM\Query\QueryException; |
|
12
|
|
|
use Doctrine\Tests\Mocks\DriverConnectionMock; |
|
13
|
|
|
use Doctrine\Tests\Mocks\StatementArrayMock; |
|
14
|
|
|
use Doctrine\Tests\Models\CMS\CmsAddress; |
|
15
|
|
|
use Doctrine\Tests\Models\CMS\CmsUser; |
|
16
|
|
|
use Doctrine\Tests\OrmTestCase; |
|
17
|
|
|
|
|
18
|
|
|
class QueryTest extends OrmTestCase |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var EntityManager */ |
|
21
|
|
|
protected $_em = null; |
|
22
|
|
|
|
|
23
|
|
|
protected function setUp() |
|
24
|
|
|
{ |
|
25
|
|
|
$this->_em = $this->_getTestEntityManager(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function testGetParameters() |
|
29
|
|
|
{ |
|
30
|
|
|
$query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1"); |
|
31
|
|
|
|
|
32
|
|
|
$parameters = new ArrayCollection(); |
|
33
|
|
|
|
|
34
|
|
|
$this->assertEquals($parameters, $query->getParameters()); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
View Code Duplication |
public function testGetParameters_HasSomeAlready() |
|
38
|
|
|
{ |
|
39
|
|
|
$query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1"); |
|
40
|
|
|
$query->setParameter(2, 84); |
|
41
|
|
|
|
|
42
|
|
|
$parameters = new ArrayCollection(); |
|
43
|
|
|
$parameters->add(new Parameter(2, 84)); |
|
44
|
|
|
|
|
45
|
|
|
$this->assertEquals($parameters, $query->getParameters()); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
View Code Duplication |
public function testSetParameters() |
|
49
|
|
|
{ |
|
50
|
|
|
$query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1"); |
|
51
|
|
|
|
|
52
|
|
|
$parameters = new ArrayCollection(); |
|
53
|
|
|
$parameters->add(new Parameter(1, 'foo')); |
|
54
|
|
|
$parameters->add(new Parameter(2, 'bar')); |
|
55
|
|
|
|
|
56
|
|
|
$query->setParameters($parameters); |
|
57
|
|
|
|
|
58
|
|
|
$this->assertEquals($parameters, $query->getParameters()); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function testFree() |
|
62
|
|
|
{ |
|
63
|
|
|
$query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1"); |
|
64
|
|
|
$query->setParameter(2, 84, \PDO::PARAM_INT); |
|
65
|
|
|
|
|
66
|
|
|
$query->free(); |
|
67
|
|
|
|
|
68
|
|
|
$this->assertEquals(0, count($query->getParameters())); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function testClone() |
|
72
|
|
|
{ |
|
73
|
|
|
$dql = "select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1"; |
|
74
|
|
|
|
|
75
|
|
|
$query = $this->_em->createQuery($dql); |
|
76
|
|
|
$query->setParameter(2, 84, \PDO::PARAM_INT); |
|
77
|
|
|
$query->setHint('foo', 'bar'); |
|
78
|
|
|
|
|
79
|
|
|
$cloned = clone $query; |
|
80
|
|
|
|
|
81
|
|
|
$this->assertEquals($dql, $cloned->getDQL()); |
|
82
|
|
|
$this->assertEquals(0, count($cloned->getParameters())); |
|
83
|
|
|
$this->assertFalse($cloned->getHint('foo')); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function testFluentQueryInterface() |
|
87
|
|
|
{ |
|
88
|
|
|
$q = $this->_em->createQuery("select a from Doctrine\Tests\Models\CMS\CmsArticle a"); |
|
89
|
|
|
$q2 = $q->expireQueryCache(true) |
|
90
|
|
|
->setQueryCacheLifetime(3600) |
|
91
|
|
|
->setQueryCacheDriver(null) |
|
92
|
|
|
->expireResultCache(true) |
|
93
|
|
|
->setHint('foo', 'bar') |
|
94
|
|
|
->setHint('bar', 'baz') |
|
95
|
|
|
->setParameter(1, 'bar') |
|
96
|
|
|
->setParameters(new ArrayCollection([new Parameter(2, 'baz')])) |
|
97
|
|
|
->setResultCacheDriver(null) |
|
98
|
|
|
->setResultCacheId('foo') |
|
99
|
|
|
->setDQL('foo') |
|
100
|
|
|
->setFirstResult(10) |
|
101
|
|
|
->setMaxResults(10); |
|
102
|
|
|
|
|
103
|
|
|
$this->assertSame($q2, $q); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @group DDC-968 |
|
108
|
|
|
*/ |
|
109
|
|
|
public function testHints() |
|
110
|
|
|
{ |
|
111
|
|
|
$q = $this->_em->createQuery("select a from Doctrine\Tests\Models\CMS\CmsArticle a"); |
|
112
|
|
|
$q->setHint('foo', 'bar')->setHint('bar', 'baz'); |
|
113
|
|
|
|
|
114
|
|
|
$this->assertEquals('bar', $q->getHint('foo')); |
|
115
|
|
|
$this->assertEquals('baz', $q->getHint('bar')); |
|
116
|
|
|
$this->assertEquals(['foo' => 'bar', 'bar' => 'baz'], $q->getHints()); |
|
117
|
|
|
$this->assertTrue($q->hasHint('foo')); |
|
118
|
|
|
$this->assertFalse($q->hasHint('barFooBaz')); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @group DDC-1588 |
|
123
|
|
|
*/ |
|
124
|
|
|
public function testQueryDefaultResultCache() |
|
125
|
|
|
{ |
|
126
|
|
|
$this->_em->getConfiguration()->setResultCacheImpl(new ArrayCache()); |
|
127
|
|
|
$q = $this->_em->createQuery("select a from Doctrine\Tests\Models\CMS\CmsArticle a"); |
|
128
|
|
|
$q->useResultCache(true); |
|
129
|
|
|
$this->assertSame($this->_em->getConfiguration()->getResultCacheImpl(), $q->getQueryCacheProfile()->getResultCacheDriver()); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @expectedException Doctrine\ORM\Query\QueryException |
|
134
|
|
|
**/ |
|
135
|
|
|
public function testIterateWithNoDistinctAndWrongSelectClause() |
|
136
|
|
|
{ |
|
137
|
|
|
$q = $this->_em->createQuery("select u, a from Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a"); |
|
138
|
|
|
$q->iterate(); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @expectedException Doctrine\ORM\Query\QueryException |
|
143
|
|
|
**/ |
|
144
|
|
|
public function testIterateWithNoDistinctAndWithValidSelectClause() |
|
145
|
|
|
{ |
|
146
|
|
|
$q = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a"); |
|
147
|
|
|
$q->iterate(); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public function testIterateWithDistinct() |
|
151
|
|
|
{ |
|
152
|
|
|
$q = $this->_em->createQuery("SELECT DISTINCT u from Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a"); |
|
153
|
|
|
|
|
154
|
|
|
self::assertInstanceOf(IterableResult::class, $q->iterate()); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @group DDC-1697 |
|
159
|
|
|
*/ |
|
160
|
|
|
public function testCollectionParameters() |
|
161
|
|
|
{ |
|
162
|
|
|
$cities = [ |
|
163
|
|
|
0 => "Paris", |
|
164
|
|
|
3 => "Canne", |
|
165
|
|
|
9 => "St Julien" |
|
166
|
|
|
]; |
|
167
|
|
|
|
|
168
|
|
|
$query = $this->_em |
|
169
|
|
|
->createQuery("SELECT a FROM Doctrine\Tests\Models\CMS\CmsAddress a WHERE a.city IN (:cities)") |
|
170
|
|
|
->setParameter('cities', $cities); |
|
171
|
|
|
|
|
172
|
|
|
$parameters = $query->getParameters(); |
|
173
|
|
|
$parameter = $parameters->first(); |
|
174
|
|
|
|
|
175
|
|
|
$this->assertEquals('cities', $parameter->getName()); |
|
176
|
|
|
$this->assertEquals($cities, $parameter->getValue()); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @group DDC-2224 |
|
181
|
|
|
*/ |
|
182
|
|
|
public function testProcessParameterValueClassMetadata() |
|
183
|
|
|
{ |
|
184
|
|
|
$query = $this->_em->createQuery("SELECT a FROM Doctrine\Tests\Models\CMS\CmsAddress a WHERE a.city IN (:cities)"); |
|
185
|
|
|
$this->assertEquals( |
|
186
|
|
|
CmsAddress::class, |
|
187
|
|
|
$query->processParameterValue($this->_em->getClassMetadata(CmsAddress::class)) |
|
188
|
|
|
); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
public function testDefaultQueryHints() |
|
192
|
|
|
{ |
|
193
|
|
|
$config = $this->_em->getConfiguration(); |
|
194
|
|
|
$defaultHints = [ |
|
195
|
|
|
'hint_name_1' => 'hint_value_1', |
|
196
|
|
|
'hint_name_2' => 'hint_value_2', |
|
197
|
|
|
'hint_name_3' => 'hint_value_3', |
|
198
|
|
|
]; |
|
199
|
|
|
|
|
200
|
|
|
$config->setDefaultQueryHints($defaultHints); |
|
201
|
|
|
$query = $this->_em->createQuery(); |
|
202
|
|
|
$this->assertSame($config->getDefaultQueryHints(), $query->getHints()); |
|
203
|
|
|
$this->_em->getConfiguration()->setDefaultQueryHint('hint_name_1', 'hint_another_value_1'); |
|
204
|
|
|
$this->assertNotSame($config->getDefaultQueryHints(), $query->getHints()); |
|
205
|
|
|
$q2 = clone $query; |
|
206
|
|
|
$this->assertSame($config->getDefaultQueryHints(), $q2->getHints()); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @group DDC-3714 |
|
211
|
|
|
*/ |
|
212
|
|
|
public function testResultCacheCaching() |
|
213
|
|
|
{ |
|
214
|
|
|
$this->_em->getConfiguration()->setResultCacheImpl(new ArrayCache()); |
|
215
|
|
|
$this->_em->getConfiguration()->setQueryCacheImpl(new ArrayCache()); |
|
216
|
|
|
/** @var DriverConnectionMock $driverConnectionMock */ |
|
217
|
|
|
$driverConnectionMock = $this->_em->getConnection()->getWrappedConnection(); |
|
218
|
|
|
$stmt = new StatementArrayMock([ |
|
219
|
|
|
[ |
|
220
|
|
|
'id_0' => 1, |
|
221
|
|
|
] |
|
222
|
|
|
]); |
|
223
|
|
|
$driverConnectionMock->setStatementMock($stmt); |
|
224
|
|
|
$res = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u") |
|
225
|
|
|
->useQueryCache(true) |
|
226
|
|
|
->useResultCache(true, 60) |
|
227
|
|
|
//let it cache |
|
228
|
|
|
->getResult(); |
|
229
|
|
|
|
|
230
|
|
|
$this->assertCount(1, $res); |
|
231
|
|
|
|
|
232
|
|
|
$driverConnectionMock->setStatementMock(null); |
|
233
|
|
|
|
|
234
|
|
|
$res = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u") |
|
235
|
|
|
->useQueryCache(true) |
|
236
|
|
|
->useResultCache(false) |
|
237
|
|
|
->getResult(); |
|
238
|
|
|
$this->assertCount(0, $res); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* @group DDC-3741 |
|
243
|
|
|
*/ |
|
244
|
|
|
public function testSetHydrationCacheProfileNull() |
|
245
|
|
|
{ |
|
246
|
|
|
$query = $this->_em->createQuery(); |
|
247
|
|
|
$query->setHydrationCacheProfile(null); |
|
248
|
|
|
$this->assertNull($query->getHydrationCacheProfile()); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* @group 2947 |
|
253
|
|
|
*/ |
|
254
|
|
|
public function testResultCacheEviction() |
|
255
|
|
|
{ |
|
256
|
|
|
$this->_em->getConfiguration()->setResultCacheImpl(new ArrayCache()); |
|
257
|
|
|
|
|
258
|
|
|
$query = $this->_em->createQuery("SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u") |
|
259
|
|
|
->useResultCache(true); |
|
260
|
|
|
|
|
261
|
|
|
/** @var DriverConnectionMock $driverConnectionMock */ |
|
262
|
|
|
$driverConnectionMock = $this->_em->getConnection() |
|
263
|
|
|
->getWrappedConnection(); |
|
264
|
|
|
|
|
265
|
|
|
$driverConnectionMock->setStatementMock(new StatementArrayMock([['id_0' => 1]])); |
|
266
|
|
|
|
|
267
|
|
|
// Performs the query and sets up the initial cache |
|
268
|
|
|
self::assertCount(1, $query->getResult()); |
|
269
|
|
|
|
|
270
|
|
|
$driverConnectionMock->setStatementMock(new StatementArrayMock([['id_0' => 1], ['id_0' => 2]])); |
|
271
|
|
|
|
|
272
|
|
|
// Retrieves cached data since expire flag is false and we have a cached result set |
|
273
|
|
|
self::assertCount(1, $query->getResult()); |
|
274
|
|
|
|
|
275
|
|
|
// Performs the query and caches the result set since expire flag is true |
|
276
|
|
|
self::assertCount(2, $query->expireResultCache(true)->getResult()); |
|
277
|
|
|
|
|
278
|
|
|
$driverConnectionMock->setStatementMock(new StatementArrayMock([['id_0' => 1]])); |
|
279
|
|
|
|
|
280
|
|
|
// Retrieves cached data since expire flag is false and we have a cached result set |
|
281
|
|
|
self::assertCount(2, $query->expireResultCache(false)->getResult()); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* @group #6162 |
|
286
|
|
|
*/ |
|
287
|
|
|
public function testSelectJoinSubquery() |
|
288
|
|
|
{ |
|
289
|
|
|
$query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u JOIN (SELECT )"); |
|
290
|
|
|
|
|
291
|
|
|
$this->expectException(QueryException::class); |
|
292
|
|
|
$this->expectExceptionMessage('Subquery'); |
|
293
|
|
|
$query->getSQL(); |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* @group #6162 |
|
298
|
|
|
*/ |
|
299
|
|
|
public function testSelectFromSubquery() |
|
300
|
|
|
{ |
|
301
|
|
|
$query = $this->_em->createQuery("select u from (select Doctrine\Tests\Models\CMS\CmsUser c) as u"); |
|
302
|
|
|
|
|
303
|
|
|
$this->expectException(QueryException::class); |
|
304
|
|
|
$this->expectExceptionMessage('Subquery'); |
|
305
|
|
|
$query->getSQL(); |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* @group 6699 |
|
310
|
|
|
*/ |
|
311
|
|
View Code Duplication |
public function testGetParameterTypeJuggling() : void |
|
312
|
|
|
{ |
|
313
|
|
|
$query = $this->_em->createQuery('select u from ' . CmsUser::class . ' u where u.id = ?0'); |
|
314
|
|
|
|
|
315
|
|
|
$query->setParameter(0, 0); |
|
316
|
|
|
|
|
317
|
|
|
self::assertCount(1, $query->getParameters()); |
|
318
|
|
|
self::assertSame(0, $query->getParameter(0)->getValue()); |
|
319
|
|
|
self::assertSame(0, $query->getParameter('0')->getValue()); |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
/** |
|
323
|
|
|
* @group 6699 |
|
324
|
|
|
*/ |
|
325
|
|
View Code Duplication |
public function testSetParameterWithNameZeroIsNotOverridden() : void |
|
326
|
|
|
{ |
|
327
|
|
|
$query = $this->_em->createQuery('select u from ' . CmsUser::class . ' u where u.id != ?0 and u.username = :name'); |
|
328
|
|
|
|
|
329
|
|
|
$query->setParameter(0, 0); |
|
330
|
|
|
$query->setParameter('name', 'Doctrine'); |
|
331
|
|
|
|
|
332
|
|
|
self::assertCount(2, $query->getParameters()); |
|
333
|
|
|
self::assertSame(0, $query->getParameter('0')->getValue()); |
|
334
|
|
|
self::assertSame('Doctrine', $query->getParameter('name')->getValue()); |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
/** |
|
338
|
|
|
* @group 6699 |
|
339
|
|
|
*/ |
|
340
|
|
View Code Duplication |
public function testSetParameterWithNameZeroDoesNotOverrideAnotherParameter() : void |
|
341
|
|
|
{ |
|
342
|
|
|
$query = $this->_em->createQuery('select u from ' . CmsUser::class . ' u where u.id != ?0 and u.username = :name'); |
|
343
|
|
|
|
|
344
|
|
|
$query->setParameter('name', 'Doctrine'); |
|
345
|
|
|
$query->setParameter(0, 0); |
|
346
|
|
|
|
|
347
|
|
|
self::assertCount(2, $query->getParameters()); |
|
348
|
|
|
self::assertSame(0, $query->getParameter(0)->getValue()); |
|
349
|
|
|
self::assertSame('Doctrine', $query->getParameter('name')->getValue()); |
|
350
|
|
|
} |
|
351
|
|
|
|
|
352
|
|
|
/** |
|
353
|
|
|
* @group 6699 |
|
354
|
|
|
*/ |
|
355
|
|
|
public function testSetParameterWithTypeJugglingWorks() : void |
|
356
|
|
|
{ |
|
357
|
|
|
$query = $this->_em->createQuery('select u from ' . CmsUser::class . ' u where u.id != ?0 and u.username = :name'); |
|
358
|
|
|
|
|
359
|
|
|
$query->setParameter('0', 1); |
|
360
|
|
|
$query->setParameter('name', 'Doctrine'); |
|
361
|
|
|
$query->setParameter(0, 2); |
|
362
|
|
|
$query->setParameter('0', 3); |
|
363
|
|
|
|
|
364
|
|
|
self::assertCount(2, $query->getParameters()); |
|
365
|
|
|
self::assertSame(3, $query->getParameter(0)->getValue()); |
|
366
|
|
|
self::assertSame(3, $query->getParameter('0')->getValue()); |
|
367
|
|
|
self::assertSame('Doctrine', $query->getParameter('name')->getValue()); |
|
368
|
|
|
} |
|
369
|
|
|
} |
|
370
|
|
|
|