77
88namespace Magento \Elasticsearch \Model \CatalogSearch ;
99
10+ use Magento \Catalog \Api \ProductRepositoryInterface ;
1011use Magento \CatalogSearch \Model \Advanced ;
1112use Magento \Catalog \Model \Product \Visibility ;
1213use Magento \Catalog \Api \Data \ProductInterface ;
14+ use Magento \Framework \ObjectManagerInterface ;
15+ use Magento \Framework \Registry ;
1316use Magento \TestFramework \Helper \Bootstrap ;
1417use PHPUnit \Framework \TestCase ;
1518
1821 */
1922class AdvancedTest extends TestCase
2023{
24+ /**
25+ * @var ObjectManagerInterface
26+ */
27+ private $ objectManager ;
28+
29+ /**
30+ * @var Registry
31+ */
32+ private $ registry ;
33+
2134 /**
2235 * @var Visibility
2336 */
2437 private $ productVisibility ;
2538
2639 /**
27- * @var Advanced
40+ * @var ProductRepositoryInterface
2841 */
29- private $ advancedSearch ;
42+ private $ productRepository ;
3043
3144 /**
3245 * @inheritDoc
@@ -35,9 +48,10 @@ protected function setUp(): void
3548 {
3649 parent ::setUp ();
3750
38- $ objectManager = Bootstrap::getObjectManager ();
39- $ this ->productVisibility = $ objectManager ->get (Visibility::class);
40- $ this ->advancedSearch = $ objectManager ->get (Advanced::class);
51+ $ this ->objectManager = Bootstrap::getObjectManager ();
52+ $ this ->registry = $ this ->objectManager ->get (Registry::class);
53+ $ this ->productVisibility = $ this ->objectManager ->get (Visibility::class);
54+ $ this ->productRepository = $ this ->objectManager ->get (ProductRepositoryInterface::class);
4155 }
4256
4357 /**
@@ -50,17 +64,38 @@ protected function setUp(): void
5064 */
5165 public function testAddFilters (): void
5266 {
53- $ searchName = 'Configurable ' ;
67+ $ this ->assertResultsAfterRequest (1 );
68+
69+ /** @var ProductInterface $configurableProductOption */
70+ $ configurableProductOption = $ this ->productRepository ->get ('Simple option 1 ' );
71+ $ configurableProductOption ->setVisibility (Visibility::VISIBILITY_IN_SEARCH );
72+ $ this ->productRepository ->save ($ configurableProductOption );
73+
74+ $ this ->registry ->unregister ('advanced_search_conditions ' );
75+ $ this ->assertResultsAfterRequest (2 );
76+ }
77+
78+ /**
79+ * Do Elasticsearch query and assert results.
80+ *
81+ * @param int $count
82+ * @return void
83+ */
84+ private function assertResultsAfterRequest (int $ count ): void
85+ {
86+ /** @var Advanced $advancedSearch */
87+ $ advancedSearch = $ this ->objectManager ->create (Advanced::class);
88+ $ advancedSearch ->addFilters (['name ' => 'Configurable ' ]);
5489
55- $ this ->advancedSearch ->addFilters (['name ' => $ searchName ]);
5690 /** @var ProductInterface[] $itemsResult */
57- $ itemsResult = $ this -> advancedSearch ->getProductCollection ()
91+ $ itemsResult = $ advancedSearch ->getProductCollection ()
5892 ->addAttributeToSelect (ProductInterface::VISIBILITY )
5993 ->getItems ();
60- $ this ->assertCount (1 , $ itemsResult );
6194
62- $ product = array_shift ($ itemsResult );
63- $ this ->assertStringContainsString ($ searchName , $ product ->getName ());
64- $ this ->assertContains ((int )$ product ->getVisibility (), $ this ->productVisibility ->getVisibleInSearchIds ());
95+ $ this ->assertCount ($ count , $ itemsResult );
96+ foreach ($ itemsResult as $ product ) {
97+ $ this ->assertStringContainsString ('Configurable ' , $ product ->getName ());
98+ $ this ->assertContains ((int )$ product ->getVisibility (), $ this ->productVisibility ->getVisibleInSearchIds ());
99+ }
65100 }
66101}
0 commit comments