Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
IndexSwitcherTest.php
Go to the documentation of this file.
1 <?php
7 
16 
17 class IndexSwitcherTest extends \PHPUnit\Framework\TestCase
18 {
22  private $connection;
23 
27  private $indexScopeResolver;
28 
32  private $scopeState;
33 
37  private $indexSwitcher;
38 
42  private $resource;
43 
44  protected function setUp()
45  {
46  $this->resource = $this->getMockBuilder(ResourceConnection::class)
47  ->setMethods(['getConnection'])
48  ->disableOriginalConstructor()
49  ->getMock();
50  $this->connection = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
51  ->setMethods(['isTableExists', 'dropTable', 'renameTable'])
52  ->getMockForAbstractClass();
53  $this->resource->expects($this->any())
54  ->method('getConnection')
55  ->willReturn($this->connection);
56  $this->indexScopeResolver = $this->getMockBuilder(
57  \Magento\Framework\Search\Request\IndexScopeResolverInterface::class
58  )
59  ->setMethods(['resolve'])
60  ->getMockForAbstractClass();
61  $this->scopeState = $this->getMockBuilder(State::class)
62  ->setMethods(['getState', 'useRegularIndex', 'useTemporaryIndex'])
63  ->disableOriginalConstructor()
64  ->getMock();
65 
66  $objectManagerHelper = new ObjectManagerHelper($this);
67  $this->indexSwitcher = $objectManagerHelper->getObject(
68  \Magento\CatalogSearch\Model\Indexer\Scope\IndexSwitcher::class,
69  [
70  'resource' => $this->resource,
71  'indexScopeResolver' => $this->indexScopeResolver,
72  'state' => $this->scopeState,
73  ]
74  );
75  }
76 
77  public function testSwitchRegularIndex()
78  {
79  $dimensions = [$this->getMockBuilder(Dimension::class)->setConstructorArgs(['scope', '1'])];
80 
81  $this->scopeState->expects($this->once())
82  ->method('getState')
83  ->willReturn(State::USE_REGULAR_INDEX);
84 
85  $this->indexScopeResolver->expects($this->never())->method('resolve');
86  $this->connection->expects($this->never())->method('renameTable');
87 
88  $this->indexSwitcher->switchIndex($dimensions);
89  }
90 
92  {
93  $dimensions = [$this->getMockBuilder(Dimension::class)->setConstructorArgs(['scope', '1'])];
94 
95  $this->scopeState->expects($this->once())
96  ->method('getState')
97  ->willReturn('unknown_state');
98 
99  $this->indexScopeResolver->expects($this->never())->method('resolve');
100  $this->connection->expects($this->never())->method('renameTable');
101 
102  $this->indexSwitcher->switchIndex($dimensions);
103  }
104 
106  {
107  $dimensions = [$this->getMockBuilder(Dimension::class)->setConstructorArgs(['scope', '1'])];
108 
109  $this->scopeState->expects($this->once())
110  ->method('getState')
111  ->willReturn(State::USE_TEMPORARY_INDEX);
112 
113  $this->scopeState->expects($this->at(1))->method('useRegularIndex');
114  $this->scopeState->expects($this->at(2))->method('useTemporaryIndex');
115 
116  $this->indexScopeResolver->expects($this->exactly(2))->method('resolve')
117  ->withConsecutive(
118  [$this->equalTo(FulltextIndexer::INDEXER_ID), $this->equalTo($dimensions)],
119  [$this->equalTo(FulltextIndexer::INDEXER_ID), $this->equalTo($dimensions)]
120  )
121  ->willReturnOnConsecutiveCalls(
122  'catalogsearch_fulltext_scope1_tmp1',
123  'catalogsearch_fulltext_scope1'
124  );
125 
126  $this->connection->expects($this->exactly(2))->method('isTableExists')
127  ->withConsecutive(
128  [$this->equalTo('catalogsearch_fulltext_scope1_tmp1'), $this->equalTo(null)],
129  [$this->equalTo('catalogsearch_fulltext_scope1'), $this->equalTo(null)]
130  )
131  ->willReturnOnConsecutiveCalls(
132  true,
133  true
134  );
135 
136  $this->connection->expects($this->once())->method('dropTable')->with('catalogsearch_fulltext_scope1', null);
137  $this->connection->expects($this->once())
138  ->method('renameTable')
139  ->with('catalogsearch_fulltext_scope1_tmp1', 'catalogsearch_fulltext_scope1');
140 
141  $this->indexSwitcher->switchIndex($dimensions);
142  }
143 
145  {
146  $dimensions = [$this->getMockBuilder(Dimension::class)->setConstructorArgs(['scope', '1'])];
147 
148  $this->scopeState->expects($this->once())
149  ->method('getState')
150  ->willReturn(State::USE_TEMPORARY_INDEX);
151 
152  $this->scopeState->expects($this->at(1))->method('useRegularIndex');
153  $this->scopeState->expects($this->at(2))->method('useTemporaryIndex');
154 
155  $this->indexScopeResolver->expects($this->exactly(2))->method('resolve')
156  ->withConsecutive(
157  [$this->equalTo(FulltextIndexer::INDEXER_ID), $this->equalTo($dimensions)],
158  [$this->equalTo(FulltextIndexer::INDEXER_ID), $this->equalTo($dimensions)]
159  )
160  ->willReturnOnConsecutiveCalls(
161  'catalogsearch_fulltext_scope1_tmp1',
162  'catalogsearch_fulltext_scope1'
163  );
164 
165  $this->connection->expects($this->exactly(2))->method('isTableExists')
166  ->withConsecutive(
167  [$this->equalTo('catalogsearch_fulltext_scope1_tmp1'), $this->equalTo(null)],
168  [$this->equalTo('catalogsearch_fulltext_scope1'), $this->equalTo(null)]
169  )
170  ->willReturnOnConsecutiveCalls(
171  true,
172  false
173  );
174 
175  $this->connection->expects($this->never())->method('dropTable')->with('catalogsearch_fulltext_scope1', null);
176  $this->connection->expects($this->once())
177  ->method('renameTable')
178  ->with('catalogsearch_fulltext_scope1_tmp1', 'catalogsearch_fulltext_scope1');
179 
180  $this->indexSwitcher->switchIndex($dimensions);
181  }
182 
188  {
189  $dimensions = [$this->getMockBuilder(Dimension::class)->setConstructorArgs(['scope', '1'])];
190 
191  $this->scopeState->expects($this->once())
192  ->method('getState')
193  ->willReturn(State::USE_TEMPORARY_INDEX);
194 
195  $this->scopeState->expects($this->never())->method('useRegularIndex');
196  $this->scopeState->expects($this->never())->method('useTemporaryIndex');
197 
198  $this->indexScopeResolver->expects($this->once())->method('resolve')
199  ->with(FulltextIndexer::INDEXER_ID, $dimensions)
200  ->willReturn('catalogsearch_fulltext_scope1_tmp1');
201 
202  $this->connection->expects($this->once())
203  ->method('isTableExists')
204  ->with('catalogsearch_fulltext_scope1_tmp1', null)
205  ->willReturn(false);
206 
207  $this->connection->expects($this->never())->method('dropTable')->with('catalogsearch_fulltext_scope1', null);
208  $this->connection->expects($this->never())->method('renameTable');
209 
210  $this->indexSwitcher->switchIndex($dimensions);
211  }
212 }