diff --git a/app/code/Magento/Indexer/Console/Command/IndexerReindexCommand.php b/app/code/Magento/Indexer/Console/Command/IndexerReindexCommand.php index fffa4503e14a7..c7207c853b95e 100644 --- a/app/code/Magento/Indexer/Console/Command/IndexerReindexCommand.php +++ b/app/code/Magento/Indexer/Console/Command/IndexerReindexCommand.php @@ -6,16 +6,16 @@ namespace Magento\Indexer\Console\Command; +use Magento\Framework\App\ObjectManagerFactory; use Magento\Framework\Console\Cli; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Indexer\Config\DependencyInfoProvider; +use Magento\Framework\Indexer\ConfigInterface; use Magento\Framework\Indexer\IndexerInterface; use Magento\Framework\Indexer\IndexerRegistry; use Magento\Framework\Indexer\StateInterface; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -use Magento\Framework\Indexer\ConfigInterface; -use Magento\Framework\App\ObjectManagerFactory; /** * Command to run indexers @@ -78,6 +78,9 @@ protected function execute(InputInterface $input, OutputInterface $output) foreach ($this->getIndexers($input) as $indexer) { try { $this->validateIndexerStatus($indexer); + + $output->write($indexer->getTitle() . ' index '); + $startTime = microtime(true); $indexerConfig = $this->getConfig()->getIndexer($indexer->getId()); $sharedIndex = $indexerConfig['shared_index']; @@ -90,17 +93,21 @@ protected function execute(InputInterface $input, OutputInterface $output) } } $resultTime = microtime(true) - $startTime; + $output->writeln( - $indexer->getTitle() . ' index has been rebuilt successfully in ' . gmdate('H:i:s', $resultTime) + __('has been rebuilt successfully in %time', ['time' => gmdate('H:i:s', $resultTime)]) ); $returnValue = Cli::RETURN_SUCCESS; } catch (LocalizedException $e) { - $output->writeln($e->getMessage()); + $output->writeln(__('exception: %message', ['message' => $e->getMessage()])); } catch (\Exception $e) { - $output->writeln($indexer->getTitle() . ' indexer process unknown error:'); + $output->writeln('process unknown error:'); $output->writeln($e->getMessage()); + + $output->writeln($e->getTraceAsString(), OutputInterface::VERBOSITY_DEBUG); } } + return $returnValue; } @@ -111,25 +118,23 @@ protected function execute(InputInterface $input, OutputInterface $output) */ protected function getIndexers(InputInterface $input) { - $indexers = parent::getIndexers($input); + $indexers = parent::getIndexers($input); $allIndexers = $this->getAllIndexers(); if (!array_diff_key($allIndexers, $indexers)) { return $indexers; } - $relatedIndexers = []; - $dependentIndexers = []; + $relatedIndexers = [[]]; + $dependentIndexers = [[]]; + foreach ($indexers as $indexer) { - $relatedIndexers = array_merge( - $relatedIndexers, - $this->getRelatedIndexerIds($indexer->getId()) - ); - $dependentIndexers = array_merge( - $dependentIndexers, - $this->getDependentIndexerIds($indexer->getId()) - ); + $relatedIndexers[] = $this->getRelatedIndexerIds($indexer->getId()); + $dependentIndexers[] = $this->getDependentIndexerIds($indexer->getId()); } + $relatedIndexers = array_merge(...$relatedIndexers); + $dependentIndexers = array_merge(...$dependentIndexers); + $invalidRelatedIndexers = []; foreach (array_unique($relatedIndexers) as $relatedIndexer) { if ($allIndexers[$relatedIndexer]->isInvalid()) { @@ -157,18 +162,15 @@ protected function getIndexers(InputInterface $input) * @param string $indexerId * @return array */ - private function getRelatedIndexerIds(string $indexerId) + private function getRelatedIndexerIds(string $indexerId): array { - $relatedIndexerIds = []; + $relatedIndexerIds = [[]]; foreach ($this->getDependencyInfoProvider()->getIndexerIdsToRunBefore($indexerId) as $relatedIndexerId) { - $relatedIndexerIds = array_merge( - $relatedIndexerIds, - [$relatedIndexerId], - $this->getRelatedIndexerIds($relatedIndexerId) - ); + $relatedIndexerIds[] = [$relatedIndexerId]; + $relatedIndexerIds[] = $this->getRelatedIndexerIds($relatedIndexerId); } - return array_unique($relatedIndexerIds); + return array_unique(array_merge(...$relatedIndexerIds)); } /** @@ -177,21 +179,18 @@ private function getRelatedIndexerIds(string $indexerId) * @param string $indexerId * @return array */ - private function getDependentIndexerIds(string $indexerId) + private function getDependentIndexerIds(string $indexerId): array { - $dependentIndexerIds = []; + $dependentIndexerIds = [[]]; foreach (array_keys($this->getConfig()->getIndexers()) as $id) { $dependencies = $this->getDependencyInfoProvider()->getIndexerIdsToRunBefore($id); if (array_search($indexerId, $dependencies) !== false) { - $dependentIndexerIds = array_merge( - $dependentIndexerIds, - [$id], - $this->getDependentIndexerIds($id) - ); + $dependentIndexerIds[] = [$id]; + $dependentIndexerIds[] = $this->getDependentIndexerIds($id); } } - return array_unique($dependentIndexerIds); + return array_unique(array_merge(...$dependentIndexerIds)); } /** diff --git a/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerReindexCommandTest.php b/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerReindexCommandTest.php index bdfeff8a89eb9..3a1bf113b942a 100644 --- a/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerReindexCommandTest.php +++ b/app/code/Magento/Indexer/Test/Unit/Console/Command/IndexerReindexCommandTest.php @@ -22,6 +22,7 @@ */ class IndexerReindexCommandTest extends AbstractIndexerCommandCommonSetup { + const STUB_INDEXER_NAME = 'Indexer Name'; /** * Command being tested * @@ -107,7 +108,7 @@ public function testExecuteAll() [ $this->getIndexerMock( ['reindexAll', 'getStatus'], - ['indexer_id' => 'id_indexerOne', 'title' => 'Title_indexerOne'] + ['indexer_id' => 'id_indexerOne', 'title' => self::STUB_INDEXER_NAME] ) ] ); @@ -117,7 +118,10 @@ public function testExecuteAll() $commandTester->execute([]); $actualValue = $commandTester->getDisplay(); $this->assertSame(Cli::RETURN_SUCCESS, $commandTester->getStatusCode()); - $this->assertStringStartsWith('Title_indexerOne index has been rebuilt successfully in', $actualValue); + $this->assertStringStartsWith( + self::STUB_INDEXER_NAME . ' index has been rebuilt successfully in', + $actualValue + ); } /** @@ -174,6 +178,7 @@ public function testExecuteWithIndex( $this->objectManagerFactory, $this->indexerRegistryMock ); + $commandTester = new CommandTester($this->command); $commandTester->execute(['index' => $inputIndexers]); $this->assertSame(Cli::RETURN_SUCCESS, $commandTester->getStatusCode()); @@ -344,7 +349,8 @@ public function executeWithIndexDataProvider() ], 'With dependencies and multiple indexers in request' => [ 'inputIndexers' => [ - 'indexer_1', 'indexer_3' + 'indexer_1', + 'indexer_3' ], 'indexers' => [ 'indexer_2' => [ @@ -405,7 +411,10 @@ public function executeWithIndexDataProvider() public function testExecuteWithLocalizedException() { $this->configureAdminArea(); - $indexerOne = $this->getIndexerMock(['reindexAll', 'getStatus'], ['indexer_id' => 'indexer_1']); + $indexerOne = $this->getIndexerMock( + ['reindexAll', 'getStatus'], + ['indexer_id' => 'indexer_1', 'title' => self::STUB_INDEXER_NAME] + ); $localizedException = new LocalizedException(new Phrase('Some Exception Message')); $indexerOne->expects($this->once())->method('reindexAll')->will($this->throwException($localizedException)); $this->initIndexerCollectionByItems([$indexerOne]); @@ -414,7 +423,10 @@ public function testExecuteWithLocalizedException() $commandTester->execute(['index' => ['indexer_1']]); $actualValue = $commandTester->getDisplay(); $this->assertSame(Cli::RETURN_FAILURE, $commandTester->getStatusCode()); - $this->assertStringStartsWith('Some Exception Message', $actualValue); + $this->assertStringStartsWith( + self::STUB_INDEXER_NAME . ' index exception: Some Exception Message', + $actualValue + ); } public function testExecuteWithException() @@ -433,7 +445,7 @@ public function testExecuteWithException() $commandTester->execute(['index' => ['indexer_1']]); $actualValue = $commandTester->getDisplay(); $this->assertSame(Cli::RETURN_FAILURE, $commandTester->getStatusCode()); - $this->assertStringStartsWith('Title_indexer_1' . ' indexer process unknown error:', $actualValue); + $this->assertStringStartsWith('Title_indexer_1' . ' index process unknown error:', $actualValue); } public function testExecuteWithExceptionInGetIndexers()