Skip to content

Commit

Permalink
#26206 Improve the output, fix the implementation of array_merge() in…
Browse files Browse the repository at this point in the history
… foreach()
  • Loading branch information
lbajsarowicz committed Dec 31, 2019
1 parent 1ca03a3 commit 79fe786
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 35 deletions.
55 changes: 26 additions & 29 deletions app/code/Magento/Indexer/Console/Command/IndexerReindexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -76,9 +76,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$returnValue = Cli::RETURN_FAILURE;
foreach ($this->getIndexers($input) as $indexer) {
$output->write($indexer->getTitle() . ' ');
try {
$this->validateIndexerStatus($indexer);

$output->write($indexer->getTitle() . ' index ');

$startTime = microtime(true);
$indexerConfig = $this->getConfig()->getIndexer($indexer->getId());
$sharedIndex = $indexerConfig['shared_index'];
Expand All @@ -93,16 +95,19 @@ protected function execute(InputInterface $input, OutputInterface $output)
$resultTime = microtime(true) - $startTime;

$output->writeln(
__('index has been rebuilt successfully in %time', ['time' => 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;
}

Expand All @@ -119,19 +124,17 @@ protected function getIndexers(InputInterface $input)
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())
);
array_push($relatedIndexers, $this->getRelatedIndexerIds($indexer->getId()));
array_push($dependentIndexers, $this->getDependentIndexerIds($indexer->getId()));
}

$relatedIndexers = array_merge(...$relatedIndexers);
$dependentIndexers = array_merge(...$dependentIndexers);

$invalidRelatedIndexers = [];
foreach (array_unique($relatedIndexers) as $relatedIndexer) {
if ($allIndexers[$relatedIndexer]->isInvalid()) {
Expand Down Expand Up @@ -161,15 +164,13 @@ protected function getIndexers(InputInterface $input)
*/
private function getRelatedIndexerIds(string $indexerId)
{
$relatedIndexerIds = [];
$relatedIndexerIds = [[]];
foreach ($this->getDependencyInfoProvider()->getIndexerIdsToRunBefore($indexerId) as $relatedIndexerId) {
$relatedIndexerIds = array_merge(
$relatedIndexerIds,
[$relatedIndexerId],
$this->getRelatedIndexerIds($relatedIndexerId)
);
array_push($relatedIndexerIds, [$relatedIndexerId], $this->getRelatedIndexerIds($relatedIndexerId));
}

$relatedIndexerIds = array_merge(...$relatedIndexerIds);

return array_unique($relatedIndexerIds);
}

Expand All @@ -181,19 +182,15 @@ private function getRelatedIndexerIds(string $indexerId)
*/
private function getDependentIndexerIds(string $indexerId)
{
$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)
);
array_push($dependentIndexerIds, [$id], $this->getDependentIndexerIds($id));
}
}

return array_unique($dependentIndexerIds);
return array_unique(array_merge(...$dependentIndexerIds));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
class IndexerReindexCommandTest extends AbstractIndexerCommandCommonSetup
{
const STUB_INDEXER_NAME = 'Indexer Name';
/**
* Command being tested
*
Expand Down Expand Up @@ -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]
)
]
);
Expand All @@ -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
);
}

/**
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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' => [
Expand Down Expand Up @@ -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]);
Expand All @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit 79fe786

Please sign in to comment.