Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
IndexerHandlerTest.php
Go to the documentation of this file.
1 <?php
7 
10 
11 class IndexerHandlerTest extends \PHPUnit\Framework\TestCase
12 {
16  private $model;
17 
21  private $adapter;
22 
26  private $batch;
27 
31  private $adapterFactory;
32 
36  private $indexStructure;
37 
41  private $indexNameResolver;
42 
46  private $client;
47 
51  private $scopeResolver;
52 
56  private $scopeInterface;
57 
63  protected function setUp()
64  {
65  $this->adapter = $this->getMockBuilder(\Magento\Elasticsearch\Model\Adapter\Elasticsearch::class)
66  ->disableOriginalConstructor()
67  ->getMock();
68 
69  $this->adapterFactory = $this->getMockBuilder(\Magento\Elasticsearch\Model\Adapter\ElasticsearchFactory::class)
70  ->disableOriginalConstructor()
71  ->setMethods(['create'])
72  ->getMock();
73 
74  $this->adapterFactory->expects($this->any())
75  ->method('create')
76  ->willReturn($this->adapter);
77 
78  $this->batch = $this->getMockBuilder(\Magento\Framework\Indexer\SaveHandler\Batch::class)
79  ->disableOriginalConstructor()
80  ->getMock();
81 
82  $this->indexStructure = $this->getMockBuilder(\Magento\Framework\Indexer\IndexStructureInterface::class)
83  ->disableOriginalConstructor()
84  ->getMock();
85 
86  $this->indexNameResolver = $this->getMockBuilder(
87  \Magento\Elasticsearch\Model\Adapter\Index\IndexNameResolver::class
88  )
89  ->disableOriginalConstructor()
90  ->getMock();
91 
92  $this->client = $this->getMockBuilder(\Magento\Elasticsearch\Model\Client\Elasticsearch::class)
93  ->setMethods(['ping'])
94  ->disableOriginalConstructor()
95  ->getMock();
96 
97  $this->scopeResolver = $this->getMockForAbstractClass(
98  \Magento\Framework\App\ScopeResolverInterface::class,
99  [],
100  '',
101  false
102  );
103 
104  $this->scopeInterface = $this->getMockForAbstractClass(
105  \Magento\Framework\App\ScopeInterface::class,
106  [],
107  '',
108  false
109  );
110 
111  $objectManager = new ObjectManagerHelper($this);
112 
113  $this->model = $objectManager->getObject(
114  \Magento\Elasticsearch\Model\Indexer\IndexerHandler::class,
115  [
116  'indexStructure' => $this->indexStructure,
117  'adapter' => $this->adapter,
118  'indexNameResolver' => $this->indexNameResolver,
119  'batch' => $this->batch,
120  'data' => ['indexer_id' => 'catalogsearch_fulltext'],
121  500,
122  'scopeResolver' => $this->scopeResolver
123  ]
124  );
125  }
126 
127  public function testIsAvailable()
128  {
129  $this->adapter->expects($this->any())
130  ->method('ping')
131  ->willReturn(true);
132 
133  $this->client->expects($this->any())
134  ->method('ping')
135  ->willReturn(true);
136 
137  $result = $this->model->isAvailable();
138 
139  $this->assertTrue($result);
140  }
141 
142  public function testDeleteIndex()
143  {
144  $dimensionValue = 3;
145  $documentId = 123;
146 
147  $dimension = $this->getMockBuilder(\Magento\Framework\Search\Request\Dimension::class)
148  ->disableOriginalConstructor()
149  ->getMock();
150  $dimension->expects($this->once())
151  ->method('getValue')
152  ->willReturn($dimensionValue);
153  $this->scopeResolver->expects($this->once())
154  ->method('getScope')
155  ->willReturn($this->scopeInterface);
156  $this->scopeInterface->expects($this->once())
157  ->method('getId')
158  ->willReturn($dimensionValue);
159 
160  $result = $this->model->deleteIndex([$dimension], new \ArrayIterator([$documentId]));
161 
162  $this->assertEquals($this->model, $result);
163  }
164 
165  public function testSaveIndex()
166  {
167  $dimensionValue = 3;
168  $documentId = 123;
169  $documents = new \ArrayIterator([$documentId]);
170 
171  $dimension = $this->getMockBuilder(\Magento\Framework\Search\Request\Dimension::class)
172  ->disableOriginalConstructor()
173  ->getMock();
174  $dimension->expects($this->once())
175  ->method('getValue')
176  ->willReturn($dimensionValue);
177 
178  $this->batch->expects($this->once())
179  ->method('getItems')
180  ->with($documents, 500)
181  ->willReturn([[]]);
182 
183  $this->adapter->expects($this->once())
184  ->method('prepareDocsPerStore')
185  ->with([], $dimensionValue)
186  ->willReturn([$documentId]);
187  $this->adapter->expects($this->once())
188  ->method('addDocs')
189  ->with([$documentId]);
190  $this->scopeResolver->expects($this->once())
191  ->method('getScope')
192  ->willReturn($this->scopeInterface);
193  $this->scopeInterface->expects($this->once())
194  ->method('getId')
195  ->willReturn($dimensionValue);
196 
197  $result = $this->model->saveIndex([$dimension], $documents);
198 
199  $this->assertEquals($this->model, $result);
200  }
201 
206  {
207  $dimensionValue = 'SomeDimension';
208 
209  $dimension = $this->getMockBuilder(\Magento\Framework\Search\Request\Dimension::class)
210  ->disableOriginalConstructor()
211  ->getMock();
212  $dimension->expects($this->any())
213  ->method('getValue')
214  ->willReturn($dimensionValue);
215 
216  $this->adapter->expects($this->any())
217  ->method('cleanIndex');
218 
219  $result = $this->model->cleanIndex([$dimension]);
220 
221  $this->assertEquals($this->model, $result);
222  }
223 
227  public function testCleanIndex()
228  {
229  $objectManager = new ObjectManagerHelper($this);
230  $model = $objectManager->getObject(
231  \Magento\Elasticsearch\Model\Indexer\IndexerHandler::class,
232  [
233  'adapterFactory' => $this->adapterFactory,
234  'batch' => $this->batch,
235  'data' => ['indexer_id' => 'else_indexer_id'],
236  ]
237  );
238  $dimensionValue = 'SomeDimension';
239 
240  $dimension = $this->getMockBuilder(\Magento\Framework\Search\Request\Dimension::class)
241  ->disableOriginalConstructor()
242  ->getMock();
243  $dimension->expects($this->any())
244  ->method('getValue')
245  ->willReturn($dimensionValue);
246 
247  $this->adapter->expects($this->any())
248  ->method('cleanIndex');
249 
250  $result = $model->cleanIndex([$dimension]);
251 
252  $this->assertEquals($model, $result);
253  }
254 }
$objectManager
Definition: bootstrap.php:17