4
4
5
5
namespace Doctrine \Tests \ORM \Functional ;
6
6
7
- use Doctrine \Common \Cache \ArrayCache ;
8
7
use Doctrine \Common \Cache \Cache ;
9
8
use Doctrine \Common \Cache \CacheProvider ;
9
+ use Doctrine \Common \Cache \Psr6 \DoctrineProvider ;
10
10
use Doctrine \ORM \Query ;
11
11
use Doctrine \ORM \Query \Exec \AbstractSqlExecutor ;
12
12
use Doctrine \ORM \Query \ParserResult ;
13
13
use Doctrine \Tests \OrmFunctionalTestCase ;
14
- use ReflectionProperty ;
14
+ use Symfony \ Component \ Cache \ Adapter \ ArrayAdapter ;
15
15
16
- use function class_exists ;
16
+ use function assert ;
17
17
use function count ;
18
18
19
- /**
20
- * QueryCacheTest
21
- */
22
19
class QueryCacheTest extends OrmFunctionalTestCase
23
20
{
24
- /** @var ReflectionProperty */
25
- private $ cacheDataReflection ;
26
-
27
21
protected function setUp (): void
28
22
{
29
- if (! class_exists (ArrayCache::class)) {
30
- $ this ->markTestSkipped ('Test only applies with doctrine/cache 1.x ' );
31
- }
32
-
33
- $ this ->cacheDataReflection = new ReflectionProperty (ArrayCache::class, 'data ' );
34
- $ this ->cacheDataReflection ->setAccessible (true );
35
-
36
23
$ this ->useModelSet ('cms ' );
37
24
38
25
parent ::setUp ();
39
26
}
40
27
41
- private function getCacheSize (ArrayCache $ cache ): int
42
- {
43
- return count ($ this ->cacheDataReflection ->getValue ($ cache ));
44
- }
45
-
46
- public function testQueryCacheDependsOnHints (): Query
28
+ public function testQueryCacheDependsOnHints (): array
47
29
{
48
30
$ query = $ this ->_em ->createQuery ('select ux from Doctrine\Tests\Models\CMS\CmsUser ux ' );
49
31
50
- $ cache = new ArrayCache ();
51
- $ query ->setQueryCacheDriver ($ cache );
32
+ $ cache = new ArrayAdapter ();
33
+ $ query ->setQueryCacheDriver (DoctrineProvider:: wrap ( $ cache) );
52
34
53
35
$ query ->getResult ();
54
- $ this -> assertEquals ( 1 , $ this -> getCacheSize ( $ cache ));
36
+ self :: assertCount ( 2 , $ cache -> getValues ( ));
55
37
56
38
$ query ->setHint ('foo ' , 'bar ' );
57
39
58
40
$ query ->getResult ();
59
- $ this -> assertEquals ( 2 , $ this -> getCacheSize ( $ cache ));
41
+ self :: assertCount ( 3 , $ cache -> getValues ( ));
60
42
61
- return $ query ;
43
+ return [ $ query, $ cache ] ;
62
44
}
63
45
64
46
/**
65
- * @param <type> $query
66
- *
67
47
* @depends testQueryCacheDependsOnHints
68
48
*/
69
- public function testQueryCacheDependsOnFirstResult ($ query ): void
49
+ public function testQueryCacheDependsOnFirstResult (array $ previous ): void
70
50
{
71
- $ cache = $ query ->getQueryCacheDriver ();
72
- $ cacheCount = $ this ->getCacheSize ($ cache );
51
+ [$ query , $ cache ] = $ previous ;
52
+ assert ($ query instanceof Query);
53
+ assert ($ cache instanceof ArrayAdapter);
54
+
55
+ $ cacheCount = count ($ cache ->getValues ());
73
56
74
57
$ query ->setFirstResult (10 );
75
58
$ query ->setMaxResults (9999 );
76
59
77
60
$ query ->getResult ();
78
- $ this -> assertEquals ($ cacheCount + 1 , $ this -> getCacheSize ( $ cache ));
61
+ self :: assertCount ($ cacheCount + 1 , $ cache -> getValues ( ));
79
62
}
80
63
81
64
/**
82
- * @param <type> $query
83
- *
84
65
* @depends testQueryCacheDependsOnHints
85
66
*/
86
- public function testQueryCacheDependsOnMaxResults ($ query ): void
67
+ public function testQueryCacheDependsOnMaxResults (array $ previous ): void
87
68
{
88
- $ cache = $ query ->getQueryCacheDriver ();
89
- $ cacheCount = $ this ->getCacheSize ($ cache );
69
+ [$ query , $ cache ] = $ previous ;
70
+ assert ($ query instanceof Query);
71
+ assert ($ cache instanceof ArrayAdapter);
72
+
73
+ $ cacheCount = count ($ cache ->getValues ());
90
74
91
75
$ query ->setMaxResults (10 );
92
76
93
77
$ query ->getResult ();
94
- $ this -> assertEquals ($ cacheCount + 1 , $ this -> getCacheSize ( $ cache ));
78
+ self :: assertCount ($ cacheCount + 1 , $ cache -> getValues ( ));
95
79
}
96
80
97
81
/**
98
- * @param <type> $query
99
- *
100
82
* @depends testQueryCacheDependsOnHints
101
83
*/
102
- public function testQueryCacheDependsOnHydrationMode ($ query ): void
84
+ public function testQueryCacheDependsOnHydrationMode (array $ previous ): void
103
85
{
104
- $ cache = $ query ->getQueryCacheDriver ();
105
- $ cacheCount = $ this ->getCacheSize ($ cache );
86
+ [$ query , $ cache ] = $ previous ;
87
+ assert ($ query instanceof Query);
88
+ assert ($ cache instanceof ArrayAdapter);
89
+
90
+ $ cacheCount = count ($ cache ->getValues ());
106
91
107
92
$ query ->getArrayResult ();
108
- $ this -> assertEquals ($ cacheCount + 1 , $ this -> getCacheSize ( $ cache ));
93
+ self :: assertCount ($ cacheCount + 1 , $ cache -> getValues ( ));
109
94
}
110
95
111
96
public function testQueryCacheNoHitSaveParserResult (): void
112
97
{
113
- $ this ->_em ->getConfiguration ()->setQueryCacheImpl (new ArrayCache ( ));
98
+ $ this ->_em ->getConfiguration ()->setQueryCacheImpl ($ this -> createMock (Cache::class ));
114
99
115
100
$ query = $ this ->_em ->createQuery ('select ux from Doctrine\Tests\Models\CMS\CmsUser ux ' );
116
101
@@ -128,7 +113,7 @@ public function testQueryCacheNoHitSaveParserResult(): void
128
113
129
114
public function testQueryCacheHitDoesNotSaveParserResult (): void
130
115
{
131
- $ this ->_em ->getConfiguration ()->setQueryCacheImpl (new ArrayCache ( ));
116
+ $ this ->_em ->getConfiguration ()->setQueryCacheImpl ($ this -> createMock (Cache::class ));
132
117
133
118
$ query = $ this ->_em ->createQuery ('select ux from Doctrine\Tests\Models\CMS\CmsUser ux ' );
134
119
@@ -167,6 +152,6 @@ public function testQueryCacheHitDoesNotSaveParserResult(): void
167
152
168
153
$ query ->setQueryCacheDriver ($ cache );
169
154
170
- $ users = $ query ->getResult ();
155
+ $ query ->getResult ();
171
156
}
172
157
}
0 commit comments