Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
IndexerStatusCommandTest.php
Go to the documentation of this file.
1 <?php
7 
10 use Symfony\Component\Console\Tester\CommandTester;
11 
13 {
19  private $command;
20 
26  private function attachViewToIndexerMock($indexerMock, array $data)
27  {
29  $changelog = $this->getMockBuilder(\Magento\Framework\Mview\View\Changelog::class)
30  ->disableOriginalConstructor()
31  ->getMock();
32 
33  $changelog->expects($this->any())
34  ->method('getList')
35  ->willReturn(range(0, $data['view']['changelog']['list_size']-1));
36 
38  $stateMock = $this->getMockBuilder(\Magento\Indexer\Model\Mview\View\State::class)
39  ->disableOriginalConstructor()
40  ->setMethods(null)
41  ->getMock();
42 
43  $stateMock->addData($data['view']['state']);
44 
46  $viewMock = $this->getMockBuilder(\Magento\Framework\Mview\View::class)
47  ->disableOriginalConstructor()
48  ->setMethods(['getChangelog', 'getState'])
49  ->getMock();
50 
51  $viewMock->expects($this->any())
52  ->method('getState')
53  ->willReturn($stateMock);
54  $viewMock->expects($this->any())
55  ->method('getChangelog')
56  ->willReturn($changelog);
57 
58  $indexerMock->method('getView')
59  ->willReturn($viewMock);
60 
61  return $indexerMock;
62  }
63 
69  public function testExecuteAll(array $indexers)
70  {
71  $this->configureAdminArea();
72  $indexerMocks = [];
73  foreach ($indexers as $indexerData) {
74  $indexerMock = $this->getIndexerMock(
75  ['getStatus', 'isScheduled', 'getState', 'getView'],
76  $indexerData
77  );
78 
79  $indexerMock->method('getStatus')
80  ->willReturn($indexerData['status']);
81  $indexerMock->method('isScheduled')
82  ->willReturn($indexerData['is_scheduled']);
83 
84  if ($indexerData['is_scheduled']) {
85  $this->attachViewToIndexerMock($indexerMock, $indexerData);
86  }
87 
88  $indexerMocks[] = $indexerMock;
89  }
90 
91  $this->initIndexerCollectionByItems($indexerMocks);
92  $this->command = new IndexerStatusCommand($this->objectManagerFactory);
93 
94  $commandTester = new CommandTester($this->command);
95  $commandTester->execute([]);
96 
97  $linesOutput = array_filter(explode(PHP_EOL, $commandTester->getDisplay()));
98 
99  $spacer = '+----------------+------------------+-----------+-------------------------+---------------------+';
100 
101  $this->assertCount(8, $linesOutput, 'There should be 8 lines output. 3 Spacers, 1 header, 4 content.');
102  $this->assertEquals($linesOutput[0], $spacer, "Lines 0, 2, 7 should be spacer lines");
103  $this->assertEquals($linesOutput[2], $spacer, "Lines 0, 2, 7 should be spacer lines");
104  $this->assertEquals($linesOutput[7], $spacer, "Lines 0, 2, 7 should be spacer lines");
105 
106  $headerValues = array_values(array_filter(explode('|', $linesOutput[1])));
107  $this->assertEquals('Title', trim($headerValues[0]));
108  $this->assertEquals('Status', trim($headerValues[1]));
109  $this->assertEquals('Update On', trim($headerValues[2]));
110  $this->assertEquals('Schedule Status', trim($headerValues[3]));
111  $this->assertEquals('Schedule Updated', trim($headerValues[4]));
112 
113  $indexer1 = array_values(array_filter(explode('|', $linesOutput[3])));
114  $this->assertEquals('Title_indexer1', trim($indexer1[0]));
115  $this->assertEquals('Ready', trim($indexer1[1]));
116  $this->assertEquals('Schedule', trim($indexer1[2]));
117  $this->assertEquals('idle (10 in backlog)', trim($indexer1[3]));
118  $this->assertEquals('2017-01-01 11:11:11', trim($indexer1[4]));
119 
120  $indexer2 = array_values(array_filter(explode('|', $linesOutput[4])));
121  $this->assertEquals('Title_indexer2', trim($indexer2[0]));
122  $this->assertEquals('Reindex required', trim($indexer2[1]));
123  $this->assertEquals('Save', trim($indexer2[2]));
124  $this->assertEquals('', trim($indexer2[3]));
125  $this->assertEquals('', trim($indexer2[4]));
126 
127  $indexer3 = array_values(array_filter(explode('|', $linesOutput[5])));
128  $this->assertEquals('Title_indexer3', trim($indexer3[0]));
129  $this->assertEquals('Processing', trim($indexer3[1]));
130  $this->assertEquals('Schedule', trim($indexer3[2]));
131  $this->assertEquals('idle (100 in backlog)', trim($indexer3[3]));
132  $this->assertEquals('2017-01-01 11:11:11', trim($indexer3[4]));
133 
134  $indexer4 = array_values(array_filter(explode('|', $linesOutput[6])));
135  $this->assertEquals('Title_indexer4', trim($indexer4[0]));
136  $this->assertEquals('unknown', trim($indexer4[1]));
137  $this->assertEquals('Schedule', trim($indexer4[2]));
138  $this->assertEquals('running (20 in backlog)', trim($indexer4[3]));
139  $this->assertEquals('2017-01-01 11:11:11', trim($indexer4[4]));
140  }
141 
145  public function executeAllDataProvider()
146  {
147  return [
148  [
149  'indexers' => [
150  'indexer_1' => [
151  'indexer_id' => 'indexer_1',
152  'title' => 'Title_indexer1',
153  'status' => StateInterface::STATUS_VALID,
154  'is_scheduled' => true,
155  'view' => [
156  'state' => [
157  'status' => 'idle',
158  'updated' => '2017-01-01 11:11:11',
159  ],
160  'changelog' => [
161  'list_size' => 10
162  ]
163  ]
164  ],
165  'indexer_2' => [
166  'indexer_id' => 'indexer_2',
167  'title' => 'Title_indexer2',
168  'status' => StateInterface::STATUS_INVALID,
169  'is_scheduled' => false,
170  'view' => [
171  'state' => [
172  'status' => 'idle',
173  'updated' => '2017-01-01 11:11:11',
174  ],
175  'changelog' => [
176  'list_size' => 99999999
177  ]
178  ]
179  ],
180  'indexer_3' => [
181  'indexer_id' => 'indexer_3',
182  'title' => 'Title_indexer3',
183  'status' => StateInterface::STATUS_WORKING,
184  'is_scheduled' => true,
185  'view' => [
186  'state' => [
187  'status' => 'idle',
188  'updated' => '2017-01-01 11:11:11',
189  ],
190  'changelog' => [
191  'list_size' => 100
192  ]
193  ]
194  ],
195  'indexer_4' => [
196  'indexer_id' => 'indexer_4',
197  'title' => 'Title_indexer4',
198  'status' => null,
199  'is_scheduled' => true,
200  'view' => [
201  'state' => [
202  'status' => 'running',
203  'updated' => '2017-01-01 11:11:11',
204  ],
205  'changelog' => [
206  'list_size' => 20
207  ]
208  ]
209  ],
210  ],
211  ],
212  ];
213  }
214 }