From 71ae63cb964adc50e356f9bb21282798f5e05b30 Mon Sep 17 00:00:00 2001 From: Luke Rodgers Date: Sun, 5 Nov 2017 20:51:31 +0000 Subject: [PATCH 01/12] Add command to view mview state and queue This is similar to the magerun1 command here: https://github.com/netz98/n98-magerun/pull/891 I like the ability to view the mview queue in realtime as its being processed, it can be quite helpful when debugging indexing issues. This command will actually show how many items are in the list pending processing, as well information from the `mview_state` table. ``` php bin/magento indexer:status:mview +---------------------------+----------+--------+---------------------+------------+---------+ | ID | Mode | Status | Updated | Version ID | Backlog | +---------------------------+----------+--------+---------------------+------------+---------+ | catalog_category_product | enabled | idle | 2017-11-02 10:00:00 | 1 | 0 | | catalog_product_attribute | enabled | idle | 2017-11-02 10:00:00 | 1 | 1 | | catalog_product_category | disabled | idle | 2017-11-02 10:00:00 | 1 | 0 | | catalog_product_price | enabled | idle | 2017-11-02 10:00:00 | 1 | 0 | +---------------------------+----------+--------+---------------------+------------+---------+ ``` I'll point this PR into 2.1.x and raise a separate PR to pop it into 2.2.x. --- .../Command/IndexerStatusMviewCommand.php | 95 +++++++ .../Command/IndexerStatusMviewCommandTest.php | 233 ++++++++++++++++++ app/code/Magento/Indexer/etc/di.xml | 1 + 3 files changed, 329 insertions(+) create mode 100644 app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php create mode 100644 app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php diff --git a/app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php b/app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php new file mode 100644 index 0000000000000..4fb0c0bcb5649 --- /dev/null +++ b/app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php @@ -0,0 +1,95 @@ +mviewIndexersCollection = $collection; + parent::__construct(); + } + + /** + * {@inheritdoc} + */ + protected function configure() + { + $this->setName('indexer:status:mview') + ->setDescription('Shows status of Mview Indexers and their queue status'); + + parent::configure(); + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + try { + $table = $this->getHelperSet()->get('table'); + $table->setHeaders(['ID', 'Mode', 'Status', 'Updated', 'Version ID', 'Backlog']); + + $rows = []; + + /** @var \Magento\Framework\Mview\View $indexer */ + foreach ($this->mviewIndexersCollection as $indexer) { + $state = $indexer->getState(); + $changelog = $indexer->getChangelog(); + + try { + $currentVersionId = $changelog->getVersion(); + } catch (View\ChangelogTableNotExistsException $e) { + continue; + } + + $pendingCount = count($changelog->getList($state->getVersionId(), $currentVersionId)); + + $pendingString = "$pendingCount"; + if ($pendingCount <= 0) { + $pendingString = "$pendingCount"; + } + + $rows[] = [ + $indexer->getData('view_id'), + $state->getData('mode'), + $state->getData('status'), + $state->getData('updated'), + $state->getData('version_id'), + $pendingString, + ]; + } + + usort($rows, function($a, $b) { + return $a[0] <=> $b[0]; + }); + + $table->addRows($rows); + $table->render($output); + + return \Magento\Framework\Console\Cli::RETURN_SUCCESS; + } catch (\Exception $e) { + $output->writeln('' . $e->getMessage() . ''); + if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { + $output->writeln($e->getTraceAsString()); + } + + return \Magento\Framework\Console\Cli::RETURN_FAILURE; + } + } +} diff --git a/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php b/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php new file mode 100644 index 0000000000000..7266d009a5ee7 --- /dev/null +++ b/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php @@ -0,0 +1,233 @@ +objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + + /** @var \Magento\Framework\Mview\View\Collection $collection */ + $this->collection = $this->objectManager->getObject(Mview\View\Collection::class); + + $reflectedCollection = new \ReflectionObject($this->collection); + $isLoadedProperty = $reflectedCollection->getProperty('_isCollectionLoaded'); + $isLoadedProperty->setAccessible(true); + $isLoadedProperty->setValue($this->collection, true); + + $this->command = $this->objectManager->getObject( + IndexerStatusMviewCommand::class, + ['collection' => $this->collection] + ); + + /** @var HelperSet $helperSet */ + $helperSet = $this->objectManager->getObject( + HelperSet::class, + ['helpers' => [$this->objectManager->getObject(TableHelper::class)]] + ); + + //Inject table helper for output + $this->command->setHelperSet($helperSet); + } + + public function testExecute() + { + $mviews = [ + [ + 'view' => [ + 'view_id' => 'catalog_category_product', + 'mode' => 'enabled', + 'status' => 'idle', + 'updated' => '2017-01-01 11:11:11', + 'version_id' => 100, + ], + 'changelog' => [ + 'version_id' => 110 + ], + ], + [ + 'view' => [ + 'view_id' => 'catalog_product_category', + 'mode' => 'disabled', + 'status' => 'idle', + 'updated' => '2017-01-01 11:11:11', + 'version_id' => 100, + ], + 'changelog' => [ + 'version_id' => 200 + ], + ], + [ + 'view' => [ + 'view_id' => 'catalog_product_attribute', + 'mode' => 'enabled', + 'status' => 'idle', + 'updated' => '2017-01-01 11:11:11', + 'version_id' => 100, + ], + 'changelog' => [ + 'version_id' => 100 + ], + ], + ]; + + foreach ($mviews as $data) { + $this->collection->addItem($this->generateMviewStub($data['view'], $data['changelog'])); + } + + /** @var Mview\View\Changelog|\PHPUnit_Framework_MockObject_MockObject $stub */ + $changelog = $this->getMockBuilder(\Magento\Framework\Mview\View\Changelog::class) + ->disableOriginalConstructor() + ->getMock(); + + $changelog->expects($this->any()) + ->method('getVersion') + ->willThrowException( + new Mview\View\ChangelogTableNotExistsException(new \Magento\Framework\Phrase("Do not render")) + ); + + /** @var Mview\View|\PHPUnit_Framework_MockObject_MockObject $notInitiatedMview */ + $notInitiatedMview = $this->getMockBuilder(\Magento\Framework\Mview\View::class) + ->disableOriginalConstructor() + ->getMock(); + + $notInitiatedMview->expects($this->any()) + ->method('getChangelog') + ->willReturn($changelog); + + $this->collection->addItem($notInitiatedMview); + + $tester = new CommandTester($this->command); + $this->assertEquals(Cli::RETURN_SUCCESS, $tester->execute([])); + + $linesOutput = array_filter(explode(PHP_EOL, $tester->getDisplay())); + $this->assertCount(7, $linesOutput, 'There should be 7 lines output. 3 Spacers, 1 header, 3 content.'); + $this->assertEquals($linesOutput[0], $linesOutput[2], "Lines 0, 2, 7 should be spacer lines"); + $this->assertEquals($linesOutput[2], $linesOutput[6], "Lines 0, 2, 6 should be spacer lines"); + + $headerValues = array_values(array_filter(explode('|', $linesOutput[1]))); + $this->assertEquals('ID', trim($headerValues[0])); + $this->assertEquals('Mode', trim($headerValues[1])); + $this->assertEquals('Status', trim($headerValues[2])); + $this->assertEquals('Updated', trim($headerValues[3])); + $this->assertEquals('Version ID', trim($headerValues[4])); + $this->assertEquals('Backlog', trim($headerValues[5])); + + $catalogCategoryProductMviewData = array_values(array_filter(explode('|', $linesOutput[3]))); + $this->assertEquals('catalog_category_product', trim($catalogCategoryProductMviewData[0])); + $this->assertEquals('enabled', trim($catalogCategoryProductMviewData[1])); + $this->assertEquals('idle', trim($catalogCategoryProductMviewData[2])); + $this->assertEquals('2017-01-01 11:11:11', trim($catalogCategoryProductMviewData[3])); + $this->assertEquals('100', trim($catalogCategoryProductMviewData[4])); + $this->assertEquals('10', trim($catalogCategoryProductMviewData[5])); + unset($catalogCategoryProductMviewData); + + $catalogProductAttributeMviewData = array_values(array_filter(explode('|', $linesOutput[4]))); + $this->assertEquals('catalog_product_attribute', trim($catalogProductAttributeMviewData[0])); + $this->assertEquals('enabled', trim($catalogProductAttributeMviewData[1])); + $this->assertEquals('idle', trim($catalogProductAttributeMviewData[2])); + $this->assertEquals('2017-01-01 11:11:11', trim($catalogProductAttributeMviewData[3])); + $this->assertEquals('100', trim($catalogProductAttributeMviewData[4])); + $this->assertEquals('0', trim($catalogProductAttributeMviewData[5])); + unset($catalogProductAttributeMviewData); + + $catalogCategoryProductMviewData = array_values(array_filter(explode('|', $linesOutput[5]))); + $this->assertEquals('catalog_product_category', trim($catalogCategoryProductMviewData[0])); + $this->assertEquals('disabled', trim($catalogCategoryProductMviewData[1])); + $this->assertEquals('idle', trim($catalogCategoryProductMviewData[2])); + $this->assertEquals('2017-01-01 11:11:11', trim($catalogCategoryProductMviewData[3])); + $this->assertEquals('100', trim($catalogCategoryProductMviewData[4])); + $this->assertEquals('100', trim($catalogCategoryProductMviewData[5])); + unset($catalogCategoryProductMviewData); + } + + /** + * @param array $viewData + * @param array $changelogData + * @return Mview\View|Mview\View\Changelog|\PHPUnit_Framework_MockObject_MockObject + */ + protected function generateMviewStub(array $viewData, array $changelogData) + { + /** @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']); + } + + $changelog->expects($this->any()) + ->method('getList') + ->willReturn($list); + + $changelog->expects($this->any()) + ->method('getVersion') + ->willReturn($changelogData['version_id']); + + /** @var Mview\View|\PHPUnit_Framework_MockObject_MockObject $stub */ + $stub = $this->getMockBuilder(\Magento\Framework\Mview\View::class) + ->disableOriginalConstructor() + ->setMethods(['getChangelog', 'getState']) + ->getMock(); + + $stub->expects($this->any()) + ->method('getChangelog') + ->willReturn($changelog); + + $stub->expects($this->any()) + ->method('getState') + ->willReturnSelf(); + + $stub->setData($viewData); + + return $stub; + } + + public function testExecuteExceptionNoVerbosity() + { + /** @var \Magento\Framework\Mview\View|\PHPUnit_Framework_MockObject_MockObject $stub */ + $stub = $this->getMockBuilder(Mview\View::class) + ->disableOriginalConstructor() + ->getMock(); + + $stub->expects($this->any()) + ->method('getChangelog') + ->willThrowException(new \Exception("Dummy test exception")); + + $this->collection->addItem($stub); + + $tester = new CommandTester($this->command); + $this->assertEquals(Cli::RETURN_FAILURE, $tester->execute([])); + $linesOutput = array_filter(explode(PHP_EOL, $tester->getDisplay())); + $this->assertEquals('Dummy test exception', $linesOutput[0]); + } +} diff --git a/app/code/Magento/Indexer/etc/di.xml b/app/code/Magento/Indexer/etc/di.xml index 1d3f125406f7d..2ad57b1495baf 100644 --- a/app/code/Magento/Indexer/etc/di.xml +++ b/app/code/Magento/Indexer/etc/di.xml @@ -51,6 +51,7 @@ Magento\Indexer\Console\Command\IndexerSetModeCommand Magento\Indexer\Console\Command\IndexerShowModeCommand Magento\Indexer\Console\Command\IndexerStatusCommand + Magento\Indexer\Console\Command\IndexerStatusMviewCommand Magento\Indexer\Console\Command\IndexerResetStateCommand From c0078bef6b1af1307879cada6525c600fff85722 Mon Sep 17 00:00:00 2001 From: Luke Rodgers Date: Sun, 5 Nov 2017 21:23:23 +0000 Subject: [PATCH 02/12] Make indexer status mview 5.5 compatible --- .../Command/IndexerStatusMviewCommand.php | 4 +- .../Command/IndexerStatusMviewCommandTest.php | 54 +++++++++++-------- 2 files changed, 34 insertions(+), 24 deletions(-) diff --git a/app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php b/app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php index 4fb0c0bcb5649..61461a0ba610c 100644 --- a/app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php +++ b/app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php @@ -75,8 +75,8 @@ protected function execute(InputInterface $input, OutputInterface $output) ]; } - usort($rows, function($a, $b) { - return $a[0] <=> $b[0]; + usort($rows, function ($a, $b) { + return strcmp($a[0], $b[0]); }); $table->addRows($rows); diff --git a/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php b/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php index 7266d009a5ee7..e6a782cba92fd 100644 --- a/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php +++ b/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php @@ -13,6 +13,9 @@ use Magento\Store\Model\Website; use Magento\Framework\Console\Cli; +/** + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + */ class IndexerStatusMviewCommandTest extends \PHPUnit_Framework_TestCase { /** @@ -101,28 +104,7 @@ public function testExecute() foreach ($mviews as $data) { $this->collection->addItem($this->generateMviewStub($data['view'], $data['changelog'])); } - - /** @var Mview\View\Changelog|\PHPUnit_Framework_MockObject_MockObject $stub */ - $changelog = $this->getMockBuilder(\Magento\Framework\Mview\View\Changelog::class) - ->disableOriginalConstructor() - ->getMock(); - - $changelog->expects($this->any()) - ->method('getVersion') - ->willThrowException( - new Mview\View\ChangelogTableNotExistsException(new \Magento\Framework\Phrase("Do not render")) - ); - - /** @var Mview\View|\PHPUnit_Framework_MockObject_MockObject $notInitiatedMview */ - $notInitiatedMview = $this->getMockBuilder(\Magento\Framework\Mview\View::class) - ->disableOriginalConstructor() - ->getMock(); - - $notInitiatedMview->expects($this->any()) - ->method('getChangelog') - ->willReturn($changelog); - - $this->collection->addItem($notInitiatedMview); + $this->collection->addItem($this->getNeverEnabledMviewIndexerWithNoTable()); $tester = new CommandTester($this->command); $this->assertEquals(Cli::RETURN_SUCCESS, $tester->execute([])); @@ -212,6 +194,34 @@ protected function generateMviewStub(array $viewData, array $changelogData) return $stub; } + /** + * @return Mview\View|\PHPUnit_Framework_MockObject_MockObject + */ + protected function getNeverEnabledMviewIndexerWithNoTable() + { + /** @var Mview\View\Changelog|\PHPUnit_Framework_MockObject_MockObject $stub */ + $changelog = $this->getMockBuilder(\Magento\Framework\Mview\View\Changelog::class) + ->disableOriginalConstructor() + ->getMock(); + + $changelog->expects($this->any()) + ->method('getVersion') + ->willThrowException( + new Mview\View\ChangelogTableNotExistsException(new \Magento\Framework\Phrase("Do not render")) + ); + + /** @var Mview\View|\PHPUnit_Framework_MockObject_MockObject $notInitiatedMview */ + $notInitiatedMview = $this->getMockBuilder(\Magento\Framework\Mview\View::class) + ->disableOriginalConstructor() + ->getMock(); + + $notInitiatedMview->expects($this->any()) + ->method('getChangelog') + ->willReturn($changelog); + + return $notInitiatedMview; + } + public function testExecuteExceptionNoVerbosity() { /** @var \Magento\Framework\Mview\View|\PHPUnit_Framework_MockObject_MockObject $stub */ From b52f7c6e6efb7a46b50a290e768acdb5097f2243 Mon Sep 17 00:00:00 2001 From: Luke Rodgers Date: Mon, 6 Nov 2017 17:55:45 +0000 Subject: [PATCH 03/12] Use factories/interfaces correctly --- .../Command/IndexerStatusMviewCommand.php | 33 ++++++++++-------- .../Command/IndexerStatusMviewCommandTest.php | 34 +++++++++++++++---- 2 files changed, 46 insertions(+), 21 deletions(-) diff --git a/app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php b/app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php index 61461a0ba610c..cb60d4f31da7f 100644 --- a/app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php +++ b/app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php @@ -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(); } @@ -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(); @@ -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, ]; } @@ -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('' . $e->getMessage() . ''); if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { $output->writeln($e->getTraceAsString()); } - return \Magento\Framework\Console\Cli::RETURN_FAILURE; + return Cli::RETURN_FAILURE; } } } diff --git a/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php b/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php index e6a782cba92fd..43ffed3fd1e93 100644 --- a/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php +++ b/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php @@ -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) @@ -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 */ @@ -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', @@ -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', @@ -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', @@ -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()); @@ -153,9 +166,10 @@ 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) @@ -163,8 +177,8 @@ protected function generateMviewStub(array $viewData, array $changelogData) ->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()) @@ -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() @@ -187,7 +209,7 @@ protected function generateMviewStub(array $viewData, array $changelogData) $stub->expects($this->any()) ->method('getState') - ->willReturnSelf(); + ->willReturn($state); $stub->setData($viewData); From 9c72a0d9aca931e5c30df73f20369031e492167e Mon Sep 17 00:00:00 2001 From: Luke Rodgers Date: Tue, 7 Nov 2017 17:32:41 +0000 Subject: [PATCH 04/12] Update code style --- .../Command/IndexerStatusMviewCommand.php | 4 +- .../Command/IndexerStatusMviewCommandTest.php | 52 +++++++++---------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php b/app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php index cb60d4f31da7f..5451df34645e9 100644 --- a/app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php +++ b/app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php @@ -78,8 +78,8 @@ protected function execute(InputInterface $input, OutputInterface $output) ]; } - usort($rows, function ($a, $b) { - return strcmp($a[0], $b[0]); + usort($rows, function ($comp1, $comp2) { + return strcmp($comp1[0], $comp2[0]); }); $table->addRows($rows); diff --git a/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php b/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php index 43ffed3fd1e93..b58596be70c48 100644 --- a/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php +++ b/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php @@ -135,32 +135,32 @@ public function testExecute() $this->assertEquals('Version ID', trim($headerValues[4])); $this->assertEquals('Backlog', trim($headerValues[5])); - $catalogCategoryProductMviewData = array_values(array_filter(explode('|', $linesOutput[3]))); - $this->assertEquals('catalog_category_product', trim($catalogCategoryProductMviewData[0])); - $this->assertEquals('enabled', trim($catalogCategoryProductMviewData[1])); - $this->assertEquals('idle', trim($catalogCategoryProductMviewData[2])); - $this->assertEquals('2017-01-01 11:11:11', trim($catalogCategoryProductMviewData[3])); - $this->assertEquals('100', trim($catalogCategoryProductMviewData[4])); - $this->assertEquals('10', trim($catalogCategoryProductMviewData[5])); - unset($catalogCategoryProductMviewData); - - $catalogProductAttributeMviewData = array_values(array_filter(explode('|', $linesOutput[4]))); - $this->assertEquals('catalog_product_attribute', trim($catalogProductAttributeMviewData[0])); - $this->assertEquals('enabled', trim($catalogProductAttributeMviewData[1])); - $this->assertEquals('idle', trim($catalogProductAttributeMviewData[2])); - $this->assertEquals('2017-01-01 11:11:11', trim($catalogProductAttributeMviewData[3])); - $this->assertEquals('100', trim($catalogProductAttributeMviewData[4])); - $this->assertEquals('0', trim($catalogProductAttributeMviewData[5])); - unset($catalogProductAttributeMviewData); - - $catalogCategoryProductMviewData = array_values(array_filter(explode('|', $linesOutput[5]))); - $this->assertEquals('catalog_product_category', trim($catalogCategoryProductMviewData[0])); - $this->assertEquals('disabled', trim($catalogCategoryProductMviewData[1])); - $this->assertEquals('idle', trim($catalogCategoryProductMviewData[2])); - $this->assertEquals('2017-01-01 11:11:11', trim($catalogCategoryProductMviewData[3])); - $this->assertEquals('100', trim($catalogCategoryProductMviewData[4])); - $this->assertEquals('100', trim($catalogCategoryProductMviewData[5])); - unset($catalogCategoryProductMviewData); + $categoryProduct = array_values(array_filter(explode('|', $linesOutput[3]))); + $this->assertEquals('catalog_category_product', trim($categoryProduct[0])); + $this->assertEquals('enabled', trim($categoryProduct[1])); + $this->assertEquals('idle', trim($categoryProduct[2])); + $this->assertEquals('2017-01-01 11:11:11', trim($categoryProduct[3])); + $this->assertEquals('100', trim($categoryProduct[4])); + $this->assertEquals('10', trim($categoryProduct[5])); + unset($categoryProduct); + + $productAttribute = array_values(array_filter(explode('|', $linesOutput[4]))); + $this->assertEquals('catalog_product_attribute', trim($productAttribute[0])); + $this->assertEquals('enabled', trim($productAttribute[1])); + $this->assertEquals('idle', trim($productAttribute[2])); + $this->assertEquals('2017-01-01 11:11:11', trim($productAttribute[3])); + $this->assertEquals('100', trim($productAttribute[4])); + $this->assertEquals('0', trim($productAttribute[5])); + unset($productAttribute); + + $productCategory = array_values(array_filter(explode('|', $linesOutput[5]))); + $this->assertEquals('catalog_product_category', trim($productCategory[0])); + $this->assertEquals('disabled', trim($productCategory[1])); + $this->assertEquals('idle', trim($productCategory[2])); + $this->assertEquals('2017-01-01 11:11:11', trim($productCategory[3])); + $this->assertEquals('100', trim($productCategory[4])); + $this->assertEquals('100', trim($productCategory[5])); + unset($productCategory); } /** From 81f8f6302d222d7cc48c4d5c31ce06e24eef341d Mon Sep 17 00:00:00 2001 From: Luke Rodgers Date: Wed, 6 Dec 2017 14:19:40 +0000 Subject: [PATCH 05/12] Revert "Update code style" This reverts commit 9c72a0d9aca931e5c30df73f20369031e492167e. --- .../Command/IndexerStatusMviewCommand.php | 4 +- .../Command/IndexerStatusMviewCommandTest.php | 52 +++++++++---------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php b/app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php index 5451df34645e9..cb60d4f31da7f 100644 --- a/app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php +++ b/app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php @@ -78,8 +78,8 @@ protected function execute(InputInterface $input, OutputInterface $output) ]; } - usort($rows, function ($comp1, $comp2) { - return strcmp($comp1[0], $comp2[0]); + usort($rows, function ($a, $b) { + return strcmp($a[0], $b[0]); }); $table->addRows($rows); diff --git a/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php b/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php index b58596be70c48..43ffed3fd1e93 100644 --- a/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php +++ b/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php @@ -135,32 +135,32 @@ public function testExecute() $this->assertEquals('Version ID', trim($headerValues[4])); $this->assertEquals('Backlog', trim($headerValues[5])); - $categoryProduct = array_values(array_filter(explode('|', $linesOutput[3]))); - $this->assertEquals('catalog_category_product', trim($categoryProduct[0])); - $this->assertEquals('enabled', trim($categoryProduct[1])); - $this->assertEquals('idle', trim($categoryProduct[2])); - $this->assertEquals('2017-01-01 11:11:11', trim($categoryProduct[3])); - $this->assertEquals('100', trim($categoryProduct[4])); - $this->assertEquals('10', trim($categoryProduct[5])); - unset($categoryProduct); - - $productAttribute = array_values(array_filter(explode('|', $linesOutput[4]))); - $this->assertEquals('catalog_product_attribute', trim($productAttribute[0])); - $this->assertEquals('enabled', trim($productAttribute[1])); - $this->assertEquals('idle', trim($productAttribute[2])); - $this->assertEquals('2017-01-01 11:11:11', trim($productAttribute[3])); - $this->assertEquals('100', trim($productAttribute[4])); - $this->assertEquals('0', trim($productAttribute[5])); - unset($productAttribute); - - $productCategory = array_values(array_filter(explode('|', $linesOutput[5]))); - $this->assertEquals('catalog_product_category', trim($productCategory[0])); - $this->assertEquals('disabled', trim($productCategory[1])); - $this->assertEquals('idle', trim($productCategory[2])); - $this->assertEquals('2017-01-01 11:11:11', trim($productCategory[3])); - $this->assertEquals('100', trim($productCategory[4])); - $this->assertEquals('100', trim($productCategory[5])); - unset($productCategory); + $catalogCategoryProductMviewData = array_values(array_filter(explode('|', $linesOutput[3]))); + $this->assertEquals('catalog_category_product', trim($catalogCategoryProductMviewData[0])); + $this->assertEquals('enabled', trim($catalogCategoryProductMviewData[1])); + $this->assertEquals('idle', trim($catalogCategoryProductMviewData[2])); + $this->assertEquals('2017-01-01 11:11:11', trim($catalogCategoryProductMviewData[3])); + $this->assertEquals('100', trim($catalogCategoryProductMviewData[4])); + $this->assertEquals('10', trim($catalogCategoryProductMviewData[5])); + unset($catalogCategoryProductMviewData); + + $catalogProductAttributeMviewData = array_values(array_filter(explode('|', $linesOutput[4]))); + $this->assertEquals('catalog_product_attribute', trim($catalogProductAttributeMviewData[0])); + $this->assertEquals('enabled', trim($catalogProductAttributeMviewData[1])); + $this->assertEquals('idle', trim($catalogProductAttributeMviewData[2])); + $this->assertEquals('2017-01-01 11:11:11', trim($catalogProductAttributeMviewData[3])); + $this->assertEquals('100', trim($catalogProductAttributeMviewData[4])); + $this->assertEquals('0', trim($catalogProductAttributeMviewData[5])); + unset($catalogProductAttributeMviewData); + + $catalogCategoryProductMviewData = array_values(array_filter(explode('|', $linesOutput[5]))); + $this->assertEquals('catalog_product_category', trim($catalogCategoryProductMviewData[0])); + $this->assertEquals('disabled', trim($catalogCategoryProductMviewData[1])); + $this->assertEquals('idle', trim($catalogCategoryProductMviewData[2])); + $this->assertEquals('2017-01-01 11:11:11', trim($catalogCategoryProductMviewData[3])); + $this->assertEquals('100', trim($catalogCategoryProductMviewData[4])); + $this->assertEquals('100', trim($catalogCategoryProductMviewData[5])); + unset($catalogCategoryProductMviewData); } /** From 307542675019d7bbb80a23354ca995f79718dba3 Mon Sep 17 00:00:00 2001 From: Luke Rodgers Date: Wed, 6 Dec 2017 14:19:42 +0000 Subject: [PATCH 06/12] Revert "Use factories/interfaces correctly" This reverts commit b52f7c6e6efb7a46b50a290e768acdb5097f2243. --- .../Command/IndexerStatusMviewCommand.php | 33 ++++++++---------- .../Command/IndexerStatusMviewCommandTest.php | 34 ++++--------------- 2 files changed, 21 insertions(+), 46 deletions(-) diff --git a/app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php b/app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php index cb60d4f31da7f..61461a0ba610c 100644 --- a/app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php +++ b/app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php @@ -9,22 +9,19 @@ 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 $mviewCollection */ - private $mviewCollection; + /** @var \Magento\Framework\Mview\View\CollectionInterface $mviewIndexersCollection */ + private $mviewIndexersCollection; public function __construct( - CollectionFactory $collectionFactory + \Magento\Framework\Mview\View\CollectionInterface $collection ) { - $this->mviewCollection = $collectionFactory->create(); - + $this->mviewIndexersCollection = $collection; parent::__construct(); } @@ -50,10 +47,10 @@ protected function execute(InputInterface $input, OutputInterface $output) $rows = []; - /** @var \Magento\Framework\Mview\View $view */ - foreach ($this->mviewCollection as $view) { - $state = $view->getState(); - $changelog = $view->getChangelog(); + /** @var \Magento\Framework\Mview\View $indexer */ + foreach ($this->mviewIndexersCollection as $indexer) { + $state = $indexer->getState(); + $changelog = $indexer->getChangelog(); try { $currentVersionId = $changelog->getVersion(); @@ -69,11 +66,11 @@ protected function execute(InputInterface $input, OutputInterface $output) } $rows[] = [ - $view->getId(), - $state->getMode(), - $state->getStatus(), - $state->getUpdated(), - $state->getVersionId(), + $indexer->getData('view_id'), + $state->getData('mode'), + $state->getData('status'), + $state->getData('updated'), + $state->getData('version_id'), $pendingString, ]; } @@ -85,14 +82,14 @@ protected function execute(InputInterface $input, OutputInterface $output) $table->addRows($rows); $table->render($output); - return Cli::RETURN_SUCCESS; + return \Magento\Framework\Console\Cli::RETURN_SUCCESS; } catch (\Exception $e) { $output->writeln('' . $e->getMessage() . ''); if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { $output->writeln($e->getTraceAsString()); } - return Cli::RETURN_FAILURE; + return \Magento\Framework\Console\Cli::RETURN_FAILURE; } } } diff --git a/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php b/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php index 43ffed3fd1e93..e6a782cba92fd 100644 --- a/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php +++ b/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php @@ -12,7 +12,6 @@ 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) @@ -46,15 +45,9 @@ 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, - ['collectionFactory' => $collectionFactory] + ['collection' => $this->collection] ); /** @var HelperSet $helperSet */ @@ -73,8 +66,6 @@ public function testExecute() [ 'view' => [ 'view_id' => 'catalog_category_product', - ], - 'state' => [ 'mode' => 'enabled', 'status' => 'idle', 'updated' => '2017-01-01 11:11:11', @@ -87,8 +78,6 @@ public function testExecute() [ 'view' => [ 'view_id' => 'catalog_product_category', - ], - 'state' => [ 'mode' => 'disabled', 'status' => 'idle', 'updated' => '2017-01-01 11:11:11', @@ -101,8 +90,6 @@ public function testExecute() [ 'view' => [ 'view_id' => 'catalog_product_attribute', - ], - 'state' => [ 'mode' => 'enabled', 'status' => 'idle', 'updated' => '2017-01-01 11:11:11', @@ -115,7 +102,7 @@ public function testExecute() ]; foreach ($mviews as $data) { - $this->collection->addItem($this->generateMviewStub($data['view'], $data['changelog'], $data['state'])); + $this->collection->addItem($this->generateMviewStub($data['view'], $data['changelog'])); } $this->collection->addItem($this->getNeverEnabledMviewIndexerWithNoTable()); @@ -166,10 +153,9 @@ 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, array $stateData) + protected function generateMviewStub(array $viewData, array $changelogData) { /** @var Mview\View\Changelog|\PHPUnit_Framework_MockObject_MockObject $stub */ $changelog = $this->getMockBuilder(\Magento\Framework\Mview\View\Changelog::class) @@ -177,8 +163,8 @@ protected function generateMviewStub(array $viewData, array $changelogData, arra ->getMock(); $list = []; - if ($changelogData['version_id'] !== $stateData['version_id']) { - $list = range($stateData['version_id']+1, $changelogData['version_id']); + if ($changelogData['version_id'] !== $viewData['version_id']) { + $list = range($viewData['version_id']+1, $changelogData['version_id']); } $changelog->expects($this->any()) @@ -189,14 +175,6 @@ protected function generateMviewStub(array $viewData, array $changelogData, arra ->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() @@ -209,7 +187,7 @@ protected function generateMviewStub(array $viewData, array $changelogData, arra $stub->expects($this->any()) ->method('getState') - ->willReturn($state); + ->willReturnSelf(); $stub->setData($viewData); From 08637b05f0adecc72d1abd1c9aacf143411b66e8 Mon Sep 17 00:00:00 2001 From: Luke Rodgers Date: Wed, 6 Dec 2017 14:19:43 +0000 Subject: [PATCH 07/12] Revert "Make indexer status mview 5.5 compatible" This reverts commit c0078bef6b1af1307879cada6525c600fff85722. --- .../Command/IndexerStatusMviewCommand.php | 4 +- .../Command/IndexerStatusMviewCommandTest.php | 54 ++++++++----------- 2 files changed, 24 insertions(+), 34 deletions(-) diff --git a/app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php b/app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php index 61461a0ba610c..4fb0c0bcb5649 100644 --- a/app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php +++ b/app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php @@ -75,8 +75,8 @@ protected function execute(InputInterface $input, OutputInterface $output) ]; } - usort($rows, function ($a, $b) { - return strcmp($a[0], $b[0]); + usort($rows, function($a, $b) { + return $a[0] <=> $b[0]; }); $table->addRows($rows); diff --git a/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php b/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php index e6a782cba92fd..7266d009a5ee7 100644 --- a/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php +++ b/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php @@ -13,9 +13,6 @@ use Magento\Store\Model\Website; use Magento\Framework\Console\Cli; -/** - * @SuppressWarnings(PHPMD.CouplingBetweenObjects) - */ class IndexerStatusMviewCommandTest extends \PHPUnit_Framework_TestCase { /** @@ -104,7 +101,28 @@ public function testExecute() foreach ($mviews as $data) { $this->collection->addItem($this->generateMviewStub($data['view'], $data['changelog'])); } - $this->collection->addItem($this->getNeverEnabledMviewIndexerWithNoTable()); + + /** @var Mview\View\Changelog|\PHPUnit_Framework_MockObject_MockObject $stub */ + $changelog = $this->getMockBuilder(\Magento\Framework\Mview\View\Changelog::class) + ->disableOriginalConstructor() + ->getMock(); + + $changelog->expects($this->any()) + ->method('getVersion') + ->willThrowException( + new Mview\View\ChangelogTableNotExistsException(new \Magento\Framework\Phrase("Do not render")) + ); + + /** @var Mview\View|\PHPUnit_Framework_MockObject_MockObject $notInitiatedMview */ + $notInitiatedMview = $this->getMockBuilder(\Magento\Framework\Mview\View::class) + ->disableOriginalConstructor() + ->getMock(); + + $notInitiatedMview->expects($this->any()) + ->method('getChangelog') + ->willReturn($changelog); + + $this->collection->addItem($notInitiatedMview); $tester = new CommandTester($this->command); $this->assertEquals(Cli::RETURN_SUCCESS, $tester->execute([])); @@ -194,34 +212,6 @@ protected function generateMviewStub(array $viewData, array $changelogData) return $stub; } - /** - * @return Mview\View|\PHPUnit_Framework_MockObject_MockObject - */ - protected function getNeverEnabledMviewIndexerWithNoTable() - { - /** @var Mview\View\Changelog|\PHPUnit_Framework_MockObject_MockObject $stub */ - $changelog = $this->getMockBuilder(\Magento\Framework\Mview\View\Changelog::class) - ->disableOriginalConstructor() - ->getMock(); - - $changelog->expects($this->any()) - ->method('getVersion') - ->willThrowException( - new Mview\View\ChangelogTableNotExistsException(new \Magento\Framework\Phrase("Do not render")) - ); - - /** @var Mview\View|\PHPUnit_Framework_MockObject_MockObject $notInitiatedMview */ - $notInitiatedMview = $this->getMockBuilder(\Magento\Framework\Mview\View::class) - ->disableOriginalConstructor() - ->getMock(); - - $notInitiatedMview->expects($this->any()) - ->method('getChangelog') - ->willReturn($changelog); - - return $notInitiatedMview; - } - public function testExecuteExceptionNoVerbosity() { /** @var \Magento\Framework\Mview\View|\PHPUnit_Framework_MockObject_MockObject $stub */ From d37d0e2f34bc4aa7c892c690ccc55152961597bf Mon Sep 17 00:00:00 2001 From: Luke Rodgers Date: Wed, 6 Dec 2017 14:19:44 +0000 Subject: [PATCH 08/12] Revert "Add command to view mview state and queue" This reverts commit 71ae63cb964adc50e356f9bb21282798f5e05b30. --- .../Command/IndexerStatusMviewCommand.php | 95 ------- .../Command/IndexerStatusMviewCommandTest.php | 233 ------------------ app/code/Magento/Indexer/etc/di.xml | 1 - 3 files changed, 329 deletions(-) delete mode 100644 app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php delete mode 100644 app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php diff --git a/app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php b/app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php deleted file mode 100644 index 4fb0c0bcb5649..0000000000000 --- a/app/code/Magento/Indexer/Console/Command/IndexerStatusMviewCommand.php +++ /dev/null @@ -1,95 +0,0 @@ -mviewIndexersCollection = $collection; - parent::__construct(); - } - - /** - * {@inheritdoc} - */ - protected function configure() - { - $this->setName('indexer:status:mview') - ->setDescription('Shows status of Mview Indexers and their queue status'); - - parent::configure(); - } - - /** - * {@inheritdoc} - */ - protected function execute(InputInterface $input, OutputInterface $output) - { - try { - $table = $this->getHelperSet()->get('table'); - $table->setHeaders(['ID', 'Mode', 'Status', 'Updated', 'Version ID', 'Backlog']); - - $rows = []; - - /** @var \Magento\Framework\Mview\View $indexer */ - foreach ($this->mviewIndexersCollection as $indexer) { - $state = $indexer->getState(); - $changelog = $indexer->getChangelog(); - - try { - $currentVersionId = $changelog->getVersion(); - } catch (View\ChangelogTableNotExistsException $e) { - continue; - } - - $pendingCount = count($changelog->getList($state->getVersionId(), $currentVersionId)); - - $pendingString = "$pendingCount"; - if ($pendingCount <= 0) { - $pendingString = "$pendingCount"; - } - - $rows[] = [ - $indexer->getData('view_id'), - $state->getData('mode'), - $state->getData('status'), - $state->getData('updated'), - $state->getData('version_id'), - $pendingString, - ]; - } - - usort($rows, function($a, $b) { - return $a[0] <=> $b[0]; - }); - - $table->addRows($rows); - $table->render($output); - - return \Magento\Framework\Console\Cli::RETURN_SUCCESS; - } catch (\Exception $e) { - $output->writeln('' . $e->getMessage() . ''); - if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { - $output->writeln($e->getTraceAsString()); - } - - return \Magento\Framework\Console\Cli::RETURN_FAILURE; - } - } -} diff --git a/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php b/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php deleted file mode 100644 index 7266d009a5ee7..0000000000000 --- a/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusMviewCommandTest.php +++ /dev/null @@ -1,233 +0,0 @@ -objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - - /** @var \Magento\Framework\Mview\View\Collection $collection */ - $this->collection = $this->objectManager->getObject(Mview\View\Collection::class); - - $reflectedCollection = new \ReflectionObject($this->collection); - $isLoadedProperty = $reflectedCollection->getProperty('_isCollectionLoaded'); - $isLoadedProperty->setAccessible(true); - $isLoadedProperty->setValue($this->collection, true); - - $this->command = $this->objectManager->getObject( - IndexerStatusMviewCommand::class, - ['collection' => $this->collection] - ); - - /** @var HelperSet $helperSet */ - $helperSet = $this->objectManager->getObject( - HelperSet::class, - ['helpers' => [$this->objectManager->getObject(TableHelper::class)]] - ); - - //Inject table helper for output - $this->command->setHelperSet($helperSet); - } - - public function testExecute() - { - $mviews = [ - [ - 'view' => [ - 'view_id' => 'catalog_category_product', - 'mode' => 'enabled', - 'status' => 'idle', - 'updated' => '2017-01-01 11:11:11', - 'version_id' => 100, - ], - 'changelog' => [ - 'version_id' => 110 - ], - ], - [ - 'view' => [ - 'view_id' => 'catalog_product_category', - 'mode' => 'disabled', - 'status' => 'idle', - 'updated' => '2017-01-01 11:11:11', - 'version_id' => 100, - ], - 'changelog' => [ - 'version_id' => 200 - ], - ], - [ - 'view' => [ - 'view_id' => 'catalog_product_attribute', - 'mode' => 'enabled', - 'status' => 'idle', - 'updated' => '2017-01-01 11:11:11', - 'version_id' => 100, - ], - 'changelog' => [ - 'version_id' => 100 - ], - ], - ]; - - foreach ($mviews as $data) { - $this->collection->addItem($this->generateMviewStub($data['view'], $data['changelog'])); - } - - /** @var Mview\View\Changelog|\PHPUnit_Framework_MockObject_MockObject $stub */ - $changelog = $this->getMockBuilder(\Magento\Framework\Mview\View\Changelog::class) - ->disableOriginalConstructor() - ->getMock(); - - $changelog->expects($this->any()) - ->method('getVersion') - ->willThrowException( - new Mview\View\ChangelogTableNotExistsException(new \Magento\Framework\Phrase("Do not render")) - ); - - /** @var Mview\View|\PHPUnit_Framework_MockObject_MockObject $notInitiatedMview */ - $notInitiatedMview = $this->getMockBuilder(\Magento\Framework\Mview\View::class) - ->disableOriginalConstructor() - ->getMock(); - - $notInitiatedMview->expects($this->any()) - ->method('getChangelog') - ->willReturn($changelog); - - $this->collection->addItem($notInitiatedMview); - - $tester = new CommandTester($this->command); - $this->assertEquals(Cli::RETURN_SUCCESS, $tester->execute([])); - - $linesOutput = array_filter(explode(PHP_EOL, $tester->getDisplay())); - $this->assertCount(7, $linesOutput, 'There should be 7 lines output. 3 Spacers, 1 header, 3 content.'); - $this->assertEquals($linesOutput[0], $linesOutput[2], "Lines 0, 2, 7 should be spacer lines"); - $this->assertEquals($linesOutput[2], $linesOutput[6], "Lines 0, 2, 6 should be spacer lines"); - - $headerValues = array_values(array_filter(explode('|', $linesOutput[1]))); - $this->assertEquals('ID', trim($headerValues[0])); - $this->assertEquals('Mode', trim($headerValues[1])); - $this->assertEquals('Status', trim($headerValues[2])); - $this->assertEquals('Updated', trim($headerValues[3])); - $this->assertEquals('Version ID', trim($headerValues[4])); - $this->assertEquals('Backlog', trim($headerValues[5])); - - $catalogCategoryProductMviewData = array_values(array_filter(explode('|', $linesOutput[3]))); - $this->assertEquals('catalog_category_product', trim($catalogCategoryProductMviewData[0])); - $this->assertEquals('enabled', trim($catalogCategoryProductMviewData[1])); - $this->assertEquals('idle', trim($catalogCategoryProductMviewData[2])); - $this->assertEquals('2017-01-01 11:11:11', trim($catalogCategoryProductMviewData[3])); - $this->assertEquals('100', trim($catalogCategoryProductMviewData[4])); - $this->assertEquals('10', trim($catalogCategoryProductMviewData[5])); - unset($catalogCategoryProductMviewData); - - $catalogProductAttributeMviewData = array_values(array_filter(explode('|', $linesOutput[4]))); - $this->assertEquals('catalog_product_attribute', trim($catalogProductAttributeMviewData[0])); - $this->assertEquals('enabled', trim($catalogProductAttributeMviewData[1])); - $this->assertEquals('idle', trim($catalogProductAttributeMviewData[2])); - $this->assertEquals('2017-01-01 11:11:11', trim($catalogProductAttributeMviewData[3])); - $this->assertEquals('100', trim($catalogProductAttributeMviewData[4])); - $this->assertEquals('0', trim($catalogProductAttributeMviewData[5])); - unset($catalogProductAttributeMviewData); - - $catalogCategoryProductMviewData = array_values(array_filter(explode('|', $linesOutput[5]))); - $this->assertEquals('catalog_product_category', trim($catalogCategoryProductMviewData[0])); - $this->assertEquals('disabled', trim($catalogCategoryProductMviewData[1])); - $this->assertEquals('idle', trim($catalogCategoryProductMviewData[2])); - $this->assertEquals('2017-01-01 11:11:11', trim($catalogCategoryProductMviewData[3])); - $this->assertEquals('100', trim($catalogCategoryProductMviewData[4])); - $this->assertEquals('100', trim($catalogCategoryProductMviewData[5])); - unset($catalogCategoryProductMviewData); - } - - /** - * @param array $viewData - * @param array $changelogData - * @return Mview\View|Mview\View\Changelog|\PHPUnit_Framework_MockObject_MockObject - */ - protected function generateMviewStub(array $viewData, array $changelogData) - { - /** @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']); - } - - $changelog->expects($this->any()) - ->method('getList') - ->willReturn($list); - - $changelog->expects($this->any()) - ->method('getVersion') - ->willReturn($changelogData['version_id']); - - /** @var Mview\View|\PHPUnit_Framework_MockObject_MockObject $stub */ - $stub = $this->getMockBuilder(\Magento\Framework\Mview\View::class) - ->disableOriginalConstructor() - ->setMethods(['getChangelog', 'getState']) - ->getMock(); - - $stub->expects($this->any()) - ->method('getChangelog') - ->willReturn($changelog); - - $stub->expects($this->any()) - ->method('getState') - ->willReturnSelf(); - - $stub->setData($viewData); - - return $stub; - } - - public function testExecuteExceptionNoVerbosity() - { - /** @var \Magento\Framework\Mview\View|\PHPUnit_Framework_MockObject_MockObject $stub */ - $stub = $this->getMockBuilder(Mview\View::class) - ->disableOriginalConstructor() - ->getMock(); - - $stub->expects($this->any()) - ->method('getChangelog') - ->willThrowException(new \Exception("Dummy test exception")); - - $this->collection->addItem($stub); - - $tester = new CommandTester($this->command); - $this->assertEquals(Cli::RETURN_FAILURE, $tester->execute([])); - $linesOutput = array_filter(explode(PHP_EOL, $tester->getDisplay())); - $this->assertEquals('Dummy test exception', $linesOutput[0]); - } -} diff --git a/app/code/Magento/Indexer/etc/di.xml b/app/code/Magento/Indexer/etc/di.xml index 2ad57b1495baf..1d3f125406f7d 100644 --- a/app/code/Magento/Indexer/etc/di.xml +++ b/app/code/Magento/Indexer/etc/di.xml @@ -51,7 +51,6 @@ Magento\Indexer\Console\Command\IndexerSetModeCommand Magento\Indexer\Console\Command\IndexerShowModeCommand Magento\Indexer\Console\Command\IndexerStatusCommand - Magento\Indexer\Console\Command\IndexerStatusMviewCommand Magento\Indexer\Console\Command\IndexerResetStateCommand From debd7bb467ca3ab4e8a669c5f7dfab182017f283 Mon Sep 17 00:00:00 2001 From: Luke Rodgers Date: Wed, 6 Dec 2017 14:24:48 +0000 Subject: [PATCH 09/12] Update indexer:status command to show backlog --- .../Console/Command/IndexerStatusCommand.php | 89 +++++- .../Command/IndexerStatusCommandTest.php | 283 +++++++++++++++--- 2 files changed, 326 insertions(+), 46 deletions(-) diff --git a/app/code/Magento/Indexer/Console/Command/IndexerStatusCommand.php b/app/code/Magento/Indexer/Console/Command/IndexerStatusCommand.php index 1e57676b89ba8..4ea640093b67c 100644 --- a/app/code/Magento/Indexer/Console/Command/IndexerStatusCommand.php +++ b/app/code/Magento/Indexer/Console/Command/IndexerStatusCommand.php @@ -7,6 +7,8 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; +use Magento\Framework\Indexer; +use Magento\Framework\Mview; /** * Command for displaying status of indexers. @@ -30,21 +32,84 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { + $table = $this->getHelperSet()->get('table'); + $table->setHeaders(['Title', 'Status', 'Update On', 'Schedule Status', 'Schedule Updated']); + + $rows = []; + $indexers = $this->getIndexers($input); foreach ($indexers as $indexer) { - $status = 'unknown'; - switch ($indexer->getStatus()) { - case \Magento\Framework\Indexer\StateInterface::STATUS_VALID: - $status = 'Ready'; - break; - case \Magento\Framework\Indexer\StateInterface::STATUS_INVALID: - $status = 'Reindex required'; - break; - case \Magento\Framework\Indexer\StateInterface::STATUS_WORKING: - $status = 'Processing'; - break; + $view = $indexer->getView(); + + $rowData = [ + 'Title' => $indexer->getTitle(), + 'Status' => $this->getStatus($indexer), + 'Update On' => $indexer->isScheduled() ? 'Schedule' : 'Save', + 'Schedule Status' => '', + 'Updated' => '', + ]; + + if ($indexer->isScheduled()) { + $state = $view->getState(); + $rowData['Schedule Status'] = "{$state->getStatus()} ({$this->getPendingCount($view)} in backlog)"; + $rowData['Updated'] = $state->getUpdated(); } - $output->writeln(sprintf('%-50s ', $indexer->getTitle() . ':') . $status); + + $rows[] = $rowData; + } + + usort($rows, function ($comp1, $comp2) { + return strcmp($comp1['Title'], $comp2['Title']); + }); + + $table->addRows($rows); + $table->render($output); + } + + /** + * @param Indexer\IndexerInterface $indexer + * @return string + */ + private function getStatus(Indexer\IndexerInterface $indexer) + { + $status = 'unknown'; + switch ($indexer->getStatus()) { + case \Magento\Framework\Indexer\StateInterface::STATUS_VALID: + $status = 'Ready'; + break; + case \Magento\Framework\Indexer\StateInterface::STATUS_INVALID: + $status = 'Reindex required'; + break; + case \Magento\Framework\Indexer\StateInterface::STATUS_WORKING: + $status = 'Processing'; + break; } + return $status; + } + + /** + * @param Mview\ViewInterface $view + * @return string + */ + private function getPendingCount(Mview\ViewInterface $view) + { + $changelog = $view->getChangelog(); + + try { + $currentVersionId = $changelog->getVersion(); + } catch (Mview\View\ChangelogTableNotExistsException $e) { + return ''; + } + + $state = $view->getState(); + + $pendingCount = count($changelog->getList($state->getVersionId(), $currentVersionId)); + + $pendingString = "$pendingCount"; + if ($pendingCount <= 0) { + $pendingString = "$pendingCount"; + } + + return $pendingString; } } diff --git a/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusCommandTest.php b/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusCommandTest.php index 5bedfb2f0223c..6bf4bad9fc754 100644 --- a/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusCommandTest.php +++ b/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusCommandTest.php @@ -5,8 +5,13 @@ */ namespace Magento\Indexer\Test\Unit\Console\Command; +use Magento\Framework\Indexer\StateInterface; use Magento\Indexer\Console\Command\IndexerStatusCommand; use Symfony\Component\Console\Tester\CommandTester; +use Symfony\Component\Console\Helper\HelperSet; +use Symfony\Component\Console\Helper\TableHelper; +use Magento\Framework\Indexer\IndexerInterface; +use Magento\Indexer\Model\Indexer\Collection; class IndexerStatusCommandTest extends AbstractIndexerCommandCommonSetup { @@ -17,46 +22,256 @@ class IndexerStatusCommandTest extends AbstractIndexerCommandCommonSetup */ private $command; - public function testExecuteAll() + /** + * @var Collection|\PHPUnit_Framework_MockObject_MockObject + */ + protected $indexerCollectionMock; + + public function setUp() + { + parent::setUp(); + + $this->indexerCollectionMock = $this->getMockBuilder(Collection::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->collectionFactory + ->method('create') + ->willReturn($this->indexerCollectionMock); + } + + /** + * @param \PHPUnit_Framework_MockObject_MockObject $indexerMock + * @param array $data + * @return mixed + */ + private function attachViewToIndexerMock($indexerMock, array $data) + { + /** @var \Magento\Framework\Mview\View\Changelog|\PHPUnit_Framework_MockObject_MockObject $changelog */ + $changelog = $this->getMockBuilder(\Magento\Framework\Mview\View\Changelog::class) + ->disableOriginalConstructor() + ->getMock(); + + $changelog->expects($this->any()) + ->method('getList') + ->willReturn(range(0, $data['view']['changelog']['list_size']-1)); + + /** @var \Magento\Indexer\Model\Mview\View\State|\PHPUnit_Framework_MockObject_MockObject $stateMock */ + $stateMock = $this->getMockBuilder(\Magento\Indexer\Model\Mview\View\State::class) + ->disableOriginalConstructor() + ->setMethods(null) + ->getMock(); + + $stateMock->addData($data['view']['state']); + + /** @var \Magento\Framework\Mview\View|\PHPUnit_Framework_MockObject_MockObject $viewMock */ + $viewMock = $this->getMockBuilder(\Magento\Framework\Mview\View::class) + ->disableOriginalConstructor() + ->setMethods(['getChangelog', 'getState']) + ->getMock(); + + $viewMock->expects($this->any()) + ->method('getState') + ->willReturn($stateMock); + $viewMock->expects($this->any()) + ->method('getChangelog') + ->willReturn($changelog); + + $indexerMock->method('getView') + ->willReturn($viewMock); + + return $indexerMock; + } + + /** + * @param array $indexers + * + * @dataProvider executeAllDataProvider + */ + public function testExecuteAll(array $indexers) { $this->configureAdminArea(); - $collection = $this->getMock('Magento\Indexer\Model\Indexer\Collection', [], [], '', false); - $indexerOne = $this->getMock('Magento\Indexer\Model\Indexer', [], [], '', false); - $indexerOne->expects($this->once())->method('getTitle')->willReturn('Title_indexerOne'); - $indexerOne - ->expects($this->once()) - ->method('getStatus') - ->willReturn(\Magento\Framework\Indexer\StateInterface::STATUS_VALID); - $indexerTwo = $this->getMock('Magento\Indexer\Model\Indexer', [], [], '', false); - $indexerTwo->expects($this->once())->method('getTitle')->willReturn('Title_indexerTwo'); - $indexerTwo - ->expects($this->once()) - ->method('getStatus') - ->willReturn(\Magento\Framework\Indexer\StateInterface::STATUS_INVALID); - $indexerThree = $this->getMock('Magento\Indexer\Model\Indexer', [], [], '', false); - $indexerThree->expects($this->once())->method('getTitle')->willReturn('Title_indexerThree'); - $indexerThree - ->expects($this->once()) - ->method('getStatus') - ->willReturn(\Magento\Framework\Indexer\StateInterface::STATUS_WORKING); - $indexerFour = $this->getMock('Magento\Indexer\Model\Indexer', [], [], '', false); - $indexerFour->expects($this->once())->method('getTitle')->willReturn('Title_indexerFour'); - $collection - ->expects($this->once()) - ->method('getItems') - ->willReturn([$indexerOne, $indexerTwo, $indexerThree, $indexerFour]); + $indexerMocks = []; + foreach ($indexers as $indexerData) { + $indexerMock = $this->getIndexerMock( + ['getStatus', 'isScheduled', 'getState', 'getView'], + $indexerData + ); + + $indexerMock->method('getStatus') + ->willReturn($indexerData['status']); + $indexerMock->method('isScheduled') + ->willReturn($indexerData['is_scheduled']); + + if ($indexerData['is_scheduled']) { + $this->attachViewToIndexerMock($indexerMock, $indexerData); + } + + $indexerMocks[] = $indexerMock; + } - $this->collectionFactory->expects($this->once())->method('create')->will($this->returnValue($collection)); - $this->indexerFactory->expects($this->never())->method('create'); + $this->initIndexerCollectionByItems($indexerMocks); $this->command = new IndexerStatusCommand($this->objectManagerFactory); + + $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + + $this->command->setHelperSet( + $objectManager->getObject( + HelperSet::class, + ['helpers' => [$objectManager->getObject(TableHelper::class)]] + ) + ); + $commandTester = new CommandTester($this->command); $commandTester->execute([]); - $actualValue = $commandTester->getDisplay(); - $expectedValue = sprintf('%-50s ', 'Title_indexerOne' . ':') . 'Ready' . PHP_EOL - . sprintf('%-50s ', 'Title_indexerTwo' . ':') . 'Reindex required' . PHP_EOL - . sprintf('%-50s ', 'Title_indexerThree' . ':') . 'Processing' . PHP_EOL - . sprintf('%-50s ', 'Title_indexerFour' . ':') . 'unknown' . PHP_EOL; - $this->assertStringStartsWith($expectedValue, $actualValue); + $linesOutput = array_filter(explode(PHP_EOL, $commandTester->getDisplay())); + + $spacer = '+----------------+------------------+-----------+-------------------------+---------------------+'; + + $this->assertCount(8, $linesOutput, 'There should be 8 lines output. 3 Spacers, 1 header, 4 content.'); + $this->assertEquals($linesOutput[0], $spacer, "Lines 0, 2, 7 should be spacer lines"); + $this->assertEquals($linesOutput[2], $spacer, "Lines 0, 2, 7 should be spacer lines"); + $this->assertEquals($linesOutput[7], $spacer, "Lines 0, 2, 7 should be spacer lines"); + + $headerValues = array_values(array_filter(explode('|', $linesOutput[1]))); + $this->assertEquals('Title', trim($headerValues[0])); + $this->assertEquals('Status', trim($headerValues[1])); + $this->assertEquals('Update On', trim($headerValues[2])); + $this->assertEquals('Schedule Status', trim($headerValues[3])); + $this->assertEquals('Schedule Updated', trim($headerValues[4])); + + $indexer1 = array_values(array_filter(explode('|', $linesOutput[3]))); + $this->assertEquals('Title_indexer1', trim($indexer1[0])); + $this->assertEquals('Ready', trim($indexer1[1])); + $this->assertEquals('Schedule', trim($indexer1[2])); + $this->assertEquals('idle (10 in backlog)', trim($indexer1[3])); + $this->assertEquals('2017-01-01 11:11:11', trim($indexer1[4])); + + $indexer2 = array_values(array_filter(explode('|', $linesOutput[4]))); + $this->assertEquals('Title_indexer2', trim($indexer2[0])); + $this->assertEquals('Reindex required', trim($indexer2[1])); + $this->assertEquals('Save', trim($indexer2[2])); + $this->assertEquals('', trim($indexer2[3])); + $this->assertEquals('', trim($indexer2[4])); + + $indexer3 = array_values(array_filter(explode('|', $linesOutput[5]))); + $this->assertEquals('Title_indexer3', trim($indexer3[0])); + $this->assertEquals('Processing', trim($indexer3[1])); + $this->assertEquals('Schedule', trim($indexer3[2])); + $this->assertEquals('idle (100 in backlog)', trim($indexer3[3])); + $this->assertEquals('2017-01-01 11:11:11', trim($indexer3[4])); + + $indexer4 = array_values(array_filter(explode('|', $linesOutput[6]))); + $this->assertEquals('Title_indexer4', trim($indexer4[0])); + $this->assertEquals('unknown', trim($indexer4[1])); + $this->assertEquals('Schedule', trim($indexer4[2])); + $this->assertEquals('running (20 in backlog)', trim($indexer4[3])); + $this->assertEquals('2017-01-01 11:11:11', trim($indexer4[4])); + } + + /** + * @return array + */ + public function executeAllDataProvider() + { + return [ + [ + 'indexers' => [ + 'indexer_1' => [ + 'indexer_id' => 'indexer_1', + 'title' => 'Title_indexer1', + 'status' => StateInterface::STATUS_VALID, + 'is_scheduled' => true, + 'view' => [ + 'state' => [ + 'status' => 'idle', + 'updated' => '2017-01-01 11:11:11', + ], + 'changelog' => [ + 'list_size' => 10 + ] + ] + ], + 'indexer_2' => [ + 'indexer_id' => 'indexer_2', + 'title' => 'Title_indexer2', + 'status' => StateInterface::STATUS_INVALID, + 'is_scheduled' => false, + 'view' => [ + 'state' => [ + 'status' => 'idle', + 'updated' => '2017-01-01 11:11:11', + ], + 'changelog' => [ + 'list_size' => 99999999 + ] + ] + ], + 'indexer_3' => [ + 'indexer_id' => 'indexer_3', + 'title' => 'Title_indexer3', + 'status' => StateInterface::STATUS_WORKING, + 'is_scheduled' => true, + 'view' => [ + 'state' => [ + 'status' => 'idle', + 'updated' => '2017-01-01 11:11:11', + ], + 'changelog' => [ + 'list_size' => 100 + ] + ] + ], + 'indexer_4' => [ + 'indexer_id' => 'indexer_4', + 'title' => 'Title_indexer4', + 'status' => null, + 'is_scheduled' => true, + 'view' => [ + 'state' => [ + 'status' => 'running', + 'updated' => '2017-01-01 11:11:11', + ], + 'changelog' => [ + 'list_size' => 20 + ] + ] + ], + ], + ], + ]; + } + + /** + * @param array $methods + * @param array $data + * @return \PHPUnit_Framework_MockObject_MockObject|IndexerInterface + */ + protected function getIndexerMock(array $methods = [], array $data = []) + { + /** @var \PHPUnit_Framework_MockObject_MockObject|IndexerInterface $indexer */ + $indexer = $this->getMockBuilder(IndexerInterface::class) + ->setMethods(array_merge($methods, ['getId', 'getTitle'])) + ->getMockForAbstractClass(); + $indexer->method('getId') + ->willReturn($data['indexer_id'] ?? ''); + $indexer->method('getTitle') + ->willReturn($data['title'] ?? ''); + return $indexer; + } + + /** + * Init Indexer Collection Mock by items. + * + * @param IndexerInterface[] $items + * @throws \Exception + */ + protected function initIndexerCollectionByItems(array $items) + { + $this->indexerCollectionMock + ->method('getItems') + ->with() + ->willReturn($items); } } From 57389c2a260c243bcd90ea6736fc0b43a00e86e5 Mon Sep 17 00:00:00 2001 From: Luke Rodgers Date: Wed, 6 Dec 2017 14:38:27 +0000 Subject: [PATCH 10/12] Tweak for php 5.6 compatability in test --- .../Test/Unit/Console/Command/IndexerStatusCommandTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusCommandTest.php b/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusCommandTest.php index 6bf4bad9fc754..42f110e126a64 100644 --- a/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusCommandTest.php +++ b/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusCommandTest.php @@ -255,9 +255,9 @@ protected function getIndexerMock(array $methods = [], array $data = []) ->setMethods(array_merge($methods, ['getId', 'getTitle'])) ->getMockForAbstractClass(); $indexer->method('getId') - ->willReturn($data['indexer_id'] ?? ''); + ->willReturn(isset($data['indexer_id']) ? $data['indexer_id'] : ''); $indexer->method('getTitle') - ->willReturn($data['title'] ?? ''); + ->willReturn(isset($data['title']) ? $data['title'] : ''); return $indexer; } From b783a6ba406d318b6d01427ed6cf32af3c56acc7 Mon Sep 17 00:00:00 2001 From: Luke Rodgers Date: Wed, 6 Dec 2017 15:06:46 +0000 Subject: [PATCH 11/12] Supress test coupling warning I legit need every dependency there to test this feature --- .../Test/Unit/Console/Command/IndexerStatusCommandTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusCommandTest.php b/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusCommandTest.php index 42f110e126a64..4b7a8ab00c633 100644 --- a/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusCommandTest.php +++ b/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusCommandTest.php @@ -13,6 +13,9 @@ use Magento\Framework\Indexer\IndexerInterface; use Magento\Indexer\Model\Indexer\Collection; +/** + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + */ class IndexerStatusCommandTest extends AbstractIndexerCommandCommonSetup { /** From 49812be481eff1ab55fda0363e396b6ccc1156bd Mon Sep 17 00:00:00 2001 From: Luke Rodgers Date: Thu, 7 Dec 2017 09:24:29 +0000 Subject: [PATCH 12/12] Shorten test variable name --- .../Unit/Console/Command/IndexerStatusCommandTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusCommandTest.php b/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusCommandTest.php index 4b7a8ab00c633..55d62429cd28c 100644 --- a/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusCommandTest.php +++ b/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerStatusCommandTest.php @@ -28,19 +28,19 @@ class IndexerStatusCommandTest extends AbstractIndexerCommandCommonSetup /** * @var Collection|\PHPUnit_Framework_MockObject_MockObject */ - protected $indexerCollectionMock; + protected $indexerCollection; public function setUp() { parent::setUp(); - $this->indexerCollectionMock = $this->getMockBuilder(Collection::class) + $this->indexerCollection = $this->getMockBuilder(Collection::class) ->disableOriginalConstructor() ->getMock(); $this->collectionFactory ->method('create') - ->willReturn($this->indexerCollectionMock); + ->willReturn($this->indexerCollection); } /** @@ -272,7 +272,7 @@ protected function getIndexerMock(array $methods = [], array $data = []) */ protected function initIndexerCollectionByItems(array $items) { - $this->indexerCollectionMock + $this->indexerCollection ->method('getItems') ->with() ->willReturn($items);