From 4a233d4ec18cb10931bff5078d8ade1a2a66c58e Mon Sep 17 00:00:00 2001 From: Yuri Kovsher Date: Wed, 20 Jul 2016 15:47:28 +0300 Subject: [PATCH 01/57] MAGETWO-55691: Decrease of stack trace --- .../Framework/Interception/Chain/Chain.php | 79 ----------- .../Framework/Interception/ChainInterface.php | 26 ---- .../Framework/Interception/Interceptor.php | 113 +++++++-------- .../Test/Unit/Chain/ChainTest.php | 134 ------------------ 4 files changed, 57 insertions(+), 295 deletions(-) delete mode 100644 lib/internal/Magento/Framework/Interception/Chain/Chain.php delete mode 100644 lib/internal/Magento/Framework/Interception/ChainInterface.php delete mode 100644 lib/internal/Magento/Framework/Interception/Test/Unit/Chain/ChainTest.php diff --git a/lib/internal/Magento/Framework/Interception/Chain/Chain.php b/lib/internal/Magento/Framework/Interception/Chain/Chain.php deleted file mode 100644 index 34a8308e021e8..0000000000000 --- a/lib/internal/Magento/Framework/Interception/Chain/Chain.php +++ /dev/null @@ -1,79 +0,0 @@ -pluginList = $pluginList; - } - - /** - * Invoke next plugin in chain - * - * @param string $type - * @param string $method - * @param string $previousPluginCode - * @param InterceptorInterface $subject - * @param array $arguments - * @return mixed|void - */ - public function invokeNext( - $type, - $method, - InterceptorInterface $subject, - array $arguments, - $previousPluginCode = null - ) { - $pluginInfo = $this->pluginList->getNext($type, $method, $previousPluginCode); - $capMethod = ucfirst($method); - $result = null; - if (isset($pluginInfo[DefinitionInterface::LISTENER_BEFORE])) { - foreach ($pluginInfo[DefinitionInterface::LISTENER_BEFORE] as $code) { - $pluginInstance = $this->pluginList->getPlugin($type, $code); - $pluginMethod = 'before' . $capMethod; - $beforeResult = $pluginInstance->$pluginMethod($subject, ...array_values($arguments)); - if ($beforeResult) { - $arguments = $beforeResult; - } - unset($pluginInstance, $pluginMethod); - } - } - if (isset($pluginInfo[DefinitionInterface::LISTENER_AROUND])) { - $chain = $this; - $code = $pluginInfo[DefinitionInterface::LISTENER_AROUND]; - $next = function () use ($chain, $type, $method, $subject, $code) { - return $chain->invokeNext($type, $method, $subject, func_get_args(), $code); - }; - $pluginInstance = $this->pluginList->getPlugin($type, $code); - $pluginMethod = 'around' . $capMethod; - $result = $pluginInstance->$pluginMethod($subject, $next, ...array_values($arguments)); - unset($pluginInstance, $pluginMethod); - } else { - $result = $subject->___callParent($method, $arguments); - } - if (isset($pluginInfo[DefinitionInterface::LISTENER_AFTER])) { - foreach ($pluginInfo[DefinitionInterface::LISTENER_AFTER] as $code) { - $result = $this->pluginList->getPlugin($type, $code)->{'after' . $capMethod}($subject, $result); - } - } - return $result; - } -} diff --git a/lib/internal/Magento/Framework/Interception/ChainInterface.php b/lib/internal/Magento/Framework/Interception/ChainInterface.php deleted file mode 100644 index 648175b63038e..0000000000000 --- a/lib/internal/Magento/Framework/Interception/ChainInterface.php +++ /dev/null @@ -1,26 +0,0 @@ -pluginLocator = ObjectManager::getInstance(); - $this->pluginList = $this->pluginLocator->get('Magento\Framework\Interception\PluginListInterface'); - $this->chain = $this->pluginLocator->get('Magento\Framework\Interception\ChainInterface'); + $this->pluginList = ObjectManager::getInstance()->get('Magento\Framework\Interception\PluginListInterface'); $this->subjectType = get_parent_class($this); if (method_exists($this->subjectType, '___init')) { parent::___init(); @@ -86,7 +70,7 @@ public function __sleep() } else { $properties = array_keys(get_object_vars($this)); } - $properties = array_diff($properties, ['pluginLocator', 'pluginList', 'chain', 'subjectType', 'pluginLocator']); + $properties = array_diff($properties, ['pluginList', 'subjectType']); return $properties; } @@ -113,45 +97,62 @@ public function __wakeup() */ protected function ___callPlugins($method, array $arguments, array $pluginInfo) { - $capMethod = ucfirst($method); - $result = null; - if (isset($pluginInfo[DefinitionInterface::LISTENER_BEFORE])) { - // Call 'before' listeners - foreach ($pluginInfo[DefinitionInterface::LISTENER_BEFORE] as $code) { - $pluginInstance = $this->pluginList->getPlugin($this->subjectType, $code); - $pluginMethod = 'before' . $capMethod; - $beforeResult = $pluginInstance->$pluginMethod($this, ...array_values($arguments)); - if ($beforeResult) { - $arguments = $beforeResult; + $subject = $this; + $type = $this->subjectType; + $pluginList = $this->pluginList; + + $next = function (...$arguments) use ( + $method, + &$pluginInfo, + $subject, + $type, + $pluginList, + &$next + ) { + $capMethod = ucfirst($method); + $currentPluginInfo = $pluginInfo; + $result = null; + + if (isset($currentPluginInfo[DefinitionInterface::LISTENER_BEFORE])) { + // Call 'before' listeners + foreach ($currentPluginInfo[DefinitionInterface::LISTENER_BEFORE] as $code) { + $pluginInstance = $pluginList->getPlugin($type, $code); + $pluginMethod = 'before' . $capMethod; + $beforeResult = $pluginInstance->$pluginMethod($this, ...array_values($arguments)); + + if ($beforeResult !== null) { + $arguments = (array)$beforeResult; + } } - unset($pluginInstance, $pluginMethod); } - } - if (isset($pluginInfo[DefinitionInterface::LISTENER_AROUND])) { - // Call 'around' listener - $chain = $this->chain; - $type = $this->subjectType; - /** @var \Magento\Framework\Interception\InterceptorInterface $subject */ - $subject = $this; - $code = $pluginInfo[DefinitionInterface::LISTENER_AROUND]; - $next = function () use ($chain, $type, $method, $subject, $code) { - return $chain->invokeNext($type, $method, $subject, func_get_args(), $code); - }; - $pluginInstance = $this->pluginList->getPlugin($this->subjectType, $code); - $pluginMethod = 'around' . $capMethod; - $result = $pluginInstance->$pluginMethod($this, $next, ...array_values($arguments)); - unset($pluginInstance, $pluginMethod); - } else { - // Call original method - $result = parent::$method(...array_values($arguments)); - } - if (isset($pluginInfo[DefinitionInterface::LISTENER_AFTER])) { - // Call 'after' listeners - foreach ($pluginInfo[DefinitionInterface::LISTENER_AFTER] as $code) { - $result = $this->pluginList->getPlugin($this->subjectType, $code) - ->{'after' . $capMethod}($this, $result); + + if (isset($currentPluginInfo[DefinitionInterface::LISTENER_AROUND])) { + // Call 'around' listener + $code = $currentPluginInfo[DefinitionInterface::LISTENER_AROUND]; + $pluginInfo = $pluginList->getNext($type, $method, $code); + $pluginInstance = $this->pluginList->getPlugin($type, $code); + $pluginMethod = 'around' . $capMethod; + $result = $pluginInstance->$pluginMethod($subject, $next, ...array_values($arguments)); + } else { + // Call original method + $result = $subject->___callParent($method, $arguments); } - } + + if (isset($currentPluginInfo[DefinitionInterface::LISTENER_AFTER])) { + // Call 'after' listeners + foreach ($currentPluginInfo[DefinitionInterface::LISTENER_AFTER] as $code) { + $pluginInstance = $pluginList->getPlugin($type, $code); + $pluginMethod = 'after' . $capMethod; + $result = $pluginInstance->$pluginMethod($subject, $result, ...array_values($arguments)); + } + } + + return $result; + }; + + $result = $next(...array_values($arguments)); + $next = null; + return $result; } } diff --git a/lib/internal/Magento/Framework/Interception/Test/Unit/Chain/ChainTest.php b/lib/internal/Magento/Framework/Interception/Test/Unit/Chain/ChainTest.php deleted file mode 100644 index 9f60ecd624e8b..0000000000000 --- a/lib/internal/Magento/Framework/Interception/Test/Unit/Chain/ChainTest.php +++ /dev/null @@ -1,134 +0,0 @@ -_pluginListMock = $this->getMock('Magento\Framework\Interception\PluginListInterface'); - $this->_model = new \Magento\Framework\Interception\Chain\Chain($this->_pluginListMock); - } - - /** - * @covers \Magento\Framework\Interception\Chain\Chain::invokeNext - * @covers \Magento\Framework\Interception\Chain\Chain::__construct - */ - public function testInvokeNextBeforePlugin() - { - $type = 'type'; - $method = 'method'; - - $subjectMock = $this->getMock('Magento\Framework\Interception\InterceptorInterface'); - $pluginMock = $this->getMock('PluginClass', ['beforeMethod']); - - $pluginMock->expects($this->once()) - ->method('beforeMethod') - ->with($subjectMock, 1, 2) - ->will($this->returnValue(['beforeMethodResult'])); - - $this->_pluginListMock->expects($this->once()) - ->method('getNext') - ->with($type, $method, null) - ->will( - $this->returnValue( - [\Magento\Framework\Interception\DefinitionInterface::LISTENER_BEFORE => ['code']] - ) - ); - - $this->_pluginListMock->expects($this->once()) - ->method('getPlugin') - ->with($type, 'code') - ->will($this->returnValue($pluginMock)); - - $subjectMock->expects($this->once()) - ->method('___callParent') - ->with('method', ['beforeMethodResult']) - ->will($this->returnValue('subjectMethodResult')); - - $this->assertEquals('subjectMethodResult', $this->_model->invokeNext($type, $method, $subjectMock, [1, 2])); - } - - /** - * @covers \Magento\Framework\Interception\Chain\Chain::invokeNext - */ - public function testInvokeNextAroundPlugin() - { - $type = 'type'; - $method = 'method'; - - $subjectMock = $this->getMock('Magento\Framework\Interception\InterceptorInterface'); - $pluginMock = $this->getMock('PluginClass', ['aroundMethod']); - - $pluginMock->expects($this->once()) - ->method('aroundMethod') - ->with($this->anything()) - ->will($this->returnValue('subjectMethodResult')); - - $this->_pluginListMock->expects($this->once()) - ->method('getNext') - ->with($type, $method, null) - ->will($this->returnValue([ - \Magento\Framework\Interception\DefinitionInterface::LISTENER_AROUND => 'code', - ])); - - $this->_pluginListMock->expects($this->once()) - ->method('getPlugin') - ->with($type, 'code') - ->will($this->returnValue($pluginMock)); - - $this->assertEquals('subjectMethodResult', $this->_model->invokeNext($type, $method, $subjectMock, [])); - } - - /** - * @covers \Magento\Framework\Interception\Chain\Chain::invokeNext - */ - public function testInvokeNextAfterPlugin() - { - $type = 'type'; - $method = 'method'; - - $subjectMock = $this->getMock('Magento\Framework\Interception\InterceptorInterface'); - $pluginMock = $this->getMock('PluginClass', ['afterMethod']); - - $pluginMock->expects($this->once()) - ->method('afterMethod') - ->with($subjectMock, 'subjectMethodResult') - ->will($this->returnValue('afterMethodResult')); - - $this->_pluginListMock->expects($this->once()) - ->method('getNext') - ->with($type, $method, null) - ->will( - $this->returnValue( - [\Magento\Framework\Interception\DefinitionInterface::LISTENER_AFTER => ['code']] - ) - ); - - $this->_pluginListMock->expects($this->once()) - ->method('getPlugin') - ->with($type, 'code') - ->will($this->returnValue($pluginMock)); - - $subjectMock->expects($this->once()) - ->method('___callParent') - ->with('method', [1, 2]) - ->will($this->returnValue('subjectMethodResult')); - - $this->assertEquals('afterMethodResult', $this->_model->invokeNext($type, $method, $subjectMock, [1, 2])); - } -} From 9daa1caefe81ce0b5ef205213411387f236253ca Mon Sep 17 00:00:00 2001 From: Viktor Paladiychuk Date: Wed, 20 Jul 2016 17:39:16 +0300 Subject: [PATCH 02/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- .../App/Action/Plugin/MassactionKey.php | 19 +++++++-------- .../App/Action/Plugin/MassactionKeyTest.php | 24 +++++++++---------- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/app/code/Magento/Backend/App/Action/Plugin/MassactionKey.php b/app/code/Magento/Backend/App/Action/Plugin/MassactionKey.php index e2ce581806f63..65bcf566d6ad3 100644 --- a/app/code/Magento/Backend/App/Action/Plugin/MassactionKey.php +++ b/app/code/Magento/Backend/App/Action/Plugin/MassactionKey.php @@ -7,29 +7,28 @@ */ namespace Magento\Backend\App\Action\Plugin; +use Magento\Framework\App\RequestInterface; +use Magento\Backend\App\AbstractAction; + class MassactionKey { /** * Process massaction key * - * @param \Magento\Backend\App\AbstractAction $subject - * @param callable $proceed - * @param \Magento\Framework\App\RequestInterface $request + * @param AbstractAction $subject + * @param RequestInterface $request * - * @return mixed + * @return array * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundDispatch( - \Magento\Backend\App\AbstractAction $subject, - \Closure $proceed, - \Magento\Framework\App\RequestInterface $request - ) { + public function beforeDispatch(AbstractAction $subject, RequestInterface $request) + { $key = $request->getPost('massaction_prepare_key'); if ($key) { $postData = $request->getPost($key); $value = is_array($postData) ? $postData : explode(',', $postData); $request->setPostValue($key, $value ? $value : null); } - return $proceed($request); + return [$request]; } } diff --git a/app/code/Magento/Backend/Test/Unit/App/Action/Plugin/MassactionKeyTest.php b/app/code/Magento/Backend/Test/Unit/App/Action/Plugin/MassactionKeyTest.php index c42634027ad4c..6cf823821154e 100644 --- a/app/code/Magento/Backend/Test/Unit/App/Action/Plugin/MassactionKeyTest.php +++ b/app/code/Magento/Backend/Test/Unit/App/Action/Plugin/MassactionKeyTest.php @@ -6,6 +6,8 @@ namespace Magento\Backend\Test\Unit\App\Action\Plugin; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; +use Magento\Backend\App\AbstractAction; +use Magento\Framework\App\RequestInterface; class MassactionKeyTest extends \PHPUnit_Framework_TestCase { @@ -15,17 +17,12 @@ class MassactionKeyTest extends \PHPUnit_Framework_TestCase protected $plugin; /** - * @var \Closure - */ - protected $closureMock; - - /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \PHPUnit_Framework_MockObject_MockObject|RequestInterface */ protected $requestMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \PHPUnit_Framework_MockObject_MockObject|AbstractAction */ protected $subjectMock; @@ -35,14 +32,15 @@ protected function setUp() return 'Expected'; }; $this->subjectMock = $this->getMock('Magento\Backend\App\AbstractAction', [], [], '', false); - $this->requestMock = $this->getMock('Magento\Framework\App\Request\Http', [], [], '', false); + $this->requestMock = $this->getMockForAbstractClass( + RequestInterface::class, [], '', false, false, true, ['getPost', 'setPostValue'] + ); $objectManager = new ObjectManager($this); $this->plugin = $objectManager->getObject( 'Magento\Backend\App\Action\Plugin\MassactionKey', [ 'subject' => $this->subjectMock, - 'closure' => $this->closureMock, 'request' => $this->requestMock, ] ); @@ -70,8 +68,8 @@ public function testAroundDispatchWhenMassactionPrepareKeyRequestExists($postDat ->with('key', $convertedData); $this->assertEquals( - 'Expected', - $this->plugin->aroundDispatch($this->subjectMock, $this->closureMock, $this->requestMock) + [$this->requestMock], + $this->plugin->beforeDispatch($this->subjectMock, $this->requestMock) ); } @@ -96,8 +94,8 @@ public function testAroundDispatchWhenMassactionPrepareKeyRequestNotExists() ->method('setPostValue'); $this->assertEquals( - 'Expected', - $this->plugin->aroundDispatch($this->subjectMock, $this->closureMock, $this->requestMock) + [$this->requestMock], + $this->plugin->beforeDispatch($this->subjectMock, $this->requestMock) ); } } From 6b745c2a6237c79b3ce50f4c08ea2806b5c81e98 Mon Sep 17 00:00:00 2001 From: Yuri Kovsher Date: Wed, 20 Jul 2016 18:16:11 +0300 Subject: [PATCH 03/57] MAGETWO-55691: Decrease of stack trace --- .../Unit/Interception/InterceptorTest.php | 105 ++++++++++++++++++ .../Test/Unit/Interception/Sample/Entity.php | 50 +++++++++ .../Unit/Interception/Sample/Interceptor.php | 53 +++++++++ .../Test/Unit/Interception/Sample/Plugin1.php | 51 +++++++++ .../Test/Unit/Interception/Sample/Plugin2.php | 13 +++ .../Test/Unit/Interception/Sample/Plugin3.php | 13 +++ .../Test/Unit/Interception/Sample/Plugin4.php | 13 +++ 7 files changed, 298 insertions(+) create mode 100644 lib/internal/Magento/Framework/Test/Unit/Interception/InterceptorTest.php create mode 100644 lib/internal/Magento/Framework/Test/Unit/Interception/Sample/Entity.php create mode 100644 lib/internal/Magento/Framework/Test/Unit/Interception/Sample/Interceptor.php create mode 100644 lib/internal/Magento/Framework/Test/Unit/Interception/Sample/Plugin1.php create mode 100644 lib/internal/Magento/Framework/Test/Unit/Interception/Sample/Plugin2.php create mode 100644 lib/internal/Magento/Framework/Test/Unit/Interception/Sample/Plugin3.php create mode 100644 lib/internal/Magento/Framework/Test/Unit/Interception/Sample/Plugin4.php diff --git a/lib/internal/Magento/Framework/Test/Unit/Interception/InterceptorTest.php b/lib/internal/Magento/Framework/Test/Unit/Interception/InterceptorTest.php new file mode 100644 index 0000000000000..7b3624c501d8d --- /dev/null +++ b/lib/internal/Magento/Framework/Test/Unit/Interception/InterceptorTest.php @@ -0,0 +1,105 @@ +pluginListMock = $this->getMockBuilder(Interception\PluginListInterface::class) + ->getMockForAbstractClass(); + + $this->sampleInterceptor = new Sample\Interceptor(); + $this->samplePlugins = [ + 'plugin1' => new Sample\Plugin1(), + 'plugin2' => new Sample\Plugin2(), + 'plugin3' => new Sample\Plugin3(), + 'plugin4' => new Sample\Plugin4() + ]; + + $this->sampleInterceptor->setPluginList($this->pluginListMock); + } + + public function testCallPlugins() + { + $subjectType = Sample\Entity::class; + $method = 'doSomething'; + $capMethod = ucfirst($method); + $pluginMap = [ + [$subjectType, 'plugin1', $this->samplePlugins['plugin1']], + [$subjectType, 'plugin2', $this->samplePlugins['plugin2']], + [$subjectType, 'plugin3', $this->samplePlugins['plugin3']], + [$subjectType, 'plugin4', $this->samplePlugins['plugin4']] + ]; + $pluginInfoMap = [ + [ + $subjectType, + $method, + null, + [ + Interception\DefinitionInterface::LISTENER_BEFORE => ['plugin1', 'plugin2'], + Interception\DefinitionInterface::LISTENER_AROUND => 'plugin3', + Interception\DefinitionInterface::LISTENER_AFTER => ['plugin1', 'plugin2', 'plugin3'] + ] + ], + [ + $subjectType, + $method, + 'plugin3', + [ + Interception\DefinitionInterface::LISTENER_BEFORE => ['plugin4'], + Interception\DefinitionInterface::LISTENER_AROUND => 'plugin4', + Interception\DefinitionInterface::LISTENER_AFTER => ['plugin4'] + ] + ], + [ + $subjectType, + $method, + 'plugin4', + null + ] + ]; + $expectedPluginCalls = [ + Sample\Plugin1::class . '::before' . $capMethod, + Sample\Plugin2::class . '::before' . $capMethod, + Sample\Plugin3::class . '::around' . $capMethod, + Sample\Plugin4::class . '::before' . $capMethod, + Sample\Plugin4::class . '::around' . $capMethod, + Sample\Entity::class . '::' . $method, + Sample\Plugin4::class . '::after' . $capMethod, + Sample\Plugin1::class . '::after' . $capMethod, + Sample\Plugin2::class . '::after' . $capMethod, + Sample\Plugin3::class . '::after' . $capMethod + ]; + + $this->pluginListMock->expects(static::any()) + ->method('getPlugin') + ->willReturnMap($pluginMap); + $this->pluginListMock->expects(static::exactly(3)) + ->method('getNext') + ->willReturnMap($pluginInfoMap); + + $this->assertTrue($this->sampleInterceptor->$method()); + $this->assertEquals($expectedPluginCalls, $this->sampleInterceptor->getPluginCalls()); + } +} diff --git a/lib/internal/Magento/Framework/Test/Unit/Interception/Sample/Entity.php b/lib/internal/Magento/Framework/Test/Unit/Interception/Sample/Entity.php new file mode 100644 index 0000000000000..2ceff81795a70 --- /dev/null +++ b/lib/internal/Magento/Framework/Test/Unit/Interception/Sample/Entity.php @@ -0,0 +1,50 @@ +addPluginCall(self::class . '::' . __FUNCTION__); + + return true; + } + + /** + * Get plugin calls info for testing + * + * @return array + */ + public function getPluginCalls() + { + return $this->pluginCalls; + } + + /** + * Add plugin call info for testing + * + * @param string $call + * @return void + */ + public function addPluginCall($call) + { + $this->pluginCalls[] = $call; + } +} diff --git a/lib/internal/Magento/Framework/Test/Unit/Interception/Sample/Interceptor.php b/lib/internal/Magento/Framework/Test/Unit/Interception/Sample/Interceptor.php new file mode 100644 index 0000000000000..0e5175ed6e5a2 --- /dev/null +++ b/lib/internal/Magento/Framework/Test/Unit/Interception/Sample/Interceptor.php @@ -0,0 +1,53 @@ +___init(); + } + + /** + * {@inheritdoc} + */ + public function ___init() + { + $this->subjectType = get_parent_class($this); + } + + /** + * {@inheritdoc} + */ + public function doSomething() + { + $pluginInfo = $this->pluginList->getNext($this->subjectType, 'doSomething'); + if (!$pluginInfo) { + return parent::doSomething(); + } else { + return $this->___callPlugins('doSomething', func_get_args(), $pluginInfo); + } + } + + /** + * Set plugin list + * + * @param Interception\PluginListInterface $pluginList + * @return void + */ + public function setPluginList(Interception\PluginListInterface $pluginList) + { + $this->pluginList = $pluginList; + } +} diff --git a/lib/internal/Magento/Framework/Test/Unit/Interception/Sample/Plugin1.php b/lib/internal/Magento/Framework/Test/Unit/Interception/Sample/Plugin1.php new file mode 100644 index 0000000000000..be3669c79930e --- /dev/null +++ b/lib/internal/Magento/Framework/Test/Unit/Interception/Sample/Plugin1.php @@ -0,0 +1,51 @@ +addPluginCall(static::class . '::' . __FUNCTION__); + //Not changing arguments + } + + /** + * Sample around-plugin method + * + * @param Entity $subject + * @param \Closure $proceed + * @return mixed + */ + public function aroundDoSomething(Entity $subject, \Closure $proceed) + { + $subject->addPluginCall(static::class . '::' . __FUNCTION__); + //Not breaking the chain + return $proceed(); + } + + /** + * Sample after-plugin method + * + * @param Entity $subject + * @param mixed $result + * @return mixed + */ + public function afterDoSomething(Entity $subject, $result) + { + $subject->addPluginCall(static::class . '::' . __FUNCTION__); + //Not changing result + return $result; + } +} diff --git a/lib/internal/Magento/Framework/Test/Unit/Interception/Sample/Plugin2.php b/lib/internal/Magento/Framework/Test/Unit/Interception/Sample/Plugin2.php new file mode 100644 index 0000000000000..df4c6d5fc18de --- /dev/null +++ b/lib/internal/Magento/Framework/Test/Unit/Interception/Sample/Plugin2.php @@ -0,0 +1,13 @@ + Date: Thu, 21 Jul 2016 16:13:09 +0300 Subject: [PATCH 04/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- .../Layer/Search/Plugin/CollectionFilter.php | 7 +- .../Search/Plugin/CollectionFilterTest.php | 109 ++++++++++++++++++ 2 files changed, 112 insertions(+), 4 deletions(-) create mode 100644 app/code/Magento/CatalogSearch/Test/Unit/Model/Layer/Search/Plugin/CollectionFilterTest.php diff --git a/app/code/Magento/CatalogSearch/Model/Layer/Search/Plugin/CollectionFilter.php b/app/code/Magento/CatalogSearch/Model/Layer/Search/Plugin/CollectionFilter.php index b2e26dc04236b..bc50949736116 100644 --- a/app/code/Magento/CatalogSearch/Model/Layer/Search/Plugin/CollectionFilter.php +++ b/app/code/Magento/CatalogSearch/Model/Layer/Search/Plugin/CollectionFilter.php @@ -28,19 +28,18 @@ public function __construct(QueryFactory $queryFactory) * Add search filter criteria to search collection * * @param \Magento\Catalog\Model\Layer\Search\CollectionFilter $subject - * @param \Closure $proceed + * @param null $result * @param \Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection $collection * @param Category $category * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundFilter( + public function afterFilter( \Magento\Catalog\Model\Layer\Search\CollectionFilter $subject, - \Closure $proceed, + $result, $collection, Category $category ) { - $proceed($collection, $category); /** @var \Magento\Search\Model\Query $query */ $query = $this->queryFactory->get(); if (!$query->isQueryTextShort()) { diff --git a/app/code/Magento/CatalogSearch/Test/Unit/Model/Layer/Search/Plugin/CollectionFilterTest.php b/app/code/Magento/CatalogSearch/Test/Unit/Model/Layer/Search/Plugin/CollectionFilterTest.php new file mode 100644 index 0000000000000..2f46cf276472c --- /dev/null +++ b/app/code/Magento/CatalogSearch/Test/Unit/Model/Layer/Search/Plugin/CollectionFilterTest.php @@ -0,0 +1,109 @@ +objectManager = new ObjectManager($this); + + $this->collectionMock = $this->getMockBuilder(Collection::class) + ->disableOriginalConstructor() + ->setMethods(['addSearchFilter']) + ->getMock(); + $this->categoryMock = $this->getMockBuilder(Category::class) + ->disableOriginalConstructor() + ->getMock(); + $this->queryFactoryMock = $this->getMockBuilder(QueryFactory::class) + ->disableOriginalConstructor() + ->setMethods(['get']) + ->getMock(); + $this->collectionFilterMock = $this->getMockBuilder(CollectionFilter::class) + ->disableOriginalConstructor() + ->getMock(); + $this->queryMock = $this->getMockBuilder(Query::class) + ->disableOriginalConstructor() + ->setMethods(['isQueryTextShort', 'getQueryText']) + ->getMock(); + + $this->plugin = $this->objectManager->getObject( + CollectionFilterPlugin::class, + ['queryFactory' => $this->queryFactoryMock] + ); + } + + public function testAfterFilter() + { + $queryText = 'Test Query'; + + $this->queryFactoryMock->expects($this->once()) + ->method('get') + ->willReturn($this->queryMock); + $this->queryMock->expects($this->once()) + ->method('isQueryTextShort') + ->willReturn(false); + $this->queryMock->expects($this->once()) + ->method('getQueryText') + ->willReturn($queryText); + $this->collectionMock->expects($this->once()) + ->method('addSearchFilter') + ->with($queryText); + + $this->plugin->afterFilter( + $this->collectionFilterMock, + null, + $this->collectionMock, + $this->categoryMock + ); + } +} \ No newline at end of file From 89951c80aa368f80b19b14916dea8fcc876bb77e Mon Sep 17 00:00:00 2001 From: Viktor Paladiychuk Date: Thu, 21 Jul 2016 16:21:42 +0300 Subject: [PATCH 05/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- .../Magento/Bundle/Model/Plugin/QuoteItem.php | 25 ++--- .../Test/Unit/Model/Plugin/QuoteItemTest.php | 105 ++++++++++-------- 2 files changed, 68 insertions(+), 62 deletions(-) diff --git a/app/code/Magento/Bundle/Model/Plugin/QuoteItem.php b/app/code/Magento/Bundle/Model/Plugin/QuoteItem.php index 8861d864b8180..a398fbb4e6fca 100644 --- a/app/code/Magento/Bundle/Model/Plugin/QuoteItem.php +++ b/app/code/Magento/Bundle/Model/Plugin/QuoteItem.php @@ -5,29 +5,26 @@ */ namespace Magento\Bundle\Model\Plugin; -use Closure; +use Magento\Quote\Model\Quote\Item\ToOrderItem; +use Magento\Sales\Api\Data\OrderItemInterface; +use Magento\Quote\Model\Quote\Item\AbstractItem; class QuoteItem { /** * Add bundle attributes to order data * - * @param \Magento\Quote\Model\Quote\Item\ToOrderItem $subject - * @param callable $proceed - * @param \Magento\Quote\Model\Quote\Item\AbstractItem $item - * @param array $additional - * @return \Magento\Sales\Model\Order\Item + * @param ToOrderItem $subject + * @param OrderItemInterface $orderItem + * @param AbstractItem $item + * @return OrderItemInterface * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundConvert( - \Magento\Quote\Model\Quote\Item\ToOrderItem $subject, - Closure $proceed, - \Magento\Quote\Model\Quote\Item\AbstractItem $item, - $additional = [] + public function afterConvert( + ToOrderItem $subject, + OrderItemInterface $orderItem, + AbstractItem $item ) { - /** @var $orderItem \Magento\Sales\Model\Order\Item */ - $orderItem = $proceed($item, $additional); - if ($attributes = $item->getProduct()->getCustomOption('bundle_selection_attributes')) { $productOptions = $orderItem->getProductOptions(); $productOptions['bundle_selection_attributes'] = $attributes->getValue(); diff --git a/app/code/Magento/Bundle/Test/Unit/Model/Plugin/QuoteItemTest.php b/app/code/Magento/Bundle/Test/Unit/Model/Plugin/QuoteItemTest.php index c4497c3979a87..194458e8cf260 100644 --- a/app/code/Magento/Bundle/Test/Unit/Model/Plugin/QuoteItemTest.php +++ b/app/code/Magento/Bundle/Test/Unit/Model/Plugin/QuoteItemTest.php @@ -5,81 +5,90 @@ */ namespace Magento\Bundle\Test\Unit\Model\Plugin; +use Magento\Quote\Model\Quote\Item\ToOrderItem; +use Magento\Sales\Api\Data\OrderItemInterface; +use Magento\Quote\Model\Quote\Item\AbstractItem; +use Magento\Catalog\Model\Product; + class QuoteItemTest extends \PHPUnit_Framework_TestCase { - /** @var \Magento\Bundle\Model\Plugin\QuoteItem */ + /** + * @var \PHPUnit_Framework_MockObject_MockObject|Product + */ + private $productMock; + + /** @var \Magento\Bundle\Model\Plugin\QuoteItem */ protected $model; - /** @var \PHPUnit_Framework_MockObject_MockObject */ + /** @var \PHPUnit_Framework_MockObject_MockObject|AbstractItem */ protected $quoteItemMock; - /** @var \PHPUnit_Framework_MockObject_MockObject */ + /** @var \PHPUnit_Framework_MockObject_MockObject|OrderItemInterface */ protected $orderItemMock; /** - * @var /PHPUnit_Framework_MockObject_MockObject + * @var \PHPUnit_Framework_MockObject_MockObject|ToOrderItem */ protected $subjectMock; - /** - * @var /Closure - */ - protected $closureMock; - protected function setUp() { - $this->orderItemMock = $this->getMock('Magento\Sales\Model\Order\Item', [], [], '', false); - $this->quoteItemMock = $this->getMock('Magento\Quote\Model\Quote\Item', [], [], '', false); - $orderItem = $this->orderItemMock; - $this->closureMock = function () use ($orderItem) { - return $orderItem; - }; - $this->subjectMock = $this->getMock('Magento\Quote\Model\Quote\Item\ToOrderItem', [], [], '', false); + $this->orderItemMock = $this->getMockForAbstractClass( + OrderItemInterface::class, [], '', false, false, true, ['getProductOptions', 'setProductOptions'] + ); + $this->quoteItemMock = $this->getMockForAbstractClass( + AbstractItem::class, [], '', false, false, true, ['getProduct'] + ); + $this->subjectMock = $this->getMock(ToOrderItem::class, [], [], '', false); + /** @var \PHPUnit_Framework_MockObject_MockObject|Product $productMock */ + $this->productMock = $this->getMock(Product::class, [], [], '', false); $this->model = new \Magento\Bundle\Model\Plugin\QuoteItem(); } public function testAroundItemToOrderItemPositive() { - $productMock = $this->getMock('Magento\Catalog\Model\Product', [], [], '', false); - $bundleAttribute = $this->getMock( - 'Magento\Catalog\Model\Product\Configuration\Item\Option', - [], - [], - '', - false - ); - $productMock->expects( - $this->once() - )->method( - 'getCustomOption' - )->with( - 'bundle_selection_attributes' - )->will( - $this->returnValue($bundleAttribute) - ); - $this->quoteItemMock->expects($this->once())->method('getProduct')->will($this->returnValue($productMock)); - $this->orderItemMock->expects($this->once())->method('setProductOptions'); + $attributeValue = 'test_value'; + $productOptions = [ + 'option_1' => 'value_1', + 'option_2' => 'value_2' + ]; + $expectedOptions = $productOptions + ['bundle_selection_attributes' => $attributeValue]; + + $bundleAttribute = $this->getMock( + 'Magento\Catalog\Model\Product\Configuration\Item\Option', + [], + [], + '', + false + ); + $bundleAttribute->expects($this->once()) + ->method('getValue') + ->willReturn($attributeValue); + + $this->productMock->expects($this->once()) + ->method('getCustomOption') + ->with('bundle_selection_attributes') + ->willReturn($bundleAttribute); + $this->quoteItemMock->expects($this->once())->method('getProduct')->willReturn( $this->productMock ); - $orderItem = $this->model->aroundConvert($this->subjectMock, $this->closureMock, $this->quoteItemMock, []); + $this->orderItemMock->expects($this->once())->method('getProductOptions')->willReturn($productOptions); + $this->orderItemMock->expects($this->once())->method('setProductOptions')->with($expectedOptions); + + $orderItem = $this->model->afterConvert($this->subjectMock, $this->orderItemMock, $this->quoteItemMock); $this->assertSame($this->orderItemMock, $orderItem); } public function testAroundItemToOrderItemNegative() { - $productMock = $this->getMock('Magento\Catalog\Model\Product', [], [], '', false); - $productMock->expects( - $this->once() - )->method( - 'getCustomOption' - )->with( - 'bundle_selection_attributes' - )->will( - $this->returnValue(false) - ); - $this->quoteItemMock->expects($this->once())->method('getProduct')->will($this->returnValue($productMock)); + $this->productMock->expects($this->once()) + ->method('getCustomOption') + ->with('bundle_selection_attributes')->willReturn(false); + + $this->quoteItemMock->expects($this->once())->method('getProduct') + ->will($this->returnValue($this->productMock)); $this->orderItemMock->expects($this->never())->method('setProductOptions'); - $orderItem = $this->model->aroundConvert($this->subjectMock, $this->closureMock, $this->quoteItemMock, []); + $orderItem = $this->model->afterConvert($this->subjectMock, $this->orderItemMock, $this->quoteItemMock); $this->assertSame($this->orderItemMock, $orderItem); } } From 9f326185d7679725cdb9f95a8c1b1f933053c216 Mon Sep 17 00:00:00 2001 From: Viktor Paladiychuk Date: Thu, 21 Jul 2016 18:23:23 +0300 Subject: [PATCH 06/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- .../Flat/Plugin/IndexerConfigData.php | 25 +++++++--------- .../Flat/Plugin/IndexerConfigDataTest.php | 29 +++++++------------ 2 files changed, 22 insertions(+), 32 deletions(-) diff --git a/app/code/Magento/Catalog/Model/Indexer/Category/Flat/Plugin/IndexerConfigData.php b/app/code/Magento/Catalog/Model/Indexer/Category/Flat/Plugin/IndexerConfigData.php index ae796a8b979fc..3b8061d2f9338 100644 --- a/app/code/Magento/Catalog/Model/Indexer/Category/Flat/Plugin/IndexerConfigData.php +++ b/app/code/Magento/Catalog/Model/Indexer/Category/Flat/Plugin/IndexerConfigData.php @@ -5,17 +5,20 @@ */ namespace Magento\Catalog\Model\Indexer\Category\Flat\Plugin; +use Magento\Indexer\Model\Config\Data; +use Magento\Catalog\Model\Indexer\Category\Flat\State; + class IndexerConfigData { /** - * @var \Magento\Catalog\Model\Indexer\Category\Flat\State + * @var State */ protected $state; /** - * @param \Magento\Catalog\Model\Indexer\Category\Flat\State $state + * @param State $state */ - public function __construct(\Magento\Catalog\Model\Indexer\Category\Flat\State $state) + public function __construct(State $state) { $this->state = $state; } @@ -23,24 +26,18 @@ public function __construct(\Magento\Catalog\Model\Indexer\Category\Flat\State $ /** * Unset indexer data in configuration if flat is disabled * - * @param \Magento\Indexer\Model\Config\Data $subject - * @param callable $proceed + * @param Data $subject + * @param mixed $data * @param string $path * @param mixed $default * * @return mixed * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundGet( - \Magento\Indexer\Model\Config\Data $subject, - \Closure $proceed, - $path = null, - $default = null - ) { - $data = $proceed($path, $default); - + public function afterGet(Data $subject, $data, $path = null, $default = null) + { if (!$this->state->isFlatEnabled()) { - $indexerId = \Magento\Catalog\Model\Indexer\Category\Flat\State::INDEXER_ID; + $indexerId = State::INDEXER_ID; if (!$path && isset($data[$indexerId])) { unset($data[$indexerId]); } elseif ($path) { diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/IndexerConfigDataTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/IndexerConfigDataTest.php index 3c70e28f1803e..3f2a25ad7a894 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/IndexerConfigDataTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/IndexerConfigDataTest.php @@ -5,36 +5,32 @@ */ namespace Magento\Catalog\Test\Unit\Model\Indexer\Category\Flat\Plugin; +use Magento\Catalog\Model\Indexer\Category\Flat\Plugin\IndexerConfigData; +use Magento\Catalog\Model\Indexer\Category\Flat\State; +use Magento\Indexer\Model\Config\Data; + class IndexerConfigDataTest extends \PHPUnit_Framework_TestCase { /** - * @var \Magento\Catalog\Model\Indexer\Category\Flat\Plugin\IndexerConfigData + * @var IndexerConfigData */ protected $model; /** - * @var \Magento\Catalog\Model\Indexer\Category\Flat\State|\PHPUnit_Framework_MockObject_MockObject + * @var State|\PHPUnit_Framework_MockObject_MockObject */ protected $stateMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var Data|\PHPUnit_Framework_MockObject_MockObject */ protected $subjectMock; protected function setUp() { - $this->stateMock = $this->getMock( - 'Magento\Catalog\Model\Indexer\Category\Flat\State', - ['isFlatEnabled'], - [], - '', - false - ); - - $this->subjectMock = $this->getMock('Magento\Indexer\Model\Config\Data', [], [], '', false); - - $this->model = new \Magento\Catalog\Model\Indexer\Category\Flat\Plugin\IndexerConfigData($this->stateMock); + $this->stateMock = $this->getMock(State::class, ['isFlatEnabled'], [], '', false); + $this->subjectMock = $this->getMock(Data::class, [], [], '', false); + $this->model = new IndexerConfigData($this->stateMock); } /** @@ -48,10 +44,7 @@ protected function setUp() public function testAroundGet($isFlat, $path, $default, $inputData, $outputData) { $this->stateMock->expects($this->once())->method('isFlatEnabled')->will($this->returnValue($isFlat)); - $closureMock = function () use ($inputData) { - return $inputData; - }; - $this->assertEquals($outputData, $this->model->aroundGet($this->subjectMock, $closureMock, $path, $default)); + $this->assertEquals($outputData, $this->model->afterGet($this->subjectMock, $inputData, $path, $default)); } public function aroundGetDataProvider() From 7ffa360bcdaab448fab6804ceb6e5541dc47ef3b Mon Sep 17 00:00:00 2001 From: Viktor Paladiychuk Date: Fri, 22 Jul 2016 13:09:03 +0300 Subject: [PATCH 07/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- .../Category/Flat/Plugin/StoreGroup.php | 45 +++++----- .../Category/Flat/Plugin/StoreGroupTest.php | 90 ++++++++----------- 2 files changed, 59 insertions(+), 76 deletions(-) diff --git a/app/code/Magento/Catalog/Model/Indexer/Category/Flat/Plugin/StoreGroup.php b/app/code/Magento/Catalog/Model/Indexer/Category/Flat/Plugin/StoreGroup.php index 7c1c87278fb71..9a58a0730400f 100644 --- a/app/code/Magento/Catalog/Model/Indexer/Category/Flat/Plugin/StoreGroup.php +++ b/app/code/Magento/Catalog/Model/Indexer/Category/Flat/Plugin/StoreGroup.php @@ -5,24 +5,29 @@ */ namespace Magento\Catalog\Model\Indexer\Category\Flat\Plugin; +use Magento\Framework\Model\ResourceModel\Db\AbstractDb; +use Magento\Framework\Model\AbstractModel; +use Magento\Framework\Indexer\IndexerRegistry; +use Magento\Catalog\Model\Indexer\Category\Flat\State; + class StoreGroup { - /** @var \Magento\Framework\Indexer\IndexerRegistry */ + /** + * @var IndexerRegistry + */ protected $indexerRegistry; /** - * @var \Magento\Catalog\Model\Indexer\Category\Flat\State + * @var State */ protected $state; /** - * @param \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry - * @param \Magento\Catalog\Model\Indexer\Category\Flat\State $state + * @param IndexerRegistry $indexerRegistry + * @param State $state */ - public function __construct( - \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry, - \Magento\Catalog\Model\Indexer\Category\Flat\State $state - ) { + public function __construct(IndexerRegistry $indexerRegistry, State $state) + { $this->indexerRegistry = $indexerRegistry; $this->state = $state; } @@ -30,31 +35,29 @@ public function __construct( /** * Validate changes for invalidating indexer * - * @param \Magento\Framework\Model\AbstractModel $group + * @param AbstractModel $group * @return bool */ - protected function validate(\Magento\Framework\Model\AbstractModel $group) + protected function validate(AbstractModel $group) { return $group->dataHasChangedFor('root_category_id') && !$group->isObjectNew(); } /** - * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $subject - * @param callable $proceed - * @param \Magento\Framework\Model\AbstractModel $group + * Invalidate flat category indexer if root category changed for store group + * + * @param AbstractDb $subject + * @param AbstractDb $objectResource + * @param AbstractModel $group * - * @return \Magento\Framework\Model\ResourceModel\Db\AbstractDb + * @return AbstractDb * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundSave( - \Magento\Framework\Model\ResourceModel\Db\AbstractDb $subject, - \Closure $proceed, - \Magento\Framework\Model\AbstractModel $group - ) { + public function afterSave(AbstractDb $subject, AbstractDb $objectResource, AbstractModel $group) + { $needInvalidating = $this->validate($group); - $objectResource = $proceed($group); if ($needInvalidating && $this->state->isFlatEnabled()) { - $this->indexerRegistry->get(\Magento\Catalog\Model\Indexer\Category\Flat\State::INDEXER_ID)->invalidate(); + $this->indexerRegistry->get(State::INDEXER_ID)->invalidate(); } return $objectResource; diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/StoreGroupTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/StoreGroupTest.php index 0611a594f01b6..32f4f913033cc 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/StoreGroupTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/StoreGroupTest.php @@ -5,17 +5,22 @@ */ namespace Magento\Catalog\Test\Unit\Model\Indexer\Category\Flat\Plugin; -use \Magento\Catalog\Model\Indexer\Category\Flat\Plugin\StoreGroup; +use Magento\Catalog\Model\Indexer\Category\Flat\Plugin\StoreGroup; +use Magento\Framework\Indexer\IndexerInterface; +use Magento\Catalog\Model\Indexer\Category\Flat\State; +use Magento\Framework\Indexer\IndexerRegistry; +use Magento\Store\Model\ResourceModel\Group; +use Magento\Store\Model\Group as GroupModel; class StoreGroupTest extends \PHPUnit_Framework_TestCase { /** - * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Indexer\IndexerInterface + * @var \PHPUnit_Framework_MockObject_MockObject|IndexerInterface */ protected $indexerMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Model\Indexer\Category\Flat\State + * @var \PHPUnit_Framework_MockObject_MockObject|State */ protected $stateMock; @@ -25,29 +30,24 @@ class StoreGroupTest extends \PHPUnit_Framework_TestCase protected $model; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \PHPUnit_Framework_MockObject_MockObject|Group */ protected $subjectMock; /** - * @var \Magento\Framework\Indexer\IndexerRegistry|\PHPUnit_Framework_MockObject_MockObject + * @var IndexerRegistry|\PHPUnit_Framework_MockObject_MockObject */ protected $indexerRegistryMock; /** - * @var \Closure - */ - protected $closureMock; - - /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \PHPUnit_Framework_MockObject_MockObject|GroupModel */ protected $groupMock; protected function setUp() { $this->indexerMock = $this->getMockForAbstractClass( - 'Magento\Framework\Indexer\IndexerInterface', + IndexerInterface::class, [], '', false, @@ -55,28 +55,19 @@ protected function setUp() true, ['getId', 'getState', '__wakeup'] ); - $this->stateMock = $this->getMock( - 'Magento\Catalog\Model\Indexer\Category\Flat\State', - ['isFlatEnabled'], - [], - '', - false - ); - $this->subjectMock = $this->getMock('Magento\Store\Model\ResourceModel\Group', [], [], '', false); + $this->stateMock = $this->getMock(State::class, ['isFlatEnabled'], [], '', false); + $this->subjectMock = $this->getMock(Group::class, [], [], '', false); $this->groupMock = $this->getMock( - 'Magento\Store\Model\Group', + GroupModel::class, ['dataHasChangedFor', 'isObjectNew', '__wakeup'], [], '', false ); - $this->closureMock = function () { - return false; - }; $this->indexerRegistryMock = $this->getMock( - 'Magento\Framework\Indexer\IndexerRegistry', + IndexerRegistry::class, ['get'], [], '', @@ -88,45 +79,34 @@ protected function setUp() public function testAroundSave() { - $this->stateMock->expects($this->once())->method('isFlatEnabled')->will($this->returnValue(true)); + $this->stateMock->expects($this->once())->method('isFlatEnabled')->willReturn(true); $this->indexerMock->expects($this->once())->method('invalidate'); $this->indexerRegistryMock->expects($this->once()) ->method('get') - ->with(\Magento\Catalog\Model\Indexer\Category\Flat\State::INDEXER_ID) - ->will($this->returnValue($this->indexerMock)); - $this->groupMock->expects( - $this->once() - )->method( - 'dataHasChangedFor' - )->with( - 'root_category_id' - )->will( - $this->returnValue(true) + ->with(State::INDEXER_ID) + ->willReturn($this->indexerMock); + $this->groupMock->expects($this->once()) + ->method('dataHasChangedFor') + ->with('root_category_id') + ->willReturn(true); + $this->groupMock->expects($this->once())->method('isObjectNew')->willReturn(false); + $this->assertSame( + $this->subjectMock, + $this->model->afterSave($this->subjectMock, $this->subjectMock, $this->groupMock) ); - $this->groupMock->expects($this->once())->method('isObjectNew')->will($this->returnValue(false)); - $this->assertFalse($this->model->aroundSave($this->subjectMock, $this->closureMock, $this->groupMock)); } public function testAroundSaveNotNew() { $this->stateMock->expects($this->never())->method('isFlatEnabled'); - $this->groupMock = $this->getMock( - 'Magento\Store\Model\Group', - ['dataHasChangedFor', 'isObjectNew', '__wakeup'], - [], - '', - false - ); - $this->groupMock->expects( - $this->once() - )->method( - 'dataHasChangedFor' - )->with( - 'root_category_id' - )->will( - $this->returnValue(true) + $this->groupMock->expects($this->once()) + ->method('dataHasChangedFor') + ->with('root_category_id') + ->willReturn(true); + $this->groupMock->expects($this->once())->method('isObjectNew')->willReturn(true); + $this->assertSame( + $this->subjectMock, + $this->model->afterSave($this->subjectMock, $this->subjectMock, $this->groupMock) ); - $this->groupMock->expects($this->once())->method('isObjectNew')->will($this->returnValue(true)); - $this->assertFalse($this->model->aroundSave($this->subjectMock, $this->closureMock, $this->groupMock)); } } From bae7ae2b9ee94dc623b7c5c0e25b578ae9cbc840 Mon Sep 17 00:00:00 2001 From: Viktor Paladiychuk Date: Fri, 22 Jul 2016 16:23:21 +0300 Subject: [PATCH 08/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- .../Category/Product/Plugin/StoreGroup.php | 42 +++++---- .../Product/Plugin/StoreGroupTest.php | 91 +++++++++---------- 2 files changed, 63 insertions(+), 70 deletions(-) diff --git a/app/code/Magento/Catalog/Model/Indexer/Category/Product/Plugin/StoreGroup.php b/app/code/Magento/Catalog/Model/Indexer/Category/Product/Plugin/StoreGroup.php index dce748c34fa3f..a9fa53217b89b 100644 --- a/app/code/Magento/Catalog/Model/Indexer/Category/Product/Plugin/StoreGroup.php +++ b/app/code/Magento/Catalog/Model/Indexer/Category/Product/Plugin/StoreGroup.php @@ -5,35 +5,40 @@ */ namespace Magento\Catalog\Model\Indexer\Category\Product\Plugin; +use Magento\Framework\Indexer\IndexerRegistry; +use Magento\Framework\Model\ResourceModel\Db\AbstractDb; +use Magento\Framework\Model\AbstractModel; +use Magento\Catalog\Model\Indexer\Category\Product; + class StoreGroup { - /** @var \Magento\Framework\Indexer\IndexerRegistry */ + /** + * @var IndexerRegistry + */ protected $indexerRegistry; /** - * @param \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry + * @param IndexerRegistry $indexerRegistry */ - public function __construct(\Magento\Framework\Indexer\IndexerRegistry $indexerRegistry) + public function __construct(IndexerRegistry $indexerRegistry) { $this->indexerRegistry = $indexerRegistry; } /** - * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $subject - * @param callable $proceed - * @param \Magento\Framework\Model\AbstractModel $group + * Invalidate flat product + * + * @param AbstractDb $subject + * @param AbstractDb $objectResource + * @param AbstractModel $group * @return mixed * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundSave( - \Magento\Framework\Model\ResourceModel\Db\AbstractDb $subject, - \Closure $proceed, - \Magento\Framework\Model\AbstractModel $group - ) { + public function afterSave(AbstractDb $subject, AbstractDb $objectResource, AbstractModel $group) + { $needInvalidating = $this->validate($group); - $objectResource = $proceed($group); if ($needInvalidating) { - $this->indexerRegistry->get(\Magento\Catalog\Model\Indexer\Category\Product::INDEXER_ID)->invalidate(); + $this->indexerRegistry->get(Product::INDEXER_ID)->invalidate(); } return $objectResource; @@ -42,15 +47,12 @@ public function aroundSave( /** * Validate changes for invalidating indexer * - * @param \Magento\Framework\Model\AbstractModel $group + * @param AbstractModel $group * @return bool */ - protected function validate(\Magento\Framework\Model\AbstractModel $group) + protected function validate(AbstractModel $group) { - return ($group->dataHasChangedFor( - 'website_id' - ) || $group->dataHasChangedFor( - 'root_category_id' - )) && !$group->isObjectNew(); + return ($group->dataHasChangedFor('website_id') || $group->dataHasChangedFor('root_category_id')) + && !$group->isObjectNew(); } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Product/Plugin/StoreGroupTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Product/Plugin/StoreGroupTest.php index c6f9bd16c526e..50f0032f46702 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Product/Plugin/StoreGroupTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Product/Plugin/StoreGroupTest.php @@ -5,27 +5,32 @@ */ namespace Magento\Catalog\Test\Unit\Model\Indexer\Category\Product\Plugin; -use \Magento\Catalog\Model\Indexer\Category\Product\Plugin\StoreGroup; +use Magento\Catalog\Model\Indexer\Category\Product\Plugin\StoreGroup; +use Magento\Framework\Indexer\IndexerInterface; +use Magento\Store\Model\ResourceModel\Group; +use Magento\Framework\Indexer\IndexerRegistry; +use Magento\Store\Model\Group as GroupModel; +use Magento\Catalog\Model\Indexer\Category\Product; class StoreGroupTest extends \PHPUnit_Framework_TestCase { - /** - * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Indexer\IndexerInterface - */ - protected $indexerMock; + /** + * @var GroupModel|\PHPUnit_Framework_MockObject_MockObject + */ + private $groupMock; - /** - * @var \PHPUnit_Framework_MockObject_MockObject| + /** + * @var \PHPUnit_Framework_MockObject_MockObject|IndexerInterface */ - protected $pluginMock; + protected $indexerMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \PHPUnit_Framework_MockObject_MockObject|Group */ protected $subject; /** - * @var \Magento\Framework\Indexer\IndexerRegistry|\PHPUnit_Framework_MockObject_MockObject + * @var IndexerRegistry|\PHPUnit_Framework_MockObject_MockObject */ protected $indexerRegistryMock; @@ -37,7 +42,7 @@ class StoreGroupTest extends \PHPUnit_Framework_TestCase protected function setUp() { $this->indexerMock = $this->getMockForAbstractClass( - 'Magento\Framework\Indexer\IndexerInterface', + IndexerInterface::class, [], '', false, @@ -45,9 +50,9 @@ protected function setUp() true, ['getId', 'getState', '__wakeup'] ); - $this->subject = $this->getMock('Magento\Store\Model\ResourceModel\Group', [], [], '', false); + $this->subject = $this->getMock(Group::class, [], [], '', false); $this->indexerRegistryMock = $this->getMock( - 'Magento\Framework\Indexer\IndexerRegistry', + IndexerRegistry::class, ['get'], [], '', @@ -65,17 +70,16 @@ public function testAroundSave($valueMap) { $this->mockIndexerMethods(); $groupMock = $this->getMock( - 'Magento\Store\Model\Group', + GroupModel::class, ['dataHasChangedFor', 'isObjectNew', '__wakeup'], [], '', false ); - $groupMock->expects($this->exactly(2))->method('dataHasChangedFor')->will($this->returnValueMap($valueMap)); - $groupMock->expects($this->once())->method('isObjectNew')->will($this->returnValue(false)); + $groupMock->expects($this->exactly(2))->method('dataHasChangedFor')->willReturnMap($valueMap); + $groupMock->expects($this->once())->method('isObjectNew')->willReturn(false); - $proceed = $this->mockPluginProceed(); - $this->assertFalse($this->model->aroundSave($this->subject, $proceed, $groupMock)); + $this->assertSame($this->subject, $this->model->afterSave($this->subject, $this->subject, $groupMock)); } /** @@ -85,17 +89,16 @@ public function testAroundSave($valueMap) public function testAroundSaveNotNew($valueMap) { $groupMock = $this->getMock( - 'Magento\Store\Model\Group', + GroupModel::class, ['dataHasChangedFor', 'isObjectNew', '__wakeup'], [], '', false ); - $groupMock->expects($this->exactly(2))->method('dataHasChangedFor')->will($this->returnValueMap($valueMap)); - $groupMock->expects($this->once())->method('isObjectNew')->will($this->returnValue(true)); + $groupMock->expects($this->exactly(2))->method('dataHasChangedFor')->willReturnMap($valueMap); + $groupMock->expects($this->once())->method('isObjectNew')->willReturn(true); - $proceed = $this->mockPluginProceed(); - $this->assertFalse($this->model->aroundSave($this->subject, $proceed, $groupMock)); + $this->assertSame($this->subject, $this->model->afterSave($this->subject, $this->subject, $groupMock)); } public function changedDataProvider() @@ -110,24 +113,19 @@ public function changedDataProvider() public function testAroundSaveWithoutChanges() { - $groupMock = $this->getMock( - 'Magento\Store\Model\Group', - ['dataHasChangedFor', 'isObjectNew', '__wakeup'], - [], - '', - false - ); - $groupMock->expects( - $this->exactly(2) - )->method( - 'dataHasChangedFor' - )->will( - $this->returnValueMap([['root_category_id', false], ['website_id', false]]) - ); - $groupMock->expects($this->never())->method('isObjectNew'); - - $proceed = $this->mockPluginProceed(); - $this->assertFalse($this->model->aroundSave($this->subject, $proceed, $groupMock)); + $this->groupMock = $this->getMock( + GroupModel::class, + [ 'dataHasChangedFor', 'isObjectNew', '__wakeup' ], + [ ], + '', + false + ); + $this->groupMock->expects($this->exactly(2)) + ->method('dataHasChangedFor') + ->willReturnMap([['root_category_id', false], ['website_id', false]]); + $this->groupMock->expects($this->never())->method('isObjectNew'); + + $this->assertSame($this->subject, $this->model->afterSave($this->subject, $this->subject, $this->groupMock )); } protected function mockIndexerMethods() @@ -135,14 +133,7 @@ protected function mockIndexerMethods() $this->indexerMock->expects($this->once())->method('invalidate'); $this->indexerRegistryMock->expects($this->once()) ->method('get') - ->with(\Magento\Catalog\Model\Indexer\Category\Product::INDEXER_ID) - ->will($this->returnValue($this->indexerMock)); - } - - protected function mockPluginProceed($returnValue = false) - { - return function () use ($returnValue) { - return $returnValue; - }; + ->with(Product::INDEXER_ID) + ->willReturn($this->indexerMock); } } From 2398a929d7c3054b8802e92488707cd314a0b7a6 Mon Sep 17 00:00:00 2001 From: Viktor Paladiychuk Date: Fri, 22 Jul 2016 18:33:18 +0300 Subject: [PATCH 09/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- .../Magento/Catalog/Model/CatalogRegistry.php | 51 -------- .../Product/Eav/Plugin/AttributeSet.php | 63 ++++++---- .../Product/Eav/Plugin/AttributeSetTest.php | 111 ++++++++++++------ 3 files changed, 109 insertions(+), 116 deletions(-) delete mode 100644 app/code/Magento/Catalog/Model/CatalogRegistry.php diff --git a/app/code/Magento/Catalog/Model/CatalogRegistry.php b/app/code/Magento/Catalog/Model/CatalogRegistry.php deleted file mode 100644 index 7eb7b3f77f8b1..0000000000000 --- a/app/code/Magento/Catalog/Model/CatalogRegistry.php +++ /dev/null @@ -1,51 +0,0 @@ -entityRegistry = $entityRegistry; - } - - /** - * @param EntityManager $subject - * @param \Closure $proceed - * @param string $entityType - * @param object $entity - * @param string $identifier - * @return null|object - * @SuppressWarnings(PHPMD.UnusedFormalParameter) - */ - public function aroundLoad(EntityManager $subject, \Closure $proceed, $entityType, $entity, $identifier) - { - $object = $this->entityRegistry->retrieve($entityType, $identifier); - if (!$object) { - $object = $proceed($entityType, $entity, $identifier); - $this->entityRegistry->register($entityType, $identifier, $object); - } - return $object; - } -} diff --git a/app/code/Magento/Catalog/Model/Indexer/Product/Eav/Plugin/AttributeSet.php b/app/code/Magento/Catalog/Model/Indexer/Product/Eav/Plugin/AttributeSet.php index e911940514344..48502eb118e8b 100644 --- a/app/code/Magento/Catalog/Model/Indexer/Product/Eav/Plugin/AttributeSet.php +++ b/app/code/Magento/Catalog/Model/Indexer/Product/Eav/Plugin/AttributeSet.php @@ -5,10 +5,18 @@ */ namespace Magento\Catalog\Model\Indexer\Product\Eav\Plugin; +use Magento\Eav\Model\Entity\Attribute\Set; +use Magento\Catalog\Model\Indexer\Product\Eav\Processor; + class AttributeSet { - /** - * @var \Magento\Catalog\Model\Indexer\Product\Eav\Processor + /** + * @var bool + */ + private $requiresReindex; + + /** + * @var Processor */ protected $_indexerEavProcessor; @@ -18,13 +26,11 @@ class AttributeSet protected $_attributeFilter; /** - * @param \Magento\Catalog\Model\Indexer\Product\Eav\Processor $indexerEavProcessor + * @param Processor $indexerEavProcessor * @param AttributeSet\IndexableAttributeFilter $filter */ - public function __construct( - \Magento\Catalog\Model\Indexer\Product\Eav\Processor $indexerEavProcessor, - AttributeSet\IndexableAttributeFilter $filter - ) { + public function __construct(Processor $indexerEavProcessor, AttributeSet\IndexableAttributeFilter $filter) + { $this->_indexerEavProcessor = $indexerEavProcessor; $this->_attributeFilter = $filter; } @@ -32,31 +38,36 @@ public function __construct( /** * Invalidate EAV indexer if attribute set has indexable attributes changes * - * @param \Magento\Eav\Model\Entity\Attribute\Set $subject - * @param callable $proceed - * - * @return \Magento\Eav\Model\Entity\Attribute\Set + * @param Set $subject + * @param Set $result + * @return Set * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundSave(\Magento\Eav\Model\Entity\Attribute\Set $subject, \Closure $proceed) + public function afterSave(Set $subject, Set $result) { - $requiresReindex = false; - if ($subject->getId()) { - /** @var \Magento\Eav\Model\Entity\Attribute\Set $originalSet */ - $originalSet = clone $subject; - $originalSet->initFromSkeleton($subject->getId()); - $originalAttributeCodes = array_flip($this->_attributeFilter->filter($originalSet)); - $subjectAttributeCodes = array_flip($this->_attributeFilter->filter($subject)); - $requiresReindex = (bool)count(array_merge( - array_diff_key($subjectAttributeCodes, $originalAttributeCodes), - array_diff_key($originalAttributeCodes, $subjectAttributeCodes) - )); - } - $result = $proceed(); - if ($requiresReindex) { + if ($this->requiresReindex) { $this->_indexerEavProcessor->markIndexerAsInvalid(); } return $result; } + + /** + * @param Set $subject + * + * @return bool + */ + public function beforeSave(Set $subject) { + $this->requiresReindex = false; + if ( $subject->getId() ) { + $originalSet = clone $subject; + $originalSet->initFromSkeleton($subject->getId()); + $originalAttributeCodes = array_flip( $this->_attributeFilter->filter( $originalSet ) ); + $subjectAttributeCodes = array_flip( $this->_attributeFilter->filter( $subject ) ); + $this->requiresReindex = (bool) count( array_merge( + array_diff_key( $subjectAttributeCodes, $originalAttributeCodes ), + array_diff_key( $originalAttributeCodes, $subjectAttributeCodes ) + ) ); + } + } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Eav/Plugin/AttributeSetTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Eav/Plugin/AttributeSetTest.php index 031cf8f00005b..c049523aa8138 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Eav/Plugin/AttributeSetTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Eav/Plugin/AttributeSetTest.php @@ -5,47 +5,80 @@ */ namespace Magento\Catalog\Test\Unit\Model\Indexer\Product\Eav\Plugin; +use Magento\Catalog\Model\Indexer\Product\Eav\Plugin\AttributeSet\IndexableAttributeFilter; +use Magento\Eav\Model\Entity\Attribute\Set; +use Magento\Catalog\Model\Indexer\Product\Eav\Processor; +use Magento\Catalog\Model\Indexer\Product\Eav\Plugin\AttributeSet; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; + class AttributeSetTest extends \PHPUnit_Framework_TestCase { - public function testAroundSave() + /** + * @var AttributeSet + */ + private $model; + + /** + * @var Processor|\PHPUnit_Framework_MockObject_MockObject + */ + private $eavProcessorMock; + + /** + * @var IndexableAttributeFilter|\PHPUnit_Framework_MockObject_MockObject + */ + private $filterMock; + + /** + * @var Set|\PHPUnit_Framework_MockObject_MockObject + */ + private $subjectMock; + + public function setUp() + { + $this->filterMock = $this->getMock(IndexableAttributeFilter::class, [], [], '', false); + $this->subjectMock = $this->getMock(Set::class, [], [], '', false); + $this->eavProcessorMock = $this->getMock(Processor::class, [], [], '', false); + } + + public function testBeforeSave() + { + $this->model = (new ObjectManager($this)) + ->getObject( + AttributeSet::class, + [ + 'indexerEavProcessor' => $this->eavProcessorMock, + 'filter' => $this->filterMock + ] + ); + + $this->filterMock->expects($this->at(0)) + ->method('filter') + ->will($this->returnValue([1, 2, 3])); + $this->filterMock->expects($this->at(1)) + ->method('filter') + ->will($this->returnValue([1, 2])); + + $this->subjectMock->expects($this->exactly(2)) + ->method('getId') + ->will($this->returnValue(1)); + + $this->model->beforeSave($this->subjectMock); + } + + public function testAfterSave() { - $eavProcessorMock = $this->getMockBuilder('Magento\Catalog\Model\Indexer\Product\Eav\Processor') - ->disableOriginalConstructor() - ->getMock(); - $eavProcessorMock->expects($this->once()) - ->method('markIndexerAsInvalid'); - - $filter = $this->getMockBuilder( - 'Magento\Catalog\Model\Indexer\Product\Eav\Plugin\AttributeSet\IndexableAttributeFilter' - ) - ->disableOriginalConstructor() - ->getMock(); - $filter->expects($this->at(0)) - ->method('filter') - ->will($this->returnValue([1, 2, 3])); - $filter->expects($this->at(1)) - ->method('filter') - ->will($this->returnValue([1, 2])); - - $subjectMock = $this->getMockBuilder('Magento\Eav\Model\Entity\Attribute\Set') - ->disableOriginalConstructor() - ->getMock(); - $subjectMock->expects($this->any()) - ->method('getId') - ->will($this->returnValue(11)); - - $model = new \Magento\Catalog\Model\Indexer\Product\Eav\Plugin\AttributeSet( - $eavProcessorMock, - $filter - ); - - $closure = function () use ($subjectMock) { - return $subjectMock; - }; - - $this->assertEquals( - $subjectMock, - $model->aroundSave($subjectMock, $closure) - ); + $this->eavProcessorMock->expects($this->once())->method('markIndexerAsInvalid'); + + $this->model = (new ObjectManager($this)) + ->getObject( + AttributeSet::class, + [ + 'indexerEavProcessor' => $this->eavProcessorMock, + 'filter' => $this->filterMock, + 'requiresReindex' => true + ] + ); + + $this->assertSame($this->subjectMock, $this->model->afterSave($this->subjectMock, $this->subjectMock)); } } From 3f44762b9032f8869998b529b662229586f6ee86 Mon Sep 17 00:00:00 2001 From: Yuri Kovsher Date: Mon, 25 Jul 2016 11:15:41 +0300 Subject: [PATCH 10/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- .../Indexer/Product/Flat/Plugin/IndexerConfigData.php | 8 +++----- .../Product/Flat/Plugin/IndexerConfigDataTest.php | 11 ++++------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/app/code/Magento/Catalog/Model/Indexer/Product/Flat/Plugin/IndexerConfigData.php b/app/code/Magento/Catalog/Model/Indexer/Product/Flat/Plugin/IndexerConfigData.php index 60394766f4b52..a561fd96ada15 100644 --- a/app/code/Magento/Catalog/Model/Indexer/Product/Flat/Plugin/IndexerConfigData.php +++ b/app/code/Magento/Catalog/Model/Indexer/Product/Flat/Plugin/IndexerConfigData.php @@ -24,7 +24,7 @@ public function __construct(\Magento\Catalog\Model\Indexer\Product\Flat\State $s * Around get handler * * @param \Magento\Indexer\Model\Config\Data $subject - * @param callable $proceed + * @param mixed $data * @param string $path * @param string $default * @@ -32,14 +32,12 @@ public function __construct(\Magento\Catalog\Model\Indexer\Product\Flat\State $s * @SuppressWarnings(PHPMD.UnusedFormalParameter) * */ - public function aroundGet( + public function afterGet( \Magento\Indexer\Model\Config\Data $subject, - \Closure $proceed, + $data, $path = null, $default = null ) { - $data = $proceed($path, $default); - if (!$this->_state->isFlatEnabled()) { $indexerId = \Magento\Catalog\Model\Indexer\Product\Flat\Processor::INDEXER_ID; if (!$path && isset($data[$indexerId])) { diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Flat/Plugin/IndexerConfigDataTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Flat/Plugin/IndexerConfigDataTest.php index 77710f27d38ee..5d8e59ee91cb6 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Flat/Plugin/IndexerConfigDataTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Flat/Plugin/IndexerConfigDataTest.php @@ -42,19 +42,16 @@ protected function setUp() * @param mixed $default * @param array $inputData * @param array $outputData - * @dataProvider aroundGetDataProvider + * @dataProvider afterGetDataProvider */ - public function testAroundGet($isFlat, $path, $default, $inputData, $outputData) + public function testAfterGet($isFlat, $path, $default, $inputData, $outputData) { - $closureMock = function () use ($inputData) { - return $inputData; - }; $this->_stateMock->expects($this->once())->method('isFlatEnabled')->will($this->returnValue($isFlat)); - $this->assertEquals($outputData, $this->model->aroundGet($this->subjectMock, $closureMock, $path, $default)); + $this->assertEquals($outputData, $this->model->afterGet($this->subjectMock, $inputData, $path, $default)); } - public function aroundGetDataProvider() + public function afterGetDataProvider() { $flatIndexerData = [ 'indexer_id' => 'catalog_product_flat', From 095310795678f50e1c1fbb627da497d3f9d2f863 Mon Sep 17 00:00:00 2001 From: Viktor Paladiychuk Date: Mon, 25 Jul 2016 11:57:56 +0300 Subject: [PATCH 11/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- .../Product/Eav/Plugin/AttributeSet.php | 24 +++++++++++- .../Product/Eav/Plugin/AttributeSetTest.php | 37 +++++++++++++++---- 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/app/code/Magento/Catalog/Model/Indexer/Product/Eav/Plugin/AttributeSet.php b/app/code/Magento/Catalog/Model/Indexer/Product/Eav/Plugin/AttributeSet.php index 48502eb118e8b..1db2194bd9777 100644 --- a/app/code/Magento/Catalog/Model/Indexer/Product/Eav/Plugin/AttributeSet.php +++ b/app/code/Magento/Catalog/Model/Indexer/Product/Eav/Plugin/AttributeSet.php @@ -7,6 +7,8 @@ use Magento\Eav\Model\Entity\Attribute\Set; use Magento\Catalog\Model\Indexer\Product\Eav\Processor; +use Magento\Eav\Model\Entity\Attribute\SetFactory; +use Magento\Framework\App\ObjectManager; class AttributeSet { @@ -15,6 +17,11 @@ class AttributeSet */ private $requiresReindex; + /** + * @var SetFactory + */ + private $setFactory; + /** * @var Processor */ @@ -52,6 +59,20 @@ public function afterSave(Set $subject, Set $result) return $result; } + /** + * Return attribute set factory + * + * @return SetFactory + * @deprecated + */ + private function getAttributeSetFactory() + { + if ($this->setFactory === null) { + $this->setFactory = ObjectManager::getInstance()->get(SetFactory::class); + } + return $this->setFactory; + } + /** * @param Set $subject * @@ -60,7 +81,8 @@ public function afterSave(Set $subject, Set $result) public function beforeSave(Set $subject) { $this->requiresReindex = false; if ( $subject->getId() ) { - $originalSet = clone $subject; + /** @var Set $originalSet */ + $originalSet = $this->getAttributeSetFactory()->create(); $originalSet->initFromSkeleton($subject->getId()); $originalAttributeCodes = array_flip( $this->_attributeFilter->filter( $originalSet ) ); $subjectAttributeCodes = array_flip( $this->_attributeFilter->filter( $subject ) ); diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Eav/Plugin/AttributeSetTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Eav/Plugin/AttributeSetTest.php index c049523aa8138..58301f8d1a59b 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Eav/Plugin/AttributeSetTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Eav/Plugin/AttributeSetTest.php @@ -7,6 +7,7 @@ use Magento\Catalog\Model\Indexer\Product\Eav\Plugin\AttributeSet\IndexableAttributeFilter; use Magento\Eav\Model\Entity\Attribute\Set; +use Magento\Eav\Model\Entity\Attribute\SetFactory; use Magento\Catalog\Model\Indexer\Product\Eav\Processor; use Magento\Catalog\Model\Indexer\Product\Eav\Plugin\AttributeSet; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; @@ -33,34 +34,54 @@ class AttributeSetTest extends \PHPUnit_Framework_TestCase */ private $subjectMock; + /** + * @var SetFactory|\PHPUnit_Framework_MockObject_MockObject + */ + private $setFactoryMock; + + /** + * @var Set|\PHPUnit_Framework_MockObject_MockObject + */ + private $originalSetMock; + public function setUp() { $this->filterMock = $this->getMock(IndexableAttributeFilter::class, [], [], '', false); $this->subjectMock = $this->getMock(Set::class, [], [], '', false); $this->eavProcessorMock = $this->getMock(Processor::class, [], [], '', false); + $this->setFactoryMock = $this->getMock(SetFactory::class, [], [], '', false); } public function testBeforeSave() { + $setId = 1; + $this->originalSetMock = $this->getMock(Set::class, [], [], '', false); + $this->originalSetMock->expects($this->once())->method('initFromSkeleton')->with($setId); + + $this->setFactoryMock->expects($this->once())->method('create')->willReturn($this->originalSetMock); + $this->model = (new ObjectManager($this)) ->getObject( AttributeSet::class, [ 'indexerEavProcessor' => $this->eavProcessorMock, - 'filter' => $this->filterMock + 'filter' => $this->filterMock, + 'setFactory' => $this->setFactoryMock ] ); - $this->filterMock->expects($this->at(0)) - ->method('filter') - ->will($this->returnValue([1, 2, 3])); - $this->filterMock->expects($this->at(1)) - ->method('filter') - ->will($this->returnValue([1, 2])); + $this->filterMock->expects($this->exactly(2)) + ->method('filter') + ->willReturnMap( + [ + [$this->originalSetMock, [1, 2, 3]], + [$this->subjectMock, [1, 2]] + ] + ); $this->subjectMock->expects($this->exactly(2)) ->method('getId') - ->will($this->returnValue(1)); + ->willReturn($setId); $this->model->beforeSave($this->subjectMock); } From ea9845fc098d0485aef43a618d45636b40cca25d Mon Sep 17 00:00:00 2001 From: Yuri Kovsher Date: Mon, 25 Jul 2016 12:21:35 +0300 Subject: [PATCH 12/57] MAGETWO-55691: Decrease of stack trace --- lib/internal/Magento/Framework/Interception/Interceptor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/Magento/Framework/Interception/Interceptor.php b/lib/internal/Magento/Framework/Interception/Interceptor.php index afe5c22b243fa..5f76a2f55cf44 100644 --- a/lib/internal/Magento/Framework/Interception/Interceptor.php +++ b/lib/internal/Magento/Framework/Interception/Interceptor.php @@ -130,7 +130,7 @@ protected function ___callPlugins($method, array $arguments, array $pluginInfo) // Call 'around' listener $code = $currentPluginInfo[DefinitionInterface::LISTENER_AROUND]; $pluginInfo = $pluginList->getNext($type, $method, $code); - $pluginInstance = $this->pluginList->getPlugin($type, $code); + $pluginInstance = $pluginList->getPlugin($type, $code); $pluginMethod = 'around' . $capMethod; $result = $pluginInstance->$pluginMethod($subject, $next, ...array_values($arguments)); } else { From f1a14b39e275412dfb8b0079dfc2c91cb1c41c10 Mon Sep 17 00:00:00 2001 From: Viktor Paladiychuk Date: Mon, 25 Jul 2016 14:22:28 +0300 Subject: [PATCH 13/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- .../Model/ResourceModel/Attribute/Save.php | 33 +++++----- .../ResourceModel/Attribute/SaveTest.php | 65 ++++++++----------- 2 files changed, 41 insertions(+), 57 deletions(-) diff --git a/app/code/Magento/Catalog/Plugin/Model/ResourceModel/Attribute/Save.php b/app/code/Magento/Catalog/Plugin/Model/ResourceModel/Attribute/Save.php index 1f1f4d3fdb2e2..888879717b7ad 100644 --- a/app/code/Magento/Catalog/Plugin/Model/ResourceModel/Attribute/Save.php +++ b/app/code/Magento/Catalog/Plugin/Model/ResourceModel/Attribute/Save.php @@ -6,44 +6,41 @@ namespace Magento\Catalog\Plugin\Model\ResourceModel\Attribute; +use Magento\Catalog\Model\ResourceModel\Attribute; +use Magento\PageCache\Model\Config; +use Magento\Framework\App\Cache\TypeListInterface; + class Save { /** - * @var \Magento\PageCache\Model\Config + * @var Config */ protected $config; /** - * @var \Magento\Framework\App\Cache\TypeListInterface + * @var TypeListInterface */ protected $typeList; /** - * @param \Magento\PageCache\Model\Config $config - * @param \Magento\Framework\App\Cache\TypeListInterface $typeList + * @param Config $config + * @param TypeListInterface $typeList */ - public function __construct( - \Magento\PageCache\Model\Config $config, - \Magento\Framework\App\Cache\TypeListInterface $typeList - ) { + public function __construct(Config $config, TypeListInterface $typeList) + { $this->config = $config; $this->typeList = $typeList; } /** - * @param \Magento\Catalog\Model\ResourceModel\Attribute $subject - * @param callable $proceed - * @param \Magento\Framework\Model\AbstractModel $attribute - * @return mixed + * @param Attribute $subject + * @param Attribute $result + * @return Attribute $result * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundSave( - \Magento\Catalog\Model\ResourceModel\Attribute $subject, - \Closure $proceed, - \Magento\Framework\Model\AbstractModel $attribute - ) { - $result = $proceed($attribute); + public function afterSave(Attribute $subject, Attribute $result) + { if ($this->config->isEnabled()) { $this->typeList->invalidate('full_page'); } diff --git a/app/code/Magento/Catalog/Test/Unit/Plugin/Model/ResourceModel/Attribute/SaveTest.php b/app/code/Magento/Catalog/Test/Unit/Plugin/Model/ResourceModel/Attribute/SaveTest.php index a85041087a218..9333e1ca4b899 100644 --- a/app/code/Magento/Catalog/Test/Unit/Plugin/Model/ResourceModel/Attribute/SaveTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Plugin/Model/ResourceModel/Attribute/SaveTest.php @@ -6,70 +6,57 @@ namespace Magento\Catalog\Test\Unit\Plugin\Model\ResourceModel\Attribute; -use \Magento\Catalog\Plugin\Model\ResourceModel\Attribute\Save; +use Magento\Catalog\Plugin\Model\ResourceModel\Attribute\Save; +use Magento\PageCache\Model\Config; +use Magento\Framework\App\Cache\TypeListInterface; +use Magento\Catalog\Model\ResourceModel\Attribute; class SaveTest extends \PHPUnit_Framework_TestCase { - /** @var \Magento\Catalog\Plugin\Model\ResourceModel\Attribute\Save */ + /** + * @var + */ + private $subjectMock; + + /** + * @var Save + */ protected $save; - /** @var \Magento\PageCache\Model\Config|\PHPUnit_Framework_MockObject_MockObject */ + /** + * @var Config|\PHPUnit_Framework_MockObject_MockObject + */ protected $config; - /** @var \Magento\Framework\App\Cache\TypeListInterface|\PHPUnit_Framework_MockObject_MockObject */ + /** + * @var TypeListInterface|\PHPUnit_Framework_MockObject_MockObject + */ protected $typeList; protected function setUp() { - $this->config = $this->getMockBuilder('Magento\PageCache\Model\Config') - ->disableOriginalConstructor() - ->setMethods(['isEnabled']) - ->getMock(); - $this->typeList = $this->getMockBuilder('Magento\Framework\App\Cache\TypeListInterface') - ->disableOriginalConstructor() - ->setMethods(['invalidate']) - ->getMockForAbstractClass(); - + $this->config = $this->getMock(Config::class, ['isEnabled'], [], '', false); + $this->typeList = $this->getMockForAbstractClass( + TypeListInterface::class, [], '', false, false, true, ['invalidate'] + ); + $this->subjectMock = $this->getMock(Attribute::class, [], [], '', false); $this->save = new Save($this->config, $this->typeList); } public function testAroundSaveWithoutInvalidate() { - $subject = $this->getMockBuilder('Magento\Catalog\Model\ResourceModel\Attribute') - ->disableOriginalConstructor() - ->getMock(); - $attribute = $this->getMockBuilder('Magento\Catalog\Model\ResourceModel\Eav\Attribute') - ->disableOriginalConstructor() - ->getMock(); - $self = $this; - $proceed = function ($object) use ($self, $attribute) { - $self->assertEquals($object, $attribute); - }; - - $this->config->expects($this->once()) + $this->config->expects($this->once()) ->method('isEnabled') ->willReturn(false); $this->typeList->expects($this->never()) ->method('invalidate'); - $this->save->aroundSave($subject, $proceed, $attribute); + $this->assertSame($this->subjectMock, $this->save->afterSave($this->subjectMock, $this->subjectMock)); } public function testAroundSave() { - $subject = $this->getMockBuilder('Magento\Catalog\Model\ResourceModel\Attribute') - ->disableOriginalConstructor() - ->getMock(); - $attribute = $this->getMockBuilder('Magento\Catalog\Model\ResourceModel\Eav\Attribute') - ->disableOriginalConstructor() - ->getMock(); - - $self = $this; - $proceed = function ($object) use ($self, $attribute) { - $self->assertEquals($object, $attribute); - }; - $this->config->expects($this->once()) ->method('isEnabled') ->willReturn(true); @@ -78,6 +65,6 @@ public function testAroundSave() ->method('invalidate') ->with('full_page'); - $this->save->aroundSave($subject, $proceed, $attribute); + $this->assertSame($this->subjectMock, $this->save->afterSave($this->subjectMock, $this->subjectMock)); } } From 47ee24fb0bf5ba468999a7737555ee4883f1453d Mon Sep 17 00:00:00 2001 From: Yuri Kovsher Date: Mon, 25 Jul 2016 17:19:55 +0300 Subject: [PATCH 14/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- .../Model/Plugin/QuoteItemProductOption.php | 30 +++++++------------ .../Plugin/QuoteItemProductOptionTest.php | 21 ++++--------- 2 files changed, 16 insertions(+), 35 deletions(-) diff --git a/app/code/Magento/Catalog/Model/Plugin/QuoteItemProductOption.php b/app/code/Magento/Catalog/Model/Plugin/QuoteItemProductOption.php index 206acf7bf0f95..4dbd9e18d9928 100644 --- a/app/code/Magento/Catalog/Model/Plugin/QuoteItemProductOption.php +++ b/app/code/Magento/Catalog/Model/Plugin/QuoteItemProductOption.php @@ -9,33 +9,26 @@ class QuoteItemProductOption { /** * @param \Magento\Quote\Model\Quote\Item\ToOrderItem $subject - * @param callable $proceed - * @param \Magento\Quote\Model\Quote\Item\AbstractItem $item - * @param array $additional + * @param \Magento\Quote\Model\Quote\Item\AbstractItem $quoteItem * @return \Magento\Sales\Model\Order\Item * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundConvert( + public function beforeConvert( \Magento\Quote\Model\Quote\Item\ToOrderItem $subject, - \Closure $proceed, - \Magento\Quote\Model\Quote\Item\AbstractItem $item, - $additional = [] + \Magento\Quote\Model\Quote\Item\AbstractItem $quoteItem ) { - /** @var $orderItem \Magento\Sales\Model\Order\Item */ - $orderItem = $proceed($item, $additional); - - if (is_array($item->getOptions())) { - foreach ($item->getOptions() as $itemOption) { + if (is_array($quoteItem->getOptions())) { + foreach ($quoteItem->getOptions() as $itemOption) { $code = explode('_', $itemOption->getCode()); + if (isset($code[1]) && is_numeric($code[1])) { - $option = $item->getProduct()->getOptionById($code[1]); + $option = $quoteItem->getProduct()->getOptionById($code[1]); + if ($option && $option->getType() == \Magento\Catalog\Model\Product\Option::OPTION_TYPE_FILE) { try { - $option->groupFactory( - $option->getType() - )->setQuoteItemOption( - $itemOption - )->copyQuoteToOrder(); + $option->groupFactory($option->getType()) + ->setQuoteItemOption($itemOption) + ->copyQuoteToOrder(); } catch (\Exception $e) { continue; } @@ -43,6 +36,5 @@ public function aroundConvert( } } } - return $orderItem; } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Plugin/QuoteItemProductOptionTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Plugin/QuoteItemProductOptionTest.php index ebf3ff2d6d6ff..b47a7a2b571e7 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Plugin/QuoteItemProductOptionTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Plugin/QuoteItemProductOptionTest.php @@ -17,12 +17,7 @@ class QuoteItemProductOptionTest extends \PHPUnit_Framework_TestCase protected $model; /** - * @var \Closure - */ - protected $closureMock; - - /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Quote\Model\Quote\Item\ToOrderItem|\PHPUnit_Framework_MockObject_MockObject */ protected $subjectMock; @@ -30,23 +25,18 @@ protected function setUp() { $this->orderItemMock = $this->getMock('Magento\Sales\Model\Order\Item', [], [], '', false); $this->quoteItemMock = $this->getMock('Magento\Quote\Model\Quote\Item', [], [], '', false); - $orderItem = $this->orderItemMock; $this->subjectMock = $this->getMock('Magento\Quote\Model\Quote\Item\ToOrderItem', [], [], '', false); - $this->closureMock = function () use ($orderItem) { - return $orderItem; - }; $this->model = new \Magento\Catalog\Model\Plugin\QuoteItemProductOption(); } - public function testAroundItemToOrderItemEmptyOptions() + public function testBeforeItemToOrderItemEmptyOptions() { $this->quoteItemMock->expects($this->exactly(2))->method('getOptions')->will($this->returnValue([])); - $orderItem = $this->model->aroundConvert($this->subjectMock, $this->closureMock, $this->quoteItemMock); - $this->assertSame($this->orderItemMock, $orderItem); + $this->assertNull($this->model->beforeConvert($this->subjectMock, $this->quoteItemMock)); } - public function testAroundItemToOrderItemWithOptions() + public function testBeforeItemToOrderItemWithOptions() { $itemOption = $this->getMock( 'Magento\Quote\Model\Quote\Item\Option', @@ -74,7 +64,6 @@ public function testAroundItemToOrderItemWithOptions() $this->quoteItemMock->expects($this->once())->method('getProduct')->will($this->returnValue($productMock)); - $orderItem = $this->model->aroundConvert($this->subjectMock, $this->closureMock, $this->quoteItemMock); - $this->assertSame($this->orderItemMock, $orderItem); + $this->assertNull($this->model->beforeConvert($this->subjectMock, $this->quoteItemMock)); } } From 5dc4d8e80925eb462374898fe799bae1dccc0ef6 Mon Sep 17 00:00:00 2001 From: Viktor Paladiychuk Date: Mon, 25 Jul 2016 17:31:29 +0300 Subject: [PATCH 15/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- .../Plugin/AroundProductRepositorySave.php | 27 +++++++------------ .../AroundProductRepositorySaveTest.php | 27 +++++++------------ 2 files changed, 18 insertions(+), 36 deletions(-) diff --git a/app/code/Magento/CatalogInventory/Model/Plugin/AroundProductRepositorySave.php b/app/code/Magento/CatalogInventory/Model/Plugin/AroundProductRepositorySave.php index df91025408218..310e64e80bf8a 100644 --- a/app/code/Magento/CatalogInventory/Model/Plugin/AroundProductRepositorySave.php +++ b/app/code/Magento/CatalogInventory/Model/Plugin/AroundProductRepositorySave.php @@ -14,6 +14,7 @@ use Magento\CatalogInventory\Api\StockRegistryInterface; use Magento\Framework\Exception\LocalizedException; use Magento\Store\Model\StoreManagerInterface; +use Magento\Framework\Exception\CouldNotSaveException; class AroundProductRepositorySave { @@ -53,26 +54,16 @@ public function __construct( * * Pay attention that in this code we mostly work with original product object to process stock item data, * not with received result (saved product) because it is already contains new empty stock item object. - * It is a reason why this plugin cannot be rewritten to after plugin + * Now "after plugins" can receive arguments of original method - plugin is rewritten to "after" * - * @param \Magento\Catalog\Api\ProductRepositoryInterface $subject - * @param callable $proceed - * @param \Magento\Catalog\Api\Data\ProductInterface $product - * @param bool $saveOptions - * @return \Magento\Catalog\Api\Data\ProductInterface - * @throws \Magento\Framework\Exception\CouldNotSaveException + * @param ProductRepositoryInterface $subject + * @param ProductInterface $result + * @param ProductInterface $product + * @return ProductInterface + * @throws CouldNotSaveException */ - public function aroundSave( - \Magento\Catalog\Api\ProductRepositoryInterface $subject, - \Closure $proceed, - \Magento\Catalog\Api\Data\ProductInterface $product, - $saveOptions = false - ) { - /** - * @var \Magento\Catalog\Api\Data\ProductInterface $result - */ - $result = $proceed($product, $saveOptions); - + public function afterSave(ProductRepositoryInterface $subject, ProductInterface $result, ProductInterface $product) + { /* @var StockItemInterface $stockItem */ $stockItem = $this->getStockItemToBeUpdated($product); if (null === $stockItem) { diff --git a/app/code/Magento/CatalogInventory/Test/Unit/Model/Plugin/AroundProductRepositorySaveTest.php b/app/code/Magento/CatalogInventory/Test/Unit/Model/Plugin/AroundProductRepositorySaveTest.php index cb26fda9be79e..573e6f924e133 100644 --- a/app/code/Magento/CatalogInventory/Test/Unit/Model/Plugin/AroundProductRepositorySaveTest.php +++ b/app/code/Magento/CatalogInventory/Test/Unit/Model/Plugin/AroundProductRepositorySaveTest.php @@ -23,17 +23,12 @@ class AroundProductRepositorySaveTest extends \PHPUnit_Framework_TestCase { /** - * @var \Closure - */ - private $closure; - - /** - * @var ProductInterface + * @var ProductInterface|\PHPUnit_Framework_MockObject_MockObject */ private $product; /** - * @var ProductInterface + * @var ProductInterface|\PHPUnit_Framework_MockObject_MockObject */ private $savedProduct; @@ -73,7 +68,7 @@ class AroundProductRepositorySaveTest extends \PHPUnit_Framework_TestCase private $stockConfiguration; /** - * @var \Magento\CatalogInventory\Model\Plugin\AroundProductRepositorySave + * @var AroundProductRepositorySave */ private $plugin; @@ -101,10 +96,6 @@ protected function setUp() ->setMethods(['getExtensionAttributes', 'getStoreId']) ->getMockForAbstractClass(); - $this->closure = function () use ($savedProduct) { - return $savedProduct; - }; - $this->productRepository = $this->getMockBuilder(ProductRepositoryInterface::class) ->setMethods(['get']) ->getMockForAbstractClass(); @@ -137,7 +128,7 @@ public function testAroundSaveWhenProductHasNoStockItemNeedingToBeUpdated() $this->stockItem->expects($this->never())->method('setWebsiteId'); // expect that there are no changes to the existing stock item information - $result = $this->plugin->aroundSave($this->productRepository, $this->closure, $this->product); + $result = $this->plugin->afterSave($this->productRepository, $this->savedProduct, $this->product); $this->assertEquals( $this->savedProduct, $result @@ -169,7 +160,7 @@ public function testAroundSaveWhenProductHasNoPersistentStockItemInfo() $this->assertEquals( $newProductMock, - $this->plugin->aroundSave($this->productRepository, $this->closure, $this->product) + $this->plugin->afterSave($this->productRepository, $this->savedProduct, $this->product) ); } @@ -224,7 +215,7 @@ public function testAroundSave() $this->assertEquals( $newProductMock, - $this->plugin->aroundSave($this->productRepository, $this->closure, $this->product) + $this->plugin->afterSave($this->productRepository, $this->savedProduct, $this->product) ); } @@ -259,7 +250,7 @@ public function testAroundSaveWithInvalidStockId() ->method('getStockItem') ->willReturn($this->stockItem); - $this->plugin->aroundSave($this->productRepository, $this->closure, $this->product); + $this->plugin->afterSave($this->productRepository, $this->savedProduct, $this->product); } /** @@ -298,7 +289,7 @@ public function testAroundSaveWithInvalidStockItemId() ->method('getItemId') ->willReturn($stockItemId); - $this->plugin->aroundSave($this->productRepository, $this->closure, $this->product); + $this->plugin->afterSave($this->productRepository, $this->savedProduct, $this->product); } /** @@ -348,6 +339,6 @@ public function testAroundSaveWithNotAssignedStockItemId() ->method('getStockItem') ->willReturn($storedStockItem); - $this->plugin->aroundSave($this->productRepository, $this->closure, $this->product); + $this->plugin->afterSave($this->productRepository, $this->savedProduct, $this->product); } } From c88d148b9a6c2a5a8637b548e98443d047aa3e92 Mon Sep 17 00:00:00 2001 From: Viktor Paladiychuk Date: Mon, 25 Jul 2016 17:57:34 +0300 Subject: [PATCH 16/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- .../App/Action/Plugin/MassactionKeyTest.php | 2 +- .../Magento/Bundle/Model/Plugin/QuoteItem.php | 6 +- .../Test/Unit/Model/Plugin/QuoteItemTest.php | 68 +++---- .../Product/Eav/Plugin/AttributeSet.php | 76 ++++---- .../Product/Plugin/StoreGroupTest.php | 30 ++-- .../Product/Eav/Plugin/AttributeSetTest.php | 168 +++++++++--------- 6 files changed, 175 insertions(+), 175 deletions(-) diff --git a/app/code/Magento/Backend/Test/Unit/App/Action/Plugin/MassactionKeyTest.php b/app/code/Magento/Backend/Test/Unit/App/Action/Plugin/MassactionKeyTest.php index 6cf823821154e..c9451b8adf3e7 100644 --- a/app/code/Magento/Backend/Test/Unit/App/Action/Plugin/MassactionKeyTest.php +++ b/app/code/Magento/Backend/Test/Unit/App/Action/Plugin/MassactionKeyTest.php @@ -33,7 +33,7 @@ protected function setUp() }; $this->subjectMock = $this->getMock('Magento\Backend\App\AbstractAction', [], [], '', false); $this->requestMock = $this->getMockForAbstractClass( - RequestInterface::class, [], '', false, false, true, ['getPost', 'setPostValue'] + RequestInterface::class, [], '', false, false, true, ['getPost', 'setPostValue'] ); $objectManager = new ObjectManager($this); diff --git a/app/code/Magento/Bundle/Model/Plugin/QuoteItem.php b/app/code/Magento/Bundle/Model/Plugin/QuoteItem.php index a398fbb4e6fca..b04659dcb8bb1 100644 --- a/app/code/Magento/Bundle/Model/Plugin/QuoteItem.php +++ b/app/code/Magento/Bundle/Model/Plugin/QuoteItem.php @@ -21,9 +21,9 @@ class QuoteItem * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterConvert( - ToOrderItem $subject, - OrderItemInterface $orderItem, - AbstractItem $item + ToOrderItem $subject, + OrderItemInterface $orderItem, + AbstractItem $item ) { if ($attributes = $item->getProduct()->getCustomOption('bundle_selection_attributes')) { $productOptions = $orderItem->getProductOptions(); diff --git a/app/code/Magento/Bundle/Test/Unit/Model/Plugin/QuoteItemTest.php b/app/code/Magento/Bundle/Test/Unit/Model/Plugin/QuoteItemTest.php index 194458e8cf260..42db60ad650a2 100644 --- a/app/code/Magento/Bundle/Test/Unit/Model/Plugin/QuoteItemTest.php +++ b/app/code/Magento/Bundle/Test/Unit/Model/Plugin/QuoteItemTest.php @@ -12,12 +12,12 @@ class QuoteItemTest extends \PHPUnit_Framework_TestCase { - /** - * @var \PHPUnit_Framework_MockObject_MockObject|Product - */ - private $productMock; + /** + * @var \PHPUnit_Framework_MockObject_MockObject|Product + */ + private $productMock; - /** @var \Magento\Bundle\Model\Plugin\QuoteItem */ + /** @var \Magento\Bundle\Model\Plugin\QuoteItem */ protected $model; /** @var \PHPUnit_Framework_MockObject_MockObject|AbstractItem */ @@ -34,44 +34,44 @@ class QuoteItemTest extends \PHPUnit_Framework_TestCase protected function setUp() { $this->orderItemMock = $this->getMockForAbstractClass( - OrderItemInterface::class, [], '', false, false, true, ['getProductOptions', 'setProductOptions'] + OrderItemInterface::class, [], '', false, false, true, ['getProductOptions', 'setProductOptions'] ); $this->quoteItemMock = $this->getMockForAbstractClass( - AbstractItem::class, [], '', false, false, true, ['getProduct'] + AbstractItem::class, [], '', false, false, true, ['getProduct'] ); $this->subjectMock = $this->getMock(ToOrderItem::class, [], [], '', false); - /** @var \PHPUnit_Framework_MockObject_MockObject|Product $productMock */ - $this->productMock = $this->getMock(Product::class, [], [], '', false); + /** @var \PHPUnit_Framework_MockObject_MockObject|Product $productMock */ + $this->productMock = $this->getMock(Product::class, [], [], '', false); $this->model = new \Magento\Bundle\Model\Plugin\QuoteItem(); } public function testAroundItemToOrderItemPositive() { - $attributeValue = 'test_value'; - $productOptions = [ - 'option_1' => 'value_1', - 'option_2' => 'value_2' - ]; - $expectedOptions = $productOptions + ['bundle_selection_attributes' => $attributeValue]; + $attributeValue = 'test_value'; + $productOptions = [ + 'option_1' => 'value_1', + 'option_2' => 'value_2' + ]; + $expectedOptions = $productOptions + ['bundle_selection_attributes' => $attributeValue]; - $bundleAttribute = $this->getMock( - 'Magento\Catalog\Model\Product\Configuration\Item\Option', - [], - [], - '', - false - ); - $bundleAttribute->expects($this->once()) - ->method('getValue') - ->willReturn($attributeValue); + $bundleAttribute = $this->getMock( + 'Magento\Catalog\Model\Product\Configuration\Item\Option', + [], + [], + '', + false + ); + $bundleAttribute->expects($this->once()) + ->method('getValue') + ->willReturn($attributeValue); - $this->productMock->expects($this->once()) - ->method('getCustomOption') - ->with('bundle_selection_attributes') - ->willReturn($bundleAttribute); - $this->quoteItemMock->expects($this->once())->method('getProduct')->willReturn( $this->productMock ); + $this->productMock->expects($this->once()) + ->method('getCustomOption') + ->with('bundle_selection_attributes') + ->willReturn($bundleAttribute); + $this->quoteItemMock->expects($this->once())->method('getProduct')->willReturn( $this->productMock ); - $this->orderItemMock->expects($this->once())->method('getProductOptions')->willReturn($productOptions); + $this->orderItemMock->expects($this->once())->method('getProductOptions')->willReturn($productOptions); $this->orderItemMock->expects($this->once())->method('setProductOptions')->with($expectedOptions); $orderItem = $this->model->afterConvert($this->subjectMock, $this->orderItemMock, $this->quoteItemMock); @@ -81,11 +81,11 @@ public function testAroundItemToOrderItemPositive() public function testAroundItemToOrderItemNegative() { $this->productMock->expects($this->once()) - ->method('getCustomOption') - ->with('bundle_selection_attributes')->willReturn(false); + ->method('getCustomOption') + ->with('bundle_selection_attributes')->willReturn(false); $this->quoteItemMock->expects($this->once())->method('getProduct') - ->will($this->returnValue($this->productMock)); + ->will($this->returnValue($this->productMock)); $this->orderItemMock->expects($this->never())->method('setProductOptions'); $orderItem = $this->model->afterConvert($this->subjectMock, $this->orderItemMock, $this->quoteItemMock); diff --git a/app/code/Magento/Catalog/Model/Indexer/Product/Eav/Plugin/AttributeSet.php b/app/code/Magento/Catalog/Model/Indexer/Product/Eav/Plugin/AttributeSet.php index 1db2194bd9777..77bd5e1ea7479 100644 --- a/app/code/Magento/Catalog/Model/Indexer/Product/Eav/Plugin/AttributeSet.php +++ b/app/code/Magento/Catalog/Model/Indexer/Product/Eav/Plugin/AttributeSet.php @@ -12,17 +12,17 @@ class AttributeSet { - /** - * @var bool - */ - private $requiresReindex; + /** + * @var bool + */ + private $requiresReindex; - /** - * @var SetFactory - */ - private $setFactory; + /** + * @var SetFactory + */ + private $setFactory; - /** + /** * @var Processor */ protected $_indexerEavProcessor; @@ -59,37 +59,37 @@ public function afterSave(Set $subject, Set $result) return $result; } - /** - * Return attribute set factory - * - * @return SetFactory - * @deprecated - */ + /** + * Return attribute set factory + * + * @return SetFactory + * @deprecated + */ private function getAttributeSetFactory() { - if ($this->setFactory === null) { - $this->setFactory = ObjectManager::getInstance()->get(SetFactory::class); - } - return $this->setFactory; + if ($this->setFactory === null) { + $this->setFactory = ObjectManager::getInstance()->get(SetFactory::class); + } + return $this->setFactory; } - /** - * @param Set $subject - * - * @return bool - */ - public function beforeSave(Set $subject) { - $this->requiresReindex = false; - if ( $subject->getId() ) { - /** @var Set $originalSet */ - $originalSet = $this->getAttributeSetFactory()->create(); - $originalSet->initFromSkeleton($subject->getId()); - $originalAttributeCodes = array_flip( $this->_attributeFilter->filter( $originalSet ) ); - $subjectAttributeCodes = array_flip( $this->_attributeFilter->filter( $subject ) ); - $this->requiresReindex = (bool) count( array_merge( - array_diff_key( $subjectAttributeCodes, $originalAttributeCodes ), - array_diff_key( $originalAttributeCodes, $subjectAttributeCodes ) - ) ); - } - } + /** + * @param Set $subject + * + * @return bool + */ + public function beforeSave(Set $subject) { + $this->requiresReindex = false; + if ( $subject->getId() ) { + /** @var Set $originalSet */ + $originalSet = $this->getAttributeSetFactory()->create(); + $originalSet->initFromSkeleton($subject->getId()); + $originalAttributeCodes = array_flip( $this->_attributeFilter->filter( $originalSet ) ); + $subjectAttributeCodes = array_flip( $this->_attributeFilter->filter( $subject ) ); + $this->requiresReindex = (bool) count( array_merge( + array_diff_key( $subjectAttributeCodes, $originalAttributeCodes ), + array_diff_key( $originalAttributeCodes, $subjectAttributeCodes ) + ) ); + } + } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Product/Plugin/StoreGroupTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Product/Plugin/StoreGroupTest.php index 50f0032f46702..537462588ddda 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Product/Plugin/StoreGroupTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Product/Plugin/StoreGroupTest.php @@ -14,12 +14,12 @@ class StoreGroupTest extends \PHPUnit_Framework_TestCase { - /** - * @var GroupModel|\PHPUnit_Framework_MockObject_MockObject - */ - private $groupMock; + /** + * @var GroupModel|\PHPUnit_Framework_MockObject_MockObject + */ + private $groupMock; - /** + /** * @var \PHPUnit_Framework_MockObject_MockObject|IndexerInterface */ protected $indexerMock; @@ -113,16 +113,16 @@ public function changedDataProvider() public function testAroundSaveWithoutChanges() { - $this->groupMock = $this->getMock( - GroupModel::class, - [ 'dataHasChangedFor', 'isObjectNew', '__wakeup' ], - [ ], - '', - false - ); - $this->groupMock->expects($this->exactly(2)) - ->method('dataHasChangedFor') - ->willReturnMap([['root_category_id', false], ['website_id', false]]); + $this->groupMock = $this->getMock( + GroupModel::class, + [ 'dataHasChangedFor', 'isObjectNew', '__wakeup' ], + [ ], + '', + false + ); + $this->groupMock->expects($this->exactly(2)) + ->method('dataHasChangedFor') + ->willReturnMap([['root_category_id', false], ['website_id', false]]); $this->groupMock->expects($this->never())->method('isObjectNew'); $this->assertSame($this->subject, $this->model->afterSave($this->subject, $this->subject, $this->groupMock )); diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Eav/Plugin/AttributeSetTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Eav/Plugin/AttributeSetTest.php index 58301f8d1a59b..5c0f730e9fea1 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Eav/Plugin/AttributeSetTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Eav/Plugin/AttributeSetTest.php @@ -14,92 +14,92 @@ class AttributeSetTest extends \PHPUnit_Framework_TestCase { - /** - * @var AttributeSet - */ - private $model; - - /** - * @var Processor|\PHPUnit_Framework_MockObject_MockObject - */ - private $eavProcessorMock; - - /** - * @var IndexableAttributeFilter|\PHPUnit_Framework_MockObject_MockObject - */ - private $filterMock; - - /** - * @var Set|\PHPUnit_Framework_MockObject_MockObject - */ - private $subjectMock; - - /** - * @var SetFactory|\PHPUnit_Framework_MockObject_MockObject - */ - private $setFactoryMock; - - /** - * @var Set|\PHPUnit_Framework_MockObject_MockObject - */ - private $originalSetMock; - - public function setUp() - { - $this->filterMock = $this->getMock(IndexableAttributeFilter::class, [], [], '', false); - $this->subjectMock = $this->getMock(Set::class, [], [], '', false); - $this->eavProcessorMock = $this->getMock(Processor::class, [], [], '', false); - $this->setFactoryMock = $this->getMock(SetFactory::class, [], [], '', false); - } - - public function testBeforeSave() - { - $setId = 1; - $this->originalSetMock = $this->getMock(Set::class, [], [], '', false); - $this->originalSetMock->expects($this->once())->method('initFromSkeleton')->with($setId); - - $this->setFactoryMock->expects($this->once())->method('create')->willReturn($this->originalSetMock); - - $this->model = (new ObjectManager($this)) - ->getObject( - AttributeSet::class, - [ - 'indexerEavProcessor' => $this->eavProcessorMock, - 'filter' => $this->filterMock, - 'setFactory' => $this->setFactoryMock - ] - ); - - $this->filterMock->expects($this->exactly(2)) - ->method('filter') - ->willReturnMap( - [ - [$this->originalSetMock, [1, 2, 3]], - [$this->subjectMock, [1, 2]] - ] - ); - - $this->subjectMock->expects($this->exactly(2)) - ->method('getId') - ->willReturn($setId); - - $this->model->beforeSave($this->subjectMock); - } + /** + * @var AttributeSet + */ + private $model; + + /** + * @var Processor|\PHPUnit_Framework_MockObject_MockObject + */ + private $eavProcessorMock; + + /** + * @var IndexableAttributeFilter|\PHPUnit_Framework_MockObject_MockObject + */ + private $filterMock; + + /** + * @var Set|\PHPUnit_Framework_MockObject_MockObject + */ + private $subjectMock; + + /** + * @var SetFactory|\PHPUnit_Framework_MockObject_MockObject + */ + private $setFactoryMock; + + /** + * @var Set|\PHPUnit_Framework_MockObject_MockObject + */ + private $originalSetMock; + + public function setUp() + { + $this->filterMock = $this->getMock(IndexableAttributeFilter::class, [], [], '', false); + $this->subjectMock = $this->getMock(Set::class, [], [], '', false); + $this->eavProcessorMock = $this->getMock(Processor::class, [], [], '', false); + $this->setFactoryMock = $this->getMock(SetFactory::class, [], [], '', false); + } + + public function testBeforeSave() + { + $setId = 1; + $this->originalSetMock = $this->getMock(Set::class, [], [], '', false); + $this->originalSetMock->expects($this->once())->method('initFromSkeleton')->with($setId); + + $this->setFactoryMock->expects($this->once())->method('create')->willReturn($this->originalSetMock); + + $this->model = (new ObjectManager($this)) + ->getObject( + AttributeSet::class, + [ + 'indexerEavProcessor' => $this->eavProcessorMock, + 'filter' => $this->filterMock, + 'setFactory' => $this->setFactoryMock + ] + ); + + $this->filterMock->expects($this->exactly(2)) + ->method('filter') + ->willReturnMap( + [ + [$this->originalSetMock, [1, 2, 3]], + [$this->subjectMock, [1, 2]] + ] + ); + + $this->subjectMock->expects($this->exactly(2)) + ->method('getId') + ->willReturn($setId); + + $this->model->beforeSave($this->subjectMock); + } public function testAfterSave() { - $this->eavProcessorMock->expects($this->once())->method('markIndexerAsInvalid'); - - $this->model = (new ObjectManager($this)) - ->getObject( - AttributeSet::class, - [ - 'indexerEavProcessor' => $this->eavProcessorMock, - 'filter' => $this->filterMock, - 'requiresReindex' => true - ] - ); - - $this->assertSame($this->subjectMock, $this->model->afterSave($this->subjectMock, $this->subjectMock)); + $this->eavProcessorMock->expects($this->once())->method('markIndexerAsInvalid'); + + $this->model = (new ObjectManager($this)) + ->getObject( + AttributeSet::class, + [ + 'indexerEavProcessor' => $this->eavProcessorMock, + 'filter' => $this->filterMock, + 'requiresReindex' => true + ] + ); + + $this->assertSame($this->subjectMock, $this->model->afterSave($this->subjectMock, $this->subjectMock)); } } From 26f12076dfb101a7594bcf259175bc8fe26a1c96 Mon Sep 17 00:00:00 2001 From: Dmytro Poperechnyy Date: Mon, 25 Jul 2016 18:06:54 +0300 Subject: [PATCH 17/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- .../Indexer/Product/Save/ApplyRules.php | 8 +- .../Indexer/Product/Save/ApplyRulesTest.php | 73 +++++++++++++++++++ 2 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 app/code/Magento/CatalogRule/Test/Unit/Plugin/Indexer/Product/Save/ApplyRulesTest.php diff --git a/app/code/Magento/CatalogRule/Plugin/Indexer/Product/Save/ApplyRules.php b/app/code/Magento/CatalogRule/Plugin/Indexer/Product/Save/ApplyRules.php index 2f007a83a5fe1..00cdfb5aaaf79 100644 --- a/app/code/Magento/CatalogRule/Plugin/Indexer/Product/Save/ApplyRules.php +++ b/app/code/Magento/CatalogRule/Plugin/Indexer/Product/Save/ApplyRules.php @@ -26,18 +26,16 @@ public function __construct(ProductRuleProcessor $productRuleProcessor) * Apply catalog rules after product resource model save * * @param \Magento\Catalog\Model\ResourceModel\Product $subject - * @param callable $proceed + * @param \Magento\Catalog\Model\ResourceModel\Product $productResource * @param \Magento\Framework\Model\AbstractModel $product * @return \Magento\Catalog\Model\ResourceModel\Product - * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundSave( + public function afterSave( \Magento\Catalog\Model\ResourceModel\Product $subject, - callable $proceed, + \Magento\Catalog\Model\ResourceModel\Product $productResource, \Magento\Framework\Model\AbstractModel $product ) { - $productResource = $proceed($product); if (!$product->getIsMassupdate()) { $this->productRuleProcessor->reindexRow($product->getId()); } diff --git a/app/code/Magento/CatalogRule/Test/Unit/Plugin/Indexer/Product/Save/ApplyRulesTest.php b/app/code/Magento/CatalogRule/Test/Unit/Plugin/Indexer/Product/Save/ApplyRulesTest.php new file mode 100644 index 0000000000000..339311c674a94 --- /dev/null +++ b/app/code/Magento/CatalogRule/Test/Unit/Plugin/Indexer/Product/Save/ApplyRulesTest.php @@ -0,0 +1,73 @@ +productRuleProcessor = $this->getMockBuilder( + \Magento\CatalogRule\Model\Indexer\Product\ProductRuleProcessor::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->subject = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->model = $this->getMockForAbstractClass( + \Magento\Framework\Model\AbstractModel::class, + [], + '', + false, + true, + true, + ['getIsMassupdate', 'getId'] + ); + + $this->plugin = (new ObjectManager($this))->getObject( + \Magento\CatalogRule\Plugin\Indexer\Product\Save\ApplyRules::class, + [ + 'productRuleProcessor' => $this->productRuleProcessor, + ] + ); + } + + public function testAfterSave() + { + $this->model->expects($this->once())->method('getIsMassupdate')->willReturn(null); + $this->model->expects($this->once())->method('getId')->willReturn(1); + + $this->productRuleProcessor->expects($this->once())->method('reindexRow')->willReturnSelf(); + + $this->assertEquals( + $this->subject, + $this->plugin->afterSave($this->subject, $this->subject, $this->model) + ); + } +} From 04449204f7d7e81b6979507952bfd9ee78466c89 Mon Sep 17 00:00:00 2001 From: Yuri Kovsher Date: Tue, 26 Jul 2016 11:31:23 +0300 Subject: [PATCH 18/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- .../Product/Save/ApplyRulesAfterReindex.php | 10 +-- .../Save/ApplyRulesAfterReindexTest.php | 64 +++++++++++++++++++ 2 files changed, 67 insertions(+), 7 deletions(-) create mode 100644 app/code/Magento/CatalogRule/Test/Unit/Plugin/Indexer/Product/Save/ApplyRulesAfterReindexTest.php diff --git a/app/code/Magento/CatalogRule/Plugin/Indexer/Product/Save/ApplyRulesAfterReindex.php b/app/code/Magento/CatalogRule/Plugin/Indexer/Product/Save/ApplyRulesAfterReindex.php index af00ef3aada02..d249136298964 100644 --- a/app/code/Magento/CatalogRule/Plugin/Indexer/Product/Save/ApplyRulesAfterReindex.php +++ b/app/code/Magento/CatalogRule/Plugin/Indexer/Product/Save/ApplyRulesAfterReindex.php @@ -26,15 +26,11 @@ public function __construct(ProductRuleProcessor $productRuleProcessor) * Apply catalog rules after product resource model save * * @param \Magento\Catalog\Model\Product $subject - * @param callable $proceed - * @return \Magento\Catalog\Model\Product + * @return void */ - public function aroundReindex( - \Magento\Catalog\Model\Product $subject, - callable $proceed + public function afterReindex( + \Magento\Catalog\Model\Product $subject ) { - $proceed(); $this->productRuleProcessor->reindexRow($subject->getId()); - return; } } diff --git a/app/code/Magento/CatalogRule/Test/Unit/Plugin/Indexer/Product/Save/ApplyRulesAfterReindexTest.php b/app/code/Magento/CatalogRule/Test/Unit/Plugin/Indexer/Product/Save/ApplyRulesAfterReindexTest.php new file mode 100644 index 0000000000000..4d2068c9f5265 --- /dev/null +++ b/app/code/Magento/CatalogRule/Test/Unit/Plugin/Indexer/Product/Save/ApplyRulesAfterReindexTest.php @@ -0,0 +1,64 @@ +productRuleProcessorMock = $this->getMockBuilder(ProductRuleProcessor::class) + ->disableOriginalConstructor() + ->getMock(); + $this->subjectMock = $this->getMockBuilder(Product::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->objectManagerHelper = new ObjectManagerHelper($this); + $this->plugin = $this->objectManagerHelper->getObject( + ApplyRulesAfterReindex::class, + ['productRuleProcessor' => $this->productRuleProcessorMock] + ); + } + + public function testAfterReindex() + { + $id = 'test_id'; + + $this->subjectMock->expects(static::any()) + ->method('getId') + ->willReturn($id); + $this->productRuleProcessorMock->expects(static::once()) + ->method('reindexRow') + ->with($id, false); + + $this->plugin->afterReindex($this->subjectMock); + } +} From 9579d6797ef42337a64f23fbac57322a15514978 Mon Sep 17 00:00:00 2001 From: Yuri Kovsher Date: Tue, 26 Jul 2016 13:08:52 +0300 Subject: [PATCH 19/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- .../Block/Plugin/FrontTabPlugin.php | 26 ++-- .../Unit/Block/Plugin/FrontTabPluginTest.php | 128 ++++++++++++++++++ 2 files changed, 136 insertions(+), 18 deletions(-) create mode 100644 app/code/Magento/CatalogSearch/Test/Unit/Block/Plugin/FrontTabPluginTest.php diff --git a/app/code/Magento/CatalogSearch/Block/Plugin/FrontTabPlugin.php b/app/code/Magento/CatalogSearch/Block/Plugin/FrontTabPlugin.php index de344754d16fe..4348ecfb6ec0d 100644 --- a/app/code/Magento/CatalogSearch/Block/Plugin/FrontTabPlugin.php +++ b/app/code/Magento/CatalogSearch/Block/Plugin/FrontTabPlugin.php @@ -5,7 +5,7 @@ */ namespace Magento\CatalogSearch\Block\Plugin; -use Magento\Catalog\Block\Adminhtml\Product\Attribute\Edit\Tab\Front; +use Magento\Catalog\Block\Adminhtml\Product\Attribute\Edit\Tab\Front as ProductAttributeFrontTabBlock; use Magento\CatalogSearch\Model\Source\Weight; use Magento\Framework\Data\Form; use Magento\Framework\Data\Form\Element\Fieldset; @@ -26,15 +26,14 @@ public function __construct(Weight $weightSource) } /** - * @param Front $subject - * @param callable $proceed + * Add Search Weight field + * + * @param ProductAttributeFrontTabBlock $subject * @param Form $form - * @return Front - * @SuppressWarnings(PHPMD.UnusedFormalParameter) + * @return void */ - public function aroundSetForm(Front $subject, \Closure $proceed, Form $form) + public function beforeSetForm(ProductAttributeFrontTabBlock $subject, Form $form) { - $block = $proceed($form); /** @var Fieldset $fieldset */ $fieldset = $form->getElement('front_fieldset'); $fieldset->addField( @@ -47,17 +46,8 @@ public function aroundSetForm(Front $subject, \Closure $proceed, Form $form) ], 'is_searchable' ); - $subject->getChildBlock('form_after') - ->addFieldMap( - 'search_weight', - 'search_weight' - ) - ->addFieldDependence( - 'search_weight', - 'searchable', - '1' - ); - return $block; + ->addFieldMap('search_weight', 'search_weight') + ->addFieldDependence('search_weight', 'searchable', '1'); } } diff --git a/app/code/Magento/CatalogSearch/Test/Unit/Block/Plugin/FrontTabPluginTest.php b/app/code/Magento/CatalogSearch/Test/Unit/Block/Plugin/FrontTabPluginTest.php new file mode 100644 index 0000000000000..636795b41c219 --- /dev/null +++ b/app/code/Magento/CatalogSearch/Test/Unit/Block/Plugin/FrontTabPluginTest.php @@ -0,0 +1,128 @@ +weightSourceMock = $this->getMockBuilder(WeightSource::class) + ->disableOriginalConstructor() + ->getMock(); + $this->subjectMock = $this->getMockBuilder(ProductAttributeFrontTabBlock::class) + ->disableOriginalConstructor() + ->getMock(); + $this->formMock = $this->getMockBuilder(Form::class) + ->disableOriginalConstructor() + ->getMock(); + $this->fieldsetMock = $this->getMockBuilder(Fieldset::class) + ->disableOriginalConstructor() + ->getMock(); + $this->childElementMock = $this->getMockBuilder(AbstractElement::class) + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + $this->childBlockMock = $this->getMockBuilder(AbstractBlock::class) + ->disableOriginalConstructor() + ->setMethods(['addFieldMap', 'addFieldDependence']) + ->getMockForAbstractClass(); + + $this->objectManagerHelper = new ObjectManagerHelper($this); + $this->plugin = $this->objectManagerHelper->getObject( + FrontTabPlugin::class, + ['weightSource' => $this->weightSourceMock] + ); + } + + public function testBeforeSetForm() + { + $weightOptions = [1 => '1', 2 => '2']; + + $this->formMock->expects(static::any()) + ->method('getElement') + ->with('front_fieldset') + ->willReturn($this->fieldsetMock); + $this->weightSourceMock->expects(static::any()) + ->method('getOptions') + ->willReturn($weightOptions); + $this->fieldsetMock->expects(static::once()) + ->method('addField') + ->with( + 'search_weight', + 'select', + [ + 'name' => 'search_weight', + 'label' => __('Search Weight'), + 'values' => $weightOptions + ], + 'is_searchable', + false + ) + ->willReturn($this->childElementMock); + $this->subjectMock->expects(static::any()) + ->method('getChildBlock') + ->with('form_after') + ->willReturn($this->childBlockMock); + $this->childBlockMock->expects(static::once()) + ->method('addFieldMap') + ->with('search_weight', 'search_weight') + ->willReturnSelf(); + $this->childBlockMock->expects(static::once()) + ->method('addFieldDependence') + ->with('search_weight', 'searchable', '1') + ->willReturnSelf(); + + $this->plugin->beforeSetForm($this->subjectMock, $this->formMock); + } +} From 6990ab1733cdb9b0993424f4337836a3f6428ebe Mon Sep 17 00:00:00 2001 From: Yuri Kovsher Date: Tue, 26 Jul 2016 15:39:21 +0300 Subject: [PATCH 20/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- .../Fulltext/Plugin/Product/Action.php | 31 +++++-------- .../Fulltext/Plugin/Product/ActionTest.php | 44 ++++--------------- 2 files changed, 18 insertions(+), 57 deletions(-) diff --git a/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Product/Action.php b/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Product/Action.php index 5c43eb673591a..f3925b825fee7 100644 --- a/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Product/Action.php +++ b/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Product/Action.php @@ -14,45 +14,34 @@ class Action extends AbstractPlugin * Reindex on product attribute mass change * * @param \Magento\Catalog\Model\Product\Action $subject - * @param \Closure $closure + * @param \Magento\Catalog\Model\Product\Action $action * @param array $productIds - * @param array $attrData - * @param int $storeId * @return \Magento\Catalog\Model\Product\Action * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundUpdateAttributes( + public function afterUpdateAttributes( \Magento\Catalog\Model\Product\Action $subject, - \Closure $closure, - array $productIds, - array $attrData, - $storeId + \Magento\Catalog\Model\Product\Action $action, + array $productIds ) { - $result = $closure($productIds, $attrData, $storeId); $this->reindexList(array_unique($productIds)); - return $result; + return $action; } /** * Reindex on product websites mass change * * @param \Magento\Catalog\Model\Product\Action $subject - * @param \Closure $closure + * @param null $result * @param array $productIds - * @param array $websiteIds - * @param string $type - * @return \Magento\Catalog\Model\Product\Action + * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundUpdateWebsites( + public function afterUpdateWebsites( \Magento\Catalog\Model\Product\Action $subject, - \Closure $closure, - array $productIds, - array $websiteIds, - $type + $result, + array $productIds ) { - $result = $closure($productIds, $websiteIds, $type); $this->reindexList(array_unique($productIds)); - return $result; } } diff --git a/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/Product/ActionTest.php b/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/Product/ActionTest.php index 10defac381320..b5bc0854f1678 100644 --- a/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/Product/ActionTest.php +++ b/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/Product/ActionTest.php @@ -54,74 +54,46 @@ protected function setUp() $this->model = new Action($this->indexerRegistryMock); } - public function testAroundUpdateAttributesNonScheduled() + public function testAfterUpdateAttributesNonScheduled() { $this->indexerMock->expects($this->once())->method('isScheduled')->will($this->returnValue(false)); $this->indexerMock->expects($this->once())->method('reindexList')->with([1, 2, 3]); $this->prepareIndexer(); - $closureMock = function ($productIds, $attrData, $storeId) { - $this->assertEquals([1, 2, 3], $productIds); - $this->assertEquals([4, 5, 6], $attrData); - $this->assertEquals(1, $storeId); - return $this->subjectMock; - }; - $this->assertEquals( $this->subjectMock, - $this->model->aroundUpdateAttributes($this->subjectMock, $closureMock, [1, 2, 3], [4, 5, 6], 1) + $this->model->afterUpdateAttributes($this->subjectMock, $this->subjectMock, [1, 2, 3]) ); } - public function testAroundUpdateAttributesScheduled() + public function testAfterUpdateAttributesScheduled() { $this->indexerMock->expects($this->once())->method('isScheduled')->will($this->returnValue(true)); $this->indexerMock->expects($this->never())->method('reindexList'); $this->prepareIndexer(); - $closureMock = function ($productIds, $attrData, $storeId) { - $this->assertEquals([1, 2, 3], $productIds); - $this->assertEquals([4, 5, 6], $attrData); - $this->assertEquals(1, $storeId); - return $this->subjectMock; - }; - $this->assertEquals( $this->subjectMock, - $this->model->aroundUpdateAttributes($this->subjectMock, $closureMock, [1, 2, 3], [4, 5, 6], 1) + $this->model->afterUpdateAttributes($this->subjectMock, $this->subjectMock, [1, 2, 3]) ); } - public function testAroundUpdateWebsitesNonScheduled() + public function testAfterUpdateWebsitesNonScheduled() { $this->indexerMock->expects($this->once())->method('isScheduled')->will($this->returnValue(false)); $this->indexerMock->expects($this->once())->method('reindexList')->with([1, 2, 3]); $this->prepareIndexer(); - $closureMock = function ($productIds, $websiteIds, $type) { - $this->assertEquals([1, 2, 3], $productIds); - $this->assertEquals([4, 5, 6], $websiteIds); - $this->assertEquals('type', $type); - return $this->subjectMock; - }; - - $this->model->aroundUpdateWebsites($this->subjectMock, $closureMock, [1, 2, 3], [4, 5, 6], 'type'); + $this->model->afterUpdateWebsites($this->subjectMock, $this->subjectMock, [1, 2, 3]); } - public function testAroundUpdateWebsitesScheduled() + public function testAfterUpdateWebsitesScheduled() { $this->indexerMock->expects($this->once())->method('isScheduled')->will($this->returnValue(true)); $this->indexerMock->expects($this->never())->method('reindexList'); $this->prepareIndexer(); - $closureMock = function ($productIds, $websiteIds, $type) { - $this->assertEquals([1, 2, 3], $productIds); - $this->assertEquals([4, 5, 6], $websiteIds); - $this->assertEquals('type', $type); - return $this->subjectMock; - }; - - $this->model->aroundUpdateWebsites($this->subjectMock, $closureMock, [1, 2, 3], [4, 5, 6], 'type'); + $this->model->afterUpdateWebsites($this->subjectMock, $this->subjectMock, [1, 2, 3]); } protected function prepareIndexer() From 79cdbb196087460975cd34ee0a2729ab20556d40 Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Tue, 26 Jul 2016 16:03:59 +0300 Subject: [PATCH 21/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- .../Indexer/Fulltext/Plugin/Attribute.php | 80 ++++++-- .../Indexer/Fulltext/Plugin/AttributeTest.php | 183 ++++++++++-------- 2 files changed, 162 insertions(+), 101 deletions(-) diff --git a/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Attribute.php b/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Attribute.php index e358fe2ca5a28..177382724dff8 100644 --- a/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Attribute.php +++ b/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Attribute.php @@ -14,6 +14,22 @@ class Attribute extends AbstractPlugin */ private $config; + /** + * @var boolean + */ + private $deleteNeedInvalidation; + + /** + * @var boolean + */ + private $saveNeedInvalidation; + + /** + * @var boolean + */ + private $saveIsNew; + + /** * @param \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry * @param \Magento\Framework\Search\Request\Config $config @@ -27,32 +43,44 @@ public function __construct( } /** - * Invalidate indexer on attribute save (searchable flag change) + * Check for needed indexer invalidation on attribute save (searchable flag change) * * @param \Magento\Catalog\Model\ResourceModel\Attribute $subject - * @param \Closure $proceed * @param \Magento\Framework\Model\AbstractModel $attribute * - * @return \Magento\Catalog\Model\ResourceModel\Attribute + * @return array * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundSave( + public function beforeSave( \Magento\Catalog\Model\ResourceModel\Attribute $subject, - \Closure $proceed, \Magento\Framework\Model\AbstractModel $attribute ) { - $isNew = $attribute->isObjectNew(); - $needInvalidation = ( + $this->saveIsNew = $attribute->isObjectNew(); + $this->saveNeedInvalidation = ( $attribute->dataHasChangedFor('is_searchable') || $attribute->dataHasChangedFor('is_filterable') || $attribute->dataHasChangedFor('is_visible_in_advanced_search') - ) && !$isNew; + ) && ! $this->saveIsNew; + return [$attribute]; + } - $result = $proceed($attribute); - if ($needInvalidation) { + /** + * Invalidate indexer on attribute save (searchable flag change) + * + * @param \Magento\Framework\Model\AbstractModel $subject + * @param \Magento\Framework\Model\AbstractModel $result + * + * @return \Magento\Framework\Model\AbstractModel + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function afterSave( + \Magento\Framework\Model\AbstractModel $subject, + \Magento\Framework\Model\AbstractModel $result + ) { + if ($this->saveNeedInvalidation) { $this->indexerRegistry->get(Fulltext::INDEXER_ID)->invalidate(); } - if ($isNew || $needInvalidation) { + if ($this->saveIsNew || $this->saveNeedInvalidation) { $this->config->reset(); } @@ -60,26 +88,38 @@ public function aroundSave( } /** - * Invalidate indexer on searchable attribute delete + * Check for needed indexer invalidation on searchable attribute delete * * @param \Magento\Catalog\Model\ResourceModel\Attribute $subject - * @param \Closure $proceed * @param \Magento\Framework\Model\AbstractModel $attribute * - * @return \Magento\Catalog\Model\ResourceModel\Attribute + * @return array * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundDelete( + public function beforeDelete( \Magento\Catalog\Model\ResourceModel\Attribute $subject, - \Closure $proceed, \Magento\Framework\Model\AbstractModel $attribute ) { - $needInvalidation = !$attribute->isObjectNew() && $attribute->getIsSearchable(); - $result = $proceed($attribute); - if ($needInvalidation) { + $this->deleteNeedInvalidation = !$attribute->isObjectNew() && $attribute->getIsSearchable(); + return [$attribute]; + } + + /** + * Invalidate indexer on searchable attribute delete + * + * @param \Magento\Framework\Model\AbstractModel $subject + * @param \Magento\Framework\Model\AbstractModel $result + * + * @return \Magento\Framework\Model\AbstractModel + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function afterDelete( + \Magento\Framework\Model\AbstractModel $subject, + \Magento\Framework\Model\AbstractModel $result + ) { + if ($this->deleteNeedInvalidation) { $this->indexerRegistry->get(Fulltext::INDEXER_ID)->invalidate(); } - return $result; } } diff --git a/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/AttributeTest.php b/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/AttributeTest.php index 78acc3cc513df..76a8008c216e4 100644 --- a/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/AttributeTest.php +++ b/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/AttributeTest.php @@ -5,7 +5,8 @@ */ namespace Magento\CatalogSearch\Test\Unit\Model\Indexer\Fulltext\Plugin; -use \Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin\Attribute; +use Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin\Attribute; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; class AttributeTest extends \PHPUnit_Framework_TestCase { @@ -24,14 +25,30 @@ class AttributeTest extends \PHPUnit_Framework_TestCase */ protected $indexerRegistryMock; + /** + * @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute|\PHPUnit_Framework_MockObject_MockObject + */ + private $attributeMock; + /** * @var Attribute */ protected $model; + /** + * @var ObjectManager + */ + private $objectManager; + + /** + * @var \Magento\Framework\Search\Request\Config|\PHPUnit_Framework_MockObject_MockObject + */ + private $config; + protected function setUp() { - $this->subjectMock = $this->getMock('Magento\Catalog\Model\ResourceModel\Attribute', [], [], '', false); + $this->objectManager = new ObjectManager($this); + $this->subjectMock = $this->getMock(\Magento\Catalog\Model\ResourceModel\Attribute::class, [], [], '', false); $this->indexerMock = $this->getMockForAbstractClass( 'Magento\Framework\Indexer\IndexerInterface', [], @@ -48,113 +65,117 @@ protected function setUp() '', false ); + $this->attributeMock = $this->getMock( + \Magento\Catalog\Model\ResourceModel\Eav\Attribute::class, + ['dataHasChangedFor', 'isObjectNew', 'getIsSearchable'], + [], + '', + false + ); $this->config = $this->getMockBuilder(\Magento\Framework\Search\Request\Config::class) ->disableOriginalConstructor() + ->setMethods(['reset']) ->getMock(); - $this->model = new Attribute($this->indexerRegistryMock, $this->config); + $this->model = $this->objectManager->getObject( + Attribute::class, + [ + 'indexerRegistry' => $this->indexerRegistryMock, + 'config' => $this->config + ] + ); } - /** - * @param bool $isObjectNew - * @param bool $isSearchableChanged - * @param int $invalidateCounter - * @return void - * @dataProvider aroundSaveDataProvider - */ - public function testAroundSave($isObjectNew, $isSearchableChanged, $invalidateCounter) + public function testBeforeSave() { - $attributeMock = $this->getMock( - '\Magento\Catalog\Model\ResourceModel\Eav\Attribute', - ['dataHasChangedFor', 'isObjectNew', '__wakeup'], - [], - '', - false - ); - $attributeMock->expects($this->any()) + $this->attributeMock->expects($this->once()) + ->method('isObjectNew') + ->willReturn(true); + $this->attributeMock->expects($this->once()) ->method('dataHasChangedFor') - ->will($this->returnValue($isSearchableChanged)); + ->with('is_searchable') + ->willReturn(true); + $this->assertEquals( + [$this->attributeMock], + $this->model->beforeSave($this->subjectMock, $this->attributeMock) + ); + } - $attributeMock->expects($this->any())->method('isObjectNew')->will($this->returnValue($isObjectNew)); + public function testAfterSaveNoInvalidation() + { + $this->assertEquals( + $this->attributeMock, + $this->model->afterSave($this->attributeMock, $this->attributeMock) + ); + } - $closureMock = function (\Magento\Catalog\Model\ResourceModel\Eav\Attribute $object) use ($attributeMock) { - $this->assertEquals($object, $attributeMock); - return $this->subjectMock; - }; + public function testAfterSaveWithInvalidation() + { + $model = $this->objectManager->getObject( + Attribute::class, + [ + 'indexerRegistry' => $this->indexerRegistryMock, + 'config' => $this->config, + 'saveNeedInvalidation' => true, + 'saveIsNew' => true + ] + ); - $this->indexerMock->expects($this->exactly($invalidateCounter))->method('invalidate'); - $this->prepareIndexer($invalidateCounter); + $this->indexerMock->expects($this->once())->method('invalidate'); + $this->prepareIndexer(); + $this->config->expects($this->once()) + ->method('reset'); $this->assertEquals( - $this->subjectMock, - $this->model->aroundSave($this->subjectMock, $closureMock, $attributeMock) + $this->attributeMock, + $model->afterSave($this->attributeMock, $this->attributeMock) ); } - /** - * @return array - */ - public function aroundSaveDataProvider() + public function testBeforeDelete() { - return [ - [false, false, 0], - [false, true, 1], - [true, false, 0], - [true, true, 0], - ]; + $this->attributeMock->expects($this->once()) + ->method('isObjectNew') + ->willReturn(false); + $this->attributeMock->expects($this->once()) + ->method('getIsSearchable') + ->willReturn(true); + $this->assertEquals( + [$this->attributeMock], + $this->model->beforeDelete($this->subjectMock, $this->attributeMock) + ); } - /** - * @param bool $isObjectNew - * @param bool $isSearchable - * @param int $invalidateCounter - * @return void - * @dataProvider aroundDeleteDataProvider - */ - public function testAroundDelete($isObjectNew, $isSearchable, $invalidateCounter) + public function testAfterDeleteNoInvalidation() { - $attributeMock = $this->getMock( - '\Magento\Catalog\Model\ResourceModel\Eav\Attribute', - ['getIsSearchable', 'isObjectNew', '__wakeup'], - [], - '', - false + $this->assertEquals( + $this->attributeMock, + $this->model->afterDelete($this->attributeMock, $this->attributeMock) ); - $attributeMock->expects($this->any())->method('getIsSearchable')->will($this->returnValue($isSearchable)); - $attributeMock->expects($this->once())->method('isObjectNew')->will($this->returnValue($isObjectNew)); + } - $closureMock = function (\Magento\Catalog\Model\ResourceModel\Eav\Attribute $object) use ($attributeMock) { - $this->assertEquals($object, $attributeMock); - return $this->subjectMock; - }; + public function testAfterDeleteWithInvalidation() + { + $model = $this->objectManager->getObject( + Attribute::class, + [ + 'indexerRegistry' => $this->indexerRegistryMock, + 'config' => $this->config, + 'deleteNeedInvalidation' => true + ] + ); - $this->indexerMock->expects($this->exactly($invalidateCounter))->method('invalidate'); - $this->prepareIndexer($invalidateCounter); + $this->indexerMock->expects($this->once())->method('invalidate'); + $this->prepareIndexer(); $this->assertEquals( - $this->subjectMock, - $this->model->aroundDelete($this->subjectMock, $closureMock, $attributeMock) + $this->attributeMock, + $model->afterDelete($this->attributeMock, $this->attributeMock) ); } - /** - * @return array - */ - public function aroundDeleteDataProvider() - { - return [ - [false, false, 0], - [false, true, 1], - [true, false, 0], - [true, true, 0], - ]; - } - - /** - * @param $invalidateCounter - */ - protected function prepareIndexer($invalidateCounter) + private function prepareIndexer() { - $this->indexerRegistryMock->expects($this->exactly($invalidateCounter)) + $this->indexerRegistryMock->expects($this->once()) ->method('get') ->with(\Magento\CatalogSearch\Model\Indexer\Fulltext::INDEXER_ID) ->will($this->returnValue($this->indexerMock)); From aeb9f2748a089a9472a145188f2fe86ff25aaa53 Mon Sep 17 00:00:00 2001 From: Yuri Kovsher Date: Tue, 26 Jul 2016 17:06:16 +0300 Subject: [PATCH 22/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- .../Indexer/Fulltext/Plugin/Store/Group.php | 31 ++++++++++++++----- .../Fulltext/Plugin/Store/GroupTest.php | 10 ++---- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Store/Group.php b/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Store/Group.php index 0836760a2f589..22ddb2cc1ed2f 100644 --- a/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Store/Group.php +++ b/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Store/Group.php @@ -11,23 +11,38 @@ class Group extends AbstractPlugin { /** - * Invalidate indexer on store group save + * @var bool + */ + private $needInvalidation; + + /** + * Check if indexer requires invalidation after store group save * * @param \Magento\Store\Model\ResourceModel\Group $subject - * @param \Closure $proceed * @param \Magento\Framework\Model\AbstractModel $group + * @return void + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function beforeSave( + \Magento\Store\Model\ResourceModel\Group $subject, + \Magento\Framework\Model\AbstractModel $group + ) { + $this->needInvalidation = !$group->isObjectNew() && $group->dataHasChangedFor('website_id'); + } + + /** + * Invalidate indexer on store group save * + * @param \Magento\Store\Model\ResourceModel\Group $subject + * @param \Magento\Store\Model\ResourceModel\Group $result * @return \Magento\Store\Model\ResourceModel\Group * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundSave( + public function afterSave( \Magento\Store\Model\ResourceModel\Group $subject, - \Closure $proceed, - \Magento\Framework\Model\AbstractModel $group + \Magento\Store\Model\ResourceModel\Group $result ) { - $needInvalidation = !$group->isObjectNew() && $group->dataHasChangedFor('website_id'); - $result = $proceed($group); - if ($needInvalidation) { + if ($this->needInvalidation) { $this->indexerRegistry->get(Fulltext::INDEXER_ID)->invalidate(); } diff --git a/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/Store/GroupTest.php b/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/Store/GroupTest.php index 488596e5c003e..8d49a35953f78 100644 --- a/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/Store/GroupTest.php +++ b/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/Store/GroupTest.php @@ -58,7 +58,7 @@ protected function setUp() * @return void * @dataProvider aroundSaveDataProvider */ - public function testAroundSave($isObjectNew, $websiteChanged, $invalidateCounter) + public function testBeforeAfterSave($isObjectNew, $websiteChanged, $invalidateCounter) { $groupMock = $this->getMock( 'Magento\Store\Model\Group', @@ -73,17 +73,13 @@ public function testAroundSave($isObjectNew, $websiteChanged, $invalidateCounter ->will($this->returnValue($websiteChanged)); $groupMock->expects($this->once())->method('isObjectNew')->will($this->returnValue($isObjectNew)); - $closureMock = function (\Magento\Store\Model\Group $object) use ($groupMock) { - $this->assertEquals($object, $groupMock); - return $this->subjectMock; - }; - $this->indexerMock->expects($this->exactly($invalidateCounter))->method('invalidate'); $this->prepareIndexer($invalidateCounter); + $this->model->beforeSave($this->subjectMock, $groupMock); $this->assertEquals( $this->subjectMock, - $this->model->aroundSave($this->subjectMock, $closureMock, $groupMock) + $this->model->afterSave($this->subjectMock, $this->subjectMock) ); } From d346588c11222a78b783e0d699d42716bbedb4ac Mon Sep 17 00:00:00 2001 From: Yuri Kovsher Date: Tue, 26 Jul 2016 17:25:05 +0300 Subject: [PATCH 23/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- .../Indexer/Fulltext/Plugin/Store/View.php | 34 ++++++++++++++----- .../Fulltext/Plugin/Store/GroupTest.php | 4 +-- .../Fulltext/Plugin/Store/ViewTest.php | 9 ++--- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Store/View.php b/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Store/View.php index 128a1c07e93ff..5df3165111aad 100644 --- a/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Store/View.php +++ b/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Store/View.php @@ -11,25 +11,43 @@ class View extends AbstractPlugin { /** - * Invalidate indexer on store view save + * @var bool + */ + private $needInvalidation; + + /** + * Check if indexer requires invalidation after store view save * * @param \Magento\Store\Model\ResourceModel\Store $subject - * @param \Closure $proceed * @param \Magento\Framework\Model\AbstractModel $store + * @return void * - * @return \Magento\Store\Model\ResourceModel\Store * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundSave( + public function beforeSave( \Magento\Store\Model\ResourceModel\Store $subject, - \Closure $proceed, \Magento\Framework\Model\AbstractModel $store ) { - $needInvalidation = $store->isObjectNew(); - $result = $proceed($store); - if ($needInvalidation) { + $this->needInvalidation = $store->isObjectNew(); + } + + /** + * Invalidate indexer on store view save + * + * @param \Magento\Store\Model\ResourceModel\Store $subject + * @param \Magento\Store\Model\ResourceModel\Store $result + * @return \Magento\Store\Model\ResourceModel\Store + * + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function afterSave( + \Magento\Store\Model\ResourceModel\Store $subject, + \Magento\Store\Model\ResourceModel\Store $result + ) { + if ($this->needInvalidation) { $this->indexerRegistry->get(Fulltext::INDEXER_ID)->invalidate(); } + return $result; } diff --git a/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/Store/GroupTest.php b/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/Store/GroupTest.php index 8d49a35953f78..4d0a67c7e5a32 100644 --- a/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/Store/GroupTest.php +++ b/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/Store/GroupTest.php @@ -56,7 +56,7 @@ protected function setUp() * @param bool $websiteChanged * @param int $invalidateCounter * @return void - * @dataProvider aroundSaveDataProvider + * @dataProvider beforeAfterSaveDataProvider */ public function testBeforeAfterSave($isObjectNew, $websiteChanged, $invalidateCounter) { @@ -86,7 +86,7 @@ public function testBeforeAfterSave($isObjectNew, $websiteChanged, $invalidateCo /** * @return array */ - public function aroundSaveDataProvider() + public function beforeAfterSaveDataProvider() { return [ [false, false, 0], diff --git a/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/Store/ViewTest.php b/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/Store/ViewTest.php index 326ca3abbfaca..b4e41a6191783 100644 --- a/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/Store/ViewTest.php +++ b/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/Store/ViewTest.php @@ -55,9 +55,9 @@ protected function setUp() * @param bool $isObjectNew * @param int $invalidateCounter * @return void - * @dataProvider aroundSaveDataProvider + * @dataProvider beforeAfterSaveDataProvider */ - public function testAroundSave($isObjectNew, $invalidateCounter) + public function testBeforeAfterSave($isObjectNew, $invalidateCounter) { $viewMock = $this->getMock( 'Magento\Store\Model\Store', @@ -76,16 +76,17 @@ public function testAroundSave($isObjectNew, $invalidateCounter) $this->indexerMock->expects($this->exactly($invalidateCounter))->method('invalidate'); $this->prepareIndexer($invalidateCounter); + $this->model->beforeSave($this->subjectMock, $viewMock); $this->assertEquals( $this->subjectMock, - $this->model->aroundSave($this->subjectMock, $closureMock, $viewMock) + $this->model->afterSave($this->subjectMock, $this->subjectMock) ); } /** * @return array */ - public function aroundSaveDataProvider() + public function beforeAfterSaveDataProvider() { return [ [false, 0], From 7b5bfc715c01ca37783596b43dc503a042c4f4c8 Mon Sep 17 00:00:00 2001 From: Viktor Paladiychuk Date: Tue, 26 Jul 2016 17:45:04 +0300 Subject: [PATCH 24/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- .../CatalogRule/Model/Rule/Validation.php | 25 ++++++++++--------- .../CatalogRule/Model/Rule/ValidationTest.php | 6 +---- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/app/code/Magento/CatalogRuleConfigurable/Plugin/CatalogRule/Model/Rule/Validation.php b/app/code/Magento/CatalogRuleConfigurable/Plugin/CatalogRule/Model/Rule/Validation.php index 7164de93bc705..907b1c7973cf8 100644 --- a/app/code/Magento/CatalogRuleConfigurable/Plugin/CatalogRule/Model/Rule/Validation.php +++ b/app/code/Magento/CatalogRuleConfigurable/Plugin/CatalogRule/Model/Rule/Validation.php @@ -6,18 +6,23 @@ */ namespace Magento\CatalogRuleConfigurable\Plugin\CatalogRule\Model\Rule; -use \Magento\ConfigurableProduct\Model\Product\Type\Configurable; +use Magento\ConfigurableProduct\Model\Product\Type\Configurable; +use Magento\CatalogRule\Model\Rule; +use Magento\Framework\DataObject; +use Magento\Catalog\Model\Product; /** * Class Validation. Call validate method for configurable product instead simple product */ class Validation { - /** @var Configurable */ + /** + * @var Configurable + */ private $configurable; /** - * @param \Magento\ConfigurableProduct\Model\Product\Type\Configurable $configurableType + * @param Configurable $configurableType */ public function __construct(Configurable $configurableType) { @@ -25,17 +30,13 @@ public function __construct(Configurable $configurableType) } /** - * @param \Magento\CatalogRule\Model\Rule $rule - * @param \Closure $proceed - * @param \Magento\Framework\DataObject|\Magento\Catalog\Model\Product $product + * @param Rule $rule + * @param bool $validateResult + * @param DataObject|Product $product * @return bool */ - public function aroundValidate( - \Magento\CatalogRule\Model\Rule $rule, - \Closure $proceed, - \Magento\Framework\DataObject $product - ) { - $validateResult = $proceed($product); + public function afterValidate(Rule $rule, $validateResult, DataObject $product) + { if (!$validateResult && ($configurableProducts = $this->configurable->getParentIdsByChild($product->getId()))) { foreach ($configurableProducts as $configurableProductId) { $validateResult = $rule->getConditions()->validateByEntityId($configurableProductId); diff --git a/app/code/Magento/CatalogRuleConfigurable/Test/Unit/Plugin/CatalogRule/Model/Rule/ValidationTest.php b/app/code/Magento/CatalogRuleConfigurable/Test/Unit/Plugin/CatalogRule/Model/Rule/ValidationTest.php index 5b94f30c50431..7802289c5fe9e 100644 --- a/app/code/Magento/CatalogRuleConfigurable/Test/Unit/Plugin/CatalogRule/Model/Rule/ValidationTest.php +++ b/app/code/Magento/CatalogRuleConfigurable/Test/Unit/Plugin/CatalogRule/Model/Rule/ValidationTest.php @@ -69,10 +69,6 @@ public function testAroundValidateWithValidConfigurableProduct( $runValidateAmount, $result ) { - $closureMock = function () { - return false; - }; - $this->productMock->expects($this->once())->method('getId')->willReturn('product_id'); $this->configurableMock->expects($this->once())->method('getParentIdsByChild')->with('product_id') ->willReturn($parentsIds); @@ -83,7 +79,7 @@ public function testAroundValidateWithValidConfigurableProduct( $this->assertEquals( $result, - $this->validation->aroundValidate($this->ruleMock, $closureMock, $this->productMock) + $this->validation->afterValidate($this->ruleMock, false, $this->productMock) ); } From 0a8f292fb73674c93b676bc1d389761eaa6344c7 Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Wed, 27 Jul 2016 09:20:49 +0300 Subject: [PATCH 25/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- .../Indexer/Fulltext/Plugin/Attribute.php | 20 +++++++++---------- .../Indexer/Fulltext/Plugin/AttributeTest.php | 18 ++++++++--------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Attribute.php b/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Attribute.php index 177382724dff8..916180df944b1 100644 --- a/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Attribute.php +++ b/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Attribute.php @@ -67,15 +67,15 @@ public function beforeSave( /** * Invalidate indexer on attribute save (searchable flag change) * - * @param \Magento\Framework\Model\AbstractModel $subject - * @param \Magento\Framework\Model\AbstractModel $result + * @param \Magento\Catalog\Model\ResourceModel\Attribute $subject + * @param \Magento\Catalog\Model\ResourceModel\Attribute $result * - * @return \Magento\Framework\Model\AbstractModel + * @return \Magento\Catalog\Model\ResourceModel\Attribute * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterSave( - \Magento\Framework\Model\AbstractModel $subject, - \Magento\Framework\Model\AbstractModel $result + \Magento\Catalog\Model\ResourceModel\Attribute $subject, + \Magento\Catalog\Model\ResourceModel\Attribute $result ) { if ($this->saveNeedInvalidation) { $this->indexerRegistry->get(Fulltext::INDEXER_ID)->invalidate(); @@ -107,15 +107,15 @@ public function beforeDelete( /** * Invalidate indexer on searchable attribute delete * - * @param \Magento\Framework\Model\AbstractModel $subject - * @param \Magento\Framework\Model\AbstractModel $result + * @param \Magento\Catalog\Model\ResourceModel\Attribute $subject + * @param \Magento\Catalog\Model\ResourceModel\Attribute $result * - * @return \Magento\Framework\Model\AbstractModel + * @return \Magento\Catalog\Model\ResourceModel\Attribute * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterDelete( - \Magento\Framework\Model\AbstractModel $subject, - \Magento\Framework\Model\AbstractModel $result + \Magento\Catalog\Model\ResourceModel\Attribute $subject, + \Magento\Catalog\Model\ResourceModel\Attribute $result ) { if ($this->deleteNeedInvalidation) { $this->indexerRegistry->get(Fulltext::INDEXER_ID)->invalidate(); diff --git a/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/AttributeTest.php b/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/AttributeTest.php index 76a8008c216e4..5598392ccad0a 100644 --- a/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/AttributeTest.php +++ b/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/AttributeTest.php @@ -26,7 +26,7 @@ class AttributeTest extends \PHPUnit_Framework_TestCase protected $indexerRegistryMock; /** - * @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute|\PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Catalog\Model\ResourceModel\Attribute|\PHPUnit_Framework_MockObject_MockObject */ private $attributeMock; @@ -103,8 +103,8 @@ public function testBeforeSave() public function testAfterSaveNoInvalidation() { $this->assertEquals( - $this->attributeMock, - $this->model->afterSave($this->attributeMock, $this->attributeMock) + $this->subjectMock, + $this->model->afterSave($this->subjectMock, $this->subjectMock) ); } @@ -126,8 +126,8 @@ public function testAfterSaveWithInvalidation() ->method('reset'); $this->assertEquals( - $this->attributeMock, - $model->afterSave($this->attributeMock, $this->attributeMock) + $this->subjectMock, + $model->afterSave($this->subjectMock, $this->subjectMock) ); } @@ -148,8 +148,8 @@ public function testBeforeDelete() public function testAfterDeleteNoInvalidation() { $this->assertEquals( - $this->attributeMock, - $this->model->afterDelete($this->attributeMock, $this->attributeMock) + $this->subjectMock, + $this->model->afterDelete($this->subjectMock, $this->subjectMock) ); } @@ -168,8 +168,8 @@ public function testAfterDeleteWithInvalidation() $this->prepareIndexer(); $this->assertEquals( - $this->attributeMock, - $model->afterDelete($this->attributeMock, $this->attributeMock) + $this->subjectMock, + $model->afterDelete($this->subjectMock, $this->subjectMock) ); } From f091b97181f2f49ebc8b35232ce8cb2017fa0d53 Mon Sep 17 00:00:00 2001 From: Viktor Paladiychuk Date: Wed, 27 Jul 2016 12:13:44 +0300 Subject: [PATCH 26/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- .../Category/Flat/Plugin/StoreViewTest.php | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/StoreViewTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/StoreViewTest.php index d585d64159b7c..173ab44a2986a 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/StoreViewTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/StoreViewTest.php @@ -71,7 +71,7 @@ protected function setUp() $this->model = new StoreView($this->indexerRegistryMock, $this->stateMock); } - public function testAroundSaveNewObject() + public function testAfterSaveNewObject() { $this->mockConfigFlatEnabled(); $this->mockIndexerMethods(); @@ -83,10 +83,13 @@ public function testAroundSaveNewObject() false ); $storeMock->expects($this->once())->method('isObjectNew')->will($this->returnValue(true)); - $this->assertFalse($this->model->aroundSave($this->subjectMock, $this->closureMock, $storeMock)); + $this->assertSame( + $this->subjectMock, + $this->model->afterSave($this->subjectMock, $this->subjectMock, $storeMock) + ); } - public function testAroundSaveHasChanged() + public function testAfterSaveHasChanged() { $storeMock = $this->getMock( 'Magento\Store\Model\Store', @@ -95,10 +98,13 @@ public function testAroundSaveHasChanged() '', false ); - $this->assertFalse($this->model->aroundSave($this->subjectMock, $this->closureMock, $storeMock)); + $this->assertSame( + $this->subjectMock, + $this->model->afterSave($this->subjectMock, $this->subjectMock, $storeMock) + ); } - public function testAroundSaveNoNeed() + public function testAfterSaveNoNeed() { $this->mockConfigFlatEnabledNeever(); $storeMock = $this->getMock( @@ -108,7 +114,9 @@ public function testAroundSaveNoNeed() '', false ); - $this->assertFalse($this->model->aroundSave($this->subjectMock, $this->closureMock, $storeMock)); + $this->assertSame( + $this->subjectMock, + $this->model->afterSave($this->subjectMock, $this->subjectMock, $storeMock)); } protected function mockIndexerMethods() From 15de2bd89faf76130a5428e8cf33864fa74f8883 Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Wed, 27 Jul 2016 12:15:03 +0300 Subject: [PATCH 27/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- .../Unit/App/Action/Plugin/MassactionKeyTest.php | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/app/code/Magento/Backend/Test/Unit/App/Action/Plugin/MassactionKeyTest.php b/app/code/Magento/Backend/Test/Unit/App/Action/Plugin/MassactionKeyTest.php index c9451b8adf3e7..016967d974771 100644 --- a/app/code/Magento/Backend/Test/Unit/App/Action/Plugin/MassactionKeyTest.php +++ b/app/code/Magento/Backend/Test/Unit/App/Action/Plugin/MassactionKeyTest.php @@ -47,13 +47,11 @@ protected function setUp() } /** - * @covers \Magento\Backend\App\Action\Plugin\MassactionKey::aroundDispatch - * * @param $postData array|string * @param array $convertedData - * @dataProvider aroundDispatchDataProvider + * @dataProvider beforeDispatchDataProvider */ - public function testAroundDispatchWhenMassactionPrepareKeyRequestExists($postData, $convertedData) + public function testBeforeDispatchWhenMassactionPrepareKeyRequestExists($postData, $convertedData) { $this->requestMock->expects($this->at(0)) ->method('getPost') @@ -73,7 +71,7 @@ public function testAroundDispatchWhenMassactionPrepareKeyRequestExists($postDat ); } - public function aroundDispatchDataProvider() + public function beforeDispatchDataProvider() { return [ 'post_data_is_array' => [['key'], ['key']], @@ -81,10 +79,7 @@ public function aroundDispatchDataProvider() ]; } - /** - * @covers \Magento\Backend\App\Action\Plugin\MassactionKey::aroundDispatch - */ - public function testAroundDispatchWhenMassactionPrepareKeyRequestNotExists() + public function testBeforeDispatchWhenMassactionPrepareKeyRequestNotExists() { $this->requestMock->expects($this->once()) ->method('getPost') From 2f18173a5055d5697cb148ebdaf20ae3489cf86b Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Wed, 27 Jul 2016 12:20:24 +0300 Subject: [PATCH 28/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- .../CatalogSearch/Model/Indexer/Fulltext/Plugin/Attribute.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Attribute.php b/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Attribute.php index 916180df944b1..701123bf2dbb2 100644 --- a/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Attribute.php +++ b/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Attribute.php @@ -29,7 +29,6 @@ class Attribute extends AbstractPlugin */ private $saveIsNew; - /** * @param \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry * @param \Magento\Framework\Search\Request\Config $config From 4ce02adaf8ad2d6265dd7e5873a4cbcda164431c Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Wed, 27 Jul 2016 14:09:44 +0300 Subject: [PATCH 29/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- .../Unit/Model/Indexer/Fulltext/Plugin/Store/ViewTest.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/Store/ViewTest.php b/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/Store/ViewTest.php index b4e41a6191783..deccb88b295a1 100644 --- a/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/Store/ViewTest.php +++ b/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/Store/ViewTest.php @@ -68,11 +68,6 @@ public function testBeforeAfterSave($isObjectNew, $invalidateCounter) ); $viewMock->expects($this->once())->method('isObjectNew')->will($this->returnValue($isObjectNew)); - $closureMock = function (\Magento\Store\Model\Store $object) use ($viewMock) { - $this->assertEquals($object, $viewMock); - return $this->subjectMock; - }; - $this->indexerMock->expects($this->exactly($invalidateCounter))->method('invalidate'); $this->prepareIndexer($invalidateCounter); From ecf2932de22c2a28ce6edfe7b3841bd7632ca629 Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Wed, 27 Jul 2016 14:10:20 +0300 Subject: [PATCH 30/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- .../Test/Unit/Model/Plugin/AroundProductRepositorySaveTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/CatalogInventory/Test/Unit/Model/Plugin/AroundProductRepositorySaveTest.php b/app/code/Magento/CatalogInventory/Test/Unit/Model/Plugin/AroundProductRepositorySaveTest.php index 573e6f924e133..07351e32a9786 100644 --- a/app/code/Magento/CatalogInventory/Test/Unit/Model/Plugin/AroundProductRepositorySaveTest.php +++ b/app/code/Magento/CatalogInventory/Test/Unit/Model/Plugin/AroundProductRepositorySaveTest.php @@ -92,7 +92,7 @@ protected function setUp() $this->stockConfiguration ); - $this->savedProduct = $savedProduct = $this->getMockBuilder(ProductInterface::class) + $this->savedProduct = $this->getMockBuilder(ProductInterface::class) ->setMethods(['getExtensionAttributes', 'getStoreId']) ->getMockForAbstractClass(); From f82d2000df97fedea781ea750a076cae823d6a09 Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Wed, 27 Jul 2016 16:00:28 +0300 Subject: [PATCH 31/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- .../Model/Indexer/Fulltext/Plugin/Attribute.php | 10 ++++------ .../Model/Layer/Search/Plugin/CollectionFilterTest.php | 3 --- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Attribute.php b/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Attribute.php index 701123bf2dbb2..e21f4a976af8e 100644 --- a/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Attribute.php +++ b/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Attribute.php @@ -42,12 +42,12 @@ public function __construct( } /** - * Check for needed indexer invalidation on attribute save (searchable flag change) + * Check if indexer invalidation is needed on attribute save (searchable flag change) * * @param \Magento\Catalog\Model\ResourceModel\Attribute $subject * @param \Magento\Framework\Model\AbstractModel $attribute * - * @return array + * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function beforeSave( @@ -60,7 +60,6 @@ public function beforeSave( || $attribute->dataHasChangedFor('is_filterable') || $attribute->dataHasChangedFor('is_visible_in_advanced_search') ) && ! $this->saveIsNew; - return [$attribute]; } /** @@ -87,12 +86,12 @@ public function afterSave( } /** - * Check for needed indexer invalidation on searchable attribute delete + * Check if indexer invalidation is needed on searchable attribute delete * * @param \Magento\Catalog\Model\ResourceModel\Attribute $subject * @param \Magento\Framework\Model\AbstractModel $attribute * - * @return array + * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function beforeDelete( @@ -100,7 +99,6 @@ public function beforeDelete( \Magento\Framework\Model\AbstractModel $attribute ) { $this->deleteNeedInvalidation = !$attribute->isObjectNew() && $attribute->getIsSearchable(); - return [$attribute]; } /** diff --git a/app/code/Magento/CatalogSearch/Test/Unit/Model/Layer/Search/Plugin/CollectionFilterTest.php b/app/code/Magento/CatalogSearch/Test/Unit/Model/Layer/Search/Plugin/CollectionFilterTest.php index 2f46cf276472c..ad32a516baee2 100644 --- a/app/code/Magento/CatalogSearch/Test/Unit/Model/Layer/Search/Plugin/CollectionFilterTest.php +++ b/app/code/Magento/CatalogSearch/Test/Unit/Model/Layer/Search/Plugin/CollectionFilterTest.php @@ -13,9 +13,6 @@ use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; use Magento\Search\Model\Query; -/** - * Class CollectionFilterTest - */ class CollectionFilterTest extends \PHPUnit_Framework_TestCase { /** From f44008629950601fdd84817be276db10789db2d8 Mon Sep 17 00:00:00 2001 From: Dmytro Poperechnyy Date: Wed, 27 Jul 2016 17:48:01 +0300 Subject: [PATCH 32/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- .../Test/Unit/Plugin/Indexer/Product/Save/ApplyRulesTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/CatalogRule/Test/Unit/Plugin/Indexer/Product/Save/ApplyRulesTest.php b/app/code/Magento/CatalogRule/Test/Unit/Plugin/Indexer/Product/Save/ApplyRulesTest.php index 339311c674a94..e54192cf6ea2b 100644 --- a/app/code/Magento/CatalogRule/Test/Unit/Plugin/Indexer/Product/Save/ApplyRulesTest.php +++ b/app/code/Magento/CatalogRule/Test/Unit/Plugin/Indexer/Product/Save/ApplyRulesTest.php @@ -65,7 +65,7 @@ public function testAfterSave() $this->productRuleProcessor->expects($this->once())->method('reindexRow')->willReturnSelf(); - $this->assertEquals( + $this->assertSame( $this->subject, $this->plugin->afterSave($this->subject, $this->subject, $this->model) ); From ad574a28c35f6b79533488e0b8d7e869b9bdd342 Mon Sep 17 00:00:00 2001 From: Viktor Paladiychuk Date: Wed, 27 Jul 2016 17:59:01 +0300 Subject: [PATCH 33/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- .../App/Action/Plugin/MassactionKey.php | 5 +- .../App/Action/Plugin/MassactionKeyTest.php | 10 +- .../Test/Unit/Model/Plugin/QuoteItemTest.php | 9 +- .../Category/Flat/Plugin/StoreGroup.php | 25 ++++- .../Category/Product/Plugin/StoreGroup.php | 28 +++++- .../Product/Eav/Plugin/AttributeSet.php | 45 +++++---- .../Model/ResourceModel/Attribute/Save.php | 2 + .../Flat/Plugin/IndexerConfigDataTest.php | 3 +- .../Category/Flat/Plugin/StoreGroupTest.php | 13 ++- .../Category/Flat/Plugin/StoreViewTest.php | 17 ++-- .../Product/Plugin/StoreGroupTest.php | 64 ++++++------- .../Category/Product/Plugin/StoreViewTest.php | 96 +++++++------------ .../Product/Eav/Plugin/AttributeSetTest.php | 12 ++- .../ResourceModel/Attribute/SaveTest.php | 22 ++--- .../Plugin/AroundProductRepositorySave.php | 1 - .../AroundProductRepositorySaveTest.php | 28 +++--- .../CatalogRule/Model/Rule/Validation.php | 2 + .../CatalogRule/Model/Rule/ValidationTest.php | 2 +- 18 files changed, 197 insertions(+), 187 deletions(-) diff --git a/app/code/Magento/Backend/App/Action/Plugin/MassactionKey.php b/app/code/Magento/Backend/App/Action/Plugin/MassactionKey.php index 65bcf566d6ad3..5365c14a6d451 100644 --- a/app/code/Magento/Backend/App/Action/Plugin/MassactionKey.php +++ b/app/code/Magento/Backend/App/Action/Plugin/MassactionKey.php @@ -18,7 +18,7 @@ class MassactionKey * @param AbstractAction $subject * @param RequestInterface $request * - * @return array + * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function beforeDispatch(AbstractAction $subject, RequestInterface $request) @@ -28,7 +28,6 @@ public function beforeDispatch(AbstractAction $subject, RequestInterface $reques $postData = $request->getPost($key); $value = is_array($postData) ? $postData : explode(',', $postData); $request->setPostValue($key, $value ? $value : null); - } - return [$request]; + }; } } diff --git a/app/code/Magento/Backend/Test/Unit/App/Action/Plugin/MassactionKeyTest.php b/app/code/Magento/Backend/Test/Unit/App/Action/Plugin/MassactionKeyTest.php index 016967d974771..66043a726c5b1 100644 --- a/app/code/Magento/Backend/Test/Unit/App/Action/Plugin/MassactionKeyTest.php +++ b/app/code/Magento/Backend/Test/Unit/App/Action/Plugin/MassactionKeyTest.php @@ -65,10 +65,7 @@ public function testBeforeDispatchWhenMassactionPrepareKeyRequestExists($postDat ->method('setPostValue') ->with('key', $convertedData); - $this->assertEquals( - [$this->requestMock], - $this->plugin->beforeDispatch($this->subjectMock, $this->requestMock) - ); + $this->plugin->beforeDispatch($this->subjectMock, $this->requestMock); } public function beforeDispatchDataProvider() @@ -88,9 +85,6 @@ public function testBeforeDispatchWhenMassactionPrepareKeyRequestNotExists() $this->requestMock->expects($this->never()) ->method('setPostValue'); - $this->assertEquals( - [$this->requestMock], - $this->plugin->beforeDispatch($this->subjectMock, $this->requestMock) - ); + $this->plugin->beforeDispatch($this->subjectMock, $this->requestMock); } } diff --git a/app/code/Magento/Bundle/Test/Unit/Model/Plugin/QuoteItemTest.php b/app/code/Magento/Bundle/Test/Unit/Model/Plugin/QuoteItemTest.php index 42db60ad650a2..990af4d4f66e4 100644 --- a/app/code/Magento/Bundle/Test/Unit/Model/Plugin/QuoteItemTest.php +++ b/app/code/Magento/Bundle/Test/Unit/Model/Plugin/QuoteItemTest.php @@ -40,7 +40,6 @@ protected function setUp() AbstractItem::class, [], '', false, false, true, ['getProduct'] ); $this->subjectMock = $this->getMock(ToOrderItem::class, [], [], '', false); - /** @var \PHPUnit_Framework_MockObject_MockObject|Product $productMock */ $this->productMock = $this->getMock(Product::class, [], [], '', false); $this->model = new \Magento\Bundle\Model\Plugin\QuoteItem(); } @@ -66,9 +65,9 @@ public function testAroundItemToOrderItemPositive() ->willReturn($attributeValue); $this->productMock->expects($this->once()) - ->method('getCustomOption') - ->with('bundle_selection_attributes') - ->willReturn($bundleAttribute); + ->method('getCustomOption') + ->with('bundle_selection_attributes') + ->willReturn($bundleAttribute); $this->quoteItemMock->expects($this->once())->method('getProduct')->willReturn( $this->productMock ); $this->orderItemMock->expects($this->once())->method('getProductOptions')->willReturn($productOptions); @@ -85,7 +84,7 @@ public function testAroundItemToOrderItemNegative() ->with('bundle_selection_attributes')->willReturn(false); $this->quoteItemMock->expects($this->once())->method('getProduct') - ->will($this->returnValue($this->productMock)); + ->willReturn($this->productMock); $this->orderItemMock->expects($this->never())->method('setProductOptions'); $orderItem = $this->model->afterConvert($this->subjectMock, $this->orderItemMock, $this->quoteItemMock); diff --git a/app/code/Magento/Catalog/Model/Indexer/Category/Flat/Plugin/StoreGroup.php b/app/code/Magento/Catalog/Model/Indexer/Category/Flat/Plugin/StoreGroup.php index 9a58a0730400f..502bb8b542f4e 100644 --- a/app/code/Magento/Catalog/Model/Indexer/Category/Flat/Plugin/StoreGroup.php +++ b/app/code/Magento/Catalog/Model/Indexer/Category/Flat/Plugin/StoreGroup.php @@ -12,6 +12,11 @@ class StoreGroup { + /** + * @var bool + */ + private $needInvalidating; + /** * @var IndexerRegistry */ @@ -43,20 +48,32 @@ protected function validate(AbstractModel $group) return $group->dataHasChangedFor('root_category_id') && !$group->isObjectNew(); } + /** + * Check if need invalidate flat category indexer + * + * @param AbstractDb $subject + * @param AbstractModel $group + * + * @return void + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function beforeSave(AbstractDb $subject, AbstractModel $group) + { + $this->needInvalidating = $this->validate($group); + } + /** * Invalidate flat category indexer if root category changed for store group * * @param AbstractDb $subject * @param AbstractDb $objectResource - * @param AbstractModel $group * * @return AbstractDb * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function afterSave(AbstractDb $subject, AbstractDb $objectResource, AbstractModel $group) + public function afterSave(AbstractDb $subject, AbstractDb $objectResource) { - $needInvalidating = $this->validate($group); - if ($needInvalidating && $this->state->isFlatEnabled()) { + if ($this->needInvalidating && $this->state->isFlatEnabled()) { $this->indexerRegistry->get(State::INDEXER_ID)->invalidate(); } diff --git a/app/code/Magento/Catalog/Model/Indexer/Category/Product/Plugin/StoreGroup.php b/app/code/Magento/Catalog/Model/Indexer/Category/Product/Plugin/StoreGroup.php index a9fa53217b89b..f81c0165b638a 100644 --- a/app/code/Magento/Catalog/Model/Indexer/Category/Product/Plugin/StoreGroup.php +++ b/app/code/Magento/Catalog/Model/Indexer/Category/Product/Plugin/StoreGroup.php @@ -12,6 +12,11 @@ class StoreGroup { + /** + * @var bool + */ + private $needInvalidating; + /** * @var IndexerRegistry */ @@ -25,19 +30,32 @@ public function __construct(IndexerRegistry $indexerRegistry) $this->indexerRegistry = $indexerRegistry; } + /** + * Check if need invalidate flat category indexer + * + * @param AbstractDb $subject + * @param AbstractModel $group + * + * @return void + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function beforeSave(AbstractDb $subject, AbstractModel $group) + { + $this->needInvalidating = $this->validate($group); + } + /** * Invalidate flat product * * @param AbstractDb $subject * @param AbstractDb $objectResource - * @param AbstractModel $group - * @return mixed + * + * @return AbstractDb * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function afterSave(AbstractDb $subject, AbstractDb $objectResource, AbstractModel $group) + public function afterSave(AbstractDb $subject, AbstractDb $objectResource) { - $needInvalidating = $this->validate($group); - if ($needInvalidating) { + if ($this->needInvalidating) { $this->indexerRegistry->get(Product::INDEXER_ID)->invalidate(); } diff --git a/app/code/Magento/Catalog/Model/Indexer/Product/Eav/Plugin/AttributeSet.php b/app/code/Magento/Catalog/Model/Indexer/Product/Eav/Plugin/AttributeSet.php index 77bd5e1ea7479..beee265673c31 100644 --- a/app/code/Magento/Catalog/Model/Indexer/Product/Eav/Plugin/AttributeSet.php +++ b/app/code/Magento/Catalog/Model/Indexer/Product/Eav/Plugin/AttributeSet.php @@ -5,7 +5,7 @@ */ namespace Magento\Catalog\Model\Indexer\Product\Eav\Plugin; -use Magento\Eav\Model\Entity\Attribute\Set; +use Magento\Eav\Model\Entity\Attribute\Set as EavAttributeSet; use Magento\Catalog\Model\Indexer\Product\Eav\Processor; use Magento\Eav\Model\Entity\Attribute\SetFactory; use Magento\Framework\App\ObjectManager; @@ -42,23 +42,6 @@ public function __construct(Processor $indexerEavProcessor, AttributeSet\Indexab $this->_attributeFilter = $filter; } - /** - * Invalidate EAV indexer if attribute set has indexable attributes changes - * - * @param Set $subject - * @param Set $result - * @return Set - * - * @SuppressWarnings(PHPMD.UnusedFormalParameter) - */ - public function afterSave(Set $subject, Set $result) - { - if ($this->requiresReindex) { - $this->_indexerEavProcessor->markIndexerAsInvalid(); - } - return $result; - } - /** * Return attribute set factory * @@ -74,14 +57,17 @@ private function getAttributeSetFactory() } /** - * @param Set $subject + * Check whether is needed to invalidate EAV indexer + * + * @param EavAttributeSet $subject * * @return bool */ - public function beforeSave(Set $subject) { + public function beforeSave(EavAttributeSet $subject) + { $this->requiresReindex = false; if ( $subject->getId() ) { - /** @var Set $originalSet */ + /** @var EavAttributeSet $originalSet */ $originalSet = $this->getAttributeSetFactory()->create(); $originalSet->initFromSkeleton($subject->getId()); $originalAttributeCodes = array_flip( $this->_attributeFilter->filter( $originalSet ) ); @@ -92,4 +78,21 @@ public function beforeSave(Set $subject) { ) ); } } + + /** + * Invalidate EAV indexer if attribute set has indexable attributes changes + * + * @param EavAttributeSet $subject + * @param EavAttributeSet $result + * @return EavAttributeSet + * + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function afterSave(EavAttributeSet $subject, EavAttributeSet $result) + { + if ($this->requiresReindex) { + $this->_indexerEavProcessor->markIndexerAsInvalid(); + } + return $result; + } } diff --git a/app/code/Magento/Catalog/Plugin/Model/ResourceModel/Attribute/Save.php b/app/code/Magento/Catalog/Plugin/Model/ResourceModel/Attribute/Save.php index 888879717b7ad..b7d77f40f6899 100644 --- a/app/code/Magento/Catalog/Plugin/Model/ResourceModel/Attribute/Save.php +++ b/app/code/Magento/Catalog/Plugin/Model/ResourceModel/Attribute/Save.php @@ -33,6 +33,8 @@ public function __construct(Config $config, TypeListInterface $typeList) } /** + * Invalidate full page cache after saving attribute + * * @param Attribute $subject * @param Attribute $result * @return Attribute $result diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/IndexerConfigDataTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/IndexerConfigDataTest.php index 3f2a25ad7a894..cde2365e21b6b 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/IndexerConfigDataTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/IndexerConfigDataTest.php @@ -7,6 +7,7 @@ use Magento\Catalog\Model\Indexer\Category\Flat\Plugin\IndexerConfigData; use Magento\Catalog\Model\Indexer\Category\Flat\State; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; use Magento\Indexer\Model\Config\Data; class IndexerConfigDataTest extends \PHPUnit_Framework_TestCase @@ -30,7 +31,7 @@ protected function setUp() { $this->stateMock = $this->getMock(State::class, ['isFlatEnabled'], [], '', false); $this->subjectMock = $this->getMock(Data::class, [], [], '', false); - $this->model = new IndexerConfigData($this->stateMock); + $this->model = (new ObjectManager($this))->getObject(IndexerConfigData::class, ['state' => $this->stateMock]); } /** diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/StoreGroupTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/StoreGroupTest.php index 32f4f913033cc..dce5ce9d85228 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/StoreGroupTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/StoreGroupTest.php @@ -9,6 +9,7 @@ use Magento\Framework\Indexer\IndexerInterface; use Magento\Catalog\Model\Indexer\Category\Flat\State; use Magento\Framework\Indexer\IndexerRegistry; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; use Magento\Store\Model\ResourceModel\Group; use Magento\Store\Model\Group as GroupModel; @@ -74,10 +75,14 @@ protected function setUp() false ); - $this->model = new StoreGroup($this->indexerRegistryMock, $this->stateMock); + $this->model = (new ObjectManager($this)) + ->getObject( + StoreGroup::class, + ['indexerRegistry' => $this->indexerRegistryMock, 'state' => $this->stateMock] + ); } - public function testAroundSave() + public function testBeforeAndAfterSave() { $this->stateMock->expects($this->once())->method('isFlatEnabled')->willReturn(true); $this->indexerMock->expects($this->once())->method('invalidate'); @@ -90,13 +95,14 @@ public function testAroundSave() ->with('root_category_id') ->willReturn(true); $this->groupMock->expects($this->once())->method('isObjectNew')->willReturn(false); + $this->model->beforeSave($this->subjectMock, $this->groupMock); $this->assertSame( $this->subjectMock, $this->model->afterSave($this->subjectMock, $this->subjectMock, $this->groupMock) ); } - public function testAroundSaveNotNew() + public function testBeforeAndAfterSaveNotNew() { $this->stateMock->expects($this->never())->method('isFlatEnabled'); $this->groupMock->expects($this->once()) @@ -104,6 +110,7 @@ public function testAroundSaveNotNew() ->with('root_category_id') ->willReturn(true); $this->groupMock->expects($this->once())->method('isObjectNew')->willReturn(true); + $this->model->beforeSave($this->subjectMock, $this->groupMock); $this->assertSame( $this->subjectMock, $this->model->afterSave($this->subjectMock, $this->subjectMock, $this->groupMock) diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/StoreViewTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/StoreViewTest.php index 173ab44a2986a..8c6b1c9074824 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/StoreViewTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/StoreViewTest.php @@ -29,11 +29,6 @@ class StoreViewTest extends \PHPUnit_Framework_TestCase */ protected $indexerRegistryMock; - /** - * @var \Closure - */ - protected $closureMock; - /** * @var \PHPUnit_Framework_MockObject_MockObject */ @@ -57,9 +52,6 @@ protected function setUp() '', false ); - $this->closureMock = function () { - return false; - }; $this->subjectMock = $this->getMock('Magento\Store\Model\ResourceModel\Store', [], [], '', false); $this->indexerRegistryMock = $this->getMock( 'Magento\Framework\Indexer\IndexerRegistry', @@ -71,7 +63,7 @@ protected function setUp() $this->model = new StoreView($this->indexerRegistryMock, $this->stateMock); } - public function testAfterSaveNewObject() + public function testBeforeAndAfterSaveNewObject() { $this->mockConfigFlatEnabled(); $this->mockIndexerMethods(); @@ -83,13 +75,14 @@ public function testAfterSaveNewObject() false ); $storeMock->expects($this->once())->method('isObjectNew')->will($this->returnValue(true)); + $this->model->beforeSave($this->subjectMock, $storeMock); $this->assertSame( $this->subjectMock, $this->model->afterSave($this->subjectMock, $this->subjectMock, $storeMock) ); } - public function testAfterSaveHasChanged() + public function testBeforeAndAfterSaveHasChanged() { $storeMock = $this->getMock( 'Magento\Store\Model\Store', @@ -98,13 +91,14 @@ public function testAfterSaveHasChanged() '', false ); + $this->model->beforeSave($this->subjectMock, $storeMock); $this->assertSame( $this->subjectMock, $this->model->afterSave($this->subjectMock, $this->subjectMock, $storeMock) ); } - public function testAfterSaveNoNeed() + public function testBeforeAndAfterSaveNoNeed() { $this->mockConfigFlatEnabledNeever(); $storeMock = $this->getMock( @@ -114,6 +108,7 @@ public function testAfterSaveNoNeed() '', false ); + $this->model->beforeSave($this->subjectMock, $storeMock); $this->assertSame( $this->subjectMock, $this->model->afterSave($this->subjectMock, $this->subjectMock, $storeMock)); diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Product/Plugin/StoreGroupTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Product/Plugin/StoreGroupTest.php index 537462588ddda..6a1c2b5f046a3 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Product/Plugin/StoreGroupTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Product/Plugin/StoreGroupTest.php @@ -7,6 +7,7 @@ use Magento\Catalog\Model\Indexer\Category\Product\Plugin\StoreGroup; use Magento\Framework\Indexer\IndexerInterface; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; use Magento\Store\Model\ResourceModel\Group; use Magento\Framework\Indexer\IndexerRegistry; use Magento\Store\Model\Group as GroupModel; @@ -22,25 +23,32 @@ class StoreGroupTest extends \PHPUnit_Framework_TestCase /** * @var \PHPUnit_Framework_MockObject_MockObject|IndexerInterface */ - protected $indexerMock; + private $indexerMock; /** * @var \PHPUnit_Framework_MockObject_MockObject|Group */ - protected $subject; + private $subject; /** * @var IndexerRegistry|\PHPUnit_Framework_MockObject_MockObject */ - protected $indexerRegistryMock; + private $indexerRegistryMock; /** * @var StoreGroup */ - protected $model; + private $model; protected function setUp() { + $this->groupMock = $this->getMock( + GroupModel::class, + [ 'dataHasChangedFor', 'isObjectNew', '__wakeup' ], + [ ], + '', + false + ); $this->indexerMock = $this->getMockForAbstractClass( IndexerInterface::class, [], @@ -59,46 +67,34 @@ protected function setUp() false ); - $this->model = new StoreGroup($this->indexerRegistryMock); + $this->model = (new ObjectManager($this))->getObject(StoreGroup::class, ['indexerRegistry' => $this->indexerRegistryMock]); } /** * @param array $valueMap * @dataProvider changedDataProvider */ - public function testAroundSave($valueMap) + public function testBeforeAndAfterSave($valueMap) { $this->mockIndexerMethods(); - $groupMock = $this->getMock( - GroupModel::class, - ['dataHasChangedFor', 'isObjectNew', '__wakeup'], - [], - '', - false - ); - $groupMock->expects($this->exactly(2))->method('dataHasChangedFor')->willReturnMap($valueMap); - $groupMock->expects($this->once())->method('isObjectNew')->willReturn(false); + $this->groupMock->expects($this->exactly(2))->method('dataHasChangedFor')->willReturnMap($valueMap); + $this->groupMock->expects($this->once())->method('isObjectNew')->willReturn(false); - $this->assertSame($this->subject, $this->model->afterSave($this->subject, $this->subject, $groupMock)); + $this->model->beforeSave($this->subject, $this->groupMock); + $this->assertSame($this->subject, $this->model->afterSave($this->subject, $this->subject, $this->groupMock)); } /** * @param array $valueMap * @dataProvider changedDataProvider */ - public function testAroundSaveNotNew($valueMap) + public function testBeforeAndAfterSaveNotNew($valueMap) { - $groupMock = $this->getMock( - GroupModel::class, - ['dataHasChangedFor', 'isObjectNew', '__wakeup'], - [], - '', - false - ); - $groupMock->expects($this->exactly(2))->method('dataHasChangedFor')->willReturnMap($valueMap); - $groupMock->expects($this->once())->method('isObjectNew')->willReturn(true); + $this->groupMock->expects($this->exactly(2))->method('dataHasChangedFor')->willReturnMap($valueMap); + $this->groupMock->expects($this->once())->method('isObjectNew')->willReturn(true); - $this->assertSame($this->subject, $this->model->afterSave($this->subject, $this->subject, $groupMock)); + $this->model->beforeSave($this->subject, $this->groupMock); + $this->assertSame($this->subject, $this->model->afterSave($this->subject, $this->subject, $this->groupMock)); } public function changedDataProvider() @@ -111,20 +107,14 @@ public function changedDataProvider() ]; } - public function testAroundSaveWithoutChanges() + public function testBeforeAndAfterSaveWithoutChanges() { - $this->groupMock = $this->getMock( - GroupModel::class, - [ 'dataHasChangedFor', 'isObjectNew', '__wakeup' ], - [ ], - '', - false - ); $this->groupMock->expects($this->exactly(2)) - ->method('dataHasChangedFor') - ->willReturnMap([['root_category_id', false], ['website_id', false]]); + ->method('dataHasChangedFor') + ->willReturnMap([['root_category_id', false], ['website_id', false]]); $this->groupMock->expects($this->never())->method('isObjectNew'); + $this->model->beforeSave($this->subject, $this->groupMock); $this->assertSame($this->subject, $this->model->afterSave($this->subject, $this->subject, $this->groupMock )); } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Product/Plugin/StoreViewTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Product/Plugin/StoreViewTest.php index 080a7ecaf93a0..bbe025f09603c 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Product/Plugin/StoreViewTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Product/Plugin/StoreViewTest.php @@ -5,19 +5,23 @@ */ namespace Magento\Catalog\Test\Unit\Model\Indexer\Category\Product\Plugin; -use \Magento\Catalog\Model\Indexer\Category\Product\Plugin\StoreView; +use Magento\Catalog\Model\Indexer\Category\Product\Plugin\StoreView; +use Magento\Framework\Indexer\IndexerInterface; +use Magento\Framework\Indexer\IndexerRegistry; +use Magento\Store\Model\ResourceModel\Group; +use Magento\Store\Model\Store; class StoreViewTest extends \PHPUnit_Framework_TestCase { /** - * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Indexer\IndexerInterface + * @var Store|\PHPUnit_Framework_MockObject_MockObject */ - protected $indexerMock; + private $storeMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject| + * @var \PHPUnit_Framework_MockObject_MockObject|IndexerInterface */ - protected $pluginMock; + protected $indexerMock; /** * @var StoreView @@ -25,7 +29,7 @@ class StoreViewTest extends \PHPUnit_Framework_TestCase protected $model; /** - * @var \Magento\Framework\Indexer\IndexerRegistry|\PHPUnit_Framework_MockObject_MockObject + * @var IndexerRegistry|\PHPUnit_Framework_MockObject_MockObject */ protected $indexerRegistryMock; @@ -37,7 +41,7 @@ class StoreViewTest extends \PHPUnit_Framework_TestCase protected function setUp() { $this->indexerMock = $this->getMockForAbstractClass( - 'Magento\Framework\Indexer\IndexerInterface', + IndexerInterface::class, [], '', false, @@ -45,14 +49,21 @@ protected function setUp() true, ['getId', 'getState', '__wakeup'] ); - $this->subject = $this->getMock('Magento\Store\Model\ResourceModel\Group', [], [], '', false); + $this->subject = $this->getMock(Group::class, [], [], '', false); $this->indexerRegistryMock = $this->getMock( - 'Magento\Framework\Indexer\IndexerRegistry', + IndexerRegistry::class, ['get'], [], '', false ); + $this->storeMock = $this->getMock( + Store::class, + ['isObjectNew', 'dataHasChangedFor', '__wakeup'], + [], + '', + false + ); $this->model = new StoreView($this->indexerRegistryMock); } @@ -60,61 +71,31 @@ protected function setUp() public function testAroundSaveNewObject() { $this->mockIndexerMethods(); - $storeMock = $this->getMock( - 'Magento\Store\Model\Store', - ['isObjectNew', 'dataHasChangedFor', '__wakeup'], - [], - '', - false - ); - $storeMock->expects($this->once())->method('isObjectNew')->will($this->returnValue(true)); - $proceed = $this->mockPluginProceed(); - $this->assertFalse($this->model->aroundSave($this->subject, $proceed, $storeMock)); + $this->storeMock->expects($this->once())->method('isObjectNew')->will($this->returnValue(true)); + $this->model->beforeSave($this->subject, $this->storeMock); + $this->assertSame($this->subject, $this->model->afterSave($this->subject, $this->subject, $this->storeMock)); } public function testAroundSaveHasChanged() { $this->mockIndexerMethods(); - $storeMock = $this->getMock( - 'Magento\Store\Model\Store', - ['isObjectNew', 'dataHasChangedFor', '__wakeup'], - [], - '', - false - ); - $storeMock->expects( - $this->once() - )->method( - 'dataHasChangedFor' - )->with( - 'group_id' - )->will( - $this->returnValue(true) - ); - $proceed = $this->mockPluginProceed(); - $this->assertFalse($this->model->aroundSave($this->subject, $proceed, $storeMock)); + $this->storeMock->expects($this->once()) + ->method('dataHasChangedFor') + ->with('group_id') + ->will($this->returnValue(true)); + $this->model->beforeSave($this->subject, $this->storeMock); + $this->assertSame($this->subject, $this->model->afterSave($this->subject, $this->subject, $this->storeMock)); } public function testAroundSaveNoNeed() { - $storeMock = $this->getMock( - 'Magento\Store\Model\Store', - ['isObjectNew', 'dataHasChangedFor', '__wakeup'], - [], - '', - false - ); - $storeMock->expects( - $this->once() - )->method( - 'dataHasChangedFor' - )->with( - 'group_id' - )->will( - $this->returnValue(false) + $this->storeMock->expects($this->once()) + ->method('dataHasChangedFor') + ->with('group_id') + ->will($this->returnValue(false) ); - $proceed = $this->mockPluginProceed(); - $this->assertFalse($this->model->aroundSave($this->subject, $proceed, $storeMock)); + $this->model->beforeSave($this->subject, $this->storeMock); + $this->assertSame($this->subject, $this->model->afterSave($this->subject, $this->subject, $this->storeMock)); } /** @@ -143,11 +124,4 @@ protected function mockIndexerMethods() ->with(\Magento\Catalog\Model\Indexer\Category\Product::INDEXER_ID) ->will($this->returnValue($this->indexerMock)); } - - protected function mockPluginProceed($returnValue = false) - { - return function () use ($returnValue) { - return $returnValue; - }; - } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Eav/Plugin/AttributeSetTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Eav/Plugin/AttributeSetTest.php index 5c0f730e9fea1..7b6a5123d9fc3 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Eav/Plugin/AttributeSetTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Eav/Plugin/AttributeSetTest.php @@ -14,6 +14,11 @@ class AttributeSetTest extends \PHPUnit_Framework_TestCase { + /** + * @var ObjectManager + */ + private $objectManager; + /** * @var AttributeSet */ @@ -50,6 +55,7 @@ public function setUp() $this->subjectMock = $this->getMock(Set::class, [], [], '', false); $this->eavProcessorMock = $this->getMock(Processor::class, [], [], '', false); $this->setFactoryMock = $this->getMock(SetFactory::class, [], [], '', false); + $this->objectManager = new ObjectManager($this); } public function testBeforeSave() @@ -59,9 +65,7 @@ public function testBeforeSave() $this->originalSetMock->expects($this->once())->method('initFromSkeleton')->with($setId); $this->setFactoryMock->expects($this->once())->method('create')->willReturn($this->originalSetMock); - - $this->model = (new ObjectManager($this)) - ->getObject( + $this->model = $this->objectManager->getObject( AttributeSet::class, [ 'indexerEavProcessor' => $this->eavProcessorMock, @@ -90,7 +94,7 @@ public function testAfterSave() { $this->eavProcessorMock->expects($this->once())->method('markIndexerAsInvalid'); - $this->model = (new ObjectManager($this)) + $this->model = $this->objectManager ->getObject( AttributeSet::class, [ diff --git a/app/code/Magento/Catalog/Test/Unit/Plugin/Model/ResourceModel/Attribute/SaveTest.php b/app/code/Magento/Catalog/Test/Unit/Plugin/Model/ResourceModel/Attribute/SaveTest.php index 9333e1ca4b899..9971029bc22da 100644 --- a/app/code/Magento/Catalog/Test/Unit/Plugin/Model/ResourceModel/Attribute/SaveTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Plugin/Model/ResourceModel/Attribute/SaveTest.php @@ -13,12 +13,12 @@ class SaveTest extends \PHPUnit_Framework_TestCase { - /** - * @var - */ - private $subjectMock; + /** + * @var Attribute|\PHPUnit_Framework_MockObject_MockObject + */ + private $subjectMock; - /** + /** * @var Save */ protected $save; @@ -37,15 +37,15 @@ protected function setUp() { $this->config = $this->getMock(Config::class, ['isEnabled'], [], '', false); $this->typeList = $this->getMockForAbstractClass( - TypeListInterface::class, [], '', false, false, true, ['invalidate'] + TypeListInterface::class, [], '', false, false, true, ['invalidate'] ); - $this->subjectMock = $this->getMock(Attribute::class, [], [], '', false); + $this->subjectMock = $this->getMock(Attribute::class, [], [], '', false); $this->save = new Save($this->config, $this->typeList); } - public function testAroundSaveWithoutInvalidate() + public function testAfterSaveWithoutInvalidate() { - $this->config->expects($this->once()) + $this->config->expects($this->once()) ->method('isEnabled') ->willReturn(false); @@ -55,7 +55,7 @@ public function testAroundSaveWithoutInvalidate() $this->assertSame($this->subjectMock, $this->save->afterSave($this->subjectMock, $this->subjectMock)); } - public function testAroundSave() + public function testAfterSave() { $this->config->expects($this->once()) ->method('isEnabled') @@ -65,6 +65,6 @@ public function testAroundSave() ->method('invalidate') ->with('full_page'); - $this->assertSame($this->subjectMock, $this->save->afterSave($this->subjectMock, $this->subjectMock)); + $this->assertSame($this->subjectMock, $this->save->afterSave($this->subjectMock, $this->subjectMock)); } } diff --git a/app/code/Magento/CatalogInventory/Model/Plugin/AroundProductRepositorySave.php b/app/code/Magento/CatalogInventory/Model/Plugin/AroundProductRepositorySave.php index 310e64e80bf8a..7ffc36b1143ab 100644 --- a/app/code/Magento/CatalogInventory/Model/Plugin/AroundProductRepositorySave.php +++ b/app/code/Magento/CatalogInventory/Model/Plugin/AroundProductRepositorySave.php @@ -54,7 +54,6 @@ public function __construct( * * Pay attention that in this code we mostly work with original product object to process stock item data, * not with received result (saved product) because it is already contains new empty stock item object. - * Now "after plugins" can receive arguments of original method - plugin is rewritten to "after" * * @param ProductRepositoryInterface $subject * @param ProductInterface $result diff --git a/app/code/Magento/CatalogInventory/Test/Unit/Model/Plugin/AroundProductRepositorySaveTest.php b/app/code/Magento/CatalogInventory/Test/Unit/Model/Plugin/AroundProductRepositorySaveTest.php index 07351e32a9786..b53b53da9120f 100644 --- a/app/code/Magento/CatalogInventory/Test/Unit/Model/Plugin/AroundProductRepositorySaveTest.php +++ b/app/code/Magento/CatalogInventory/Test/Unit/Model/Plugin/AroundProductRepositorySaveTest.php @@ -6,7 +6,7 @@ namespace Magento\CatalogInventory\Test\Unit\Model\Plugin; -use Magento\Catalog\Api\Data\ProductExtension; +use Magento\Catalog\Api\Data\ProductExtensionInterface; use Magento\Catalog\Api\Data\ProductInterface; use Magento\Catalog\Api\ProductRepositoryInterface; use Magento\CatalogInventory\Api\Data\StockInterface; @@ -33,7 +33,7 @@ class AroundProductRepositorySaveTest extends \PHPUnit_Framework_TestCase private $savedProduct; /** - * @var ProductExtension|\PHPUnit_Framework_MockObject_MockObject + * @var ProductExtensionInterface|\PHPUnit_Framework_MockObject_MockObject */ private $productExtension; @@ -102,9 +102,15 @@ protected function setUp() $this->product = $this->getMockBuilder(ProductInterface::class) ->setMethods(['getExtensionAttributes', 'getStoreId']) ->getMockForAbstractClass(); - $this->productExtension = $this->getMockBuilder(ProductExtension::class) - ->setMethods(['getStockItem']) - ->getMock(); + $this->productExtension = $this->getMockForAbstractClass( + ProductExtensionInterface::class, + [], + '', + false, + false, + true, + ['getStockItem'] + ); $this->stockItem = $this->getMockBuilder(StockItemInterface::class) ->setMethods(['setWebsiteId', 'getWebsiteId', 'getStockId']) ->getMockForAbstractClass(); @@ -113,7 +119,7 @@ protected function setUp() ->getMockForAbstractClass(); } - public function testAroundSaveWhenProductHasNoStockItemNeedingToBeUpdated() + public function testAfterSaveWhenProductHasNoStockItemNeedingToBeUpdated() { // pretend we have no extension attributes at all $this->product->expects($this->once()) @@ -135,7 +141,7 @@ public function testAroundSaveWhenProductHasNoStockItemNeedingToBeUpdated() ); } - public function testAroundSaveWhenProductHasNoPersistentStockItemInfo() + public function testAfterSaveWhenProductHasNoPersistentStockItemInfo() { // pretend we do have extension attributes, but none for 'stock_item' $this->product->expects($this->once()) @@ -164,7 +170,7 @@ public function testAroundSaveWhenProductHasNoPersistentStockItemInfo() ); } - public function testAroundSave() + public function testAfterSave() { $productId = 5494; $storeId = 2; @@ -223,7 +229,7 @@ public function testAroundSave() * @expectedException \Magento\Framework\Exception\LocalizedException * @expectedExceptionMessage Invalid stock id: 100500. Only default stock with id 50 allowed */ - public function testAroundSaveWithInvalidStockId() + public function testAfterSaveWithInvalidStockId() { $stockId = 100500; $defaultScopeId = 100; @@ -257,7 +263,7 @@ public function testAroundSaveWithInvalidStockId() * @expectedException \Magento\Framework\Exception\LocalizedException * @expectedExceptionMessage Invalid stock item id: 0. Should be null or numeric value greater than 0 */ - public function testAroundSaveWithInvalidStockItemId() + public function testAfterSaveWithInvalidStockItemId() { $stockId = 80; $stockItemId = 0; @@ -296,7 +302,7 @@ public function testAroundSaveWithInvalidStockItemId() * @expectedException \Magento\Framework\Exception\LocalizedException * @expectedExceptionMessage Invalid stock item id: 35. Assigned stock item id is 40 */ - public function testAroundSaveWithNotAssignedStockItemId() + public function testAfterSaveWithNotAssignedStockItemId() { $stockId = 80; $stockItemId = 35; diff --git a/app/code/Magento/CatalogRuleConfigurable/Plugin/CatalogRule/Model/Rule/Validation.php b/app/code/Magento/CatalogRuleConfigurable/Plugin/CatalogRule/Model/Rule/Validation.php index 907b1c7973cf8..607f02ba7902c 100644 --- a/app/code/Magento/CatalogRuleConfigurable/Plugin/CatalogRule/Model/Rule/Validation.php +++ b/app/code/Magento/CatalogRuleConfigurable/Plugin/CatalogRule/Model/Rule/Validation.php @@ -30,6 +30,8 @@ public function __construct(Configurable $configurableType) } /** + * Define if it is needed to apply rule if parent configurable product match conditions + * * @param Rule $rule * @param bool $validateResult * @param DataObject|Product $product diff --git a/app/code/Magento/CatalogRuleConfigurable/Test/Unit/Plugin/CatalogRule/Model/Rule/ValidationTest.php b/app/code/Magento/CatalogRuleConfigurable/Test/Unit/Plugin/CatalogRule/Model/Rule/ValidationTest.php index 7802289c5fe9e..63447f067faf6 100644 --- a/app/code/Magento/CatalogRuleConfigurable/Test/Unit/Plugin/CatalogRule/Model/Rule/ValidationTest.php +++ b/app/code/Magento/CatalogRuleConfigurable/Test/Unit/Plugin/CatalogRule/Model/Rule/ValidationTest.php @@ -63,7 +63,7 @@ protected function setUp() * @dataProvider dataProviderForValidateWithValidConfigurableProduct * @return void */ - public function testAroundValidateWithValidConfigurableProduct( + public function testAfterValidateWithValidConfigurableProduct( $parentsIds, $validationResult, $runValidateAmount, From 7039ceef1339866837b9e703ee2828744641b6f6 Mon Sep 17 00:00:00 2001 From: Viktor Paladiychuk Date: Wed, 27 Jul 2016 18:17:26 +0300 Subject: [PATCH 34/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- app/code/Magento/Backend/App/Action/Plugin/MassactionKey.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Backend/App/Action/Plugin/MassactionKey.php b/app/code/Magento/Backend/App/Action/Plugin/MassactionKey.php index 5365c14a6d451..44cfe2bb17dc8 100644 --- a/app/code/Magento/Backend/App/Action/Plugin/MassactionKey.php +++ b/app/code/Magento/Backend/App/Action/Plugin/MassactionKey.php @@ -28,6 +28,6 @@ public function beforeDispatch(AbstractAction $subject, RequestInterface $reques $postData = $request->getPost($key); $value = is_array($postData) ? $postData : explode(',', $postData); $request->setPostValue($key, $value ? $value : null); - }; + } } } From cb4c244bb2de5d0d016bb222d223f6c8d9f16c27 Mon Sep 17 00:00:00 2001 From: Viktor Paladiychuk Date: Thu, 28 Jul 2016 10:53:15 +0300 Subject: [PATCH 35/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- .../Product/Eav/Plugin/AttributeSet.php | 18 +++++++------ .../Product/Plugin/StoreGroupTest.php | 9 ++++--- .../Category/Product/Plugin/StoreViewTest.php | 25 +++---------------- .../Product/Eav/Plugin/AttributeSetTest.php | 4 +-- 4 files changed, 20 insertions(+), 36 deletions(-) diff --git a/app/code/Magento/Catalog/Model/Indexer/Product/Eav/Plugin/AttributeSet.php b/app/code/Magento/Catalog/Model/Indexer/Product/Eav/Plugin/AttributeSet.php index beee265673c31..66251853e2bbb 100644 --- a/app/code/Magento/Catalog/Model/Indexer/Product/Eav/Plugin/AttributeSet.php +++ b/app/code/Magento/Catalog/Model/Indexer/Product/Eav/Plugin/AttributeSet.php @@ -61,21 +61,23 @@ private function getAttributeSetFactory() * * @param EavAttributeSet $subject * - * @return bool + * @return void */ public function beforeSave(EavAttributeSet $subject) { $this->requiresReindex = false; - if ( $subject->getId() ) { + if ($subject->getId()) { /** @var EavAttributeSet $originalSet */ $originalSet = $this->getAttributeSetFactory()->create(); $originalSet->initFromSkeleton($subject->getId()); - $originalAttributeCodes = array_flip( $this->_attributeFilter->filter( $originalSet ) ); - $subjectAttributeCodes = array_flip( $this->_attributeFilter->filter( $subject ) ); - $this->requiresReindex = (bool) count( array_merge( - array_diff_key( $subjectAttributeCodes, $originalAttributeCodes ), - array_diff_key( $originalAttributeCodes, $subjectAttributeCodes ) - ) ); + $originalAttributeCodes = array_flip($this->_attributeFilter->filter($originalSet)); + $subjectAttributeCodes = array_flip($this->_attributeFilter->filter($subject)); + $this->requiresReindex = (bool)count( + array_merge( + array_diff_key($subjectAttributeCodes, $originalAttributeCodes), + array_diff_key($originalAttributeCodes, $subjectAttributeCodes) + ) + ); } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Product/Plugin/StoreGroupTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Product/Plugin/StoreGroupTest.php index 6a1c2b5f046a3..04db59adc2f25 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Product/Plugin/StoreGroupTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Product/Plugin/StoreGroupTest.php @@ -44,8 +44,8 @@ protected function setUp() { $this->groupMock = $this->getMock( GroupModel::class, - [ 'dataHasChangedFor', 'isObjectNew', '__wakeup' ], - [ ], + ['dataHasChangedFor', 'isObjectNew', '__wakeup'], + [], '', false ); @@ -67,7 +67,8 @@ protected function setUp() false ); - $this->model = (new ObjectManager($this))->getObject(StoreGroup::class, ['indexerRegistry' => $this->indexerRegistryMock]); + $this->model = (new ObjectManager($this)) + ->getObject(StoreGroup::class, ['indexerRegistry' => $this->indexerRegistryMock]); } /** @@ -118,7 +119,7 @@ public function testBeforeAndAfterSaveWithoutChanges() $this->assertSame($this->subject, $this->model->afterSave($this->subject, $this->subject, $this->groupMock )); } - protected function mockIndexerMethods() + private function mockIndexerMethods() { $this->indexerMock->expects($this->once())->method('invalidate'); $this->indexerRegistryMock->expects($this->once()) diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Product/Plugin/StoreViewTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Product/Plugin/StoreViewTest.php index bbe025f09603c..fc0721d6b4e99 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Product/Plugin/StoreViewTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Product/Plugin/StoreViewTest.php @@ -82,7 +82,7 @@ public function testAroundSaveHasChanged() $this->storeMock->expects($this->once()) ->method('dataHasChangedFor') ->with('group_id') - ->will($this->returnValue(true)); + ->willReturn(true); $this->model->beforeSave($this->subject, $this->storeMock); $this->assertSame($this->subject, $this->model->afterSave($this->subject, $this->subject, $this->storeMock)); } @@ -92,31 +92,12 @@ public function testAroundSaveNoNeed() $this->storeMock->expects($this->once()) ->method('dataHasChangedFor') ->with('group_id') - ->will($this->returnValue(false) - ); + ->willReturn(false); $this->model->beforeSave($this->subject, $this->storeMock); $this->assertSame($this->subject, $this->model->afterSave($this->subject, $this->subject, $this->storeMock)); } - /** - * @return \PHPUnit_Framework_MockObject_MockObject|\Magento\Indexer\Model\Indexer\State - */ - protected function getStateMock() - { - $stateMock = $this->getMock( - 'Magento\Indexer\Model\Indexer\State', - ['setStatus', 'save', '__wakeup'], - [], - '', - false - ); - $stateMock->expects($this->once())->method('setStatus')->with('invalid')->will($this->returnSelf()); - $stateMock->expects($this->once())->method('save')->will($this->returnSelf()); - - return $stateMock; - } - - protected function mockIndexerMethods() + private function mockIndexerMethods() { $this->indexerMock->expects($this->once())->method('invalidate'); $this->indexerRegistryMock->expects($this->once()) diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Eav/Plugin/AttributeSetTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Eav/Plugin/AttributeSetTest.php index 7b6a5123d9fc3..6656b728c49e7 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Eav/Plugin/AttributeSetTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Eav/Plugin/AttributeSetTest.php @@ -84,8 +84,8 @@ public function testBeforeSave() ); $this->subjectMock->expects($this->exactly(2)) - ->method('getId') - ->willReturn($setId); + ->method('getId') + ->willReturn($setId); $this->model->beforeSave($this->subjectMock); } From a56f96b835849e1c9c38d1a4fcd7db4218280bda Mon Sep 17 00:00:00 2001 From: Viktor Paladiychuk Date: Thu, 28 Jul 2016 12:48:28 +0300 Subject: [PATCH 36/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- app/code/Magento/CatalogRuleConfigurable/composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/CatalogRuleConfigurable/composer.json b/app/code/Magento/CatalogRuleConfigurable/composer.json index 994958bb03b86..188300d39fe76 100644 --- a/app/code/Magento/CatalogRuleConfigurable/composer.json +++ b/app/code/Magento/CatalogRuleConfigurable/composer.json @@ -5,7 +5,8 @@ "php": "~5.6.0|7.0.2|7.0.4|~7.0.6", "magento/module-configurable-product": "100.2.*", "magento/framework": "100.2.*", - "magento/magento-composer-installer": "*" + "magento/magento-composer-installer": "*", + "magento/module-catalog": "101.1.*" }, "suggest": { "magento/module-catalog-rule": "100.2.*" From 5f4ebf7b78610d9489eab789abd89ee8c771c6ce Mon Sep 17 00:00:00 2001 From: Viktor Paladiychuk Date: Thu, 28 Jul 2016 13:51:04 +0300 Subject: [PATCH 37/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- .../Model/Indexer/Category/Product/Plugin/StoreViewTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Product/Plugin/StoreViewTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Product/Plugin/StoreViewTest.php index fc0721d6b4e99..f27d2b14f31cd 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Product/Plugin/StoreViewTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Product/Plugin/StoreViewTest.php @@ -71,7 +71,7 @@ protected function setUp() public function testAroundSaveNewObject() { $this->mockIndexerMethods(); - $this->storeMock->expects($this->once())->method('isObjectNew')->will($this->returnValue(true)); + $this->storeMock->expects($this->once())->method('isObjectNew')->willReturn(true); $this->model->beforeSave($this->subject, $this->storeMock); $this->assertSame($this->subject, $this->model->afterSave($this->subject, $this->subject, $this->storeMock)); } @@ -103,6 +103,6 @@ private function mockIndexerMethods() $this->indexerRegistryMock->expects($this->once()) ->method('get') ->with(\Magento\Catalog\Model\Indexer\Category\Product::INDEXER_ID) - ->will($this->returnValue($this->indexerMock)); + ->willReturn($this->indexerMock); } } From 807fd5b73f6f4d0e814f22083de46847b585223f Mon Sep 17 00:00:00 2001 From: Viktor Paladiychuk Date: Thu, 28 Jul 2016 14:42:25 +0300 Subject: [PATCH 38/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- .../Unit/Model/Indexer/Product/Eav/Plugin/AttributeSetTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Eav/Plugin/AttributeSetTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Eav/Plugin/AttributeSetTest.php index 6656b728c49e7..6169ff9960fbb 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Eav/Plugin/AttributeSetTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Eav/Plugin/AttributeSetTest.php @@ -54,7 +54,7 @@ public function setUp() $this->filterMock = $this->getMock(IndexableAttributeFilter::class, [], [], '', false); $this->subjectMock = $this->getMock(Set::class, [], [], '', false); $this->eavProcessorMock = $this->getMock(Processor::class, [], [], '', false); - $this->setFactoryMock = $this->getMock(SetFactory::class, [], [], '', false); + $this->setFactoryMock = $this->getMock(SetFactory::class, ['create'], [], '', false); $this->objectManager = new ObjectManager($this); } From 284949493e490613a47e17ab408bf985a6b1bc56 Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Thu, 28 Jul 2016 15:34:13 +0300 Subject: [PATCH 39/57] MAGETWO-55693: Stabilize code & prepare PR --- .../Test/Unit/App/Action/Plugin/MassactionKeyTest.php | 8 +++++++- .../Model/Indexer/Category/Flat/Plugin/StoreGroupTest.php | 2 +- .../Model/Plugin/AroundProductRepositorySave.php | 3 +++ .../Test/Unit/Block/Plugin/FrontTabPluginTest.php | 3 +++ 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Backend/Test/Unit/App/Action/Plugin/MassactionKeyTest.php b/app/code/Magento/Backend/Test/Unit/App/Action/Plugin/MassactionKeyTest.php index 66043a726c5b1..16d2b7ade9ee9 100644 --- a/app/code/Magento/Backend/Test/Unit/App/Action/Plugin/MassactionKeyTest.php +++ b/app/code/Magento/Backend/Test/Unit/App/Action/Plugin/MassactionKeyTest.php @@ -33,7 +33,13 @@ protected function setUp() }; $this->subjectMock = $this->getMock('Magento\Backend\App\AbstractAction', [], [], '', false); $this->requestMock = $this->getMockForAbstractClass( - RequestInterface::class, [], '', false, false, true, ['getPost', 'setPostValue'] + RequestInterface::class, + [], + '', + false, + false, + true, + ['getPost', 'setPostValue'] ); $objectManager = new ObjectManager($this); diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/StoreGroupTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/StoreGroupTest.php index dce5ce9d85228..adf59b5721707 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/StoreGroupTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/StoreGroupTest.php @@ -48,7 +48,7 @@ class StoreGroupTest extends \PHPUnit_Framework_TestCase protected function setUp() { $this->indexerMock = $this->getMockForAbstractClass( - IndexerInterface::class, + IndexerInterface::class, [], '', false, diff --git a/app/code/Magento/CatalogInventory/Model/Plugin/AroundProductRepositorySave.php b/app/code/Magento/CatalogInventory/Model/Plugin/AroundProductRepositorySave.php index 7ffc36b1143ab..92c8c02f75585 100644 --- a/app/code/Magento/CatalogInventory/Model/Plugin/AroundProductRepositorySave.php +++ b/app/code/Magento/CatalogInventory/Model/Plugin/AroundProductRepositorySave.php @@ -16,6 +16,9 @@ use Magento\Store\Model\StoreManagerInterface; use Magento\Framework\Exception\CouldNotSaveException; +/** + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + */ class AroundProductRepositorySave { /** diff --git a/app/code/Magento/CatalogSearch/Test/Unit/Block/Plugin/FrontTabPluginTest.php b/app/code/Magento/CatalogSearch/Test/Unit/Block/Plugin/FrontTabPluginTest.php index 636795b41c219..e108def990bfe 100644 --- a/app/code/Magento/CatalogSearch/Test/Unit/Block/Plugin/FrontTabPluginTest.php +++ b/app/code/Magento/CatalogSearch/Test/Unit/Block/Plugin/FrontTabPluginTest.php @@ -14,6 +14,9 @@ use Magento\Framework\Data\Form\Element\AbstractElement; use Magento\Framework\View\Element\AbstractBlock; +/** + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + */ class FrontTabPluginTest extends \PHPUnit_Framework_TestCase { /** From db58407f5e56a4e50974b4ce368ee4702926a73f Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Thu, 28 Jul 2016 16:02:40 +0300 Subject: [PATCH 40/57] MAGETWO-55693: Stabilize code & prepare PR --- .../Category/Flat/Plugin/StoreGroupTest.php | 20 +++++++++---------- .../CatalogRule/Model/Rule/ValidationTest.php | 1 - 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/StoreGroupTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/StoreGroupTest.php index adf59b5721707..9757f0aef26aa 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/StoreGroupTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/StoreGroupTest.php @@ -91,14 +91,14 @@ public function testBeforeAndAfterSave() ->with(State::INDEXER_ID) ->willReturn($this->indexerMock); $this->groupMock->expects($this->once()) - ->method('dataHasChangedFor') - ->with('root_category_id') - ->willReturn(true); + ->method('dataHasChangedFor') + ->with('root_category_id') + ->willReturn(true); $this->groupMock->expects($this->once())->method('isObjectNew')->willReturn(false); $this->model->beforeSave($this->subjectMock, $this->groupMock); $this->assertSame( - $this->subjectMock, - $this->model->afterSave($this->subjectMock, $this->subjectMock, $this->groupMock) + $this->subjectMock, + $this->model->afterSave($this->subjectMock, $this->subjectMock, $this->groupMock) ); } @@ -106,14 +106,14 @@ public function testBeforeAndAfterSaveNotNew() { $this->stateMock->expects($this->never())->method('isFlatEnabled'); $this->groupMock->expects($this->once()) - ->method('dataHasChangedFor') - ->with('root_category_id') - ->willReturn(true); + ->method('dataHasChangedFor') + ->with('root_category_id') + ->willReturn(true); $this->groupMock->expects($this->once())->method('isObjectNew')->willReturn(true); $this->model->beforeSave($this->subjectMock, $this->groupMock); $this->assertSame( - $this->subjectMock, - $this->model->afterSave($this->subjectMock, $this->subjectMock, $this->groupMock) + $this->subjectMock, + $this->model->afterSave($this->subjectMock, $this->subjectMock, $this->groupMock) ); } } diff --git a/app/code/Magento/CatalogRuleConfigurable/Test/Unit/Plugin/CatalogRule/Model/Rule/ValidationTest.php b/app/code/Magento/CatalogRuleConfigurable/Test/Unit/Plugin/CatalogRule/Model/Rule/ValidationTest.php index 63447f067faf6..c727747caaffb 100644 --- a/app/code/Magento/CatalogRuleConfigurable/Test/Unit/Plugin/CatalogRule/Model/Rule/ValidationTest.php +++ b/app/code/Magento/CatalogRuleConfigurable/Test/Unit/Plugin/CatalogRule/Model/Rule/ValidationTest.php @@ -45,7 +45,6 @@ protected function setUp() false ); - $this->ruleMock = $this->getMock('Magento\CatalogRule\Model\Rule', [], [], '', false); $this->ruleConditionsMock = $this->getMock('Magento\Rule\Model\Condition\Combine', [], [], '', false); $this->productMock = $this->getMock('Magento\Framework\DataObject', ['getId']); From f0c3f37b5b1cd7ca34e0604326312deb8ceb9cb0 Mon Sep 17 00:00:00 2001 From: Yuri Kovsher Date: Thu, 28 Jul 2016 16:18:31 +0300 Subject: [PATCH 41/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- .../Product/Flat/Plugin/IndexerConfigData.php | 61 +++++---- .../Model/Plugin/QuoteItemProductOption.php | 56 +++++--- .../Flat/Plugin/IndexerConfigDataTest.php | 57 +++++--- .../Plugin/QuoteItemProductOptionTest.php | 107 +++++++++------ .../Product/Save/ApplyRulesAfterReindex.php | 11 +- .../Block/Plugin/FrontTabPlugin.php | 3 + .../Fulltext/Plugin/AbstractPlugin.php | 23 ++-- .../Fulltext/Plugin/Product/Action.php | 34 ++--- .../Indexer/Fulltext/Plugin/Store/Group.php | 52 ++++---- .../Indexer/Fulltext/Plugin/Store/View.php | 50 +++---- .../Fulltext/Plugin/Product/ActionTest.php | 126 ++++++++++-------- .../Fulltext/Plugin/Store/GroupTest.php | 120 +++++++++-------- .../Fulltext/Plugin/Store/ViewTest.php | 116 ++++++++-------- 13 files changed, 468 insertions(+), 348 deletions(-) diff --git a/app/code/Magento/Catalog/Model/Indexer/Product/Flat/Plugin/IndexerConfigData.php b/app/code/Magento/Catalog/Model/Indexer/Product/Flat/Plugin/IndexerConfigData.php index a561fd96ada15..0777e9a06e348 100644 --- a/app/code/Magento/Catalog/Model/Indexer/Product/Flat/Plugin/IndexerConfigData.php +++ b/app/code/Magento/Catalog/Model/Indexer/Product/Flat/Plugin/IndexerConfigData.php @@ -5,49 +5,62 @@ */ namespace Magento\Catalog\Model\Indexer\Product\Flat\Plugin; +use Magento\Catalog\Model\Indexer\Product\Flat\State as ProductFlatIndexerState; +use Magento\Indexer\Model\Config\Data as ConfigData; +use Magento\Catalog\Model\Indexer\Product\Flat\Processor as ProductFlatIndexerProcessor; + +/** + * Plugin for Magento\Indexer\Model\Config\Data + */ class IndexerConfigData { /** - * @var \Magento\Catalog\Model\Indexer\Product\Flat\State + * @var ProductFlatIndexerState */ - protected $_state; + protected $state; /** - * @param \Magento\Catalog\Model\Indexer\Product\Flat\State $state + * @param ProductFlatIndexerState $state */ - public function __construct(\Magento\Catalog\Model\Indexer\Product\Flat\State $state) + public function __construct(ProductFlatIndexerState $state) { - $this->_state = $state; + $this->state = $state; } /** - * Around get handler + * Modify returned config when flat indexer is disabled * - * @param \Magento\Indexer\Model\Config\Data $subject + * @param ConfigData $subject * @param mixed $data * @param string $path * @param string $default + * @return mixed * - * @return mixed|null * @SuppressWarnings(PHPMD.UnusedFormalParameter) * */ - public function afterGet( - \Magento\Indexer\Model\Config\Data $subject, - $data, - $path = null, - $default = null - ) { - if (!$this->_state->isFlatEnabled()) { - $indexerId = \Magento\Catalog\Model\Indexer\Product\Flat\Processor::INDEXER_ID; - if (!$path && isset($data[$indexerId])) { - unset($data[$indexerId]); - } elseif ($path) { - list($firstKey,) = explode('/', $path); - if ($firstKey == $indexerId) { - $data = $default; - } - } + public function afterGet(ConfigData $subject, $data, $path = null, $default = null) + { + if ($this->state->isFlatEnabled()) { + return $data; + } + + $indexerId = ProductFlatIndexerProcessor::INDEXER_ID; + + if (!$path && isset($data[$indexerId])) { + unset($data[$indexerId]); + + return $data; + } + + if (!$path) { + return $data; + } + + list($firstKey) = explode('/', $path); + + if ($firstKey == $indexerId) { + $data = $default; } return $data; diff --git a/app/code/Magento/Catalog/Model/Plugin/QuoteItemProductOption.php b/app/code/Magento/Catalog/Model/Plugin/QuoteItemProductOption.php index 4dbd9e18d9928..57cf1fdc4994e 100644 --- a/app/code/Magento/Catalog/Model/Plugin/QuoteItemProductOption.php +++ b/app/code/Magento/Catalog/Model/Plugin/QuoteItemProductOption.php @@ -5,35 +5,51 @@ */ namespace Magento\Catalog\Model\Plugin; +use Magento\Quote\Model\Quote\Item\ToOrderItem as QuoteToOrderItem; +use Magento\Quote\Model\Quote\Item\AbstractItem as AbstractQuoteItem; +use Magento\Catalog\Model\Product\Option as ProductOption; + +/** + * Plugin for Magento\Quote\Model\Quote\Item\ToOrderItem + */ class QuoteItemProductOption { /** - * @param \Magento\Quote\Model\Quote\Item\ToOrderItem $subject - * @param \Magento\Quote\Model\Quote\Item\AbstractItem $quoteItem - * @return \Magento\Sales\Model\Order\Item + * Perform preparations for custom options + * + * @param QuoteToOrderItem $subject + * @param AbstractQuoteItem $quoteItem + * @return void + * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function beforeConvert( - \Magento\Quote\Model\Quote\Item\ToOrderItem $subject, - \Magento\Quote\Model\Quote\Item\AbstractItem $quoteItem + QuoteToOrderItem $subject, + AbstractQuoteItem $quoteItem ) { - if (is_array($quoteItem->getOptions())) { - foreach ($quoteItem->getOptions() as $itemOption) { - $code = explode('_', $itemOption->getCode()); + if (!is_array($quoteItem->getOptions())) { + return; + } + + foreach ($quoteItem->getOptions() as $itemOption) { + $code = explode('_', $itemOption->getCode()); - if (isset($code[1]) && is_numeric($code[1])) { - $option = $quoteItem->getProduct()->getOptionById($code[1]); + if (!isset($code[1]) || !is_numeric($code[1])) { + continue; + } + + $option = $quoteItem->getProduct()->getOptionById($code[1]); + + if (!$option || $option->getType() != ProductOption::OPTION_TYPE_FILE) { + continue; + } - if ($option && $option->getType() == \Magento\Catalog\Model\Product\Option::OPTION_TYPE_FILE) { - try { - $option->groupFactory($option->getType()) - ->setQuoteItemOption($itemOption) - ->copyQuoteToOrder(); - } catch (\Exception $e) { - continue; - } - } - } + try { + $option->groupFactory($option->getType()) + ->setQuoteItemOption($itemOption) + ->copyQuoteToOrder(); + } catch (\Exception $e) { + continue; } } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Flat/Plugin/IndexerConfigDataTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Flat/Plugin/IndexerConfigDataTest.php index 5d8e59ee91cb6..53d1a8fd004a0 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Flat/Plugin/IndexerConfigDataTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Flat/Plugin/IndexerConfigDataTest.php @@ -5,35 +5,47 @@ */ namespace Magento\Catalog\Test\Unit\Model\Indexer\Product\Flat\Plugin; +use Magento\Catalog\Model\Indexer\Product\Flat\Plugin\IndexerConfigData as IndexerConfigDataPlugin; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; +use Magento\Catalog\Model\Indexer\Product\Flat\State as ProductFlatIndexerState; +use Magento\Indexer\Model\Config\Data as ConfigData; + class IndexerConfigDataTest extends \PHPUnit_Framework_TestCase { /** - * @var \Magento\Catalog\Model\Indexer\Product\Flat\Plugin\IndexerConfigData + * @var IndexerConfigDataPlugin */ - protected $model; + private $plugin; /** - * @var \Magento\Catalog\Model\Indexer\Product\Flat\State|\PHPUnit_Framework_MockObject_MockObject + * @var ObjectManagerHelper */ - protected $_stateMock; + private $objectManagerHelper; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var ProductFlatIndexerState|\PHPUnit_Framework_MockObject_MockObject */ - protected $subjectMock; + private $indexerStateMock; + + /** + * @var ConfigData|\PHPUnit_Framework_MockObject_MockObject + */ + private $subjectMock; protected function setUp() { - $this->_stateMock = $this->getMock( - 'Magento\Catalog\Model\Indexer\Product\Flat\State', - ['isFlatEnabled'], - [], - '', - false - ); - $this->subjectMock = $this->getMock('Magento\Indexer\Model\Config\Data', [], [], '', false); + $this->indexerStateMock = $this->getMockBuilder(ProductFlatIndexerState::class) + ->disableOriginalConstructor() + ->getMock(); + $this->subjectMock = $this->getMockBuilder(ConfigData::class) + ->disableOriginalConstructor() + ->getMock(); - $this->model = new \Magento\Catalog\Model\Indexer\Product\Flat\Plugin\IndexerConfigData($this->_stateMock); + $this->objectManagerHelper = new ObjectManagerHelper($this); + $this->plugin = $this->objectManagerHelper->getObject( + IndexerConfigDataPlugin::class, + ['state' => $this->indexerStateMock] + ); } /** @@ -42,29 +54,36 @@ protected function setUp() * @param mixed $default * @param array $inputData * @param array $outputData + * * @dataProvider afterGetDataProvider */ public function testAfterGet($isFlat, $path, $default, $inputData, $outputData) { - $this->_stateMock->expects($this->once())->method('isFlatEnabled')->will($this->returnValue($isFlat)); + $this->indexerStateMock->expects(static::once()) + ->method('isFlatEnabled') + ->willReturn($isFlat); - $this->assertEquals($outputData, $this->model->afterGet($this->subjectMock, $inputData, $path, $default)); + $this->assertEquals($outputData, $this->plugin->afterGet($this->subjectMock, $inputData, $path, $default)); } + /** + * @return array + */ public function afterGetDataProvider() { $flatIndexerData = [ 'indexer_id' => 'catalog_product_flat', 'action' => '\Action\Class', 'title' => 'Title', - 'description' => 'Description', + 'description' => 'Description' ]; $otherIndexerData = [ 'indexer_id' => 'other_indexer', 'action' => '\Action\Class', 'title' => 'Title', - 'description' => 'Description', + 'description' => 'Description' ]; + return [ // flat is enabled, nothing is being changed [ diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Plugin/QuoteItemProductOptionTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Plugin/QuoteItemProductOptionTest.php index b47a7a2b571e7..4ca33d89ba3f7 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Plugin/QuoteItemProductOptionTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Plugin/QuoteItemProductOptionTest.php @@ -5,65 +5,92 @@ */ namespace Magento\Catalog\Test\Unit\Model\Plugin; +use Magento\Catalog\Model\Plugin\QuoteItemProductOption as QuoteItemProductOptionPlugin; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; +use Magento\Quote\Model\Quote\Item\ToOrderItem as QuoteToOrderItem; +use Magento\Quote\Model\Quote\Item\AbstractItem as AbstractQuoteItem; +use Magento\Quote\Model\Quote\Item\Option as QuoteItemOption; +use Magento\Catalog\Model\Product; +use Magento\Framework\DataObject; +use Magento\Catalog\Model\Product\Option as ProductOption; + class QuoteItemProductOptionTest extends \PHPUnit_Framework_TestCase { - /** @var \PHPUnit_Framework_MockObject_MockObject */ - protected $quoteItemMock; + /** + * @var QuoteItemProductOptionPlugin + */ + private $plugin; + + /** + * @var ObjectManagerHelper + */ + private $objectManagerHelper; + + /** + * @var QuoteToOrderItem|\PHPUnit_Framework_MockObject_MockObject + */ + private $subjectMock; - /** @var \PHPUnit_Framework_MockObject_MockObject */ - protected $orderItemMock; + /** + * @var AbstractQuoteItem|\PHPUnit_Framework_MockObject_MockObject + */ + private $quoteItemMock; - /** @var \Magento\Catalog\Model\Plugin\QuoteItemProductOption */ - protected $model; + /** + * @var QuoteItemOption|\PHPUnit_Framework_MockObject_MockObject + */ + private $quoteItemOptionMock; /** - * @var \Magento\Quote\Model\Quote\Item\ToOrderItem|\PHPUnit_Framework_MockObject_MockObject + * @var Product|\PHPUnit_Framework_MockObject_MockObject */ - protected $subjectMock; + private $productMock; protected function setUp() { - $this->orderItemMock = $this->getMock('Magento\Sales\Model\Order\Item', [], [], '', false); - $this->quoteItemMock = $this->getMock('Magento\Quote\Model\Quote\Item', [], [], '', false); - $this->subjectMock = $this->getMock('Magento\Quote\Model\Quote\Item\ToOrderItem', [], [], '', false); - $this->model = new \Magento\Catalog\Model\Plugin\QuoteItemProductOption(); + $this->subjectMock = $this->getMockBuilder(QuoteToOrderItem::class) + ->disableOriginalConstructor() + ->getMock(); + $this->quoteItemMock = $this->getMockBuilder(AbstractQuoteItem::class) + ->disableOriginalConstructor() + ->setMethods(['getOptions', 'getProduct']) + ->getMockForAbstractClass(); + $this->quoteItemOptionMock = $this->getMockBuilder(QuoteItemOption::class) + ->disableOriginalConstructor() + ->setMethods(['getCode']) + ->getMock(); + $this->productMock = $this->getMockBuilder(Product::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->objectManagerHelper = new ObjectManagerHelper($this); + $this->plugin = $this->objectManagerHelper->getObject(QuoteItemProductOptionPlugin::class); } public function testBeforeItemToOrderItemEmptyOptions() { - $this->quoteItemMock->expects($this->exactly(2))->method('getOptions')->will($this->returnValue([])); + $this->quoteItemMock->expects(static::once()) + ->method('getOptions') + ->willReturn(null); - $this->assertNull($this->model->beforeConvert($this->subjectMock, $this->quoteItemMock)); + $this->plugin->beforeConvert($this->subjectMock, $this->quoteItemMock); } public function testBeforeItemToOrderItemWithOptions() { - $itemOption = $this->getMock( - 'Magento\Quote\Model\Quote\Item\Option', - ['getCode', '__wakeup'], - [], - '', - false - ); - $this->quoteItemMock->expects( - $this->exactly(2) - )->method( - 'getOptions' - )->will( - $this->returnValue([$itemOption, $itemOption]) - ); - - $itemOption->expects($this->at(0))->method('getCode')->will($this->returnValue('someText_8')); - $itemOption->expects($this->at(1))->method('getCode')->will($this->returnValue('not_int_text')); - - $productMock = $this->getMock('Magento\Catalog\Model\Product', [], [], '', false); - $optionMock = $this->getMock('stdClass', ['getType']); - $optionMock->expects($this->once())->method('getType'); - - $productMock->expects($this->once())->method('getOptionById')->will($this->returnValue($optionMock)); - - $this->quoteItemMock->expects($this->once())->method('getProduct')->will($this->returnValue($productMock)); + $this->quoteItemMock->expects(static::exactly(2)) + ->method('getOptions') + ->willReturn([$this->quoteItemOptionMock, $this->quoteItemOptionMock]); + $this->quoteItemOptionMock->expects(static::exactly(2)) + ->method('getCode') + ->willReturnOnConsecutiveCalls('someText_8', 'not_int_text'); + $this->productMock->expects(static::once()) + ->method('getOptionById') + ->willReturn(new DataObject(['type' => ProductOption::OPTION_TYPE_FILE])); + $this->quoteItemMock->expects(static::once()) + ->method('getProduct') + ->willReturn($this->productMock); - $this->assertNull($this->model->beforeConvert($this->subjectMock, $this->quoteItemMock)); + $this->plugin->beforeConvert($this->subjectMock, $this->quoteItemMock); } } diff --git a/app/code/Magento/CatalogRule/Plugin/Indexer/Product/Save/ApplyRulesAfterReindex.php b/app/code/Magento/CatalogRule/Plugin/Indexer/Product/Save/ApplyRulesAfterReindex.php index d249136298964..5416c2e59b00c 100644 --- a/app/code/Magento/CatalogRule/Plugin/Indexer/Product/Save/ApplyRulesAfterReindex.php +++ b/app/code/Magento/CatalogRule/Plugin/Indexer/Product/Save/ApplyRulesAfterReindex.php @@ -6,7 +6,11 @@ namespace Magento\CatalogRule\Plugin\Indexer\Product\Save; use Magento\CatalogRule\Model\Indexer\Product\ProductRuleProcessor; +use Magento\Catalog\Model\Product; +/** + * Plugin for Magento\Catalog\Model\Product + */ class ApplyRulesAfterReindex { /** @@ -25,12 +29,11 @@ public function __construct(ProductRuleProcessor $productRuleProcessor) /** * Apply catalog rules after product resource model save * - * @param \Magento\Catalog\Model\Product $subject + * @param Product $subject * @return void */ - public function afterReindex( - \Magento\Catalog\Model\Product $subject - ) { + public function afterReindex(Product $subject) + { $this->productRuleProcessor->reindexRow($subject->getId()); } } diff --git a/app/code/Magento/CatalogSearch/Block/Plugin/FrontTabPlugin.php b/app/code/Magento/CatalogSearch/Block/Plugin/FrontTabPlugin.php index 4348ecfb6ec0d..9b5c324d0dfa0 100644 --- a/app/code/Magento/CatalogSearch/Block/Plugin/FrontTabPlugin.php +++ b/app/code/Magento/CatalogSearch/Block/Plugin/FrontTabPlugin.php @@ -10,6 +10,9 @@ use Magento\Framework\Data\Form; use Magento\Framework\Data\Form\Element\Fieldset; +/** + * Plugin for Magento\Catalog\Block\Adminhtml\Product\Attribute\Edit\Tab\Front + */ class FrontTabPlugin { /** diff --git a/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/AbstractPlugin.php b/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/AbstractPlugin.php index bbdb24bec0918..956f07206c20f 100644 --- a/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/AbstractPlugin.php +++ b/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/AbstractPlugin.php @@ -5,19 +5,24 @@ */ namespace Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin; -use Magento\CatalogSearch\Model\Indexer\Fulltext; +use Magento\Framework\Indexer\IndexerRegistry; +use Magento\CatalogSearch\Model\Indexer\Fulltext as FulltextIndexer; +/** + * Abstract plugin for indexers + */ abstract class AbstractPlugin { - /** @var \Magento\Framework\Indexer\IndexerRegistry */ + /** + * @var IndexerRegistry + */ protected $indexerRegistry; /** - * @param \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry + * @param IndexerRegistry $indexerRegistry */ - public function __construct( - \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry - ) { + public function __construct(IndexerRegistry $indexerRegistry) + { $this->indexerRegistry = $indexerRegistry; } @@ -29,7 +34,8 @@ public function __construct( */ protected function reindexRow($productId) { - $indexer = $this->indexerRegistry->get(Fulltext::INDEXER_ID); + $indexer = $this->indexerRegistry->get(FulltextIndexer::INDEXER_ID); + if (!$indexer->isScheduled()) { $indexer->reindexRow($productId); } @@ -43,7 +49,8 @@ protected function reindexRow($productId) */ protected function reindexList(array $productIds) { - $indexer = $this->indexerRegistry->get(Fulltext::INDEXER_ID); + $indexer = $this->indexerRegistry->get(FulltextIndexer::INDEXER_ID); + if (!$indexer->isScheduled()) { $indexer->reindexList($productIds); } diff --git a/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Product/Action.php b/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Product/Action.php index f3925b825fee7..da85976030d2b 100644 --- a/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Product/Action.php +++ b/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Product/Action.php @@ -3,45 +3,45 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - namespace Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin\Product; -use Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin\AbstractPlugin; +use Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin\AbstractPlugin as AbstractIndexerPlugin; +use Magento\Catalog\Model\Product\Action as ProductAction; -class Action extends AbstractPlugin +/** + * Plugin for Magento\Catalog\Model\Product\Action + */ +class Action extends AbstractIndexerPlugin { /** * Reindex on product attribute mass change * - * @param \Magento\Catalog\Model\Product\Action $subject - * @param \Magento\Catalog\Model\Product\Action $action + * @param ProductAction $subject + * @param ProductAction $action * @param array $productIds - * @return \Magento\Catalog\Model\Product\Action + * @return ProductAction + * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function afterUpdateAttributes( - \Magento\Catalog\Model\Product\Action $subject, - \Magento\Catalog\Model\Product\Action $action, - array $productIds - ) { + public function afterUpdateAttributes(ProductAction $subject, ProductAction $action, array $productIds) + { $this->reindexList(array_unique($productIds)); + return $action; } /** * Reindex on product websites mass change * - * @param \Magento\Catalog\Model\Product\Action $subject + * @param ProductAction $subject * @param null $result * @param array $productIds * @return void + * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function afterUpdateWebsites( - \Magento\Catalog\Model\Product\Action $subject, - $result, - array $productIds - ) { + public function afterUpdateWebsites(ProductAction $subject, $result, array $productIds) + { $this->reindexList(array_unique($productIds)); } } diff --git a/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Store/Group.php b/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Store/Group.php index 22ddb2cc1ed2f..ed431110e0490 100644 --- a/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Store/Group.php +++ b/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Store/Group.php @@ -5,10 +5,15 @@ */ namespace Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin\Store; -use Magento\CatalogSearch\Model\Indexer\Fulltext; -use Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin\AbstractPlugin; +use Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin\AbstractPlugin as AbstractIndexerPlugin; +use Magento\Store\Model\ResourceModel\Group as StoreGroupResourceModel; +use Magento\Framework\Model\AbstractModel; +use Magento\CatalogSearch\Model\Indexer\Fulltext as FulltextIndexer; -class Group extends AbstractPlugin +/** + * Plugin for Magento\Store\Model\ResourceModel\Group + */ +class Group extends AbstractIndexerPlugin { /** * @var bool @@ -18,32 +23,30 @@ class Group extends AbstractPlugin /** * Check if indexer requires invalidation after store group save * - * @param \Magento\Store\Model\ResourceModel\Group $subject - * @param \Magento\Framework\Model\AbstractModel $group + * @param StoreGroupResourceModel $subject + * @param AbstractModel $group * @return void + * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function beforeSave( - \Magento\Store\Model\ResourceModel\Group $subject, - \Magento\Framework\Model\AbstractModel $group - ) { + public function beforeSave(StoreGroupResourceModel $subject, AbstractModel $group) + { $this->needInvalidation = !$group->isObjectNew() && $group->dataHasChangedFor('website_id'); } /** * Invalidate indexer on store group save * - * @param \Magento\Store\Model\ResourceModel\Group $subject - * @param \Magento\Store\Model\ResourceModel\Group $result - * @return \Magento\Store\Model\ResourceModel\Group + * @param StoreGroupResourceModel $subject + * @param StoreGroupResourceModel $result + * @return StoreGroupResourceModel + * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function afterSave( - \Magento\Store\Model\ResourceModel\Group $subject, - \Magento\Store\Model\ResourceModel\Group $result - ) { + public function afterSave(StoreGroupResourceModel $subject, StoreGroupResourceModel $result) + { if ($this->needInvalidation) { - $this->indexerRegistry->get(Fulltext::INDEXER_ID)->invalidate(); + $this->indexerRegistry->get(FulltextIndexer::INDEXER_ID)->invalidate(); } return $result; @@ -52,17 +55,16 @@ public function afterSave( /** * Invalidate indexer on store group delete * - * @param \Magento\Store\Model\ResourceModel\Group $subject - * @param \Magento\Store\Model\ResourceModel\Group $result + * @param StoreGroupResourceModel $subject + * @param StoreGroupResourceModel $result + * @return StoreGroupResourceModel * - * @return \Magento\Store\Model\ResourceModel\Group * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function afterDelete( - \Magento\Store\Model\ResourceModel\Group $subject, - \Magento\Store\Model\ResourceModel\Group $result - ) { - $this->indexerRegistry->get(Fulltext::INDEXER_ID)->invalidate(); + public function afterDelete(StoreGroupResourceModel $subject, StoreGroupResourceModel $result) + { + $this->indexerRegistry->get(FulltextIndexer::INDEXER_ID)->invalidate(); + return $result; } } diff --git a/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Store/View.php b/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Store/View.php index 5df3165111aad..5d57b71634d56 100644 --- a/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Store/View.php +++ b/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Store/View.php @@ -5,10 +5,15 @@ */ namespace Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin\Store; -use Magento\CatalogSearch\Model\Indexer\Fulltext; -use Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin\AbstractPlugin; +use Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin\AbstractPlugin as AbstractIndexerPlugin; +use Magento\Store\Model\ResourceModel\Store as StoreResourceModel; +use Magento\Framework\Model\AbstractModel; +use Magento\CatalogSearch\Model\Indexer\Fulltext as FulltextIndexer; -class View extends AbstractPlugin +/** + * Plugin for Magento\Store\Model\ResourceModel\Store + */ +class View extends AbstractIndexerPlugin { /** * @var bool @@ -18,34 +23,30 @@ class View extends AbstractPlugin /** * Check if indexer requires invalidation after store view save * - * @param \Magento\Store\Model\ResourceModel\Store $subject - * @param \Magento\Framework\Model\AbstractModel $store + * @param StoreResourceModel $subject + * @param AbstractModel $store * @return void * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function beforeSave( - \Magento\Store\Model\ResourceModel\Store $subject, - \Magento\Framework\Model\AbstractModel $store - ) { + public function beforeSave(StoreResourceModel $subject, AbstractModel $store) + { $this->needInvalidation = $store->isObjectNew(); } /** * Invalidate indexer on store view save * - * @param \Magento\Store\Model\ResourceModel\Store $subject - * @param \Magento\Store\Model\ResourceModel\Store $result - * @return \Magento\Store\Model\ResourceModel\Store + * @param StoreResourceModel $subject + * @param StoreResourceModel $result + * @return StoreResourceModel * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function afterSave( - \Magento\Store\Model\ResourceModel\Store $subject, - \Magento\Store\Model\ResourceModel\Store $result - ) { + public function afterSave(StoreResourceModel $subject, StoreResourceModel $result) + { if ($this->needInvalidation) { - $this->indexerRegistry->get(Fulltext::INDEXER_ID)->invalidate(); + $this->indexerRegistry->get(FulltextIndexer::INDEXER_ID)->invalidate(); } return $result; @@ -54,17 +55,16 @@ public function afterSave( /** * Invalidate indexer on store view delete * - * @param \Magento\Store\Model\ResourceModel\Store $subject - * @param \Magento\Store\Model\ResourceModel\Store $result + * @param StoreResourceModel $subject + * @param StoreResourceModel $result + * @return StoreResourceModel * - * @return \Magento\Store\Model\ResourceModel\Store * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function afterDelete( - \Magento\Store\Model\ResourceModel\Store $subject, - \Magento\Store\Model\ResourceModel\Store $result - ) { - $this->indexerRegistry->get(Fulltext::INDEXER_ID)->invalidate(); + public function afterDelete(StoreResourceModel $subject, StoreResourceModel $result) + { + $this->indexerRegistry->get(FulltextIndexer::INDEXER_ID)->invalidate(); + return $result; } } diff --git a/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/Product/ActionTest.php b/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/Product/ActionTest.php index b5bc0854f1678..f9d369a67812d 100644 --- a/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/Product/ActionTest.php +++ b/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/Product/ActionTest.php @@ -3,104 +3,122 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - namespace Magento\CatalogSearch\Test\Unit\Model\Indexer\Fulltext\Plugin\Product; -use \Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin\Product\Action; +use Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin\Product\Action as ProductActionIndexerPlugin; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; +use Magento\Framework\Indexer\IndexerRegistry; +use Magento\Framework\Indexer\IndexerInterface; +use Magento\Catalog\Model\Product\Action as ProductAction; +use Magento\CatalogSearch\Model\Indexer\Fulltext as FulltextIndexer; class ActionTest extends \PHPUnit_Framework_TestCase { /** - * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Indexer\IndexerInterface + * @var ProductActionIndexerPlugin + */ + private $plugin; + + /** + * @var ObjectManagerHelper */ - protected $indexerMock; + private $objectManagerHelper; /** - * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Model\Product\Action + * @var IndexerRegistry|\PHPUnit_Framework_MockObject_MockObject */ - protected $subjectMock; + private $indexerRegistryMock; /** - * @var \Magento\Framework\Indexer\IndexerRegistry|\PHPUnit_Framework_MockObject_MockObject + * @var IndexerInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $indexerRegistryMock; + private $indexerMock; /** - * @var Action + * @var ProductAction|\PHPUnit_Framework_MockObject_MockObject */ - protected $model; + private $subjectMock; protected function setUp() { - $this->subjectMock = $this->getMock('Magento\Catalog\Model\Product\Action', [], [], '', false); - - $this->indexerMock = $this->getMockForAbstractClass( - 'Magento\Framework\Indexer\IndexerInterface', - [], - '', - false, - false, - true, - ['getId', 'getState', '__wakeup'] - ); - $this->indexerRegistryMock = $this->getMock( - 'Magento\Framework\Indexer\IndexerRegistry', - ['get'], - [], - '', - false - ); + $this->indexerRegistryMock = $this->getMockBuilder(IndexerRegistry::class) + ->disableOriginalConstructor() + ->getMock(); + $this->indexerMock = $this->getMockBuilder(IndexerInterface::class) + ->getMockForAbstractClass(); + $this->subjectMock = $this->getMockBuilder(ProductAction::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->indexerRegistryMock->expects(static::once()) + ->method('get') + ->with(FulltextIndexer::INDEXER_ID) + ->willReturn($this->indexerMock); - $this->model = new Action($this->indexerRegistryMock); + $this->objectManagerHelper = new ObjectManagerHelper($this); + $this->plugin = $this->objectManagerHelper->getObject( + ProductActionIndexerPlugin::class, + ['indexerRegistry' => $this->indexerRegistryMock] + ); } public function testAfterUpdateAttributesNonScheduled() { - $this->indexerMock->expects($this->once())->method('isScheduled')->will($this->returnValue(false)); - $this->indexerMock->expects($this->once())->method('reindexList')->with([1, 2, 3]); - $this->prepareIndexer(); + $productIds = [1, 2, 3]; + + $this->indexerMock->expects(static::once()) + ->method('isScheduled') + ->willReturn(false); + $this->indexerMock->expects(static::once()) + ->method('reindexList') + ->with($productIds); - $this->assertEquals( + $this->assertSame( $this->subjectMock, - $this->model->afterUpdateAttributes($this->subjectMock, $this->subjectMock, [1, 2, 3]) + $this->plugin->afterUpdateAttributes($this->subjectMock, $this->subjectMock, $productIds) ); } public function testAfterUpdateAttributesScheduled() { - $this->indexerMock->expects($this->once())->method('isScheduled')->will($this->returnValue(true)); - $this->indexerMock->expects($this->never())->method('reindexList'); - $this->prepareIndexer(); + $productIds = [1, 2, 3]; - $this->assertEquals( + $this->indexerMock->expects(static::once()) + ->method('isScheduled') + ->willReturn(true); + $this->indexerMock->expects(static::never()) + ->method('reindexList'); + + $this->assertSame( $this->subjectMock, - $this->model->afterUpdateAttributes($this->subjectMock, $this->subjectMock, [1, 2, 3]) + $this->plugin->afterUpdateAttributes($this->subjectMock, $this->subjectMock, $productIds) ); } public function testAfterUpdateWebsitesNonScheduled() { - $this->indexerMock->expects($this->once())->method('isScheduled')->will($this->returnValue(false)); - $this->indexerMock->expects($this->once())->method('reindexList')->with([1, 2, 3]); - $this->prepareIndexer(); + $productIds = [1, 2, 3]; + + $this->indexerMock->expects(static::once()) + ->method('isScheduled') + ->willReturn(false); + $this->indexerMock->expects(static::once()) + ->method('reindexList') + ->with($productIds); - $this->model->afterUpdateWebsites($this->subjectMock, $this->subjectMock, [1, 2, 3]); + $this->plugin->afterUpdateWebsites($this->subjectMock, $this->subjectMock, $productIds); } public function testAfterUpdateWebsitesScheduled() { - $this->indexerMock->expects($this->once())->method('isScheduled')->will($this->returnValue(true)); - $this->indexerMock->expects($this->never())->method('reindexList'); - $this->prepareIndexer(); + $productIds = [1, 2, 3]; - $this->model->afterUpdateWebsites($this->subjectMock, $this->subjectMock, [1, 2, 3]); - } + $this->indexerMock->expects(static::once()) + ->method('isScheduled') + ->willReturn(true); + $this->indexerMock->expects(static::never()) + ->method('reindexList'); - protected function prepareIndexer() - { - $this->indexerRegistryMock->expects($this->once()) - ->method('get') - ->with(\Magento\CatalogSearch\Model\Indexer\Fulltext::INDEXER_ID) - ->will($this->returnValue($this->indexerMock)); + $this->plugin->afterUpdateWebsites($this->subjectMock, $this->subjectMock, $productIds); } } diff --git a/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/Store/GroupTest.php b/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/Store/GroupTest.php index 4d0a67c7e5a32..2fccfb7743de4 100644 --- a/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/Store/GroupTest.php +++ b/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/Store/GroupTest.php @@ -5,50 +5,66 @@ */ namespace Magento\CatalogSearch\Test\Unit\Model\Indexer\Fulltext\Plugin\Store; -use \Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin\Store\Group; +use Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin\Store\Group as StoreGroupIndexerPlugin; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; +use Magento\Framework\Indexer\IndexerRegistry; +use Magento\Framework\Indexer\IndexerInterface; +use Magento\Store\Model\ResourceModel\Group as StoreGroupResourceModel; +use Magento\Store\Model\Group as StoreGroup; +use Magento\CatalogSearch\Model\Indexer\Fulltext as FulltextIndexer; class GroupTest extends \PHPUnit_Framework_TestCase { /** - * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Indexer\IndexerInterface + * @var StoreGroupIndexerPlugin */ - protected $indexerMock; + private $plugin; /** - * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Store\Model\ResourceModel\Group + * @var ObjectManagerHelper */ - protected $subjectMock; + private $objectManagerHelper; /** - * @var \Magento\Framework\Indexer\IndexerRegistry|\PHPUnit_Framework_MockObject_MockObject + * @var IndexerRegistry|\PHPUnit_Framework_MockObject_MockObject */ - protected $indexerRegistryMock; + private $indexerRegistryMock; /** - * @var Group + * @var IndexerInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $model; + private $indexerMock; + + /** + * @var StoreGroupResourceModel|\PHPUnit_Framework_MockObject_MockObject + */ + private $subjectMock; + + /** + * @var StoreGroup|\PHPUnit_Framework_MockObject_MockObject + */ + private $storeGroupMock; protected function setUp() { - $this->subjectMock = $this->getMock('Magento\Store\Model\ResourceModel\Group', [], [], '', false); - $this->indexerMock = $this->getMockForAbstractClass( - 'Magento\Framework\Indexer\IndexerInterface', - [], - '', - false, - false, - true, - ['getId', 'getState', '__wakeup'] - ); - $this->indexerRegistryMock = $this->getMock( - 'Magento\Framework\Indexer\IndexerRegistry', - ['get'], - [], - '', - false + $this->indexerRegistryMock = $this->getMockBuilder(IndexerRegistry::class) + ->disableOriginalConstructor() + ->getMock(); + $this->indexerMock = $this->getMockBuilder(IndexerInterface::class) + ->getMockForAbstractClass(); + $this->subjectMock = $this->getMockBuilder(StoreGroupResourceModel::class) + ->disableOriginalConstructor() + ->getMock(); + $this->storeGroupMock = $this->getMockBuilder(StoreGroup::class) + ->disableOriginalConstructor() + ->setMethods(['dataHasChangedFor', 'isObjectNew']) + ->getMock(); + + $this->objectManagerHelper = new ObjectManagerHelper($this); + $this->plugin = $this->objectManagerHelper->getObject( + StoreGroupIndexerPlugin::class, + ['indexerRegistry' => $this->indexerRegistryMock] ); - $this->model = new Group($this->indexerRegistryMock); } /** @@ -60,27 +76,19 @@ protected function setUp() */ public function testBeforeAfterSave($isObjectNew, $websiteChanged, $invalidateCounter) { - $groupMock = $this->getMock( - 'Magento\Store\Model\Group', - ['dataHasChangedFor', 'isObjectNew', '__wakeup'], - [], - '', - false - ); - $groupMock->expects($this->any()) + $this->prepareIndexer($invalidateCounter); + $this->storeGroupMock->expects(static::any()) ->method('dataHasChangedFor') ->with('website_id') - ->will($this->returnValue($websiteChanged)); - $groupMock->expects($this->once())->method('isObjectNew')->will($this->returnValue($isObjectNew)); - - $this->indexerMock->expects($this->exactly($invalidateCounter))->method('invalidate'); - $this->prepareIndexer($invalidateCounter); + ->willReturn($websiteChanged); + $this->storeGroupMock->expects(static::once()) + ->method('isObjectNew') + ->willReturn($isObjectNew); + $this->indexerMock->expects(static::exactly($invalidateCounter)) + ->method('invalidate'); - $this->model->beforeSave($this->subjectMock, $groupMock); - $this->assertEquals( - $this->subjectMock, - $this->model->afterSave($this->subjectMock, $this->subjectMock) - ); + $this->plugin->beforeSave($this->subjectMock, $this->storeGroupMock); + $this->assertSame($this->subjectMock, $this->plugin->afterSave($this->subjectMock, $this->subjectMock)); } /** @@ -92,32 +100,30 @@ public function beforeAfterSaveDataProvider() [false, false, 0], [false, true, 1], [true, false, 0], - [true, true, 0], + [true, true, 0] ]; } - /** - * @return void - */ public function testAfterDelete() { - $this->indexerMock->expects($this->once())->method('invalidate'); $this->prepareIndexer(1); + $this->indexerMock->expects(static::once()) + ->method('invalidate'); - $this->assertEquals( - $this->subjectMock, - $this->model->afterDelete($this->subjectMock, $this->subjectMock) - ); + $this->assertSame($this->subjectMock, $this->plugin->afterDelete($this->subjectMock, $this->subjectMock)); } /** + * Prepare expectations for indexer + * * @param int $invalidateCounter + * @return void */ - protected function prepareIndexer($invalidateCounter) + private function prepareIndexer($invalidateCounter) { - $this->indexerRegistryMock->expects($this->exactly($invalidateCounter)) + $this->indexerRegistryMock->expects(static::exactly($invalidateCounter)) ->method('get') - ->with(\Magento\CatalogSearch\Model\Indexer\Fulltext::INDEXER_ID) - ->will($this->returnValue($this->indexerMock)); + ->with(FulltextIndexer::INDEXER_ID) + ->willReturn($this->indexerMock); } } diff --git a/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/Store/ViewTest.php b/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/Store/ViewTest.php index deccb88b295a1..d5db837eaa2bd 100644 --- a/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/Store/ViewTest.php +++ b/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/Store/ViewTest.php @@ -5,77 +5,85 @@ */ namespace Magento\CatalogSearch\Test\Unit\Model\Indexer\Fulltext\Plugin\Store; -use \Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin\Store\View; +use Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin\Store\View as StoreViewIndexerPlugin; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; +use Magento\Framework\Indexer\IndexerRegistry; +use Magento\Framework\Indexer\IndexerInterface; +use Magento\Store\Model\ResourceModel\Store as StoreResourceModel; +use Magento\Store\Model\Store; +use Magento\CatalogSearch\Model\Indexer\Fulltext as FulltextIndexer; class ViewTest extends \PHPUnit_Framework_TestCase { /** - * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Indexer\IndexerInterface + * @var StoreViewIndexerPlugin */ - protected $indexerMock; + private $plugin; /** - * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Store\Model\ResourceModel\Store + * @var ObjectManagerHelper */ - protected $subjectMock; + private $objectManagerHelper; /** - * @var \Magento\Framework\Indexer\IndexerRegistry|\PHPUnit_Framework_MockObject_MockObject + * @var IndexerRegistry|\PHPUnit_Framework_MockObject_MockObject */ - protected $indexerRegistryMock; + private $indexerRegistryMock; /** - * @var View + * @var IndexerInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $model; + private $indexerMock; + + /** + * @var StoreResourceModel|\PHPUnit_Framework_MockObject_MockObject + */ + private $subjectMock; + + /** + * @var Store|\PHPUnit_Framework_MockObject_MockObject + */ + private $storeMock; protected function setUp() { - $this->subjectMock = $this->getMock('Magento\Store\Model\ResourceModel\Store', [], [], '', false); - $this->indexerMock = $this->getMockForAbstractClass( - 'Magento\Framework\Indexer\IndexerInterface', - [], - '', - false, - false, - true, - ['getId', 'getState', '__wakeup'] - ); - $this->indexerRegistryMock = $this->getMock( - 'Magento\Framework\Indexer\IndexerRegistry', - ['get'], - [], - '', - false + $this->indexerRegistryMock = $this->getMockBuilder(IndexerRegistry::class) + ->disableOriginalConstructor() + ->getMock(); + $this->indexerMock = $this->getMockBuilder(IndexerInterface::class) + ->getMockForAbstractClass(); + $this->subjectMock = $this->getMockBuilder(StoreResourceModel::class) + ->disableOriginalConstructor() + ->getMock(); + $this->storeMock = $this->getMockBuilder(Store::class) + ->disableOriginalConstructor() + ->setMethods(['isObjectNew']) + ->getMock(); + + $this->objectManagerHelper = new ObjectManagerHelper($this); + $this->plugin = $this->objectManagerHelper->getObject( + StoreViewIndexerPlugin::class, + ['indexerRegistry' => $this->indexerRegistryMock] ); - $this->model = new View($this->indexerRegistryMock); } /** * @param bool $isObjectNew * @param int $invalidateCounter - * @return void + * * @dataProvider beforeAfterSaveDataProvider */ public function testBeforeAfterSave($isObjectNew, $invalidateCounter) { - $viewMock = $this->getMock( - 'Magento\Store\Model\Store', - ['dataHasChangedFor', 'isObjectNew', '__wakeup'], - [], - '', - false - ); - $viewMock->expects($this->once())->method('isObjectNew')->will($this->returnValue($isObjectNew)); - - $this->indexerMock->expects($this->exactly($invalidateCounter))->method('invalidate'); $this->prepareIndexer($invalidateCounter); + $this->storeMock->expects(static::once()) + ->method('isObjectNew') + ->willReturn($isObjectNew); + $this->indexerMock->expects(static::exactly($invalidateCounter)) + ->method('invalidate'); - $this->model->beforeSave($this->subjectMock, $viewMock); - $this->assertEquals( - $this->subjectMock, - $this->model->afterSave($this->subjectMock, $this->subjectMock) - ); + $this->plugin->beforeSave($this->subjectMock, $this->storeMock); + $this->assertSame($this->subjectMock, $this->plugin->afterSave($this->subjectMock, $this->subjectMock)); } /** @@ -85,32 +93,30 @@ public function beforeAfterSaveDataProvider() { return [ [false, 0], - [true, 1], + [true, 1] ]; } - /** - * @return void - */ public function testAfterDelete() { - $this->indexerMock->expects($this->once())->method('invalidate'); $this->prepareIndexer(1); + $this->indexerMock->expects(static::once()) + ->method('invalidate'); - $this->assertEquals( - $this->subjectMock, - $this->model->afterDelete($this->subjectMock, $this->subjectMock) - ); + $this->assertSame($this->subjectMock, $this->plugin->afterDelete($this->subjectMock, $this->subjectMock)); } /** + * Prepare expectations for indexer + * * @param int $invalidateCounter + * @return void */ - protected function prepareIndexer($invalidateCounter) + private function prepareIndexer($invalidateCounter) { - $this->indexerRegistryMock->expects($this->exactly($invalidateCounter)) + $this->indexerRegistryMock->expects(static::exactly($invalidateCounter)) ->method('get') - ->with(\Magento\CatalogSearch\Model\Indexer\Fulltext::INDEXER_ID) - ->will($this->returnValue($this->indexerMock)); + ->with(FulltextIndexer::INDEXER_ID) + ->willReturn($this->indexerMock); } } From f7157e21e2ddbfd72b1c0e05e158bbfb7bd85d13 Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Thu, 28 Jul 2016 16:22:58 +0300 Subject: [PATCH 42/57] MAGETWO-55693: Stabilize code & prepare PR --- .../Test/Unit/Model/Plugin/QuoteItemTest.php | 18 +++++++++++++++--- .../Category/Flat/Plugin/StoreViewTest.php | 3 ++- .../Category/Product/Plugin/StoreGroupTest.php | 2 +- .../Product/Eav/Plugin/AttributeSetTest.php | 14 +++++++------- .../Model/ResourceModel/Attribute/SaveTest.php | 8 +++++++- .../Indexer/Product/Save/ApplyRulesTest.php | 4 ++-- .../Search/Plugin/CollectionFilterTest.php | 2 +- 7 files changed, 35 insertions(+), 16 deletions(-) diff --git a/app/code/Magento/Bundle/Test/Unit/Model/Plugin/QuoteItemTest.php b/app/code/Magento/Bundle/Test/Unit/Model/Plugin/QuoteItemTest.php index 990af4d4f66e4..250d038a4c149 100644 --- a/app/code/Magento/Bundle/Test/Unit/Model/Plugin/QuoteItemTest.php +++ b/app/code/Magento/Bundle/Test/Unit/Model/Plugin/QuoteItemTest.php @@ -34,10 +34,22 @@ class QuoteItemTest extends \PHPUnit_Framework_TestCase protected function setUp() { $this->orderItemMock = $this->getMockForAbstractClass( - OrderItemInterface::class, [], '', false, false, true, ['getProductOptions', 'setProductOptions'] + OrderItemInterface::class, + [], + '', + false, + false, + true, + ['getProductOptions', 'setProductOptions'] ); $this->quoteItemMock = $this->getMockForAbstractClass( - AbstractItem::class, [], '', false, false, true, ['getProduct'] + AbstractItem::class, + [], + '', + false, + false, + true, + ['getProduct'] ); $this->subjectMock = $this->getMock(ToOrderItem::class, [], [], '', false); $this->productMock = $this->getMock(Product::class, [], [], '', false); @@ -68,7 +80,7 @@ public function testAroundItemToOrderItemPositive() ->method('getCustomOption') ->with('bundle_selection_attributes') ->willReturn($bundleAttribute); - $this->quoteItemMock->expects($this->once())->method('getProduct')->willReturn( $this->productMock ); + $this->quoteItemMock->expects($this->once())->method('getProduct')->willReturn($this->productMock); $this->orderItemMock->expects($this->once())->method('getProductOptions')->willReturn($productOptions); $this->orderItemMock->expects($this->once())->method('setProductOptions')->with($expectedOptions); diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/StoreViewTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/StoreViewTest.php index 8c6b1c9074824..2d02db090052e 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/StoreViewTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/StoreViewTest.php @@ -111,7 +111,8 @@ public function testBeforeAndAfterSaveNoNeed() $this->model->beforeSave($this->subjectMock, $storeMock); $this->assertSame( $this->subjectMock, - $this->model->afterSave($this->subjectMock, $this->subjectMock, $storeMock)); + $this->model->afterSave($this->subjectMock, $this->subjectMock, $storeMock) + ); } protected function mockIndexerMethods() diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Product/Plugin/StoreGroupTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Product/Plugin/StoreGroupTest.php index 04db59adc2f25..da2f90b313fbc 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Product/Plugin/StoreGroupTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Product/Plugin/StoreGroupTest.php @@ -116,7 +116,7 @@ public function testBeforeAndAfterSaveWithoutChanges() $this->groupMock->expects($this->never())->method('isObjectNew'); $this->model->beforeSave($this->subject, $this->groupMock); - $this->assertSame($this->subject, $this->model->afterSave($this->subject, $this->subject, $this->groupMock )); + $this->assertSame($this->subject, $this->model->afterSave($this->subject, $this->subject, $this->groupMock)); } private function mockIndexerMethods() diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Eav/Plugin/AttributeSetTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Eav/Plugin/AttributeSetTest.php index 6169ff9960fbb..a17c63920dad5 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Eav/Plugin/AttributeSetTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Eav/Plugin/AttributeSetTest.php @@ -66,13 +66,13 @@ public function testBeforeSave() $this->setFactoryMock->expects($this->once())->method('create')->willReturn($this->originalSetMock); $this->model = $this->objectManager->getObject( - AttributeSet::class, - [ - 'indexerEavProcessor' => $this->eavProcessorMock, - 'filter' => $this->filterMock, - 'setFactory' => $this->setFactoryMock - ] - ); + AttributeSet::class, + [ + 'indexerEavProcessor' => $this->eavProcessorMock, + 'filter' => $this->filterMock, + 'setFactory' => $this->setFactoryMock + ] + ); $this->filterMock->expects($this->exactly(2)) ->method('filter') diff --git a/app/code/Magento/Catalog/Test/Unit/Plugin/Model/ResourceModel/Attribute/SaveTest.php b/app/code/Magento/Catalog/Test/Unit/Plugin/Model/ResourceModel/Attribute/SaveTest.php index 9971029bc22da..58e88952de226 100644 --- a/app/code/Magento/Catalog/Test/Unit/Plugin/Model/ResourceModel/Attribute/SaveTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Plugin/Model/ResourceModel/Attribute/SaveTest.php @@ -37,7 +37,13 @@ protected function setUp() { $this->config = $this->getMock(Config::class, ['isEnabled'], [], '', false); $this->typeList = $this->getMockForAbstractClass( - TypeListInterface::class, [], '', false, false, true, ['invalidate'] + TypeListInterface::class, + [], + '', + false, + false, + true, + ['invalidate'] ); $this->subjectMock = $this->getMock(Attribute::class, [], [], '', false); $this->save = new Save($this->config, $this->typeList); diff --git a/app/code/Magento/CatalogRule/Test/Unit/Plugin/Indexer/Product/Save/ApplyRulesTest.php b/app/code/Magento/CatalogRule/Test/Unit/Plugin/Indexer/Product/Save/ApplyRulesTest.php index e54192cf6ea2b..59031959b51ad 100644 --- a/app/code/Magento/CatalogRule/Test/Unit/Plugin/Indexer/Product/Save/ApplyRulesTest.php +++ b/app/code/Magento/CatalogRule/Test/Unit/Plugin/Indexer/Product/Save/ApplyRulesTest.php @@ -31,8 +31,8 @@ class ApplyRulesTest extends \PHPUnit_Framework_TestCase protected function setUp() { - $this->productRuleProcessor = $this->getMockBuilder( - \Magento\CatalogRule\Model\Indexer\Product\ProductRuleProcessor::class) + $this->productRuleProcessor = $this + ->getMockBuilder(\Magento\CatalogRule\Model\Indexer\Product\ProductRuleProcessor::class) ->disableOriginalConstructor() ->getMock(); diff --git a/app/code/Magento/CatalogSearch/Test/Unit/Model/Layer/Search/Plugin/CollectionFilterTest.php b/app/code/Magento/CatalogSearch/Test/Unit/Model/Layer/Search/Plugin/CollectionFilterTest.php index ad32a516baee2..6ba226c572dfe 100644 --- a/app/code/Magento/CatalogSearch/Test/Unit/Model/Layer/Search/Plugin/CollectionFilterTest.php +++ b/app/code/Magento/CatalogSearch/Test/Unit/Model/Layer/Search/Plugin/CollectionFilterTest.php @@ -103,4 +103,4 @@ public function testAfterFilter() $this->categoryMock ); } -} \ No newline at end of file +} From 9793447f4d69b4c51bbff47ac011992e1c6cdbfa Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Thu, 28 Jul 2016 17:14:44 +0300 Subject: [PATCH 43/57] MAGETWO-55693: Stabilize code & prepare PR --- .../Test/Unit/Model/Plugin/QuoteItemProductOptionTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Plugin/QuoteItemProductOptionTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Plugin/QuoteItemProductOptionTest.php index 4ca33d89ba3f7..13e9ef8b32787 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Plugin/QuoteItemProductOptionTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Plugin/QuoteItemProductOptionTest.php @@ -14,6 +14,9 @@ use Magento\Framework\DataObject; use Magento\Catalog\Model\Product\Option as ProductOption; +/** + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + */ class QuoteItemProductOptionTest extends \PHPUnit_Framework_TestCase { /** From 038e4682a565cf373569416ea80251ab14a4b928 Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Thu, 28 Jul 2016 17:18:33 +0300 Subject: [PATCH 44/57] MAGETWO-55693: Stabilize code & prepare PR --- .../Indexer/Product/Eav/Plugin/AttributeSetTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Eav/Plugin/AttributeSetTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Eav/Plugin/AttributeSetTest.php index a17c63920dad5..48410da746584 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Eav/Plugin/AttributeSetTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Eav/Plugin/AttributeSetTest.php @@ -6,7 +6,7 @@ namespace Magento\Catalog\Test\Unit\Model\Indexer\Product\Eav\Plugin; use Magento\Catalog\Model\Indexer\Product\Eav\Plugin\AttributeSet\IndexableAttributeFilter; -use Magento\Eav\Model\Entity\Attribute\Set; +use Magento\Eav\Model\Entity\Attribute\Set as EavAttributeSet; use Magento\Eav\Model\Entity\Attribute\SetFactory; use Magento\Catalog\Model\Indexer\Product\Eav\Processor; use Magento\Catalog\Model\Indexer\Product\Eav\Plugin\AttributeSet; @@ -35,7 +35,7 @@ class AttributeSetTest extends \PHPUnit_Framework_TestCase private $filterMock; /** - * @var Set|\PHPUnit_Framework_MockObject_MockObject + * @var EavAttributeSet|\PHPUnit_Framework_MockObject_MockObject */ private $subjectMock; @@ -45,14 +45,14 @@ class AttributeSetTest extends \PHPUnit_Framework_TestCase private $setFactoryMock; /** - * @var Set|\PHPUnit_Framework_MockObject_MockObject + * @var EavAttributeSet|\PHPUnit_Framework_MockObject_MockObject */ private $originalSetMock; public function setUp() { $this->filterMock = $this->getMock(IndexableAttributeFilter::class, [], [], '', false); - $this->subjectMock = $this->getMock(Set::class, [], [], '', false); + $this->subjectMock = $this->getMock(EavAttributeSet::class, [], [], '', false); $this->eavProcessorMock = $this->getMock(Processor::class, [], [], '', false); $this->setFactoryMock = $this->getMock(SetFactory::class, ['create'], [], '', false); $this->objectManager = new ObjectManager($this); @@ -61,7 +61,7 @@ public function setUp() public function testBeforeSave() { $setId = 1; - $this->originalSetMock = $this->getMock(Set::class, [], [], '', false); + $this->originalSetMock = $this->getMock(EavAttributeSet::class, [], [], '', false); $this->originalSetMock->expects($this->once())->method('initFromSkeleton')->with($setId); $this->setFactoryMock->expects($this->once())->method('create')->willReturn($this->originalSetMock); From 81821edcfb1eed1cfb5eac7d3db5df8f208f9245 Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Thu, 28 Jul 2016 17:31:44 +0300 Subject: [PATCH 45/57] MAGETWO-55693: Stabilize code & prepare PR --- .../Test/Unit/Model/Indexer/Fulltext/Plugin/AttributeTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/AttributeTest.php b/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/AttributeTest.php index 5598392ccad0a..878f7fa599318 100644 --- a/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/AttributeTest.php +++ b/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/AttributeTest.php @@ -95,7 +95,7 @@ public function testBeforeSave() ->with('is_searchable') ->willReturn(true); $this->assertEquals( - [$this->attributeMock], + null, $this->model->beforeSave($this->subjectMock, $this->attributeMock) ); } @@ -140,7 +140,7 @@ public function testBeforeDelete() ->method('getIsSearchable') ->willReturn(true); $this->assertEquals( - [$this->attributeMock], + null, $this->model->beforeDelete($this->subjectMock, $this->attributeMock) ); } From 95635e30730445f2871e44f1392cf02234da67d6 Mon Sep 17 00:00:00 2001 From: Viktor Paladiychuk Date: Thu, 28 Jul 2016 18:06:36 +0300 Subject: [PATCH 46/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- .../Model/Plugin/QuoteItemProductOption.php | 4 +++- .../Interception/Code/InterfaceValidator.php | 14 ++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/app/code/Magento/Catalog/Model/Plugin/QuoteItemProductOption.php b/app/code/Magento/Catalog/Model/Plugin/QuoteItemProductOption.php index 57cf1fdc4994e..7c8249b728cab 100644 --- a/app/code/Magento/Catalog/Model/Plugin/QuoteItemProductOption.php +++ b/app/code/Magento/Catalog/Model/Plugin/QuoteItemProductOption.php @@ -19,13 +19,15 @@ class QuoteItemProductOption * * @param QuoteToOrderItem $subject * @param AbstractQuoteItem $quoteItem + * @param array $data * @return void * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function beforeConvert( QuoteToOrderItem $subject, - AbstractQuoteItem $quoteItem + AbstractQuoteItem $quoteItem, + $data = [] ) { if (!is_array($quoteItem->getOptions())) { return; diff --git a/lib/internal/Magento/Framework/Interception/Code/InterfaceValidator.php b/lib/internal/Magento/Framework/Interception/Code/InterfaceValidator.php index 438589cd51172..93945c607b53d 100644 --- a/lib/internal/Magento/Framework/Interception/Code/InterfaceValidator.php +++ b/lib/internal/Magento/Framework/Interception/Code/InterfaceValidator.php @@ -49,7 +49,6 @@ public function validate($pluginClass, $interceptedType) $plugin = new \ReflectionClass($pluginClass); $type = new \ReflectionClass($interceptedType); - $pluginMethods = []; foreach ($plugin->getMethods(\ReflectionMethod::IS_PUBLIC) as $pluginMethod) { /** @var $pluginMethod \ReflectionMethod */ $originMethodName = $this->getOriginMethodName($pluginMethod->getName()); @@ -113,11 +112,14 @@ public function validate($pluginClass, $interceptedType) break; case self::METHOD_AFTER: if (count($pluginMethodParameters) > 1) { - throw new ValidatorException( - new Phrase( - 'Invalid method signature. Detected extra parameters in %1::%2', - [$pluginClass, $pluginMethod->getName()] - ) + // remove result + array_shift($pluginMethodParameters); + $matchedParameters = array_intersect_key($originMethodParameters, $pluginMethodParameters); + $this->validateMethodsParameters( + $pluginMethodParameters, + $matchedParameters, + $pluginClass, + $pluginMethod->getName() ); } break; From 0229360b1c8e52383dcdd4dcaddcf6ac1aae52de Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Fri, 29 Jul 2016 09:11:54 +0300 Subject: [PATCH 47/57] MAGETWO-55693: Stabilize code & prepare PR --- .../Interception/Test/Unit/Code/InterfaceValidatorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/Magento/Framework/Interception/Test/Unit/Code/InterfaceValidatorTest.php b/lib/internal/Magento/Framework/Interception/Test/Unit/Code/InterfaceValidatorTest.php index 97fa71f726909..8eb884072f8f7 100644 --- a/lib/internal/Magento/Framework/Interception/Test/Unit/Code/InterfaceValidatorTest.php +++ b/lib/internal/Magento/Framework/Interception/Test/Unit/Code/InterfaceValidatorTest.php @@ -110,7 +110,7 @@ public function testValidateIncompatibleMethodArgumentsType() /** * @expectedException \Magento\Framework\Exception\ValidatorException - * @expectedExceptionMessage Invalid method signature. Detected extra parameters + * @expectedExceptionMessage Invalid method signature. Invalid method parameters count * @covers \Magento\Framework\Interception\Code\InterfaceValidator::validate */ public function testValidateExtraParameters() From 05baa6576c9d1452e2499949569cc31f5c5e3794 Mon Sep 17 00:00:00 2001 From: Viktor Paladiychuk Date: Fri, 29 Jul 2016 13:57:21 +0300 Subject: [PATCH 48/57] MAGETWO-55692: Refactor around plugins and cover w. unit tests --- .../Magento/Framework/Interception/Code/InterfaceValidator.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/internal/Magento/Framework/Interception/Code/InterfaceValidator.php b/lib/internal/Magento/Framework/Interception/Code/InterfaceValidator.php index 93945c607b53d..0536ce6c90b84 100644 --- a/lib/internal/Magento/Framework/Interception/Code/InterfaceValidator.php +++ b/lib/internal/Magento/Framework/Interception/Code/InterfaceValidator.php @@ -114,10 +114,9 @@ public function validate($pluginClass, $interceptedType) if (count($pluginMethodParameters) > 1) { // remove result array_shift($pluginMethodParameters); - $matchedParameters = array_intersect_key($originMethodParameters, $pluginMethodParameters); $this->validateMethodsParameters( $pluginMethodParameters, - $matchedParameters, + $originMethodParameters, $pluginClass, $pluginMethod->getName() ); From 56c6c209a69212cbd1c028918cd230c7d269056b Mon Sep 17 00:00:00 2001 From: Yuri Kovsher Date: Fri, 29 Jul 2016 15:36:51 +0300 Subject: [PATCH 49/57] MAGETWO-55693: Stabilize code & prepare PR --- app/code/Magento/Bundle/Model/Plugin/QuoteItem.php | 13 ++++++++----- .../Model/Plugin/AroundProductRepositorySave.php | 13 +++++++++++-- .../Indexer/Fulltext/Plugin/Product/Action.php | 11 +++++++++-- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/app/code/Magento/Bundle/Model/Plugin/QuoteItem.php b/app/code/Magento/Bundle/Model/Plugin/QuoteItem.php index b04659dcb8bb1..4009f6e3496bb 100644 --- a/app/code/Magento/Bundle/Model/Plugin/QuoteItem.php +++ b/app/code/Magento/Bundle/Model/Plugin/QuoteItem.php @@ -9,6 +9,9 @@ use Magento\Sales\Api\Data\OrderItemInterface; use Magento\Quote\Model\Quote\Item\AbstractItem; +/** + * Plugin for Magento\Quote\Model\Quote\Item\ToOrderItem + */ class QuoteItem { /** @@ -17,19 +20,19 @@ class QuoteItem * @param ToOrderItem $subject * @param OrderItemInterface $orderItem * @param AbstractItem $item + * @param array $data * @return OrderItemInterface + * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function afterConvert( - ToOrderItem $subject, - OrderItemInterface $orderItem, - AbstractItem $item - ) { + public function afterConvert(ToOrderItem $subject, OrderItemInterface $orderItem, AbstractItem $item, $data = []) + { if ($attributes = $item->getProduct()->getCustomOption('bundle_selection_attributes')) { $productOptions = $orderItem->getProductOptions(); $productOptions['bundle_selection_attributes'] = $attributes->getValue(); $orderItem->setProductOptions($productOptions); } + return $orderItem; } } diff --git a/app/code/Magento/CatalogInventory/Model/Plugin/AroundProductRepositorySave.php b/app/code/Magento/CatalogInventory/Model/Plugin/AroundProductRepositorySave.php index 92c8c02f75585..14b7207a1e43b 100644 --- a/app/code/Magento/CatalogInventory/Model/Plugin/AroundProductRepositorySave.php +++ b/app/code/Magento/CatalogInventory/Model/Plugin/AroundProductRepositorySave.php @@ -17,6 +17,8 @@ use Magento\Framework\Exception\CouldNotSaveException; /** + * Plugin for Magento\Catalog\Api\ProductRepositoryInterface + * * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class AroundProductRepositorySave @@ -61,11 +63,18 @@ public function __construct( * @param ProductRepositoryInterface $subject * @param ProductInterface $result * @param ProductInterface $product + * @param bool $saveOptions * @return ProductInterface * @throws CouldNotSaveException + * + * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function afterSave(ProductRepositoryInterface $subject, ProductInterface $result, ProductInterface $product) - { + public function afterSave( + ProductRepositoryInterface $subject, + ProductInterface $result, + ProductInterface $product, + $saveOptions = false + ) { /* @var StockItemInterface $stockItem */ $stockItem = $this->getStockItemToBeUpdated($product); if (null === $stockItem) { diff --git a/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Product/Action.php b/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Product/Action.php index da85976030d2b..94e62781b5556 100644 --- a/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Product/Action.php +++ b/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Product/Action.php @@ -19,12 +19,19 @@ class Action extends AbstractIndexerPlugin * @param ProductAction $subject * @param ProductAction $action * @param array $productIds + * @param array $attrData + * @param int $storeId * @return ProductAction * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function afterUpdateAttributes(ProductAction $subject, ProductAction $action, array $productIds) - { + public function afterUpdateAttributes( + ProductAction $subject, + ProductAction $action, + $productIds, + $attrData, + $storeId + ) { $this->reindexList(array_unique($productIds)); return $action; From 30f2ea935004f86dd4a45063695fd87d0dadd7d2 Mon Sep 17 00:00:00 2001 From: Yuri Kovsher Date: Fri, 29 Jul 2016 15:52:56 +0300 Subject: [PATCH 50/57] MAGETWO-55693: Stabilize code & prepare PR --- .../Model/Indexer/Fulltext/Plugin/Product/Action.php | 4 +++- .../Model/Indexer/Fulltext/Plugin/Product/ActionTest.php | 8 ++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Product/Action.php b/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Product/Action.php index 94e62781b5556..c144737606a38 100644 --- a/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Product/Action.php +++ b/app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Product/Action.php @@ -43,11 +43,13 @@ public function afterUpdateAttributes( * @param ProductAction $subject * @param null $result * @param array $productIds + * @param array $websiteIds + * @param string $type * @return void * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function afterUpdateWebsites(ProductAction $subject, $result, array $productIds) + public function afterUpdateWebsites(ProductAction $subject, $result, $productIds, $websiteIds, $type) { $this->reindexList(array_unique($productIds)); } diff --git a/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/Product/ActionTest.php b/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/Product/ActionTest.php index f9d369a67812d..f6767c3fc9d1c 100644 --- a/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/Product/ActionTest.php +++ b/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/Product/ActionTest.php @@ -75,7 +75,7 @@ public function testAfterUpdateAttributesNonScheduled() $this->assertSame( $this->subjectMock, - $this->plugin->afterUpdateAttributes($this->subjectMock, $this->subjectMock, $productIds) + $this->plugin->afterUpdateAttributes($this->subjectMock, $this->subjectMock, $productIds, [], null) ); } @@ -91,7 +91,7 @@ public function testAfterUpdateAttributesScheduled() $this->assertSame( $this->subjectMock, - $this->plugin->afterUpdateAttributes($this->subjectMock, $this->subjectMock, $productIds) + $this->plugin->afterUpdateAttributes($this->subjectMock, $this->subjectMock, $productIds, [], null) ); } @@ -106,7 +106,7 @@ public function testAfterUpdateWebsitesNonScheduled() ->method('reindexList') ->with($productIds); - $this->plugin->afterUpdateWebsites($this->subjectMock, $this->subjectMock, $productIds); + $this->plugin->afterUpdateWebsites($this->subjectMock, $this->subjectMock, $productIds, [], null); } public function testAfterUpdateWebsitesScheduled() @@ -119,6 +119,6 @@ public function testAfterUpdateWebsitesScheduled() $this->indexerMock->expects(static::never()) ->method('reindexList'); - $this->plugin->afterUpdateWebsites($this->subjectMock, $this->subjectMock, $productIds); + $this->plugin->afterUpdateWebsites($this->subjectMock, $this->subjectMock, $productIds, [], null); } } From 76beb73e9b765b888d1b3d014f8b6b0e9fccaae2 Mon Sep 17 00:00:00 2001 From: Yuri Kovsher Date: Mon, 1 Aug 2016 11:11:03 +0300 Subject: [PATCH 51/57] MAGETWO-55693: Stabilize code & prepare PR --- .../Magento/Framework/Interception/Code/InterfaceValidator.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/internal/Magento/Framework/Interception/Code/InterfaceValidator.php b/lib/internal/Magento/Framework/Interception/Code/InterfaceValidator.php index 0536ce6c90b84..1ff11552533b1 100644 --- a/lib/internal/Magento/Framework/Interception/Code/InterfaceValidator.php +++ b/lib/internal/Magento/Framework/Interception/Code/InterfaceValidator.php @@ -111,6 +111,7 @@ public function validate($pluginClass, $interceptedType) ); break; case self::METHOD_AFTER: + // TODO: Remove this condition check in scope of MAGETWO-56123 if (count($pluginMethodParameters) > 1) { // remove result array_shift($pluginMethodParameters); From 80d7b8b864889455729cf45bd4d1f021613fc852 Mon Sep 17 00:00:00 2001 From: Yuri Kovsher Date: Tue, 2 Aug 2016 13:37:07 +0300 Subject: [PATCH 52/57] MAGETWO-55691: Decrease of stack trace --- lib/internal/Magento/Framework/Interception/Interceptor.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/internal/Magento/Framework/Interception/Interceptor.php b/lib/internal/Magento/Framework/Interception/Interceptor.php index 5f76a2f55cf44..9aa00e1c75175 100644 --- a/lib/internal/Magento/Framework/Interception/Interceptor.php +++ b/lib/internal/Magento/Framework/Interception/Interceptor.php @@ -21,7 +21,7 @@ trait Interceptor /** * List of plugins * - * @var \Magento\Framework\Interception\PluginListInterface + * @var PluginListInterface */ private $pluginList; @@ -39,7 +39,7 @@ trait Interceptor */ public function ___init() { - $this->pluginList = ObjectManager::getInstance()->get('Magento\Framework\Interception\PluginListInterface'); + $this->pluginList = ObjectManager::getInstance()->get(PluginListInterface::class); $this->subjectType = get_parent_class($this); if (method_exists($this->subjectType, '___init')) { parent::___init(); From 09c884391e512a9d8ead49d5395f50dc18c76975 Mon Sep 17 00:00:00 2001 From: Yuri Kovsher Date: Tue, 2 Aug 2016 14:09:05 +0300 Subject: [PATCH 53/57] MAGETWO-55693: Stabilize code & prepare PR --- .../Unit/App/Action/Plugin/MassactionKeyTest.php | 4 ++-- .../Test/Unit/Model/Plugin/QuoteItemTest.php | 2 +- .../Indexer/Category/Flat/Plugin/StoreViewTest.php | 14 +++++++------- .../CatalogRule/Model/Rule/ValidationTest.php | 8 ++++---- .../Indexer/Fulltext/Plugin/AttributeTest.php | 4 ++-- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/app/code/Magento/Backend/Test/Unit/App/Action/Plugin/MassactionKeyTest.php b/app/code/Magento/Backend/Test/Unit/App/Action/Plugin/MassactionKeyTest.php index 16d2b7ade9ee9..3f4af8c3259e9 100644 --- a/app/code/Magento/Backend/Test/Unit/App/Action/Plugin/MassactionKeyTest.php +++ b/app/code/Magento/Backend/Test/Unit/App/Action/Plugin/MassactionKeyTest.php @@ -31,7 +31,7 @@ protected function setUp() $this->closureMock = function () { return 'Expected'; }; - $this->subjectMock = $this->getMock('Magento\Backend\App\AbstractAction', [], [], '', false); + $this->subjectMock = $this->getMock(\Magento\Backend\App\AbstractAction::class, [], [], '', false); $this->requestMock = $this->getMockForAbstractClass( RequestInterface::class, [], @@ -44,7 +44,7 @@ protected function setUp() $objectManager = new ObjectManager($this); $this->plugin = $objectManager->getObject( - 'Magento\Backend\App\Action\Plugin\MassactionKey', + \Magento\Backend\App\Action\Plugin\MassactionKey::class, [ 'subject' => $this->subjectMock, 'request' => $this->requestMock, diff --git a/app/code/Magento/Bundle/Test/Unit/Model/Plugin/QuoteItemTest.php b/app/code/Magento/Bundle/Test/Unit/Model/Plugin/QuoteItemTest.php index 250d038a4c149..03bac48a71be1 100644 --- a/app/code/Magento/Bundle/Test/Unit/Model/Plugin/QuoteItemTest.php +++ b/app/code/Magento/Bundle/Test/Unit/Model/Plugin/QuoteItemTest.php @@ -66,7 +66,7 @@ public function testAroundItemToOrderItemPositive() $expectedOptions = $productOptions + ['bundle_selection_attributes' => $attributeValue]; $bundleAttribute = $this->getMock( - 'Magento\Catalog\Model\Product\Configuration\Item\Option', + \Magento\Catalog\Model\Product\Configuration\Item\Option::class, [], [], '', diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/StoreViewTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/StoreViewTest.php index 2d02db090052e..88d2d7c3e394c 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/StoreViewTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/Flat/Plugin/StoreViewTest.php @@ -37,7 +37,7 @@ class StoreViewTest extends \PHPUnit_Framework_TestCase protected function setUp() { $this->indexerMock = $this->getMockForAbstractClass( - 'Magento\Framework\Indexer\IndexerInterface', + \Magento\Framework\Indexer\IndexerInterface::class, [], '', false, @@ -46,15 +46,15 @@ protected function setUp() ['getId', 'getState', '__wakeup'] ); $this->stateMock = $this->getMock( - 'Magento\Catalog\Model\Indexer\Category\Flat\State', + \Magento\Catalog\Model\Indexer\Category\Flat\State::class, ['isFlatEnabled'], [], '', false ); - $this->subjectMock = $this->getMock('Magento\Store\Model\ResourceModel\Store', [], [], '', false); + $this->subjectMock = $this->getMock(\Magento\Store\Model\ResourceModel\Store::class, [], [], '', false); $this->indexerRegistryMock = $this->getMock( - 'Magento\Framework\Indexer\IndexerRegistry', + \Magento\Framework\Indexer\IndexerRegistry::class, ['get'], [], '', @@ -68,7 +68,7 @@ public function testBeforeAndAfterSaveNewObject() $this->mockConfigFlatEnabled(); $this->mockIndexerMethods(); $storeMock = $this->getMock( - 'Magento\Store\Model\Store', + \Magento\Store\Model\Store::class, ['isObjectNew', 'dataHasChangedFor', '__wakeup'], [], '', @@ -85,7 +85,7 @@ public function testBeforeAndAfterSaveNewObject() public function testBeforeAndAfterSaveHasChanged() { $storeMock = $this->getMock( - 'Magento\Store\Model\Store', + \Magento\Store\Model\Store::class, ['isObjectNew', 'dataHasChangedFor', '__wakeup'], [], '', @@ -102,7 +102,7 @@ public function testBeforeAndAfterSaveNoNeed() { $this->mockConfigFlatEnabledNeever(); $storeMock = $this->getMock( - 'Magento\Store\Model\Store', + \Magento\Store\Model\Store::class, ['isObjectNew', 'dataHasChangedFor', '__wakeup'], [], '', diff --git a/app/code/Magento/CatalogRuleConfigurable/Test/Unit/Plugin/CatalogRule/Model/Rule/ValidationTest.php b/app/code/Magento/CatalogRuleConfigurable/Test/Unit/Plugin/CatalogRule/Model/Rule/ValidationTest.php index c727747caaffb..6ed0cb1d48979 100644 --- a/app/code/Magento/CatalogRuleConfigurable/Test/Unit/Plugin/CatalogRule/Model/Rule/ValidationTest.php +++ b/app/code/Magento/CatalogRuleConfigurable/Test/Unit/Plugin/CatalogRule/Model/Rule/ValidationTest.php @@ -38,16 +38,16 @@ class ValidationTest extends \PHPUnit_Framework_TestCase protected function setUp() { $this->configurableMock = $this->getMock( - 'Magento\ConfigurableProduct\Model\Product\Type\Configurable', + \Magento\ConfigurableProduct\Model\Product\Type\Configurable::class, ['getParentIdsByChild'], [], '', false ); - $this->ruleMock = $this->getMock('Magento\CatalogRule\Model\Rule', [], [], '', false); - $this->ruleConditionsMock = $this->getMock('Magento\Rule\Model\Condition\Combine', [], [], '', false); - $this->productMock = $this->getMock('Magento\Framework\DataObject', ['getId']); + $this->ruleMock = $this->getMock(\Magento\CatalogRule\Model\Rule::class, [], [], '', false); + $this->ruleConditionsMock = $this->getMock(\Magento\Rule\Model\Condition\Combine::class, [], [], '', false); + $this->productMock = $this->getMock(\Magento\Framework\DataObject::class, ['getId']); $this->validation = new Validation( $this->configurableMock diff --git a/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/AttributeTest.php b/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/AttributeTest.php index 878f7fa599318..6375b659fc76c 100644 --- a/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/AttributeTest.php +++ b/app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/AttributeTest.php @@ -50,7 +50,7 @@ protected function setUp() $this->objectManager = new ObjectManager($this); $this->subjectMock = $this->getMock(\Magento\Catalog\Model\ResourceModel\Attribute::class, [], [], '', false); $this->indexerMock = $this->getMockForAbstractClass( - 'Magento\Framework\Indexer\IndexerInterface', + \Magento\Framework\Indexer\IndexerInterface::class, [], '', false, @@ -59,7 +59,7 @@ protected function setUp() ['getId', 'getState', '__wakeup'] ); $this->indexerRegistryMock = $this->getMock( - 'Magento\Framework\Indexer\IndexerRegistry', + \Magento\Framework\Indexer\IndexerRegistry::class, ['get'], [], '', From f90d21ed66035035cecee15baaf392df1b5e9c7f Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Fri, 5 Aug 2016 09:16:05 +0300 Subject: [PATCH 54/57] MAGETWO-55693: Stabilize code & prepare PR --- .../Magento/Catalog/Model/CatalogRegistry.php | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 app/code/Magento/Catalog/Model/CatalogRegistry.php diff --git a/app/code/Magento/Catalog/Model/CatalogRegistry.php b/app/code/Magento/Catalog/Model/CatalogRegistry.php new file mode 100644 index 0000000000000..7eb7b3f77f8b1 --- /dev/null +++ b/app/code/Magento/Catalog/Model/CatalogRegistry.php @@ -0,0 +1,51 @@ +entityRegistry = $entityRegistry; + } + + /** + * @param EntityManager $subject + * @param \Closure $proceed + * @param string $entityType + * @param object $entity + * @param string $identifier + * @return null|object + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function aroundLoad(EntityManager $subject, \Closure $proceed, $entityType, $entity, $identifier) + { + $object = $this->entityRegistry->retrieve($entityType, $identifier); + if (!$object) { + $object = $proceed($entityType, $entity, $identifier); + $this->entityRegistry->register($entityType, $identifier, $object); + } + return $object; + } +} From 9530036b4ab466547e2ff6def608ef6f8f1f2e88 Mon Sep 17 00:00:00 2001 From: Yuri Kovsher Date: Fri, 5 Aug 2016 18:09:57 +0300 Subject: [PATCH 55/57] MAGETWO-55693: Stabilize code & prepare PR --- .../Magento/Catalog/Model/CatalogRegistry.php | 51 ------------------- 1 file changed, 51 deletions(-) delete mode 100644 app/code/Magento/Catalog/Model/CatalogRegistry.php diff --git a/app/code/Magento/Catalog/Model/CatalogRegistry.php b/app/code/Magento/Catalog/Model/CatalogRegistry.php deleted file mode 100644 index 7eb7b3f77f8b1..0000000000000 --- a/app/code/Magento/Catalog/Model/CatalogRegistry.php +++ /dev/null @@ -1,51 +0,0 @@ -entityRegistry = $entityRegistry; - } - - /** - * @param EntityManager $subject - * @param \Closure $proceed - * @param string $entityType - * @param object $entity - * @param string $identifier - * @return null|object - * @SuppressWarnings(PHPMD.UnusedFormalParameter) - */ - public function aroundLoad(EntityManager $subject, \Closure $proceed, $entityType, $entity, $identifier) - { - $object = $this->entityRegistry->retrieve($entityType, $identifier); - if (!$object) { - $object = $proceed($entityType, $entity, $identifier); - $this->entityRegistry->register($entityType, $identifier, $object); - } - return $object; - } -} From dbd53886185da410a855dd64f7fd72e371ab11ac Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Tue, 9 Aug 2016 14:12:03 +0300 Subject: [PATCH 56/57] MAGETWO-55693: Stabilize code & prepare PR --- app/code/Magento/CatalogRuleConfigurable/composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/app/code/Magento/CatalogRuleConfigurable/composer.json b/app/code/Magento/CatalogRuleConfigurable/composer.json index 06c3019cfb12f..638a5d24eae37 100644 --- a/app/code/Magento/CatalogRuleConfigurable/composer.json +++ b/app/code/Magento/CatalogRuleConfigurable/composer.json @@ -5,6 +5,7 @@ "php": "~5.6.0|7.0.2|7.0.4|~7.0.6", "magento/module-configurable-product": "100.2.*", "magento/framework": "100.2.*", + "magento/module-catalog": "101.1.*" "magento/module-catalog-rule": "100.2.*", "magento/module-store": "100.2.*", "magento/module-customer": "100.2.*", From 322a9bedd238149a9a59b833d1f8d14bfe9711f3 Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Tue, 9 Aug 2016 15:05:34 +0300 Subject: [PATCH 57/57] MAGETWO-55693: Stabilize code & prepare PR --- app/code/Magento/CatalogRuleConfigurable/composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/CatalogRuleConfigurable/composer.json b/app/code/Magento/CatalogRuleConfigurable/composer.json index 638a5d24eae37..cc51269e2d972 100644 --- a/app/code/Magento/CatalogRuleConfigurable/composer.json +++ b/app/code/Magento/CatalogRuleConfigurable/composer.json @@ -5,7 +5,7 @@ "php": "~5.6.0|7.0.2|7.0.4|~7.0.6", "magento/module-configurable-product": "100.2.*", "magento/framework": "100.2.*", - "magento/module-catalog": "101.1.*" + "magento/module-catalog": "101.1.*", "magento/module-catalog-rule": "100.2.*", "magento/module-store": "100.2.*", "magento/module-customer": "100.2.*",