77
88namespace Magento \GraphQl \CatalogGraphQl ;
99
10- use Magento \GraphQl \GetCustomerAuthenticationHeader ;
1110use Magento \TestFramework \Helper \Bootstrap ;
12- use Magento \TestFramework \ObjectManager ;
1311use 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