Skip to content

Commit afebfa9

Browse files
committed
MAGE-1245 Selectively refresh cache for miss method tests
1 parent aebe90e commit afebfa9

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

Test/Integration/Category/CategoryCacheTest.php

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class CategoryCacheTest extends AbstractController
1515
protected ?CacheManager $cacheManager;
1616
protected ?ScopeConfigInterface $config;
1717

18+
protected static $cacheResets = [];
19+
1820
protected $url = '/catalog/category/view/id/';
1921

2022
public static function getCategoryProvider(): array
@@ -36,6 +38,14 @@ protected function setUp(): void
3638
$_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';
3739
}
3840

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+
3949
public static function setUpBeforeClass(): void
4050
{
4151
// self::reindexAll();
@@ -76,8 +86,8 @@ public function testCategoryPlpMissBackendRenderOn(int $categoryId, string $name
7686
$replace = $this->config->getValue('algoliasearch_instant/instant/replace_categories', ScopeInterface::SCOPE_STORE);
7787
$this->assertEquals(1, $replace,"Replace categories must be enabled for this test.");
7888

79-
$this->cacheManager->clean(['full_page']);
80-
$this->dispatchHttpRequest($this->url . $categoryId);
89+
$this->resetCache(__METHOD__);
90+
$this->dispatchCategoryPlpRequest($categoryId);
8191
$response = $this->getResponse();
8292
$this->assertEquals(200, $response->getHttpResponseCode(), 'Request failed');
8393
$this->assertEquals(
@@ -102,7 +112,7 @@ public function testCategoryPlpMissBackendRenderOn(int $categoryId, string $name
102112
* @param string $uri
103113
* @return void
104114
*/
105-
public function dispatchHttpRequest(string $uri): void
115+
protected function dispatchHttpRequest(string $uri): void
106116
{
107117
$request = $this->_objectManager->get(\Magento\Framework\App\Request\Http::class);
108118
$request->setDispatched(false);
@@ -111,6 +121,22 @@ public function dispatchHttpRequest(string $uri): void
111121
$this->_getBootstrap()->runApp();
112122
}
113123

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+
114140
/**
115141
* @dataProvider getCategoryProvider
116142
* @depends testCategoryPlpMissBackendRenderOn
@@ -129,7 +155,7 @@ public function testCategoryPlpHitBackendRenderOn(int $categoryId, string $name)
129155

130156
$this->registerPageHitSpy();
131157

132-
$this->dispatchHttpRequest("catalog/category/view/id/{$categoryId}");
158+
$this->dispatchCategoryPlpRequest($categoryId);
133159
$response = $this->getResponse();
134160
$this->assertEquals(200, $response->getHttpResponseCode(), 'Request failed');
135161
}
@@ -150,8 +176,8 @@ public function testCategoryPlpMissBackendRenderOff(int $categoryId, string $nam
150176
$preventBackend = $this->config->getValue('algoliasearch_advanced/advanced/prevent_backend_rendering', ScopeInterface::SCOPE_STORE);
151177
$this->assertEquals(1, $preventBackend,"Prevent backend rendering must be enabled for this test.");
152178

153-
$this->cacheManager->clean(['full_page']);
154-
$this->dispatchHttpRequest($this->url . $categoryId);
179+
$this->resetCache(__METHOD__);
180+
$this->dispatchCategoryPlpRequest($categoryId);
155181
$response = $this->getResponse();
156182
$this->assertEquals(200, $response->getHttpResponseCode(), 'Request failed');
157183
$this->assertEquals(
@@ -185,8 +211,9 @@ public function testCategoryPlpHitBackendRenderOff(int $categoryId, string $name
185211

186212
$this->registerPageHitSpy();
187213

188-
$this->dispatchHttpRequest("catalog/category/view/id/{$categoryId}");
214+
$this->dispatchCategoryPlpRequest($categoryId);
189215
$response = $this->getResponse();
216+
190217
$this->assertEquals(200, $response->getHttpResponseCode(), 'Request failed');
191218
}
192219

@@ -213,7 +240,8 @@ public function testCategoryPlpMissBackendRenderWhiteList(int $categoryId, strin
213240

214241
$_SERVER['HTTP_USER_AGENT'] = $testUserAgent;
215242

216-
$this->dispatchHttpRequest($this->url . $categoryId);
243+
$this->dispatchCategoryPlpRequest($categoryId);
244+
217245
$response = $this->getResponse();
218246
$this->assertEquals(200, $response->getHttpResponseCode(), 'Request failed');
219247
$this->assertEquals(

0 commit comments

Comments
 (0)