Skip to content

Commit

Permalink
Use factories/interfaces correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
convenient committed Nov 8, 2017
1 parent 1e34fde commit 6d3bffa
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;
use Magento\Framework\Mview\View;
use Magento\Framework\Mview\View\CollectionFactory;
use Magento\Framework\Console\Cli;

/**
* Command for displaying status of mview indexers.
*/
class IndexerStatusMviewCommand extends Command
{
/** @var \Magento\Framework\Mview\View\CollectionInterface $mviewIndexersCollection */
private $mviewIndexersCollection;
/** @var \Magento\Framework\Mview\View\CollectionInterface $mviewCollection */
private $mviewCollection;

public function __construct(
\Magento\Framework\Mview\View\CollectionInterface $collection
CollectionFactory $collectionFactory
) {
$this->mviewIndexersCollection = $collection;
$this->mviewCollection = $collectionFactory->create();

parent::__construct();
}

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

$rows = [];

/** @var \Magento\Framework\Mview\View $indexer */
foreach ($this->mviewIndexersCollection as $indexer) {
$state = $indexer->getState();
$changelog = $indexer->getChangelog();
/** @var \Magento\Framework\Mview\View $view */
foreach ($this->mviewCollection as $view) {
$state = $view->getState();
$changelog = $view->getChangelog();

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

$rows[] = [
$indexer->getData('view_id'),
$state->getData('mode'),
$state->getData('status'),
$state->getData('updated'),
$state->getData('version_id'),
$view->getId(),
$state->getMode(),
$state->getStatus(),
$state->getUpdated(),
$state->getVersionId(),
$pendingString,
];
}
Expand All @@ -82,14 +85,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
$table->addRows($rows);
$table->render($output);

return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
return Cli::RETURN_SUCCESS;
} catch (\Exception $e) {
$output->writeln('<error>' . $e->getMessage() . '</error>');
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
$output->writeln($e->getTraceAsString());
}

return \Magento\Framework\Console\Cli::RETURN_FAILURE;
return Cli::RETURN_FAILURE;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Symfony\Component\Console\Helper\TableHelper;
use Magento\Store\Model\Website;
use Magento\Framework\Console\Cli;
use Magento\Framework\Mview\View\CollectionFactory;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
Expand Down Expand Up @@ -45,9 +46,15 @@ protected function setUp()
$isLoadedProperty->setAccessible(true);
$isLoadedProperty->setValue($this->collection, true);

$collectionFactory = $this->getMockBuilder(CollectionFactory::class)
->disableOriginalConstructor()
->getMock();
$collectionFactory->method('create')
->willReturn($this->collection);

$this->command = $this->objectManager->getObject(
IndexerStatusMviewCommand::class,
['collection' => $this->collection]
['collectionFactory' => $collectionFactory]
);

/** @var HelperSet $helperSet */
Expand All @@ -66,6 +73,8 @@ public function testExecute()
[
'view' => [
'view_id' => 'catalog_category_product',
],
'state' => [
'mode' => 'enabled',
'status' => 'idle',
'updated' => '2017-01-01 11:11:11',
Expand All @@ -78,6 +87,8 @@ public function testExecute()
[
'view' => [
'view_id' => 'catalog_product_category',
],
'state' => [
'mode' => 'disabled',
'status' => 'idle',
'updated' => '2017-01-01 11:11:11',
Expand All @@ -90,6 +101,8 @@ public function testExecute()
[
'view' => [
'view_id' => 'catalog_product_attribute',
],
'state' => [
'mode' => 'enabled',
'status' => 'idle',
'updated' => '2017-01-01 11:11:11',
Expand All @@ -102,7 +115,7 @@ public function testExecute()
];

foreach ($mviews as $data) {
$this->collection->addItem($this->generateMviewStub($data['view'], $data['changelog']));
$this->collection->addItem($this->generateMviewStub($data['view'], $data['changelog'], $data['state']));
}
$this->collection->addItem($this->getNeverEnabledMviewIndexerWithNoTable());

Expand Down Expand Up @@ -153,18 +166,19 @@ public function testExecute()
/**
* @param array $viewData
* @param array $changelogData
* @param array $stateData
* @return Mview\View|Mview\View\Changelog|\PHPUnit_Framework_MockObject_MockObject
*/
protected function generateMviewStub(array $viewData, array $changelogData)
protected function generateMviewStub(array $viewData, array $changelogData, array $stateData)
{
/** @var Mview\View\Changelog|\PHPUnit_Framework_MockObject_MockObject $stub */
$changelog = $this->getMockBuilder(\Magento\Framework\Mview\View\Changelog::class)
->disableOriginalConstructor()
->getMock();

$list = [];
if ($changelogData['version_id'] !== $viewData['version_id']) {
$list = range($viewData['version_id']+1, $changelogData['version_id']);
if ($changelogData['version_id'] !== $stateData['version_id']) {
$list = range($stateData['version_id']+1, $changelogData['version_id']);
}

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

/** @var \Magento\Indexer\Model\Mview\View\State|\PHPUnit_Framework_MockObject_MockObject $stub */
$state = $this->getMockBuilder(\Magento\Indexer\Model\Mview\View\State::class)
->disableOriginalConstructor()
->setMethods(['loadByView'])
->getMock();

$state->setData($stateData);

/** @var Mview\View|\PHPUnit_Framework_MockObject_MockObject $stub */
$stub = $this->getMockBuilder(\Magento\Framework\Mview\View::class)
->disableOriginalConstructor()
Expand All @@ -187,7 +209,7 @@ protected function generateMviewStub(array $viewData, array $changelogData)

$stub->expects($this->any())
->method('getState')
->willReturnSelf();
->willReturn($state);

$stub->setData($viewData);

Expand Down

0 comments on commit 6d3bffa

Please sign in to comment.