14
14
use Magento \Catalog \Model \Category ;
15
15
use Magento \Catalog \Model \CategoryLinkManagement ;
16
16
use Magento \Eav \Model \Config ;
17
- use Magento \Framework \Config \Data ;
18
- use Magento \Framework \EntityManager \MetadataPool ;
19
- use Magento \Framework \Indexer \IndexerInterface ;
20
17
use Magento \Indexer \Model \Indexer ;
21
18
use Magento \TestFramework \ObjectManager ;
22
19
use Magento \TestFramework \TestCase \GraphQlAbstract ;
@@ -115,10 +112,7 @@ public function testLayeredNavigationForConfigurableProducts()
115
112
$ firstOption = $ options [0 ]->getValue ();
116
113
$ secondOption = $ options [1 ]->getValue ();
117
114
$ 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 ();
122
116
$ response = $ this ->graphQlQuery ($ query );
123
117
124
118
$ this ->assertEquals (2 , $ response ['products ' ]['total_count ' ]);
@@ -262,23 +256,22 @@ public function testFilterProductsByDropDownCustomAttribute()
262
256
}
263
257
QUERY ;
264
258
259
+ $ objectManager = Bootstrap::getObjectManager ();
265
260
/** @var ProductRepositoryInterface $productRepository */
266
- $ productRepository = ObjectManager:: getInstance () ->get (ProductRepositoryInterface::class);
261
+ $ productRepository = $ objectManager ->get (ProductRepositoryInterface::class);
267
262
$ product1 = $ productRepository ->get ('simple ' );
268
263
$ product2 = $ productRepository ->get ('12345 ' );
269
264
$ product3 = $ productRepository ->get ('simple-4 ' );
270
265
$ 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 ();
275
268
$ response = $ this ->graphQlQuery ($ query );
276
269
$ this ->assertEquals (3 , $ response ['products ' ]['total_count ' ], 'Number of products returned is incorrect ' );
277
270
$ this ->assertTrue (count ($ response ['products ' ]['filters ' ]) > 0 , 'Product filters is not empty ' );
278
271
$ this ->assertCount (3 , $ response ['products ' ]['aggregations ' ], 'Incorrect count of aggregations ' );
272
+
279
273
$ 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 ++) {
282
275
$ this ->assertNotEmpty ($ productItemsInResponse [$ itemIndex ]);
283
276
//validate that correct products are returned
284
277
$ this ->assertResponseFields (
@@ -290,7 +283,7 @@ public function testFilterProductsByDropDownCustomAttribute()
290
283
}
291
284
292
285
/** @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);
294
287
$ attribute = $ eavConfig ->getAttribute ('catalog_product ' , 'second_test_configurable ' );
295
288
// Validate custom attribute filter layer data from aggregations
296
289
$ this ->assertResponseFields (
@@ -309,6 +302,14 @@ public function testFilterProductsByDropDownCustomAttribute()
309
302
]
310
303
);
311
304
}
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
+ }
312
313
/**
313
314
* Filter products using an array of multi select custom attributes
314
315
*
@@ -318,20 +319,17 @@ public function testFilterProductsByDropDownCustomAttribute()
318
319
public function testFilterProductsByMultiSelectCustomAttributes ()
319
320
{
320
321
$ objectManager = Bootstrap::getObjectManager ();
321
- $ indexer = $ objectManager ->create (Indexer::class);
322
- $ indexer ->load ('catalogsearch_fulltext ' );
323
- $ indexer ->reindexAll ();
324
- CacheCleaner::cleanAll ();
322
+ $ this ->reIndexAndCleanCache ();
325
323
$ attributeCode = 'multiselect_attribute ' ;
326
324
/** @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);
328
326
$ attribute = $ eavConfig ->getAttribute ('catalog_product ' , $ attributeCode );
329
327
/** @var AttributeOptionInterface[] $options */
330
328
$ options = $ attribute ->getOptions ();
331
329
array_shift ($ options );
330
+ $ countOptions = count ($ options );
332
331
$ optionValues = [];
333
- // phpcs:ignore Generic.CodeAnalysis.ForLoopWithTestFunctionCall
334
- for ($ i = 0 ; $ i < count ($ options ); $ i ++) {
332
+ for ($ i = 0 ; $ i < $ countOptions ; $ i ++) {
335
333
$ optionValues [] = $ options [$ i ]->getValue ();
336
334
}
337
335
$ query = <<<QUERY
@@ -413,11 +411,7 @@ private function getDefaultAttributeOptionValue(string $attributeCode) : string
413
411
*/
414
412
public function testSearchAndFilterByCustomAttribute ()
415
413
{
416
- $ objectManager = Bootstrap::getObjectManager ();
417
- $ indexer = $ objectManager ->create (Indexer::class);
418
- $ indexer ->load ('catalogsearch_fulltext ' );
419
- $ indexer ->reindexAll ();
420
- CacheCleaner::cleanAll ();
414
+ $ this ->reIndexAndCleanCache ();
421
415
$ attribute_code = 'second_test_configurable ' ;
422
416
$ optionValue = $ this ->getDefaultAttributeOptionValue ($ attribute_code );
423
417
@@ -564,12 +558,7 @@ public function testSearchAndFilterByCustomAttribute()
564
558
*/
565
559
public function testFilterByCategoryIdAndCustomAttribute ()
566
560
{
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 ();
573
562
$ categoryId = 13 ;
574
563
$ optionValue = $ this ->getDefaultAttributeOptionValue ('second_test_configurable ' );
575
564
$ query = <<<QUERY
@@ -765,7 +754,8 @@ public function testFilterBySingleProductUrlKey()
765
754
$ response = $ this ->graphQlQuery ($ query );
766
755
$ this ->assertEquals (1 , $ response ['products ' ]['total_count ' ], 'More than 1 product found ' );
767
756
$ this ->assertCount (2 , $ response ['products ' ]['aggregations ' ]);
768
- $ this ->assertResponseFields ($ response ['products ' ]['items ' ][0 ],
757
+ $ this ->assertResponseFields (
758
+ $ response ['products ' ]['items ' ][0 ],
769
759
[
770
760
'name ' => $ product ->getName (),
771
761
'sku ' => $ product ->getSku (),
@@ -1440,17 +1430,14 @@ public function testFilterProductsBySingleCategoryId()
1440
1430
*/
1441
1431
public function testSearchAndSortByRelevance ()
1442
1432
{
1443
- $ objectManager = Bootstrap::getObjectManager ();
1444
- $ indexer = $ objectManager ->create (Indexer::class);
1445
- $ indexer ->load ('catalogsearch_fulltext ' );
1446
- $ indexer ->reindexAll ();
1433
+ $ this ->reIndexAndCleanCache ();
1447
1434
$ search_term ="blue " ;
1448
1435
$ query
1449
1436
= <<<QUERY
1450
1437
{
1451
1438
products(
1452
1439
search:" {$ search_term }"
1453
-
1440
+ sort:{relevance:DESC}
1454
1441
pageSize: 5
1455
1442
currentPage: 1
1456
1443
)
@@ -1571,10 +1558,7 @@ public function testFilterByExactSkuAndSortByPriceDesc()
1571
1558
*/
1572
1559
public function testProductBasicFullTextSearchQuery ()
1573
1560
{
1574
- $ objectManager = Bootstrap::getObjectManager ();
1575
- $ indexer = $ objectManager ->create (Indexer::class);
1576
- $ indexer ->load ('catalogsearch_fulltext ' );
1577
- $ indexer ->reindexAll ();
1561
+ $ this ->reIndexAndCleanCache ();
1578
1562
$ textToSearch = 'blue ' ;
1579
1563
$ query
1580
1564
=<<<QUERY
0 commit comments