Skip to content

Commit afac739

Browse files
committed
GraphQL-577: Test coverage for tag cache generation for category and products
- address review comments
1 parent 124ad9d commit afac739

File tree

1 file changed

+25
-36
lines changed

1 file changed

+25
-36
lines changed

dev/tests/api-functional/testsuite/Magento/GraphQl/PageCache/CacheTagTest.php

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ public function testCacheTagsAndCacheDebugHeaderForProducts()
3131
$this->markTestSkipped(
3232
'This test will stay skipped until DEVOPS-4924 is resolved'
3333
);
34-
/** @var State $state */
35-
$state = Bootstrap::getObjectManager()->get(State::class);
36-
$state->setMode(State::MODE_DEVELOPER);
3734

3835
$productSku='simple2';
3936
$query
@@ -51,14 +48,13 @@ public function testCacheTagsAndCacheDebugHeaderForProducts()
5148
QUERY;
5249

5350
/** cache-debug should be a MISS when product is queried for first time */
54-
$responseMissHeaders = $this->graphQlQueryForHttpHeaders($query, [], '', []);
55-
preg_match('/X-Magento-Cache-Debug: (.*?)\n/', $responseMissHeaders, $matchesMiss);
56-
$this->assertEquals('MISS', rtrim($matchesMiss[1], "\r"));
51+
$responseMissHeaders = $this->graphQlQueryForHttpHeaders($query);
52+
$this->assertContains('X-Magento-Cache-Debug: MISS', $responseMissHeaders);
5753

5854
/** cache-debug should be a HIT for the second round */
59-
$responseHitHeaders = $this->graphQlQueryForHttpHeaders($query, [], '', []);
60-
preg_match('/X-Magento-Cache-Debug: (.*?)\n/', $responseHitHeaders, $matchesHit);
61-
$this->assertEquals('HIT', rtrim($matchesHit[1], "\r"));
55+
$responseHitHeaders = $this->graphQlQueryForHttpHeaders($query);
56+
//preg_match('/X-Magento-Cache-Debug: (.*?)\n/', $responseHitHeaders, $matchesHit);
57+
$this->assertContains('X-Magento-Cache-Debug: HIT', $responseHitHeaders);
6258

6359
/** @var ProductRepositoryInterface $productRepository */
6460
$productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
@@ -68,17 +64,14 @@ public function testCacheTagsAndCacheDebugHeaderForProducts()
6864
$product->setPrice(15);
6965
$product->save();
7066
/** Cache invalidation happens and cache-debug header value is a MISS after product update */
71-
$responseMissHeaders = $this->graphQlQueryForHttpHeaders($query, [], '', []);
72-
preg_match('/X-Magento-Cache-Debug: (.*?)\n/', $responseMissHeaders, $matchesMiss);
73-
$this->assertEquals('MISS', rtrim($matchesMiss[1], "\r"));
67+
$responseMissHeaders = $this->graphQlQueryForHttpHeaders($query);
68+
$this->assertContains('X-Magento-Cache-Debug: MISS', $responseMissHeaders);
7469

7570
/** checks if cache tags for products are correctly displayed in the response header */
7671
preg_match('/X-Magento-Tags: (.*?)\n/', $responseMissHeaders, $headerCacheTags);
7772
$actualCacheTags = explode(',', rtrim($headerCacheTags[1], "\r"));
7873
$expectedCacheTags=['cat_p','cat_p_' . $product->getId(),'FPC'];
79-
foreach (array_keys($actualCacheTags) as $key) {
80-
$this->assertEquals($expectedCacheTags[$key], $actualCacheTags[$key]);
81-
}
74+
$this->assertEquals($expectedCacheTags, $actualCacheTags);
8275
}
8376

8477
/**
@@ -88,9 +81,9 @@ public function testCacheTagsAndCacheDebugHeaderForProducts()
8881
*/
8982
public function testCacheTagFromResponseHeaderForCategoriesWithProduct()
9083
{
91-
/*$this->markTestSkipped(
84+
$this->markTestSkipped(
9285
'This test will stay skipped until DEVOPS-4924 is resolved'
93-
);*/
86+
);
9487
$firstProductSku = 'simple-4';
9588
$secondProductSku = 'simple-5';
9689
$categoryId ='10';
@@ -146,11 +139,10 @@ public function testCacheTagFromResponseHeaderForCategoriesWithProduct()
146139
}
147140
QUERY;
148141

149-
$responseMissHeaders = $this->graphQlQueryForHttpHeaders($categoryQuery, $variables, '', []);
142+
$responseMissHeaders = $this->graphQlQueryForHttpHeaders($categoryQuery, $variables);
150143

151144
/** cache-debug header value should be a MISS when category is loaded first time */
152-
preg_match('/X-Magento-Cache-Debug: (.*?)\n/', $responseMissHeaders, $matchesMiss);
153-
$this->assertEquals('MISS', rtrim($matchesMiss[1], "\r"));
145+
$this->assertContains('X-Magento-Cache-Debug: MISS', $responseMissHeaders);
154146

155147
/** @var ProductRepositoryInterface $productRepository */
156148
$productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
@@ -165,31 +157,28 @@ public function testCacheTagFromResponseHeaderForCategoriesWithProduct()
165157
$expectedCacheTags =
166158
['cat_c','cat_c_' . $categoryId,'cat_p','cat_p_' . $firstProduct->getId(),'cat_p_' .$secondProduct->getId(),'FPC'];
167159
$this->assertEquals($expectedCacheTags, $actualCacheTags);
160+
168161
// Cach-debug header should be a MISS for product 1 during first load
169-
$responseHeadersFirstProduct = $this->graphQlQueryForHttpHeaders($product1Query, [], '', []);
170-
preg_match('/X-Magento-Cache-Debug: (.*?)\n/', $responseHeadersFirstProduct, $match);
171-
$this->assertEquals('MISS', rtrim($match[1], "\r"));
162+
$responseHeadersFirstProduct = $this->graphQlQueryForHttpHeaders($product1Query);
163+
$this->assertContains('X-Magento-Cache-Debug: MISS', $responseHeadersFirstProduct);
172164

173165
// Cach-debug header should be a MISS for product 2 during first load
174-
$responseHeadersSecondProduct = $this->graphQlQueryForHttpHeaders($product2Query, [], '', []);
175-
preg_match('/X-Magento-Cache-Debug: (.*?)\n/', $responseHeadersSecondProduct, $match);
176-
$this->assertEquals('MISS', rtrim($match[1], "\r"));
166+
$responseHeadersSecondProduct = $this->graphQlQueryForHttpHeaders($product2Query);
167+
$this->assertContains('X-Magento-Cache-Debug: MISS', $responseHeadersSecondProduct);
177168

178-
/** cache-debug header value should be MISS after updating product1 and reloading the category */
169+
/** cache-debug header value should be MISS after updating product1 and reloading the Category */
179170
$firstProduct->setPrice(20);
180171
$firstProduct->save();
181-
$responseMissHeaders = $this->graphQlQueryForHttpHeaders($categoryQuery, $variables, '', []);
172+
$responseMissHeaders = $this->graphQlQueryForHttpHeaders($categoryQuery, $variables);
182173
preg_match('/X-Magento-Cache-Debug: (.*?)\n/', $responseMissHeaders, $matchesMiss);
183174
$this->assertEquals('MISS', rtrim($matchesMiss[1], "\r"));
184175

185176
/** cache-debug should be a MISS for product 1 after it is updated - cache invalidation */
186-
$responseHeadersForProd1 = $this->graphQlQueryForHttpHeaders($product1Query, [], '', []);
187-
preg_match('/X-Magento-Cache-Debug: (.*?)\n/', $responseHeadersForProd1, $match);
188-
$this->assertEquals('MISS', rtrim($match[1], "\r"));
189-
190-
// Cach-debug header should be a HIT for prod 2 during second load since prod 2 should be fetched from cache
191-
$responseHeadersSecondProduct = $this->graphQlQueryForHttpHeaders($product2Query, [], '', []);
192-
preg_match('/X-Magento-Cache-Debug: (.*?)\n/', $responseHeadersSecondProduct, $match);
193-
$this->assertEquals('HIT', rtrim($match[1], "\r"));
177+
$responseHeadersFirstProduct = $this->graphQlQueryForHttpHeaders($product1Query);
178+
$this->assertContains('X-Magento-Cache-Debug: MISS', $responseHeadersFirstProduct);
179+
180+
// Cach-debug header should be a HIT for prod 2 during second load since prod 2 is fetched from cache.
181+
$responseHeadersSecondProduct = $this->graphQlQueryForHttpHeaders($product2Query);
182+
$this->assertContains('X-Magento-Cache-Debug: HIT', $responseHeadersSecondProduct);
194183
}
195184
}

0 commit comments

Comments
 (0)