Skip to content

Commit 489019f

Browse files
authored
LYNX-63: Refactored tests for ProductAttributeFilterInput of products query in CatalogGraphQl module (#82)
* LYNX-63: Refactored tests for ProductAttributeFilterInput of products query in CatalogGraphQl module * LYNX-63: Refactored tests for ProductAttributeFilterInput of products query in CatalogGraphQl module * LYNX-63: Refactored tests for ProductAttributeFilterInput of products query in CatalogGraphQl module * LYNX-63: Refactored tests for ProductAttributeFilterInput of products query in CatalogGraphQl module * LYNX-63: Fix static test in ProductSearchTest.php * LYNX-63: Fix for Product Search test with search suggestions * LYNX-63: Fixture for Indexer
1 parent 79f4f31 commit 489019f

File tree

2 files changed

+169
-62
lines changed

2 files changed

+169
-62
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Indexer\Test\Fixture;
9+
10+
use Magento\Framework\DataObject;
11+
use Magento\TestFramework\Fixture\DataFixtureInterface;
12+
use Magento\Indexer\Model\Indexer as IndexerModel;
13+
use Magento\Indexer\Model\Indexer\Collection;
14+
15+
class Indexer implements DataFixtureInterface
16+
{
17+
/**
18+
* @var Collection
19+
*/
20+
private Collection $indexerCollection;
21+
22+
/**
23+
* @param Collection $indexerCollection
24+
*/
25+
public function __construct(
26+
Collection $indexerCollection
27+
) {
28+
$this->indexerCollection = $indexerCollection;
29+
}
30+
31+
/**
32+
* {@inheritdoc}
33+
* @param array $data Parameters
34+
*/
35+
public function apply(array $data = []): ?DataObject
36+
{
37+
$this->indexerCollection->load();
38+
/** @var IndexerModel $indexer */
39+
foreach ($this->indexerCollection->getItems() as $indexer) {
40+
$indexer->reindexAll();
41+
}
42+
return null;
43+
}
44+
}

dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogGraphQl/ProductSearchTest.php

Lines changed: 125 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77

88
namespace Magento\GraphQl\CatalogGraphQl;
99

10-
use Magento\GraphQl\GetCustomerAuthenticationHeader;
1110
use Magento\TestFramework\Helper\Bootstrap;
12-
use Magento\TestFramework\ObjectManager;
1311
use Magento\TestFramework\TestCase\GraphQlAbstract;
12+
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
13+
use Magento\TestFramework\Fixture\DataFixture;
14+
use Magento\Catalog\Test\Fixture\Product;
15+
use Magento\Indexer\Test\Fixture\Indexer;
1416

1517
/**
1618
* Test class to verify product search, used for GraphQL resolver
@@ -24,35 +26,11 @@ class ProductSearchTest extends GraphQlAbstract
2426
private $objectManager;
2527

2628
/**
27-
* @var GetCustomerAuthenticationHeader
29+
* Test setup
2830
*/
29-
private $getCustomerAuthenticationHeader;
30-
3131
protected function setUp(): void
3232
{
3333
$this->objectManager = Bootstrap::getObjectManager();
34-
$this->getCustomerAuthenticationHeader = $this->objectManager->get(GetCustomerAuthenticationHeader::class);
35-
}
36-
37-
/**
38-
* Test for checking if graphQL query fpr configurable product returns
39-
* expected visible items
40-
*
41-
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/configurable_products_with_different_super_attribute.php
42-
*/
43-
public function testCheckIfConfigurableProductVisibilityReturnsExpectedItem(): void
44-
{
45-
$productName = 'Configurable Product';
46-
$productSku = 'configurable';
47-
$query = $this->getProductSearchQuery($productName, $productSku);
48-
49-
$response = $this->graphQlQuery($query);
50-
51-
$this->assertNotEmpty($response['products']);
52-
$this->assertEquals(1, $response['products']['total_count']);
53-
$this->assertNotEmpty($response['products']['items']);
54-
$this->assertEquals($productName, $response['products']['items'][0]['name']);
55-
$this->assertEquals($productSku, $response['products']['items'][0]['sku']);
5634
}
5735

5836
/**
@@ -65,56 +43,141 @@ public function testCheckIfConfigurableProductVisibilityReturnsExpectedItem(): v
6543
private function getProductSearchQuery(string $productName, string $productSku): string
6644
{
6745
return <<<QUERY
68-
{
69-
products(filter: {sku: {eq: "{$productSku}"}}, search: "$productName", sort: {}, pageSize: 200, currentPage: 1) {
70-
total_count
71-
page_info {
72-
total_pages
73-
current_page
74-
page_size
75-
}
76-
items {
77-
name
78-
sku
79-
}
80-
}
81-
}
82-
QUERY;
46+
{
47+
products(filter: {
48+
sku: {
49+
eq: "{$productSku}"
50+
}},
51+
search: "$productName",
52+
sort: {},
53+
pageSize: 200,
54+
currentPage: 1)
55+
{
56+
total_count
57+
page_info {
58+
total_pages
59+
current_page
60+
page_size
61+
}
62+
items {
63+
name
64+
sku
65+
}
66+
}
67+
}
68+
QUERY;
8369
}
8470

8571
/**
86-
* @magentoApiDataFixture Magento/Catalog/_files/products_with_layered_navigation_attribute.php
72+
* Get a query which filters list of found products by array of SKUs
73+
*
74+
* @param array $products
75+
* @param string $product
76+
* @return string
8777
*/
88-
public function testSearchSuggestions() : void
78+
private function getProductSearchQueryWithMultipleSkusFilter(array $products, string $product): string
8979
{
90-
$query = $this->getSearchQueryWithSuggestions();
91-
$response = $this->graphQlQuery($query);
92-
$this->assertNotEmpty($response['products']);
93-
$this->assertEmpty($response['products']['items']);
94-
$this->assertNotEmpty($response['products']['suggestions']);
80+
return <<<QUERY
81+
{
82+
products(filter: {
83+
sku: {
84+
in: [
85+
"{$products[0]->getSku()}",
86+
"{$products[1]->getSku()}",
87+
"{$products[2]->getSku()}"
88+
]
89+
}},
90+
search: "$product",
91+
sort: {},
92+
pageSize: 200,
93+
currentPage: 1)
94+
{
95+
total_count
96+
page_info {
97+
total_pages
98+
current_page
99+
page_size
100+
}
101+
items {
102+
name
103+
sku
104+
}
105+
}
106+
}
107+
QUERY;
95108
}
96109

97110
/**
98111
* Prepare search query with suggestions
99112
*
100113
* @return string
101114
*/
102-
private function getSearchQueryWithSuggestions() : string
115+
private function getSearchQueryWithSuggestions(): string
103116
{
104117
return <<<QUERY
105-
{
106-
products(
107-
search: "smiple"
108-
) {
109-
items {
110-
name
111-
sku
118+
{
119+
products(
120+
search: "smiple"
121+
) {
122+
items {
123+
name
124+
sku
125+
}
126+
suggestions {
127+
search
128+
}
129+
}
130+
}
131+
QUERY;
112132
}
113-
suggestions {
114-
search
133+
134+
#[
135+
DataFixture(Product::class, as: 'product')
136+
]
137+
public function testSearchProductsWithSkuEqFilterQuery(): void
138+
{
139+
/** @var \Magento\Catalog\Model\Product $product */
140+
$product = DataFixtureStorageManager::getStorage()->get('product');
141+
$response = $this->graphQlQuery($this->getProductSearchQuery($product->getName(), $product->getSku()));
142+
143+
$this->assertNotEmpty($response['products']);
144+
$this->assertEquals(1, $response['products']['total_count']);
145+
$this->assertNotEmpty($response['products']['items']);
146+
$this->assertEquals($product->getName(), $response['products']['items'][0]['name']);
147+
$this->assertEquals($product->getSku(), $response['products']['items'][0]['sku']);
115148
}
116-
}
117-
}
118-
QUERY;
149+
150+
#[
151+
DataFixture(Product::class, as: 'product1'),
152+
DataFixture(Product::class, as: 'product2'),
153+
DataFixture(Product::class, as: 'product3')
154+
]
155+
public function testSearchProductsWithMultipleSkuInFilterQuery(): void
156+
{
157+
/** @var \Magento\Catalog\Model\Product $product */
158+
$response = $this->graphQlQuery(
159+
$this->getProductSearchQueryWithMultipleSkusFilter([
160+
DataFixtureStorageManager::getStorage()->get('product1'),
161+
DataFixtureStorageManager::getStorage()->get('product2'),
162+
DataFixtureStorageManager::getStorage()->get('product3')
163+
], "simple")
164+
);
165+
166+
$this->assertNotEmpty($response['products']);
167+
$this->assertEquals(3, $response['products']['total_count']);
168+
$this->assertNotEmpty($response['products']['items']);
169+
}
170+
171+
#[
172+
DataFixture(Product::class, as: 'product1'),
173+
DataFixture(Product::class, as: 'product2'),
174+
DataFixture(Indexer::class, as: 'indexer')
175+
]
176+
public function testSearchSuggestions(): void
177+
{
178+
$response = $this->graphQlQuery($this->getSearchQueryWithSuggestions());
179+
$this->assertNotEmpty($response['products']);
180+
$this->assertEmpty($response['products']['items']);
181+
$this->assertNotEmpty($response['products']['suggestions']);
119182
}
120183
}

0 commit comments

Comments
 (0)