Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BaseSelectAttributesSearchStrategyTest.php
Go to the documentation of this file.
1 <?php
8 
12 use Magento\CatalogSearch\Model\Search\SelectContainer\SelectContainerFactory;
13 
14 class BaseSelectAttributesSearchStrategyTest extends \PHPUnit\Framework\TestCase
15 {
19  private $baseSelectAttributesSearchStrategy;
20 
24  private $selectContainerFactory;
25 
29  private $resource;
30 
34  private $storeManager;
35 
39  private $scopeResolver;
40 
41  protected function setUp()
42  {
43  $this->baseSelectAttributesSearchStrategy = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
44  ->create(BaseSelectAttributesSearchStrategy::class);
45 
46  $this->selectContainerFactory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
47  ->create(SelectContainerFactory::class);
48 
50  ->create(ResourceConnection::class);
51 
53  ->create(StoreManagerInterface::class);
54 
56  ->create(IndexScopeResolver::class);
57  }
58 
60  {
61  $selectContainer = $this->getSelectContainerWithoutFullTextSearch();
62  $selectContainer = $this->baseSelectAttributesSearchStrategy->createBaseSelect($selectContainer);
63  $select = $selectContainer->getSelect();
64  $expectedSelect = $this->getMainSelect();
65 
66  $this->assertEquals((string) $expectedSelect, (string) $select);
67  }
68 
70  {
71  $selectContainer = $this->getSelectContainerWithFullTextSearch();
72  $selectContainer = $this->baseSelectAttributesSearchStrategy->createBaseSelect($selectContainer);
73  $select = $selectContainer->getSelect();
74  $expectedSelect = $this->getFulltextSelect();
75 
76  $this->assertEquals((string) $expectedSelect, (string) $select);
77  }
78 
79  private function getMainSelect()
80  {
81  $select = $this->resource->getConnection()->select();
82  $select->distinct()
83  ->from(
84  ['search_index' => $this->resource->getTableName('catalog_product_index_eav')],
85  ['entity_id' => 'entity_id']
86  )->where(
87  $this->resource->getConnection()->quoteInto(
88  'search_index.store_id = ?',
89  $this->storeManager->getStore()->getId()
90  )
91  );
92 
93  return $select;
94  }
95 
96  private function getFulltextSelect()
97  {
98  $select = $this->resource->getConnection()->select();
99  $select->distinct()
100  ->from(
101  ['eav_index' => $this->resource->getTableName('catalog_product_index_eav')],
102  ['entity_id' => 'entity_id']
103  )->where(
104  $this->resource->getConnection()->quoteInto(
105  'eav_index.store_id = ?',
106  $this->storeManager->getStore()->getId()
107  )
108  )->joinInner(
109  ['search_index' => $this->scopeResolver->resolve('', [])],
110  'eav_index.entity_id = search_index.entity_id',
111  []
112  )->joinInner(
113  ['cea' => $this->resource->getTableName('catalog_eav_attribute')],
114  'search_index.attribute_id = cea.attribute_id',
115  []
116  );
117 
118  return $select;
119  }
120 
121  private function getSelectContainerWithFullTextSearch()
122  {
123  return $this->selectContainerFactory->create(
124  [
125  'nonCustomAttributesFilters' => [],
126  'customAttributesFilters' => [],
127  'visibilityFilter' => null,
128  'isFullTextSearchRequired' => true,
129  'isShowOutOfStockEnabled' => false,
130  'usedIndex' => '',
131  'dimensions' => [],
132  'select' => $this->resource->getConnection()->select()
133  ]
134  );
135  }
136 
137  private function getSelectContainerWithoutFullTextSearch()
138  {
139  return $this->selectContainerFactory->create(
140  [
141  'nonCustomAttributesFilters' => [],
142  'customAttributesFilters' => [],
143  'visibilityFilter' => null,
144  'isFullTextSearchRequired' => false,
145  'isShowOutOfStockEnabled' => false,
146  'usedIndex' => '',
147  'dimensions' => [],
148  'select' => $this->resource->getConnection()->select()
149  ]
150  );
151  }
152 }