Skip to content

Commit bd8fa8c

Browse files
committed
MAGE-1245 Refactor integration test
1 parent 4963f2a commit bd8fa8c

File tree

1 file changed

+54
-25
lines changed

1 file changed

+54
-25
lines changed

Test/Integration/Category/CategoryCacheTest.php

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ class CategoryCacheTest extends AbstractController
1717

1818
protected static $cacheResets = [];
1919

20-
protected $url = '/catalog/category/view/id/';
20+
public const BASE_CATEGORY_URL = '/catalog/category/view/id/';
21+
public const TEST_USER_AGENT = 'Foobot';
2122

2223
public static function getCategoryProvider(): array
2324
{
@@ -88,8 +89,7 @@ protected function resetCache(string $testMethod): void
8889
*/
8990
public function testCategoryPlpMissBackendRenderOn(int $categoryId, string $name, bool $hasProducts): void
9091
{
91-
$replace = $this->config->getValue('algoliasearch_instant/instant/replace_categories', ScopeInterface::SCOPE_STORE);
92-
$this->assertEquals(1, $replace,"Replace categories must be enabled for this test.");
92+
$this->assertReplaceCategories();
9393

9494
$this->resetCache(__METHOD__);
9595
$this->dispatchCategoryPlpRequest($categoryId);
@@ -121,8 +121,7 @@ public function testCategoryPlpMissBackendRenderOn(int $categoryId, string $name
121121
*/
122122
public function testCategoryPlpHitBackendRenderOn(int $categoryId, string $name): void
123123
{
124-
$replace = $this->config->getValue('algoliasearch_instant/instant/replace_categories', ScopeInterface::SCOPE_STORE);
125-
$this->assertEquals(1, $replace,"Replace categories must be enabled for this test.");
124+
$this->assertReplaceCategories();
126125

127126
$this->registerPageHitSpy();
128127

@@ -141,8 +140,7 @@ public function testCategoryPlpHitBackendRenderOn(int $categoryId, string $name)
141140
*/
142141
public function testCategoryPlpMissBackendRenderOff(int $categoryId, string $name, bool $hasProducts): void
143142
{
144-
$preventBackend = $this->config->getValue('algoliasearch_advanced/advanced/prevent_backend_rendering', ScopeInterface::SCOPE_STORE);
145-
$this->assertEquals(1, $preventBackend,"Prevent backend rendering must be enabled for this test.");
143+
$this->assertPreventBackend();
146144

147145
$this->resetCache(__METHOD__);
148146
$this->dispatchCategoryPlpRequest($categoryId);
@@ -174,8 +172,7 @@ public function testCategoryPlpMissBackendRenderOff(int $categoryId, string $nam
174172
*/
175173
public function testCategoryPlpHitBackendRenderOff(int $categoryId, string $name): void
176174
{
177-
$preventBackend = $this->config->getValue('algoliasearch_advanced/advanced/prevent_backend_rendering', ScopeInterface::SCOPE_STORE);
178-
$this->assertEquals(1, $preventBackend,"Prevent backend rendering must be enabled for this test.");
175+
$this->assertPreventBackend();
179176

180177
$this->registerPageHitSpy();
181178

@@ -196,14 +193,9 @@ public function testCategoryPlpHitBackendRenderOff(int $categoryId, string $name
196193
*/
197194
public function testCategoryPlpMissBackendRenderWhiteList(int $categoryId, string $name, bool $hasProducts): void
198195
{
199-
$preventBackend = $this->config->getValue('algoliasearch_advanced/advanced/prevent_backend_rendering', ScopeInterface::SCOPE_STORE);
200-
$this->assertEquals(1, $preventBackend,"Prevent backend rendering must be enabled for this test.");
196+
$this->assertPreventBackend();
201197

202-
$testUserAgent = "Foobot";
203-
$whitelist = $this->config->getValue('algoliasearch_advanced/advanced/backend_rendering_allowed_user_agents', ScopeInterface::SCOPE_STORE);
204-
$this->assertStringContainsString($testUserAgent, $whitelist, "Allowed user agents for backend render must include $testUserAgent");
205-
206-
$_SERVER['HTTP_USER_AGENT'] = $testUserAgent;
198+
$this->setupUserAgent();
207199

208200
$this->dispatchCategoryPlpRequest($categoryId);
209201

@@ -236,18 +228,12 @@ public function testCategoryPlpMissBackendRenderWhiteList(int $categoryId, strin
236228
*/
237229
public function testCategoryPlpHitBackendRenderWhiteList(int $categoryId, string $name): void
238230
{
239-
$config = $this->_objectManager->get(ScopeConfigInterface::class);
240-
$preventBackend = $config->getValue('algoliasearch_advanced/advanced/prevent_backend_rendering', ScopeInterface::SCOPE_STORE);
241-
$this->assertEquals(1, $preventBackend,"Prevent backend rendering must be enabled for this test.");
231+
$this->assertPreventBackend();
242232

243-
$testUserAgent = "Foobot";
244-
$whitelist = $this->config->getValue('algoliasearch_advanced/advanced/backend_rendering_allowed_user_agents', ScopeInterface::SCOPE_STORE);
245-
$this->assertStringContainsString($testUserAgent, $whitelist, "Allowed user agents for backend render must include $testUserAgent");
233+
$this->setupUserAgent();
246234

247235
$this->registerPageHitSpy();
248236

249-
$_SERVER['HTTP_USER_AGENT'] = $testUserAgent;
250-
251237
$this->dispatchCategoryPlpRequest($categoryId);
252238
$response = $this->getResponse();
253239
$this->assertEquals(200, $response->getHttpResponseCode(), 'Request failed');
@@ -259,6 +245,46 @@ public function testFullPageCacheAvailable(): void
259245
$this->assertContains('full_page', $types);
260246
}
261247

248+
249+
protected function assertConfig(string $path, string $expected, string $message): void
250+
{
251+
$this->assertEquals($expected, $this->config->getValue($path, ScopeInterface::SCOPE_STORE), $message);
252+
}
253+
254+
protected function assertReplaceCategories(): void
255+
{
256+
$this->assertConfig(
257+
'algoliasearch_instant/instant/replace_categories',
258+
1,
259+
"Replace categories must be enabled for this test."
260+
);
261+
}
262+
263+
protected function assertPreventBackend(): void
264+
{
265+
$this->assertConfig(
266+
'algoliasearch_advanced/advanced/prevent_backend_rendering',
267+
1,
268+
"Prevent backend rendering must be enabled for this test."
269+
);
270+
}
271+
272+
protected function assertUserAgentAllowed(string $userAgent): void
273+
{
274+
$this->assertStringContainsString(
275+
$userAgent,
276+
$this->config->getValue('algoliasearch_advanced/advanced/backend_rendering_allowed_user_agents', ScopeInterface::SCOPE_STORE),
277+
"Allowed user agents for backend render must include $userAgent"
278+
);
279+
}
280+
281+
protected function setupUserAgent(): void
282+
{
283+
$testUserAgent = self::TEST_USER_AGENT;
284+
$this->assertUserAgentAllowed($testUserAgent);
285+
$_SERVER['HTTP_USER_AGENT'] = $testUserAgent;
286+
}
287+
262288
/**
263289
* The \Magento\TestFramework\TestCase\AbstractController::dispatch is flawed for this use case as it does not
264290
* populate the URI which is used to build the cache key in \Magento\Framework\App\PageCache\Identifier::getValue
@@ -285,9 +311,12 @@ protected function dispatchHttpRequest(string $uri): void
285311
*/
286312
protected function getCategoryUrl(int $categoryId): string
287313
{
288-
return $this->url . $categoryId;
314+
return self::BASE_CATEGORY_URL . $categoryId;
289315
}
290316

317+
/**
318+
* Dispatches a request using a properly formatted URL to ensure consistent cache key creation
319+
*/
291320
protected function dispatchCategoryPlpRequest(int $categoryId): void
292321
{
293322
$this->dispatchHttpRequest($this->getCategoryUrl($categoryId));

0 commit comments

Comments
 (0)