Skip to content

Commit c84a7a3

Browse files
committed
GraphQL-577: Test coverage for tag cache generation for category and products
- refactored test
1 parent 66eea55 commit c84a7a3

File tree

1 file changed

+74
-62
lines changed

1 file changed

+74
-62
lines changed

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

Lines changed: 74 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,31 @@
1010
use Magento\Catalog\Api\ProductRepositoryInterface;
1111
use Magento\Catalog\Model\Product;
1212
use Magento\TestFramework\ObjectManager;
13-
use Magento\TestFramework\App\State;
14-
use Magento\TestFramework\Helper\Bootstrap;
1513
use Magento\TestFramework\TestCase\GraphQlAbstract;
1614

17-
/**
18-
* Class CacheTagTest
19-
*/
2015
/**
2116
* Test the caching works properly for products and categories
2217
*/
2318
class CacheTagTest extends GraphQlAbstract
2419
{
20+
2521
/**
26-
* Tests if Magento cache tags and debug headers for products are generated properly
27-
* @magentoApiDataFixture Magento/Catalog/_files/multiple_products.php
22+
* @inheritdoc
2823
*/
29-
public function testCacheTagsAndCacheDebugHeaderForProducts()
24+
protected function setUp()
3025
{
3126
$this->markTestSkipped(
3227
'This test will stay skipped until DEVOPS-4924 is resolved'
3328
);
29+
}
3430

31+
/**
32+
* Tests if Magento cache tags and debug headers for products are generated properly
33+
*
34+
* @magentoApiDataFixture Magento/Catalog/_files/multiple_products.php
35+
*/
36+
public function testCacheTagsAndCacheDebugHeaderForProducts()
37+
{
3538
$productSku='simple2';
3639
$query
3740
= <<<QUERY
@@ -75,70 +78,25 @@ public function testCacheTagsAndCacheDebugHeaderForProducts()
7578
}
7679

7780
/**
78-
* Tests if Magento cache tags for categories are generated properly. Also tests the use case for cache invalidation
81+
* Tests if X-Magento-Tags for categories are generated properly. Also tests the use case for cache invalidation
7982
*
8083
* @magentoApiDataFixture Magento/Catalog/_files/product_in_multiple_categories.php
8184
*/
82-
public function testCacheTagFromResponseHeaderForCategoriesWithProduct()
85+
public function testCacheTagForCategoriesWithProduct()
8386
{
84-
$this->markTestSkipped(
85-
'This test will stay skipped until DEVOPS-4924 is resolved'
86-
);
8787
$firstProductSku = 'simple333';
8888
$secondProductSku = 'simple444';
8989
$categoryId ='4';
90-
$categoryQuery
91-
= <<<'QUERY'
92-
query GetCategoryQuery($id: Int!, $pageSize: Int!, $currentPage: Int!) {
93-
category(id: $id) {
94-
id
95-
description
96-
name
97-
product_count
98-
products(pageSize: $pageSize, currentPage: $currentPage) {
99-
items {
100-
id
101-
name
102-
url_key
103-
}
104-
total_count
105-
}
106-
}
107-
}
108-
QUERY;
90+
10991
$variables =[
110-
'id' => 4,
92+
'id' => $categoryId,
11193
'pageSize'=> 10,
11294
'currentPage' => 1
11395
];
11496

115-
$product1Query
116-
= <<<QUERY
117-
{
118-
products(filter: {sku: {eq: "{$firstProductSku}"}})
119-
{
120-
items {
121-
id
122-
name
123-
sku
124-
}
125-
}
126-
}
127-
QUERY;
128-
$product2Query
129-
= <<<QUERY
130-
{
131-
products(filter: {sku: {eq: "{$secondProductSku}"}})
132-
{
133-
items {
134-
id
135-
name
136-
sku
137-
}
138-
}
139-
}
140-
QUERY;
141-
97+
$product1Query = $this->getProductQuery($firstProductSku);
98+
$product2Query =$this->getProductQuery($secondProductSku);
99+
$categoryQuery = $this->getCategoryQuery();
142100
$responseMissHeaders = $this->graphQlQueryForHttpHeaders($categoryQuery, $variables);
143101

144102
/** cache-debug header value should be a MISS when category is loaded first time */
@@ -155,10 +113,12 @@ public function testCacheTagFromResponseHeaderForCategoriesWithProduct()
155113
preg_match('/X-Magento-Tags: (.*?)\n/', $responseMissHeaders, $headerCacheTags);
156114
$actualCacheTags = explode(',', rtrim($headerCacheTags[1], "\r"));
157115
$expectedCacheTags =
158-
['cat_c','cat_c_' . $categoryId,'cat_p','cat_p_' . $firstProduct->getId(),'cat_p_' .$secondProduct->getId(),'FPC'];
116+
['cat_c','cat_c_' . $categoryId,'cat_p','cat_p_' . $firstProduct->getId(),
117+
'cat_p_' .$secondProduct->getId(),'FPC'
118+
];
159119
$this->assertEquals($expectedCacheTags, $actualCacheTags);
160120

161-
// Cach-debug header should be a MISS for product 1 during first load
121+
// Cach-debug header should be a MISS for product 1 on first request
162122
$responseHeadersFirstProduct = $this->graphQlQueryForHttpHeaders($product1Query);
163123
$this->assertContains('X-Magento-Cache-Debug: MISS', $responseHeadersFirstProduct);
164124

@@ -181,4 +141,56 @@ public function testCacheTagFromResponseHeaderForCategoriesWithProduct()
181141
$responseHeadersSecondProduct = $this->graphQlQueryForHttpHeaders($product2Query);
182142
$this->assertContains('X-Magento-Cache-Debug: HIT', $responseHeadersSecondProduct);
183143
}
144+
145+
/**
146+
* Get Product query
147+
*
148+
* @param string $productSku
149+
* @return string
150+
*/
151+
private function getProductQuery(string $productSku): string
152+
{
153+
$productQuery = <<<QUERY
154+
{
155+
products(filter: {sku: {eq: "{$productSku}"}})
156+
{
157+
items {
158+
id
159+
name
160+
sku
161+
}
162+
}
163+
}
164+
QUERY;
165+
return $productQuery;
166+
}
167+
168+
/**
169+
* Get category query
170+
*
171+
* @return string
172+
*/
173+
private function getCategoryQuery(): string
174+
{
175+
$categoryQueryString = <<<QUERY
176+
query GetCategoryQuery(\$id: Int!, \$pageSize: Int!, \$currentPage: Int!) {
177+
category(id: \$id) {
178+
id
179+
description
180+
name
181+
product_count
182+
products(pageSize: \$pageSize, currentPage: \$currentPage) {
183+
items {
184+
id
185+
name
186+
url_key
187+
}
188+
total_count
189+
}
190+
}
191+
}
192+
QUERY;
193+
194+
return $categoryQueryString;
195+
}
184196
}

0 commit comments

Comments
 (0)