From 6d3bffa4a4487c1ffd5b0e859c61aec13e10bbd4 Mon Sep 17 00:00:00 2001
From: Luke Rodgers <lukerodgers90@gmail.com>
Date: Mon, 6 Nov 2017 17:55:45 +0000
Subject: [PATCH] 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 61461a0ba61..cb60d4f31da 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\Input\InputInterface;
 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 @@ class IndexerStatusMviewCommand extends Command
 
             $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 @@ class IndexerStatusMviewCommand extends Command
                 }
 
                 $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 @@ class IndexerStatusMviewCommand extends Command
             $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;
         }
     }
 }
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 e6a782cba92..43ffed3fd1e 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\HelperSet;
 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 @@ class IndexerStatusMviewCommandTest extends \PHPUnit_Framework_TestCase
         $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 @@ class IndexerStatusMviewCommandTest extends \PHPUnit_Framework_TestCase
             [
                 'view' => [
                     'view_id' => 'catalog_category_product',
+                ],
+                'state' => [
                     'mode' => 'enabled',
                     'status' => 'idle',
                     'updated' => '2017-01-01 11:11:11',
@@ -78,6 +87,8 @@ class IndexerStatusMviewCommandTest extends \PHPUnit_Framework_TestCase
             [
                 'view' => [
                     'view_id' => 'catalog_product_category',
+                ],
+                'state' => [
                     'mode' => 'disabled',
                     'status' => 'idle',
                     'updated' => '2017-01-01 11:11:11',
@@ -90,6 +101,8 @@ class IndexerStatusMviewCommandTest extends \PHPUnit_Framework_TestCase
             [
                 'view' => [
                     'view_id' => 'catalog_product_attribute',
+                ],
+                'state' => [
                     'mode' => 'enabled',
                     'status' => 'idle',
                     'updated' => '2017-01-01 11:11:11',
@@ -102,7 +115,7 @@ class IndexerStatusMviewCommandTest extends \PHPUnit_Framework_TestCase
         ];
 
         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 @@ class IndexerStatusMviewCommandTest extends \PHPUnit_Framework_TestCase
     /**
      * @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 @@ class IndexerStatusMviewCommandTest extends \PHPUnit_Framework_TestCase
             ->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 @@ class IndexerStatusMviewCommandTest extends \PHPUnit_Framework_TestCase
             ->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 @@ class IndexerStatusMviewCommandTest extends \PHPUnit_Framework_TestCase
 
         $stub->expects($this->any())
             ->method('getState')
-            ->willReturnSelf();
+            ->willReturn($state);
 
         $stub->setData($viewData);
 
-- 
GitLab