@@ -115,14 +115,25 @@ protected function setUp(): void
115
115
}
116
116
117
117
/**
118
- * Verify that products returned in a correct order
118
+ * Verify that products returned in a correct order
119
119
*
120
- * @magentoApiDataFixture Magento/Catalog/_files/products_for_search.php
121
120
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
121
+ * @dataProvider sortByPriceAndNameDataProvider
122
122
*/
123
- public function testSortMultipleFieldsSentInVariables (): void
123
+ #[
124
+ DataFixture(ProductFixture::class, ['price ' => 10 , 'name ' => 'search product 1 ' ], 'prod1 ' ),
125
+ DataFixture(ProductFixture::class, ['price ' => 10 , 'name ' => 'search product 2 ' ], 'prod2 ' ),
126
+ DataFixture(ProductFixture::class, ['price ' => 20 , 'name ' => 'search product 3 ' ], 'prod3 ' ),
127
+ DataFixture(ProductFixture::class, ['price ' => 30 , 'name ' => 'search product 4 ' ], 'prod4 ' ),
128
+ DataFixture(ProductFixture::class, ['price ' => 40 , 'name ' => 'search product 5 ' ], 'prod5 ' ),
129
+ ]
130
+ public function testSortMultipleFieldsSentInVariables ($ sort , $ expectedOrder ): void
124
131
{
125
- $ queryAsc = <<<'QUERY'
132
+ $ expectedOrderSku = [];
133
+ foreach ($ expectedOrder as $ productName ) {
134
+ $ expectedOrderSku [] = $ this ->fixture ->get ($ productName )->getSku ();
135
+ }
136
+ $ query = <<<'QUERY'
126
137
query GetProductsQuery(
127
138
$search: String,
128
139
$filter: ProductAttributeFilterInput,
@@ -162,85 +173,37 @@ public function testSortMultipleFieldsSentInVariables(): void
162
173
'filter ' => [],
163
174
'pageSize ' => 24 ,
164
175
'currentPage ' => 1 ,
165
- 'sort ' => [ ' price ' => ' ASC ' , ' name ' => ' ASC ' ]
176
+ 'sort ' => $ sort
166
177
];
167
- $ response = $ this ->graphQlQuery ($ queryAsc , $ variables );
168
- $ this ->assertEquals (5 , $ response ['products ' ]['total_count ' ]);
169
- $ prod1 = $ this ->productRepository ->get ('search_product_1 ' );
170
- $ prod2 = $ this ->productRepository ->get ('search_product_2 ' );
171
- $ prod3 = $ this ->productRepository ->get ('search_product_3 ' );
172
- $ prod4 = $ this ->productRepository ->get ('search_product_4 ' );
173
- $ prod5 = $ this ->productRepository ->get ('search_product_5 ' );
174
-
175
- $ filteredProducts = [$ prod1 , $ prod2 , $ prod3 , $ prod4 , $ prod5 ];
176
- $ productItemsInResponse = array_map (null , $ response ['products ' ]['items ' ], $ filteredProducts );
177
- foreach ($ productItemsInResponse as $ itemIndex => $ itemArray ) {
178
- $ this ->assertNotEmpty ($ itemArray );
179
- $ this ->assertResponseFields (
180
- $ productItemsInResponse [$ itemIndex ][0 ],
181
- [
182
- 'name ' => $ filteredProducts [$ itemIndex ]->getName (),
183
- ]
184
- );
185
- }
186
178
187
- //price DESC and name ASC order
188
- $ queryPriceDescNameAsc = <<<'QUERY'
189
- query GetProductsQuery(
190
- $search: String,
191
- $filter: ProductAttributeFilterInput,
192
- $pageSize: Int,
193
- $currentPage: Int,
194
- $sort: ProductAttributeSortInput
195
- ) {
196
- products(
197
- search: $search,
198
- filter: $filter,
199
- pageSize: $pageSize,
200
- currentPage: $currentPage,
201
- sort: $sort
202
- ) {
203
- total_count
204
- page_info{total_pages}
205
- items{
206
- __typename
207
- url_key
208
- sku
209
- name
210
- stock_status
211
- price_range {
212
- minimum_price {
213
- final_price {
214
- value
215
- currency
216
- }
217
- }
218
- }
219
- }
179
+ $ response = $ this ->graphQlQuery ($ query , $ variables );
180
+ $ this ->assertArrayNotHasKey ('errors ' , $ response );
181
+ $ this ->assertEquals ($ expectedOrderSku , array_column ($ response ['products ' ]['items ' ], 'sku ' ));
220
182
}
221
- }
222
- QUERY;
223
- $ variables = [
224
- 'search ' => null ,
225
- 'filter ' => [],
226
- 'pageSize ' => 24 ,
227
- 'currentPage ' => 1 ,
228
- 'sort ' => ['price ' => 'DESC ' , 'name ' => 'ASC ' ]
229
- ];
230
- $ response = $ this ->graphQlQuery ($ queryPriceDescNameAsc , $ variables );
231
- $ this ->assertEquals (5 , $ response ['products ' ]['total_count ' ]);
232
183
233
- $ filteredProducts = [$ prod5 , $ prod4 , $ prod3 , $ prod1 , $ prod2 ];
234
- $ productItemsInResponse = array_map (null , $ response ['products ' ]['items ' ], $ filteredProducts );
235
- foreach ($ productItemsInResponse as $ itemIndex => $ itemArray ) {
236
- $ this ->assertNotEmpty ($ itemArray );
237
- $ this ->assertResponseFields (
238
- $ productItemsInResponse [$ itemIndex ][0 ],
239
- [
240
- 'name ' => $ filteredProducts [$ itemIndex ]->getName (),
241
- ]
242
- );
243
- }
184
+ /**
185
+ * @return array
186
+ */
187
+ public function sortByPriceAndNameDataProvider (): array
188
+ {
189
+ return [
190
+ [
191
+ ['price ' => 'ASC ' , 'name ' => 'ASC ' ],
192
+ ['prod1 ' , 'prod2 ' , 'prod3 ' , 'prod4 ' , 'prod5 ' ]
193
+ ],
194
+ [
195
+ ['price ' => 'DESC ' , 'name ' => 'ASC ' ],
196
+ ['prod5 ' , 'prod4 ' , 'prod3 ' , 'prod1 ' , 'prod2 ' ]
197
+ ],
198
+ [
199
+ ['price ' => 'ASC ' , 'name ' => 'DESC ' ],
200
+ ['prod2 ' , 'prod1 ' , 'prod3 ' , 'prod4 ' , 'prod5 ' ]
201
+ ],
202
+ [
203
+ ['price ' => 'DESC ' , 'name ' => 'DESC ' ],
204
+ ['prod5 ' , 'prod4 ' , 'prod3 ' , 'prod2 ' , 'prod1 ' ]
205
+ ],
206
+ ];
244
207
}
245
208
246
209
/**
0 commit comments