Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ReindexAllTest.php
Go to the documentation of this file.
1 <?php
7 
13 use Magento\Elasticsearch\Model\Client\Elasticsearch as ElasticsearchClient;
16 
26 class ReindexAllTest extends \PHPUnit\Framework\TestCase
27 {
31  private $connectionManager;
32 
36  private $client;
37 
41  private $storeManager;
42 
46  private $clientConfig;
47 
51  private $searchIndexNameResolver;
52 
56  private $productRepository;
57 
58  protected function setUp()
59  {
60  $this->connectionManager = Bootstrap::getObjectManager()->create(ConnectionManager::class);
61  $this->client = $this->connectionManager->getConnection();
62  $this->storeManager = Bootstrap::getObjectManager()->create(StoreManagerInterface::class);
63  $this->clientConfig = Bootstrap::getObjectManager()->create(Config::class);
64  $this->searchIndexNameResolver = Bootstrap::getObjectManager()->create(SearchIndexNameResolver::class);
65  $this->productRepository = Bootstrap::getObjectManager()->create(ProductRepositoryInterface::class);
66  }
67 
75  public function testSearchAll()
76  {
77  $this->reindexAll();
78  $result = $this->searchByName('Configurable Product');
79  self::assertGreaterThanOrEqual(2, $result);
80  }
81 
89  public function testSearchSpecificProduct()
90  {
91  $this->reindexAll();
92  $result = $this->searchByName('12345');
93  self::assertCount(1, $result);
94 
95  $specificProduct = $this->productRepository->get('configurable_12345');
96  self::assertEquals($specificProduct->getId(), $result[0]['_id']);
97  }
98 
103  private function searchByName($text)
104  {
105  $storeId = $this->storeManager->getDefaultStoreView()->getId();
106  $searchQuery = [
107  'index' => $this->searchIndexNameResolver->getIndexName($storeId, 'catalogsearch_fulltext'),
108  'type' => $this->clientConfig->getEntityType(),
109  'body' => [
110  'query' => [
111  'bool' => [
112  'minimum_should_match' => 1,
113  'should' => [
114  [
115  'match' => [
116  'name' => $text,
117  ],
118  ],
119  ],
120  ],
121  ],
122  ],
123  ];
124  $queryResult = $this->client->query($searchQuery);
125  return isset($queryResult['hits']['hits']) ? $queryResult['hits']['hits'] : [];
126  }
127 
133  private function reindexAll()
134  {
135  // Perform full reindex
137  $indexer = Bootstrap::getObjectManager()->create(Indexer::class);
138  $indexer->load('catalogsearch_fulltext');
139  $indexer->reindexAll();
140  }
141 }
endifif( $block->getLastPageNum()>1)( 'Page') ?></strong >< ul class $text
Definition: pager.phtml:43