7
7
8
8
namespace Magento \GraphQlCache \Controller ;
9
9
10
- use PHPUnit \Framework \TestCase ;
11
- use Magento \TestFramework \ObjectManager ;
10
+ use Magento \Framework \App \Request \Http as HttpRequest ;
11
+ use Magento \Framework \App \Response \HttpInterface as HttpResponse ;
12
+ use Magento \Framework \Registry ;
13
+ use Magento \GraphQl \Controller \GraphQl as GraphQlController ;
14
+ use Magento \GraphQlCache \Model \CacheableQuery ;
15
+ use Magento \PageCache \Model \Cache \Type as PageCache ;
12
16
use Magento \TestFramework \Helper \Bootstrap ;
17
+ use Magento \TestFramework \ObjectManager ;
18
+ use PHPUnit \Framework \TestCase ;
13
19
14
20
/**
15
21
* Abstract test class for Graphql cache tests
@@ -21,40 +27,114 @@ abstract class AbstractGraphqlCacheTest extends TestCase
21
27
*/
22
28
protected $ objectManager ;
23
29
24
- /**
25
- * @inheritdoc
26
- */
27
30
protected function setUp (): void
28
31
{
29
32
$ this ->objectManager = Bootstrap::getObjectManager ();
33
+ $ this ->enablePageCachePlugin ();
34
+ $ this ->enableCachebleQueryTestProxy ();
35
+ }
36
+
37
+ protected function tearDown (): void
38
+ {
39
+ $ this ->disableCacheableQueryTestProxy ();
40
+ $ this ->disablePageCachePlugin ();
41
+ $ this ->flushPageCache ();
42
+ }
43
+
44
+ protected function enablePageCachePlugin (): void
45
+ {
46
+ /** @var $registry Registry */
47
+ $ registry = $ this ->objectManager ->get (Registry::class);
48
+ $ registry ->register ('use_page_cache_plugin ' , true , true );
49
+ }
50
+
51
+ protected function disablePageCachePlugin (): void
52
+ {
53
+ /** @var $registry Registry */
54
+ $ registry = $ this ->objectManager ->get (Registry::class);
55
+ $ registry ->unregister ('use_page_cache_plugin ' );
56
+ }
57
+
58
+ protected function flushPageCache (): void
59
+ {
60
+ /** @var PageCache $fullPageCache */
61
+ $ fullPageCache = $ this ->objectManager ->get (PageCache::class);
62
+ $ fullPageCache ->clean ();
30
63
}
31
64
32
65
/**
33
- * Prepare a query and return a request to be used in the same test end to end
66
+ * Regarding the SuppressWarnings annotation below: the nested class below triggers a false rule match.
34
67
*
35
- * @param string $query
36
- * @return \Magento\Framework\App\Request\Http
68
+ * @SuppressWarnings(PHPMD.UnusedLocalVariable)
37
69
*/
38
- protected function prepareRequest (string $ query ) : \Magento \Framework \App \Request \Http
39
- {
40
- $ cacheableQuery = $ this ->objectManager ->get (\Magento \GraphQlCache \Model \CacheableQuery::class);
41
- $ cacheableQueryReflection = new \ReflectionProperty (
42
- $ cacheableQuery ,
43
- 'cacheTags '
44
- );
45
- $ cacheableQueryReflection ->setAccessible (true );
46
- $ cacheableQueryReflection ->setValue ($ cacheableQuery , []);
47
-
48
- /** @var \Magento\Framework\UrlInterface $urlInterface */
49
- $ urlInterface = $ this ->objectManager ->create (\Magento \Framework \UrlInterface::class);
50
- //set unique URL
51
- $ urlInterface ->setQueryParam ('query ' , $ query );
52
-
53
- $ request = $ this ->objectManager ->get (\Magento \Framework \App \Request \Http::class);
54
- $ request ->setUri ($ urlInterface ->getUrl ('graphql ' ));
70
+ private function enableCachebleQueryTestProxy (): void
71
+ {
72
+ $ cacheableQueryProxy = new class ($ this ->objectManager ) extends CacheableQuery {
73
+ /** @var CacheableQuery */
74
+ private $ delegate ;
75
+
76
+ public function __construct (ObjectManager $ objectManager )
77
+ {
78
+ $ this ->reset ($ objectManager );
79
+ }
80
+
81
+ public function reset (ObjectManager $ objectManager ): void
82
+ {
83
+ $ this ->delegate = $ objectManager ->create (CacheableQuery::class);
84
+ }
85
+
86
+ public function getCacheTags (): array
87
+ {
88
+ return $ this ->delegate ->getCacheTags ();
89
+ }
90
+
91
+ public function addCacheTags (array $ cacheTags ): void
92
+ {
93
+ $ this ->delegate ->addCacheTags ($ cacheTags );
94
+ }
95
+
96
+ public function isCacheable (): bool
97
+ {
98
+ return $ this ->delegate ->isCacheable ();
99
+ }
100
+
101
+ public function setCacheValidity (bool $ cacheable ): void
102
+ {
103
+ $ this ->delegate ->setCacheValidity ($ cacheable );
104
+ }
105
+
106
+ public function shouldPopulateCacheHeadersWithTags (): bool
107
+ {
108
+ return $ this ->delegate ->shouldPopulateCacheHeadersWithTags ();
109
+ }
110
+ };
111
+ $ this ->objectManager ->addSharedInstance ($ cacheableQueryProxy , CacheableQuery::class);
112
+ }
113
+
114
+ private function disableCacheableQueryTestProxy (): void
115
+ {
116
+ $ this ->resetQueryCacheTags ();
117
+ $ this ->objectManager ->removeSharedInstance (CacheableQuery::class);
118
+ }
119
+
120
+ protected function resetQueryCacheTags (): void
121
+ {
122
+ $ this ->objectManager ->get (CacheableQuery::class)->reset ($ this ->objectManager );
123
+ }
124
+
125
+ protected function dispatchGraphQlGETRequest (array $ queryParams ): HttpResponse
126
+ {
127
+ $ this ->resetQueryCacheTags ();
128
+
129
+ /** @var HttpRequest $request */
130
+ $ request = $ this ->objectManager ->get (HttpRequest::class);
131
+ $ request ->setPathInfo ('/graphql ' );
55
132
$ request ->setMethod ('GET ' );
56
- //set the actual GET query
57
- $ request ->setQueryValue ('query ' , $ query );
58
- return $ request ;
133
+ $ request ->setParams ($ queryParams );
134
+
135
+ // required for \Magento\Framework\App\PageCache\Identifier to generate the correct cache key
136
+ $ request ->setUri (implode ('? ' , [$ request ->getPathInfo (), http_build_query ($ queryParams )]));
137
+
138
+ return $ this ->objectManager ->create (GraphQlController::class)->dispatch ($ request );
59
139
}
60
140
}
0 commit comments