Passed
Pull Request — 2.7 (#7213)
by
unknown
07:20
created

ResultCacheTest::testDisableResultCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Doctrine\Tests\ORM\Functional;
4
5
use Doctrine\ORM\Query;
6
use Doctrine\ORM\Query\ResultSetMapping;
7
use Doctrine\Tests\Models\CMS\CmsUser;
8
use Doctrine\Tests\Models\CMS\CmsArticle;
9
use Doctrine\Common\Cache\ArrayCache;
10
use Doctrine\Tests\OrmFunctionalTestCase;
11
12
/**
13
 * ResultCacheTest
14
 *
15
 * @author robo
16
 */
17
class ResultCacheTest extends OrmFunctionalTestCase
18
{
19
   /**
20
     * @var \ReflectionProperty
21
     */
22
    private $cacheDataReflection;
23
24
    protected function setUp() {
25
        $this->cacheDataReflection = new \ReflectionProperty(ArrayCache::class, "data");
26
        $this->cacheDataReflection->setAccessible(true);
27
        $this->useModelSet('cms');
28
        parent::setUp();
29
    }
30
31
    /**
32
     * @param   ArrayCache $cache
33
     * @return  integer
34
     */
35
    private function getCacheSize(ArrayCache $cache)
36
    {
37
        return sizeof($this->cacheDataReflection->getValue($cache));
38
    }
39
40
    public function testResultCache()
41
    {
42
        $user = new CmsUser;
43
44
        $user->name = 'Roman';
45
        $user->username = 'romanb';
46
        $user->status = 'dev';
47
48
        $this->_em->persist($user);
49
        $this->_em->flush();
50
51
        $query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
52
53
        $cache = new ArrayCache();
54
55
        $query->setResultCacheDriver($cache)->setResultCacheId('my_cache_id');
56
57
        $this->assertFalse($cache->contains('my_cache_id'));
58
59
        $users = $query->getResult();
60
61
        $this->assertTrue($cache->contains('my_cache_id'));
62
        $this->assertEquals(1, count($users));
63
        $this->assertEquals('Roman', $users[0]->name);
64
65
        $this->_em->clear();
66
67
        $query2 = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
68
        $query2->setResultCacheDriver($cache)->setResultCacheId('my_cache_id');
69
70
        $users = $query2->getResult();
71
72
        $this->assertTrue($cache->contains('my_cache_id'));
73
        $this->assertEquals(1, count($users));
74
        $this->assertEquals('Roman', $users[0]->name);
75
    }
76
77
    public function testSetResultCacheId()
78
    {
79
        $cache = new ArrayCache;
80
        $query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
81
82
        $query->setResultCacheDriver($cache);
83
        $query->setResultCacheId('testing_result_cache_id');
84
85
        $this->assertFalse($cache->contains('testing_result_cache_id'));
86
87
        $users = $query->getResult();
0 ignored issues
show
Unused Code introduced by
The assignment to $users is dead and can be removed.
Loading history...
88
89
        $this->assertTrue($cache->contains('testing_result_cache_id'));
90
    }
91
92
    public function testUseResultCacheTrue()
93
    {
94
        $cache = new ArrayCache();
95
        $query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
96
97
        $query->useResultCache(true); // FIXME: This line does not affect anything. The test should be rethought.
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\ORM\AbstractQuery::useResultCache() has been deprecated: 3.0 Use {@see enableResultCache} and {@see disableResultCache} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

97
        /** @scrutinizer ignore-deprecated */ $query->useResultCache(true); // FIXME: This line does not affect anything. The test should be rethought.

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
98
        $query->setResultCacheDriver($cache);
99
        $query->setResultCacheId('testing_result_cache_id');
100
        $users = $query->getResult();
0 ignored issues
show
Unused Code introduced by
The assignment to $users is dead and can be removed.
Loading history...
101
102
        $this->assertTrue($cache->contains('testing_result_cache_id'));
103
104
        $this->_em->getConfiguration()->setResultCacheImpl(new ArrayCache());
105
    }
106
107
    public function testUseResultCacheFalse()
108
    {
109
        $cache = new ArrayCache();
110
        $query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
111
112
        $query->setResultCacheDriver($cache);
113
        $query->setResultCacheId('testing_result_cache_id');
114
        $query->useResultCache(false);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\ORM\AbstractQuery::useResultCache() has been deprecated: 3.0 Use {@see enableResultCache} and {@see disableResultCache} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

114
        /** @scrutinizer ignore-deprecated */ $query->useResultCache(false);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
115
        $users = $query->getResult();
0 ignored issues
show
Unused Code introduced by
The assignment to $users is dead and can be removed.
Loading history...
116
117
        $this->assertFalse($cache->contains('testing_result_cache_id'));
118
119
        $this->_em->getConfiguration()->setResultCacheImpl(new ArrayCache());
120
    }
121
122
123
    /**
124
     * @group DDC-1026
125
     */
126
    public function testUseResultCacheParams()
127
    {
128
        $cache    = new ArrayCache();
129
        $sqlCount = count($this->_sqlLoggerStack->queries);
130
        $query    = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux WHERE ux.id = ?1');
131
132
        $query->setParameter(1, 1);
133
        $query->setResultCacheDriver($cache);
134
        $query->useResultCache(true);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\ORM\AbstractQuery::useResultCache() has been deprecated: 3.0 Use {@see enableResultCache} and {@see disableResultCache} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

134
        /** @scrutinizer ignore-deprecated */ $query->useResultCache(true);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
135
        $query->getResult();
136
137
        $query->setParameter(1, 2);
138
        $query->getResult();
139
140
        $this->assertEquals($sqlCount + 2, count($this->_sqlLoggerStack->queries), "Two non-cached queries.");
141
142
        $query->setParameter(1, 1);
143
        $query->useResultCache(true);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\ORM\AbstractQuery::useResultCache() has been deprecated: 3.0 Use {@see enableResultCache} and {@see disableResultCache} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

143
        /** @scrutinizer ignore-deprecated */ $query->useResultCache(true);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
144
        $query->getResult();
145
146
        $query->setParameter(1, 2);
147
        $query->getResult();
148
149
        $this->assertEquals($sqlCount + 2, count($this->_sqlLoggerStack->queries), "The next two sql should have been cached, but were not.");
150
    }
151
152
    public function testEnableResultCache()
153
    {
154
        $cache = new ArrayCache();
155
        $query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
156
157
        $query->enableResultCache(); // FIXME: This line does not affect anything. The test should be rethought.
158
        $query->setResultCacheDriver($cache);
159
        $query->setResultCacheId('testing_result_cache_id');
160
        $users = $query->getResult();
0 ignored issues
show
Unused Code introduced by
The assignment to $users is dead and can be removed.
Loading history...
161
162
        $this->assertTrue($cache->contains('testing_result_cache_id'));
163
164
        $this->_em->getConfiguration()->setResultCacheImpl(new ArrayCache());
165
    }
166
167
    /**
168
     * @group DDC-1026
169
     */
170
    public function testEnableResultCacheParams()
171
    {
172
        $cache    = new ArrayCache();
173
        $sqlCount = count($this->_sqlLoggerStack->queries);
174
        $query    = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux WHERE ux.id = ?1');
175
176
        $query->setParameter(1, 1);
177
        $query->setResultCacheDriver($cache);
178
        $query->enableResultCache();
179
        $query->getResult();
180
181
        $query->setParameter(1, 2);
182
        $query->getResult();
183
184
        $this->assertCount($sqlCount + 2, $this->_sqlLoggerStack->queries, "Two non-cached queries.");
185
186
        $query->setParameter(1, 1);
187
        $query->enableResultCache();
188
        $query->getResult();
189
190
        $query->setParameter(1, 2);
191
        $query->getResult();
192
193
        $this->assertCount($sqlCount + 2, $this->_sqlLoggerStack->queries, "The next two sql should have been cached, but were not.");
194
    }
195
196
    public function testDisableResultCache()
197
    {
198
        $cache = new ArrayCache();
199
        $query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
200
201
        $query->setResultCacheDriver($cache);
202
        $query->setResultCacheId('testing_result_cache_id');
203
        $query->disableResultCache();
204
        $users = $query->getResult();
0 ignored issues
show
Unused Code introduced by
The assignment to $users is dead and can be removed.
Loading history...
205
206
        $this->assertFalse($cache->contains('testing_result_cache_id'));
207
208
        $this->_em->getConfiguration()->setResultCacheImpl(new ArrayCache());
209
    }
210
211
    public function testNativeQueryResultCaching()
212
    {
213
        $cache = new ArrayCache();
214
        $rsm   = new ResultSetMapping();
215
216
        $rsm->addScalarResult('id', 'u', 'integer');
217
218
        $query = $this->_em->createNativeQuery('select u.id FROM cms_users u WHERE u.id = ?', $rsm);
219
220
        $query->setParameter(1, 10);
221
        $query->setResultCacheDriver($cache)->enableResultCache();
222
223
        $this->assertEquals(0, $this->getCacheSize($cache));
224
225
        $query->getResult();
226
227
        $this->assertEquals(1, $this->getCacheSize($cache));
228
229
        return $query;
230
    }
231
232
    /**
233
     * @param string $query
234
     * @depends testNativeQueryResultCaching
235
     */
236
    public function testResultCacheNotDependsOnQueryHints($query)
237
    {
238
        $cache = $query->getResultCacheDriver();
239
        $cacheCount = $this->getCacheSize($cache);
240
241
        $query->setHint('foo', 'bar');
242
        $query->getResult();
243
244
        $this->assertEquals($cacheCount, $this->getCacheSize($cache));
245
    }
246
247
    /**
248
     * @param <type> $query
0 ignored issues
show
Documentation Bug introduced by
The doc comment <type> at position 0 could not be parsed: Unknown type name '<' at position 0 in <type>.
Loading history...
249
     * @depends testNativeQueryResultCaching
250
     */
251
    public function testResultCacheDependsOnParameters($query)
252
    {
253
        $cache = $query->getResultCacheDriver();
254
        $cacheCount = $this->getCacheSize($cache);
255
256
        $query->setParameter(1, 50);
257
        $query->getResult();
258
259
        $this->assertEquals($cacheCount + 1, $this->getCacheSize($cache));
260
    }
261
262
    /**
263
     * @param <type> $query
0 ignored issues
show
Documentation Bug introduced by
The doc comment <type> at position 0 could not be parsed: Unknown type name '<' at position 0 in <type>.
Loading history...
264
     * @depends testNativeQueryResultCaching
265
     */
266
    public function testResultCacheNotDependsOnHydrationMode($query)
267
    {
268
        $cache = $query->getResultCacheDriver();
269
        $cacheCount = $this->getCacheSize($cache);
270
271
        $this->assertNotEquals(Query::HYDRATE_ARRAY, $query->getHydrationMode());
272
        $query->getArrayResult();
273
274
        $this->assertEquals($cacheCount, $this->getCacheSize($cache));
275
    }
276
277
    /**
278
     * @group DDC-909
279
     */
280
    public function testResultCacheWithObjectParameter()
281
    {
282
        $user1 = new CmsUser;
283
        $user1->name = 'Roman';
284
        $user1->username = 'romanb';
285
        $user1->status = 'dev';
286
287
        $user2 = new CmsUser;
288
        $user2->name = 'Benjamin';
289
        $user2->username = 'beberlei';
290
        $user2->status = 'dev';
291
292
        $article = new CmsArticle();
293
        $article->text = "foo";
294
        $article->topic = "baz";
295
        $article->user = $user1;
296
297
        $this->_em->persist($article);
298
        $this->_em->persist($user1);
299
        $this->_em->persist($user2);
300
        $this->_em->flush();
301
302
        $query = $this->_em->createQuery('select a from Doctrine\Tests\Models\CMS\CmsArticle a WHERE a.user = ?1');
303
        $query->setParameter(1, $user1);
304
305
        $cache = new ArrayCache();
306
307
        $query->setResultCacheDriver($cache)->enableResultCache();
308
309
        $articles = $query->getResult();
310
311
        $this->assertEquals(1, count($articles));
312
        $this->assertEquals('baz', $articles[0]->topic);
313
314
        $this->_em->clear();
315
316
        $query2 = $this->_em->createQuery('select a from Doctrine\Tests\Models\CMS\CmsArticle a WHERE a.user = ?1');
317
        $query2->setParameter(1, $user1);
318
319
        $query2->setResultCacheDriver($cache)->enableResultCache();
320
321
        $articles = $query2->getResult();
322
323
        $this->assertEquals(1, count($articles));
324
        $this->assertEquals('baz', $articles[0]->topic);
325
326
        $query3 = $this->_em->createQuery('select a from Doctrine\Tests\Models\CMS\CmsArticle a WHERE a.user = ?1');
327
        $query3->setParameter(1, $user2);
328
329
        $query3->setResultCacheDriver($cache)->enableResultCache();
330
331
        $articles = $query3->getResult();
332
333
        $this->assertEquals(0, count($articles));
334
    }
335
}
336