@@ -15,6 +15,8 @@ class CategoryCacheTest extends AbstractController
15
15
protected ?CacheManager $ cacheManager ;
16
16
protected ?ScopeConfigInterface $ config ;
17
17
18
+ protected static $ cacheResets = [];
19
+
18
20
protected $ url = '/catalog/category/view/id/ ' ;
19
21
20
22
public static function getCategoryProvider (): array
@@ -36,6 +38,14 @@ protected function setUp(): void
36
38
$ _SERVER ['HTTP_USER_AGENT ' ] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36 ' ;
37
39
}
38
40
41
+ protected function resetCache ($ testMethod ): void
42
+ {
43
+ if (!in_array ($ testMethod , self ::$ cacheResets )) {
44
+ $ this ->cacheManager ->clean (['full_page ' ]);
45
+ self ::$ cacheResets [] = $ testMethod ;
46
+ }
47
+ }
48
+
39
49
public static function setUpBeforeClass (): void
40
50
{
41
51
// self::reindexAll();
@@ -76,8 +86,8 @@ public function testCategoryPlpMissBackendRenderOn(int $categoryId, string $name
76
86
$ replace = $ this ->config ->getValue ('algoliasearch_instant/instant/replace_categories ' , ScopeInterface::SCOPE_STORE );
77
87
$ this ->assertEquals (1 , $ replace ,"Replace categories must be enabled for this test. " );
78
88
79
- $ this ->cacheManager -> clean ([ ' full_page ' ] );
80
- $ this ->dispatchHttpRequest ( $ this -> url . $ categoryId );
89
+ $ this ->resetCache ( __METHOD__ );
90
+ $ this ->dispatchCategoryPlpRequest ( $ categoryId );
81
91
$ response = $ this ->getResponse ();
82
92
$ this ->assertEquals (200 , $ response ->getHttpResponseCode (), 'Request failed ' );
83
93
$ this ->assertEquals (
@@ -102,7 +112,7 @@ public function testCategoryPlpMissBackendRenderOn(int $categoryId, string $name
102
112
* @param string $uri
103
113
* @return void
104
114
*/
105
- public function dispatchHttpRequest (string $ uri ): void
115
+ protected function dispatchHttpRequest (string $ uri ): void
106
116
{
107
117
$ request = $ this ->_objectManager ->get (\Magento \Framework \App \Request \Http::class);
108
118
$ request ->setDispatched (false );
@@ -111,6 +121,22 @@ public function dispatchHttpRequest(string $uri): void
111
121
$ this ->_getBootstrap ()->runApp ();
112
122
}
113
123
124
+ /**
125
+ * It is imperative to always use the same URL format between MISS and HIT to ensure
126
+ * that the cache key is generated consistently
127
+ * @param int $categoryId
128
+ * @return string
129
+ */
130
+ protected function getCategoryUrl (int $ categoryId ): string
131
+ {
132
+ return $ this ->url . $ categoryId ;
133
+ }
134
+
135
+ protected function dispatchCategoryPlpRequest (int $ categoryId ): void
136
+ {
137
+ $ this ->dispatchHttpRequest ($ this ->getCategoryUrl ($ categoryId ));
138
+ }
139
+
114
140
/**
115
141
* @dataProvider getCategoryProvider
116
142
* @depends testCategoryPlpMissBackendRenderOn
@@ -129,7 +155,7 @@ public function testCategoryPlpHitBackendRenderOn(int $categoryId, string $name)
129
155
130
156
$ this ->registerPageHitSpy ();
131
157
132
- $ this ->dispatchHttpRequest ( " catalog/category/view/id/ { $ categoryId}" );
158
+ $ this ->dispatchCategoryPlpRequest ( $ categoryId );
133
159
$ response = $ this ->getResponse ();
134
160
$ this ->assertEquals (200 , $ response ->getHttpResponseCode (), 'Request failed ' );
135
161
}
@@ -150,8 +176,8 @@ public function testCategoryPlpMissBackendRenderOff(int $categoryId, string $nam
150
176
$ preventBackend = $ this ->config ->getValue ('algoliasearch_advanced/advanced/prevent_backend_rendering ' , ScopeInterface::SCOPE_STORE );
151
177
$ this ->assertEquals (1 , $ preventBackend ,"Prevent backend rendering must be enabled for this test. " );
152
178
153
- $ this ->cacheManager -> clean ([ ' full_page ' ] );
154
- $ this ->dispatchHttpRequest ( $ this -> url . $ categoryId );
179
+ $ this ->resetCache ( __METHOD__ );
180
+ $ this ->dispatchCategoryPlpRequest ( $ categoryId );
155
181
$ response = $ this ->getResponse ();
156
182
$ this ->assertEquals (200 , $ response ->getHttpResponseCode (), 'Request failed ' );
157
183
$ this ->assertEquals (
@@ -185,8 +211,9 @@ public function testCategoryPlpHitBackendRenderOff(int $categoryId, string $name
185
211
186
212
$ this ->registerPageHitSpy ();
187
213
188
- $ this ->dispatchHttpRequest ( " catalog/category/view/id/ { $ categoryId}" );
214
+ $ this ->dispatchCategoryPlpRequest ( $ categoryId );
189
215
$ response = $ this ->getResponse ();
216
+
190
217
$ this ->assertEquals (200 , $ response ->getHttpResponseCode (), 'Request failed ' );
191
218
}
192
219
@@ -213,7 +240,8 @@ public function testCategoryPlpMissBackendRenderWhiteList(int $categoryId, strin
213
240
214
241
$ _SERVER ['HTTP_USER_AGENT ' ] = $ testUserAgent ;
215
242
216
- $ this ->dispatchHttpRequest ($ this ->url . $ categoryId );
243
+ $ this ->dispatchCategoryPlpRequest ($ categoryId );
244
+
217
245
$ response = $ this ->getResponse ();
218
246
$ this ->assertEquals (200 , $ response ->getHttpResponseCode (), 'Request failed ' );
219
247
$ this ->assertEquals (
0 commit comments