1414use Magento \Catalog \Model \Category ;
1515use Magento \Catalog \Model \CategoryLinkManagement ;
1616use Magento \Eav \Model \Config ;
17- use Magento \Framework \Config \Data ;
18- use Magento \Framework \EntityManager \MetadataPool ;
19- use Magento \Framework \Indexer \IndexerInterface ;
2017use Magento \Indexer \Model \Indexer ;
2118use Magento \TestFramework \ObjectManager ;
2219use Magento \TestFramework \TestCase \GraphQlAbstract ;
@@ -115,10 +112,7 @@ public function testLayeredNavigationForConfigurableProducts()
115112 $ firstOption = $ options [0 ]->getValue ();
116113 $ secondOption = $ options [1 ]->getValue ();
117114 $ query = $ this ->getQueryProductsWithArrayOfCustomAttributes ($ attributeCode , $ firstOption , $ secondOption );
118- $ objectManager = Bootstrap::getObjectManager ();
119- $ indexer = $ objectManager ->create (Indexer::class);
120- $ indexer ->load ('catalogsearch_fulltext ' );
121- $ indexer ->reindexAll ();
115+ $ this ->reIndexAndCleanCache ();
122116 $ response = $ this ->graphQlQuery ($ query );
123117
124118 $ this ->assertEquals (2 , $ response ['products ' ]['total_count ' ]);
@@ -262,23 +256,22 @@ public function testFilterProductsByDropDownCustomAttribute()
262256}
263257QUERY ;
264258
259+ $ objectManager = Bootstrap::getObjectManager ();
265260 /** @var ProductRepositoryInterface $productRepository */
266- $ productRepository = ObjectManager:: getInstance () ->get (ProductRepositoryInterface::class);
261+ $ productRepository = $ objectManager ->get (ProductRepositoryInterface::class);
267262 $ product1 = $ productRepository ->get ('simple ' );
268263 $ product2 = $ productRepository ->get ('12345 ' );
269264 $ product3 = $ productRepository ->get ('simple-4 ' );
270265 $ filteredProducts = [$ product1 , $ product2 , $ product3 ];
271- $ objectManager = Bootstrap::getObjectManager ();
272- $ indexer = $ objectManager ->create (Indexer::class);
273- $ indexer ->load ('catalogsearch_fulltext ' );
274- $ indexer ->reindexAll ();
266+ $ countOfFilteredProducts = count ($ filteredProducts );
267+ $ this ->reIndexAndCleanCache ();
275268 $ response = $ this ->graphQlQuery ($ query );
276269 $ this ->assertEquals (3 , $ response ['products ' ]['total_count ' ], 'Number of products returned is incorrect ' );
277270 $ this ->assertTrue (count ($ response ['products ' ]['filters ' ]) > 0 , 'Product filters is not empty ' );
278271 $ this ->assertCount (3 , $ response ['products ' ]['aggregations ' ], 'Incorrect count of aggregations ' );
272+
279273 $ productItemsInResponse = array_map (null , $ response ['products ' ]['items ' ], $ filteredProducts );
280- //phpcs:ignore Generic.CodeAnalysis.ForLoopWithTestFunctionCall
281- for ($ itemIndex = 0 ; $ itemIndex < count ($ filteredProducts ); $ itemIndex ++) {
274+ for ($ itemIndex = 0 ; $ itemIndex < $ countOfFilteredProducts ; $ itemIndex ++) {
282275 $ this ->assertNotEmpty ($ productItemsInResponse [$ itemIndex ]);
283276 //validate that correct products are returned
284277 $ this ->assertResponseFields (
@@ -290,7 +283,7 @@ public function testFilterProductsByDropDownCustomAttribute()
290283 }
291284
292285 /** @var \Magento\Eav\Model\Config $eavConfig */
293- $ eavConfig = \ Magento \ TestFramework \ Helper \Bootstrap:: getObjectManager () ->get (\ Magento \ Eav \ Model \ Config::class);
286+ $ eavConfig = $ objectManager ->get (Config::class);
294287 $ attribute = $ eavConfig ->getAttribute ('catalog_product ' , 'second_test_configurable ' );
295288 // Validate custom attribute filter layer data from aggregations
296289 $ this ->assertResponseFields (
@@ -309,6 +302,14 @@ public function testFilterProductsByDropDownCustomAttribute()
309302 ]
310303 );
311304 }
305+ private function reIndexAndCleanCache ()
306+ {
307+ $ objectManager = Bootstrap::getObjectManager ();
308+ $ indexer = $ objectManager ->create (Indexer::class);
309+ $ indexer ->load ('catalogsearch_fulltext ' );
310+ $ indexer ->reindexAll ();
311+ CacheCleaner::cleanAll ();
312+ }
312313 /**
313314 * Filter products using an array of multi select custom attributes
314315 *
@@ -318,20 +319,17 @@ public function testFilterProductsByDropDownCustomAttribute()
318319 public function testFilterProductsByMultiSelectCustomAttributes ()
319320 {
320321 $ objectManager = Bootstrap::getObjectManager ();
321- $ indexer = $ objectManager ->create (Indexer::class);
322- $ indexer ->load ('catalogsearch_fulltext ' );
323- $ indexer ->reindexAll ();
324- CacheCleaner::cleanAll ();
322+ $ this ->reIndexAndCleanCache ();
325323 $ attributeCode = 'multiselect_attribute ' ;
326324 /** @var \Magento\Eav\Model\Config $eavConfig */
327- $ eavConfig = \ Magento \ TestFramework \ Helper \Bootstrap:: getObjectManager () ->get (\Magento \Eav \Model \Config::class);
325+ $ eavConfig = $ objectManager ->get (\Magento \Eav \Model \Config::class);
328326 $ attribute = $ eavConfig ->getAttribute ('catalog_product ' , $ attributeCode );
329327 /** @var AttributeOptionInterface[] $options */
330328 $ options = $ attribute ->getOptions ();
331329 array_shift ($ options );
330+ $ countOptions = count ($ options );
332331 $ optionValues = [];
333- // phpcs:ignore Generic.CodeAnalysis.ForLoopWithTestFunctionCall
334- for ($ i = 0 ; $ i < count ($ options ); $ i ++) {
332+ for ($ i = 0 ; $ i < $ countOptions ; $ i ++) {
335333 $ optionValues [] = $ options [$ i ]->getValue ();
336334 }
337335 $ query = <<<QUERY
@@ -413,11 +411,7 @@ private function getDefaultAttributeOptionValue(string $attributeCode) : string
413411 */
414412 public function testSearchAndFilterByCustomAttribute ()
415413 {
416- $ objectManager = Bootstrap::getObjectManager ();
417- $ indexer = $ objectManager ->create (Indexer::class);
418- $ indexer ->load ('catalogsearch_fulltext ' );
419- $ indexer ->reindexAll ();
420- CacheCleaner::cleanAll ();
414+ $ this ->reIndexAndCleanCache ();
421415 $ attribute_code = 'second_test_configurable ' ;
422416 $ optionValue = $ this ->getDefaultAttributeOptionValue ($ attribute_code );
423417
@@ -564,12 +558,7 @@ public function testSearchAndFilterByCustomAttribute()
564558 */
565559 public function testFilterByCategoryIdAndCustomAttribute ()
566560 {
567- $ objectManager = Bootstrap::getObjectManager ();
568- $ indexer = $ objectManager ->create (Indexer::class);
569- $ indexer ->load ('catalogsearch_fulltext ' );
570- $ indexer ->reindexAll ();
571- CacheCleaner::cleanAll ();
572-
561+ $ this ->reIndexAndCleanCache ();
573562 $ categoryId = 13 ;
574563 $ optionValue = $ this ->getDefaultAttributeOptionValue ('second_test_configurable ' );
575564 $ query = <<<QUERY
@@ -765,7 +754,8 @@ public function testFilterBySingleProductUrlKey()
765754 $ response = $ this ->graphQlQuery ($ query );
766755 $ this ->assertEquals (1 , $ response ['products ' ]['total_count ' ], 'More than 1 product found ' );
767756 $ this ->assertCount (2 , $ response ['products ' ]['aggregations ' ]);
768- $ this ->assertResponseFields ($ response ['products ' ]['items ' ][0 ],
757+ $ this ->assertResponseFields (
758+ $ response ['products ' ]['items ' ][0 ],
769759 [
770760 'name ' => $ product ->getName (),
771761 'sku ' => $ product ->getSku (),
@@ -1440,17 +1430,14 @@ public function testFilterProductsBySingleCategoryId()
14401430 */
14411431 public function testSearchAndSortByRelevance ()
14421432 {
1443- $ objectManager = Bootstrap::getObjectManager ();
1444- $ indexer = $ objectManager ->create (Indexer::class);
1445- $ indexer ->load ('catalogsearch_fulltext ' );
1446- $ indexer ->reindexAll ();
1433+ $ this ->reIndexAndCleanCache ();
14471434 $ search_term ="blue " ;
14481435 $ query
14491436 = <<<QUERY
14501437{
14511438 products(
14521439 search:" {$ search_term }"
1453-
1440+ sort:{relevance:DESC}
14541441 pageSize: 5
14551442 currentPage: 1
14561443 )
@@ -1571,10 +1558,7 @@ public function testFilterByExactSkuAndSortByPriceDesc()
15711558 */
15721559 public function testProductBasicFullTextSearchQuery ()
15731560 {
1574- $ objectManager = Bootstrap::getObjectManager ();
1575- $ indexer = $ objectManager ->create (Indexer::class);
1576- $ indexer ->load ('catalogsearch_fulltext ' );
1577- $ indexer ->reindexAll ();
1561+ $ this ->reIndexAndCleanCache ();
15781562 $ textToSearch = 'blue ' ;
15791563 $ query
15801564 =<<<QUERY
0 commit comments