Skip to content

Commit b52f7c6

Browse files
committed
Use factories/interfaces correctly
1 parent c0078be commit b52f7c6

File tree

2 files changed

+46
-21
lines changed

2 files changed

+46
-21
lines changed

app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,22 @@
99
use Symfony\Component\Console\Output\OutputInterface;
1010
use Symfony\Component\Console\Command\Command;
1111
use Magento\Framework\Mview\View;
12+
use Magento\Framework\Mview\View\CollectionFactory;
13+
use Magento\Framework\Console\Cli;
1214

1315
/**
1416
* Command for displaying status of mview indexers.
1517
*/
1618
class IndexerStatusMviewCommand extends Command
1719
{
18-
/** @var \Magento\Framework\Mview\View\CollectionInterface $mviewIndexersCollection */
19-
private $mviewIndexersCollection;
20+
/** @var \Magento\Framework\Mview\View\CollectionInterface $mviewCollection */
21+
private $mviewCollection;
2022

2123
public function __construct(
22-
\Magento\Framework\Mview\View\CollectionInterface $collection
24+
CollectionFactory $collectionFactory
2325
) {
24-
$this->mviewIndexersCollection = $collection;
26+
$this->mviewCollection = $collectionFactory->create();
27+
2528
parent::__construct();
2629
}
2730

@@ -47,10 +50,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
4750

4851
$rows = [];
4952

50-
/** @var \Magento\Framework\Mview\View $indexer */
51-
foreach ($this->mviewIndexersCollection as $indexer) {
52-
$state = $indexer->getState();
53-
$changelog = $indexer->getChangelog();
53+
/** @var \Magento\Framework\Mview\View $view */
54+
foreach ($this->mviewCollection as $view) {
55+
$state = $view->getState();
56+
$changelog = $view->getChangelog();
5457

5558
try {
5659
$currentVersionId = $changelog->getVersion();
@@ -66,11 +69,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
6669
}
6770

6871
$rows[] = [
69-
$indexer->getData('view_id'),
70-
$state->getData('mode'),
71-
$state->getData('status'),
72-
$state->getData('updated'),
73-
$state->getData('version_id'),
72+
$view->getId(),
73+
$state->getMode(),
74+
$state->getStatus(),
75+
$state->getUpdated(),
76+
$state->getVersionId(),
7477
$pendingString,
7578
];
7679
}
@@ -82,14 +85,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
8285
$table->addRows($rows);
8386
$table->render($output);
8487

85-
return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
88+
return Cli::RETURN_SUCCESS;
8689
} catch (\Exception $e) {
8790
$output->writeln('<error>' . $e->getMessage() . '</error>');
8891
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
8992
$output->writeln($e->getTraceAsString());
9093
}
9194

92-
return \Magento\Framework\Console\Cli::RETURN_FAILURE;
95+
return Cli::RETURN_FAILURE;
9396
}
9497
}
9598
}

app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Symfony\Component\Console\Helper\TableHelper;
1313
use Magento\Store\Model\Website;
1414
use Magento\Framework\Console\Cli;
15+
use Magento\Framework\Mview\View\CollectionFactory;
1516

1617
/**
1718
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -45,9 +46,15 @@ protected function setUp()
4546
$isLoadedProperty->setAccessible(true);
4647
$isLoadedProperty->setValue($this->collection, true);
4748

49+
$collectionFactory = $this->getMockBuilder(CollectionFactory::class)
50+
->disableOriginalConstructor()
51+
->getMock();
52+
$collectionFactory->method('create')
53+
->willReturn($this->collection);
54+
4855
$this->command = $this->objectManager->getObject(
4956
IndexerStatusMviewCommand::class,
50-
['collection' => $this->collection]
57+
['collectionFactory' => $collectionFactory]
5158
);
5259

5360
/** @var HelperSet $helperSet */
@@ -66,6 +73,8 @@ public function testExecute()
6673
[
6774
'view' => [
6875
'view_id' => 'catalog_category_product',
76+
],
77+
'state' => [
6978
'mode' => 'enabled',
7079
'status' => 'idle',
7180
'updated' => '2017-01-01 11:11:11',
@@ -78,6 +87,8 @@ public function testExecute()
7887
[
7988
'view' => [
8089
'view_id' => 'catalog_product_category',
90+
],
91+
'state' => [
8192
'mode' => 'disabled',
8293
'status' => 'idle',
8394
'updated' => '2017-01-01 11:11:11',
@@ -90,6 +101,8 @@ public function testExecute()
90101
[
91102
'view' => [
92103
'view_id' => 'catalog_product_attribute',
104+
],
105+
'state' => [
93106
'mode' => 'enabled',
94107
'status' => 'idle',
95108
'updated' => '2017-01-01 11:11:11',
@@ -102,7 +115,7 @@ public function testExecute()
102115
];
103116

104117
foreach ($mviews as $data) {
105-
$this->collection->addItem($this->generateMviewStub($data['view'], $data['changelog']));
118+
$this->collection->addItem($this->generateMviewStub($data['view'], $data['changelog'], $data['state']));
106119
}
107120
$this->collection->addItem($this->getNeverEnabledMviewIndexerWithNoTable());
108121

@@ -153,18 +166,19 @@ public function testExecute()
153166
/**
154167
* @param array $viewData
155168
* @param array $changelogData
169+
* @param array $stateData
156170
* @return Mview\View|Mview\View\Changelog|\PHPUnit_Framework_MockObject_MockObject
157171
*/
158-
protected function generateMviewStub(array $viewData, array $changelogData)
172+
protected function generateMviewStub(array $viewData, array $changelogData, array $stateData)
159173
{
160174
/** @var Mview\View\Changelog|\PHPUnit_Framework_MockObject_MockObject $stub */
161175
$changelog = $this->getMockBuilder(\Magento\Framework\Mview\View\Changelog::class)
162176
->disableOriginalConstructor()
163177
->getMock();
164178

165179
$list = [];
166-
if ($changelogData['version_id'] !== $viewData['version_id']) {
167-
$list = range($viewData['version_id']+1, $changelogData['version_id']);
180+
if ($changelogData['version_id'] !== $stateData['version_id']) {
181+
$list = range($stateData['version_id']+1, $changelogData['version_id']);
168182
}
169183

170184
$changelog->expects($this->any())
@@ -175,6 +189,14 @@ protected function generateMviewStub(array $viewData, array $changelogData)
175189
->method('getVersion')
176190
->willReturn($changelogData['version_id']);
177191

192+
/** @var \Magento\Indexer\Model\Mview\View\State|\PHPUnit_Framework_MockObject_MockObject $stub */
193+
$state = $this->getMockBuilder(\Magento\Indexer\Model\Mview\View\State::class)
194+
->disableOriginalConstructor()
195+
->setMethods(['loadByView'])
196+
->getMock();
197+
198+
$state->setData($stateData);
199+
178200
/** @var Mview\View|\PHPUnit_Framework_MockObject_MockObject $stub */
179201
$stub = $this->getMockBuilder(\Magento\Framework\Mview\View::class)
180202
->disableOriginalConstructor()
@@ -187,7 +209,7 @@ protected function generateMviewStub(array $viewData, array $changelogData)
187209

188210
$stub->expects($this->any())
189211
->method('getState')
190-
->willReturnSelf();
212+
->willReturn($state);
191213

192214
$stub->setData($viewData);
193215

0 commit comments

Comments
 (0)