7
7
8
8
namespace Magento \Elasticsearch \Model \CatalogSearch ;
9
9
10
+ use Magento \Catalog \Api \ProductRepositoryInterface ;
10
11
use Magento \CatalogSearch \Model \Advanced ;
11
12
use Magento \Catalog \Model \Product \Visibility ;
12
13
use Magento \Catalog \Api \Data \ProductInterface ;
14
+ use Magento \Framework \ObjectManagerInterface ;
15
+ use Magento \Framework \Registry ;
13
16
use Magento \TestFramework \Helper \Bootstrap ;
14
17
use PHPUnit \Framework \TestCase ;
15
18
18
21
*/
19
22
class AdvancedTest extends TestCase
20
23
{
24
+ /**
25
+ * @var ObjectManagerInterface
26
+ */
27
+ private $ objectManager ;
28
+
29
+ /**
30
+ * @var Registry
31
+ */
32
+ private $ registry ;
33
+
21
34
/**
22
35
* @var Visibility
23
36
*/
24
37
private $ productVisibility ;
25
38
26
39
/**
27
- * @var Advanced
40
+ * @var ProductRepositoryInterface
28
41
*/
29
- private $ advancedSearch ;
42
+ private $ productRepository ;
30
43
31
44
/**
32
45
* @inheritDoc
@@ -35,9 +48,10 @@ protected function setUp(): void
35
48
{
36
49
parent ::setUp ();
37
50
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);
41
55
}
42
56
43
57
/**
@@ -50,17 +64,38 @@ protected function setUp(): void
50
64
*/
51
65
public function testAddFilters (): void
52
66
{
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 ' ]);
54
89
55
- $ this ->advancedSearch ->addFilters (['name ' => $ searchName ]);
56
90
/** @var ProductInterface[] $itemsResult */
57
- $ itemsResult = $ this -> advancedSearch ->getProductCollection ()
91
+ $ itemsResult = $ advancedSearch ->getProductCollection ()
58
92
->addAttributeToSelect (ProductInterface::VISIBILITY )
59
93
->getItems ();
60
- $ this ->assertCount (1 , $ itemsResult );
61
94
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
+ }
65
100
}
66
101
}
0 commit comments