Skip to content

Commit

Permalink
Merge pull request #239 from magento-tango/MAGETWO-55692-2
Browse files Browse the repository at this point in the history
Stories:
- MAGETWO-55675 Replacing redundant around plugins with before/after pluginization (Catalog, CatalogSearch, Bundle, Backend, Captcha module)
  • Loading branch information
magicbunneh authored Aug 11, 2016
2 parents ab36d41 + fd63277 commit 9a789ac
Show file tree
Hide file tree
Showing 56 changed files with 2,061 additions and 1,482 deletions.
18 changes: 8 additions & 10 deletions app/code/Magento/Backend/App/Action/Plugin/MassactionKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,27 @@
*/
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 void
* @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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;

Expand All @@ -35,27 +32,32 @@ protected function setUp()
return 'Expected';
};
$this->subjectMock = $this->getMock(\Magento\Backend\App\AbstractAction::class, [], [], '', false);
$this->requestMock = $this->getMock(\Magento\Framework\App\Request\Http::class, [], [], '', 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::class,
[
'subject' => $this->subjectMock,
'closure' => $this->closureMock,
'request' => $this->requestMock,
]
);
}

/**
* @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')
Expand All @@ -69,24 +71,18 @@ public function testAroundDispatchWhenMassactionPrepareKeyRequestExists($postDat
->method('setPostValue')
->with('key', $convertedData);

$this->assertEquals(
'Expected',
$this->plugin->aroundDispatch($this->subjectMock, $this->closureMock, $this->requestMock)
);
$this->plugin->beforeDispatch($this->subjectMock, $this->requestMock);
}

public function aroundDispatchDataProvider()
public function beforeDispatchDataProvider()
{
return [
'post_data_is_array' => [['key'], ['key']],
'post_data_is_string' => ['key, key_two', ['key', ' key_two']]
];
}

/**
* @covers \Magento\Backend\App\Action\Plugin\MassactionKey::aroundDispatch
*/
public function testAroundDispatchWhenMassactionPrepareKeyRequestNotExists()
public function testBeforeDispatchWhenMassactionPrepareKeyRequestNotExists()
{
$this->requestMock->expects($this->once())
->method('getPost')
Expand All @@ -95,9 +91,6 @@ public function testAroundDispatchWhenMassactionPrepareKeyRequestNotExists()
$this->requestMock->expects($this->never())
->method('setPostValue');

$this->assertEquals(
'Expected',
$this->plugin->aroundDispatch($this->subjectMock, $this->closureMock, $this->requestMock)
);
$this->plugin->beforeDispatch($this->subjectMock, $this->requestMock);
}
}
30 changes: 15 additions & 15 deletions app/code/Magento/Bundle/Model/Plugin/QuoteItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,34 @@
*/
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;

/**
* Plugin for Magento\Quote\Model\Quote\Item\ToOrderItem
*/
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
* @param array $data
* @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 = []
) {
/** @var $orderItem \Magento\Sales\Model\Order\Item */
$orderItem = $proceed($item, $additional);

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;
}
}
100 changes: 60 additions & 40 deletions app/code/Magento/Bundle/Test/Unit/Model/Plugin/QuoteItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,81 +5,101 @@
*/
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 \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::class, [], [], '', false);
$this->quoteItemMock = $this->getMock(\Magento\Quote\Model\Quote\Item::class, [], [], '', false);
$orderItem = $this->orderItemMock;
$this->closureMock = function () use ($orderItem) {
return $orderItem;
};
$this->subjectMock = $this->getMock(\Magento\Quote\Model\Quote\Item\ToOrderItem::class, [], [], '', 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);
$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::class, [], [], '', false);
$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::class,
[],
[],
'',
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');
$bundleAttribute->expects($this->once())
->method('getValue')
->willReturn($attributeValue);

$orderItem = $this->model->aroundConvert($this->subjectMock, $this->closureMock, $this->quoteItemMock, []);
$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('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::class, [], [], '', 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')
->willReturn($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);
}
}
51 changes: 0 additions & 51 deletions app/code/Magento/Catalog/Model/CatalogRegistry.php

This file was deleted.

Loading

0 comments on commit 9a789ac

Please sign in to comment.