Failed Conditions
Pull Request — master (#7214)
by
unknown
14:26
created

ResultCacheTest::testUseResultCacheParams()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 0
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Functional;
6
7
use Doctrine\Common\Cache\ArrayCache;
8
use Doctrine\DBAL\Types\Type as DBALType;
9
use Doctrine\ORM\Exception\ORMException;
10
use Doctrine\ORM\NativeQuery;
11
use Doctrine\ORM\Query;
12
use Doctrine\ORM\Query\ResultSetMapping;
13
use Doctrine\Tests\Models\CMS\CmsArticle;
14
use Doctrine\Tests\Models\CMS\CmsUser;
15
use Doctrine\Tests\OrmFunctionalTestCase;
16
use function count;
17
18
/**
19
 * ResultCacheTest
20
 */
21
class ResultCacheTest extends OrmFunctionalTestCase
22
{
23
    /** @var \ReflectionProperty */
24
    private $cacheDataReflection;
25
26
    protected function setUp() : void
27
    {
28
        $this->cacheDataReflection = new \ReflectionProperty(ArrayCache::class, 'data');
29
        $this->cacheDataReflection->setAccessible(true);
30
31
        $this->useModelSet('cms');
32
33
        parent::setUp();
34
    }
35
36
    /**
37
     * @return  int
38
     */
39
    private function getCacheSize(ArrayCache $cache)
40
    {
41
        return count($this->cacheDataReflection->getValue($cache));
42
    }
43
44
    public function testResultCache() : void
45
    {
46
        $user = new CmsUser();
47
48
        $user->name     = 'Roman';
49
        $user->username = 'romanb';
50
        $user->status   = 'dev';
51
52
        $this->em->persist($user);
53
        $this->em->flush();
54
55
        $query = $this->em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
56
        $cache = new ArrayCache();
57
58
        $query->setResultCacheDriver($cache)->setResultCacheId('my_cache_id');
59
60
        self::assertFalse($cache->contains('my_cache_id'));
61
62
        $users = $query->getResult();
63
64
        self::assertTrue($cache->contains('my_cache_id'));
65
        self::assertCount(1, $users);
66
        self::assertEquals('Roman', $users[0]->name);
67
68
        $this->em->clear();
69
70
        $query2 = $this->em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
71
        $query2->setResultCacheDriver($cache)->setResultCacheId('my_cache_id');
72
73
        $users = $query2->getResult();
74
75
        self::assertTrue($cache->contains('my_cache_id'));
76
        self::assertCount(1, $users);
77
        self::assertEquals('Roman', $users[0]->name);
78
    }
79
80
    public function testSetResultCacheId() : void
81
    {
82
        $cache = new ArrayCache();
83
        $query = $this->em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
84
85
        $query->setResultCacheDriver($cache);
86
        $query->setResultCacheId('testing_result_cache_id');
87
88
        self::assertFalse($cache->contains('testing_result_cache_id'));
89
90
        $users = $query->getResult();
0 ignored issues
show
Unused Code introduced by
The assignment to $users is dead and can be removed.
Loading history...
91
92
        self::assertTrue($cache->contains('testing_result_cache_id'));
93
    }
94
95
    public function testEnableResultCache() : void
96
    {
97
        $cache = new ArrayCache();
98
        $query = $this->em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
99
100
        $query->enableResultCache(); // FIXME: This line does not affect anything. The test should be rethought.
101
        $query->setResultCacheDriver($cache);
102
        $query->setResultCacheId('testing_result_cache_id');
103
104
        $users = $query->getResult();
0 ignored issues
show
Unused Code introduced by
The assignment to $users is dead and can be removed.
Loading history...
105
106
        self::assertTrue($cache->contains('testing_result_cache_id'));
107
108
        $this->em->getConfiguration()->setResultCacheImpl(new ArrayCache());
109
    }
110
111
    /**
112
     * @group DDC-1026
113
     */
114
    public function testEnableResultCacheParams() : void
115
    {
116
        $cache    = new ArrayCache();
117
        $sqlCount = count($this->sqlLoggerStack->queries);
118
        $query    = $this->em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux WHERE ux.id = ?1');
119
120
        $query->setParameter(1, 1);
121
        $query->setResultCacheDriver($cache);
122
        $query->enableResultCache();
123
        $query->getResult();
124
125
        $query->setParameter(1, 2);
126
        $query->getResult();
127
128
        self::assertCount($sqlCount + 2, $this->sqlLoggerStack->queries, 'Two non-cached queries.');
129
130
        $query->setParameter(1, 1);
131
        $query->enableResultCache();
132
        $query->getResult();
133
134
        $query->setParameter(1, 2);
135
        $query->getResult();
136
137
        self::assertCount($sqlCount + 2, $this->sqlLoggerStack->queries, 'The next two sql should have been cached, but were not.');
138
    }
139
140
    public function testDisableResultCache() : void
141
    {
142
        $cache = new ArrayCache();
143
        $query = $this->em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
144
145
        $query->setResultCacheDriver($cache);
146
        $query->setResultCacheId('testing_result_cache_id');
147
        $query->disableResultCache();
148
        $users = $query->getResult();
0 ignored issues
show
Unused Code introduced by
The assignment to $users is dead and can be removed.
Loading history...
149
150
        self::assertFalse($cache->contains('testing_result_cache_id'));
151
152
        $this->em->getConfiguration()->setResultCacheImpl(new ArrayCache());
153
    }
154
155
    /**
156
     * @throws ORMException
157
     */
158
    public function testNativeQueryResultCaching() : NativeQuery
159
    {
160
        $cache = new ArrayCache();
161
        $rsm   = new ResultSetMapping();
162
163
        $rsm->addScalarResult('id', 'u', DBALType::getType('integer'));
164
165
        $query = $this->em->createNativeQuery('select u.id FROM cms_users u WHERE u.id = ?', $rsm);
166
167
        $query->setParameter(1, 10);
168
        $query->setResultCacheDriver($cache)->enableResultCache();
169
170
        self::assertEquals(0, $this->getCacheSize($cache));
171
172
        $query->getResult();
173
174
        self::assertEquals(1, $this->getCacheSize($cache));
175
176
        return $query;
177
    }
178
179
    /**
180
     * @param string $query
181
     * @depends testNativeQueryResultCaching
182
     */
183
    public function testResultCacheNotDependsOnQueryHints($query) : void
184
    {
185
        $cache      = $query->getResultCacheDriver();
186
        $cacheCount = $this->getCacheSize($cache);
187
188
        $query->setHint('foo', 'bar');
189
        $query->getResult();
190
191
        self::assertEquals($cacheCount, $this->getCacheSize($cache));
192
    }
193
194
    /**
195
     * @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...
196
     * @depends testNativeQueryResultCaching
197
     */
198
    public function testResultCacheDependsOnParameters($query) : void
199
    {
200
        $cache      = $query->getResultCacheDriver();
201
        $cacheCount = $this->getCacheSize($cache);
202
203
        $query->setParameter(1, 50);
204
        $query->getResult();
205
206
        self::assertEquals($cacheCount + 1, $this->getCacheSize($cache));
207
    }
208
209
    /**
210
     * @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...
211
     * @depends testNativeQueryResultCaching
212
     */
213
    public function testResultCacheNotDependsOnHydrationMode($query) : void
214
    {
215
        $cache      = $query->getResultCacheDriver();
216
        $cacheCount = $this->getCacheSize($cache);
217
218
        self::assertNotEquals(Query::HYDRATE_ARRAY, $query->getHydrationMode());
219
        $query->getArrayResult();
220
221
        self::assertEquals($cacheCount, $this->getCacheSize($cache));
222
    }
223
224
    /**
225
     * @group DDC-909
226
     */
227
    public function testResultCacheWithObjectParameter() : void
228
    {
229
        $user1           = new CmsUser();
230
        $user1->name     = 'Roman';
231
        $user1->username = 'romanb';
232
        $user1->status   = 'dev';
233
234
        $user2           = new CmsUser();
235
        $user2->name     = 'Benjamin';
236
        $user2->username = 'beberlei';
237
        $user2->status   = 'dev';
238
239
        $article        = new CmsArticle();
240
        $article->text  = 'foo';
241
        $article->topic = 'baz';
242
        $article->user  = $user1;
243
244
        $this->em->persist($article);
245
        $this->em->persist($user1);
246
        $this->em->persist($user2);
247
        $this->em->flush();
248
249
        $query = $this->em->createQuery('select a from Doctrine\Tests\Models\CMS\CmsArticle a WHERE a.user = ?1');
250
        $query->setParameter(1, $user1);
251
252
        $cache = new ArrayCache();
253
254
        $query->setResultCacheDriver($cache)->enableResultCache();
255
256
        $articles = $query->getResult();
257
258
        self::assertCount(1, $articles);
259
        self::assertEquals('baz', $articles[0]->topic);
260
261
        $this->em->clear();
262
263
        $query2 = $this->em->createQuery('select a from Doctrine\Tests\Models\CMS\CmsArticle a WHERE a.user = ?1');
264
        $query2->setParameter(1, $user1);
265
266
        $query2->setResultCacheDriver($cache)->enableResultCache();
267
268
        $articles = $query2->getResult();
269
270
        self::assertCount(1, $articles);
271
        self::assertEquals('baz', $articles[0]->topic);
272
273
        $query3 = $this->em->createQuery('select a from Doctrine\Tests\Models\CMS\CmsArticle a WHERE a.user = ?1');
274
        $query3->setParameter(1, $user2);
275
276
        $query3->setResultCacheDriver($cache)->enableResultCache();
277
278
        $articles = $query3->getResult();
279
280
        self::assertCount(0, $articles);
281
    }
282
}
283