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