Skip to content

Commit

Permalink
Merge pull request #1125 from magento-jackalopes/MAGETWO-67538-error-…
Browse files Browse the repository at this point in the history
…handling

- MAGETWO-67538 Add error handling to implementations of SerializerInterface
- MAGETWO-63844 Shipping issue in Cart Price Rule
  • Loading branch information
buskamuza authored May 20, 2017
2 parents f63b0a1 + f542e70 commit bdb0464
Show file tree
Hide file tree
Showing 56 changed files with 1,877 additions and 391 deletions.
9 changes: 6 additions & 3 deletions app/code/Magento/Braintree/Gateway/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,12 @@ public function __construct(
*/
public function getCountrySpecificCardTypeConfig()
{
$countriesCardTypes = $this->serializer->unserialize($this->getValue(self::KEY_COUNTRY_CREDIT_CARD));

return is_array($countriesCardTypes) ? $countriesCardTypes : [];
$countryCardTypes = $this->getValue(self::KEY_COUNTRY_CREDIT_CARD);
if (!$countryCardTypes) {
return [];
}
$countryCardTypes = $this->serializer->unserialize($countryCardTypes);
return is_array($countryCardTypes) ? $countryCardTypes : [];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@ public function beforeSave()
*/
public function afterLoad()
{
$value = $this->serializer->unserialize($this->getValue());
if (is_array($value)) {
$value = $this->encodeArrayFieldValue($value);
$this->setValue($value);
if ($this->getValue()) {
$value = $this->serializer->unserialize($this->getValue());
if (is_array($value)) {
$this->setValue($this->encodeArrayFieldValue($value));
}
}
return $this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,24 +150,30 @@ public function beforeSaveDataProvider()
}

/**
* @dataProvider afterLoadDataProvider
* @param string $encodedValue
* @param array|null $value
* @param array $hashData
* @param array|null $expected
* @param int $unserializeCalledNum
* @dataProvider afterLoadDataProvider
*/
public function testAfterLoad($encodedValue, $value, array $hashData, $expected)
{
public function testAfterLoad(
$encodedValue,
$value,
array $hashData,
$expected,
$unserializeCalledNum = 1
) {
$this->model->setValue($encodedValue);
$index = 0;
foreach ($hashData as $hash) {
$this->mathRandomMock->expects(static::at($index))
$this->mathRandomMock->expects($this->at($index))
->method('getUniqueHash')
->willReturn($hash);
$index++;
}

$this->serializerMock->expects($this->once())
$this->serializerMock->expects($this->exactly($unserializeCalledNum))
->method('unserialize')
->with($encodedValue)
->willReturn($value);
Expand All @@ -178,6 +184,7 @@ public function testAfterLoad($encodedValue, $value, array $hashData, $expected)

/**
* Get data to test saved credit cards types
*
* @return array
*/
public function afterLoadDataProvider()
Expand All @@ -193,7 +200,8 @@ public function afterLoadDataProvider()
'encoded' => '',
'value' => null,
'randomHash' => [],
'expected' => null
'expected' => null,
0
],
'valid data' => [
'encoded' => '{"US":["AE","VI","MA"],"AF":["AE","MA"]}',
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Bundle/Model/Product/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ protected function getBundleSelectionIds(\Magento\Catalog\Model\Product $product
$customOption = $product->getCustomOption('bundle_selection_ids');
if ($customOption) {
$selectionIds = $this->serializer->unserialize($customOption->getValue());
if (!empty($selectionIds) && is_array($selectionIds)) {
if (is_array($selectionIds) && !empty($selectionIds)) {
return $selectionIds;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,30 @@
*/
class ConfigurationTest extends \PHPUnit_Framework_TestCase
{
/** @var \Magento\Framework\Pricing\Helper\Data|\PHPUnit_Framework_MockObject_MockObject */
protected $pricingHelper;
/**
* @var \Magento\Framework\Pricing\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
*/
private $pricingHelper;

/** @var \Magento\Catalog\Helper\Product\Configuration|\PHPUnit_Framework_MockObject_MockObject */
protected $productConfiguration;
/**
* @var \Magento\Catalog\Helper\Product\Configuration|\PHPUnit_Framework_MockObject_MockObject
*/
private $productConfiguration;

/** @var \Magento\Framework\Escaper|\PHPUnit_Framework_MockObject_MockObject */
protected $escaper;
/**
* @var \Magento\Framework\Escaper|\PHPUnit_Framework_MockObject_MockObject
*/
private $escaper;

/** @var \Magento\Bundle\Helper\Catalog\Product\Configuration */
protected $helper;
/**
* @var \Magento\Bundle\Helper\Catalog\Product\Configuration
*/
private $helper;

/** @var \Magento\Catalog\Model\Product\Configuration\Item\ItemInterface|\PHPUnit_Framework_MockObject_MockObject */
protected $item;
/**
* @var \Magento\Catalog\Model\Product\Configuration\Item\ItemInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $item;

/**
* @var \Magento\Framework\Serialize\Serializer\Json
Expand All @@ -48,7 +58,13 @@ protected function setUp()
'',
false
);
$this->escaper = $this->getMock(\Magento\Framework\Escaper::class, ['escapeHtml'], [], '', false);
$this->escaper = $this->getMock(
\Magento\Framework\Escaper::class,
['escapeHtml'],
[],
'',
false
);
$this->item = $this->getMock(
\Magento\Catalog\Model\Product\Configuration\Item\ItemInterface::class,
['getQty', 'getProduct', 'getOptionByCode', 'getFileDownloadParams']
Expand Down Expand Up @@ -79,12 +95,28 @@ public function testGetSelectionQty()
{
$selectionId = 15;
$selectionQty = 35;
$product = $this->getMock(\Magento\Catalog\Model\Product::class, [], [], '', false);
$option = $this->getMock(\Magento\Catalog\Model\Product\Option::class, ['__wakeup', 'getValue'], [], '', false);
$product = $this->getMock(
\Magento\Catalog\Model\Product::class,
[],
[],
'',
false
);
$option = $this->getMock(
\Magento\Catalog\Model\Product\Option::class,
['__wakeup', 'getValue'],
[],
'',
false
);

$product->expects($this->once())->method('getCustomOption')->with('selection_qty_' . $selectionId)
->will($this->returnValue($option));
$option->expects($this->once())->method('getValue')->will($this->returnValue($selectionQty));
$product->expects($this->once())
->method('getCustomOption')
->with('selection_qty_' . $selectionId)
->willReturn($option);
$option->expects($this->once())
->method('getValue')
->willReturn($selectionQty);

$this->assertEquals($selectionQty, $this->helper->getSelectionQty($product, $selectionId));
}
Expand All @@ -100,9 +132,6 @@ public function testGetSelectionQtyIfCustomOptionIsNotSet()
$this->assertEquals(0, $this->helper->getSelectionQty($product, $selectionId));
}

/**
* @covers \Magento\Bundle\Helper\Catalog\Product\Configuration::getSelectionFinalPrice
*/
public function testGetSelectionFinalPrice()
{
$itemQty = 2;
Expand Down Expand Up @@ -144,7 +173,13 @@ public function testGetBundleOptionsEmptyBundleOptionsIds()
public function testGetBundleOptionsEmptyBundleSelectionIds()
{
$optionIds = '{"0":"1"}';
$collection = $this->getMock(\Magento\Bundle\Model\ResourceModel\Option\Collection::class, [], [], '', false);
$collection = $this->getMock(
\Magento\Bundle\Model\ResourceModel\Option\Collection::class,
[],
[],
'',
false
);
$product = $this->getMock(
\Magento\Catalog\Model\Product::class,
['getTypeInstance',
Expand All @@ -153,28 +188,49 @@ public function testGetBundleOptionsEmptyBundleSelectionIds()
'',
false
);
$typeInstance = $this->getMock(\Magento\Bundle\Model\Product\Type::class, ['getOptionsByIds'], [], '', false);
$selectionOption =
$this->getMock(
\Magento\Catalog\Model\Product\Configuration\Item\Option\OptionInterface::class,
['getValue']
);
$itemOption =
$this->getMock(
\Magento\Catalog\Model\Product\Configuration\Item\Option\OptionInterface::class,
['getValue']
);
$typeInstance = $this->getMock(
\Magento\Bundle\Model\Product\Type::class,
['getOptionsByIds'],
[],
'',
false
);
$selectionOption = $this->getMock(
\Magento\Catalog\Model\Product\Configuration\Item\Option\OptionInterface::class,
['getValue']
);
$itemOption = $this->getMock(
\Magento\Catalog\Model\Product\Configuration\Item\Option\OptionInterface::class,
['getValue']
);

$selectionOption->expects($this->once())->method('getValue')->will($this->returnValue(''));
$itemOption->expects($this->once())->method('getValue')->will($this->returnValue($optionIds));
$typeInstance->expects($this->once())->method('getOptionsByIds')->with(json_decode($optionIds, true), $product)
->will($this->returnValue($collection));
$product->expects($this->once())->method('getTypeInstance')->will($this->returnValue($typeInstance));
$this->item->expects($this->once())->method('getProduct')->will($this->returnValue($product));
$this->item->expects($this->at(1))->method('getOptionByCode')->with('bundle_option_ids')
->will($this->returnValue($itemOption));
$this->item->expects($this->at(2))->method('getOptionByCode')->with('bundle_selection_ids')
->will($this->returnValue($selectionOption));
$selectionOption->expects($this->once())
->method('getValue')
->willReturn('[]');
$itemOption->expects($this->once())
->method('getValue')
->willReturn($optionIds);
$typeInstance->expects($this->once())
->method('getOptionsByIds')
->with(
json_decode($optionIds, true),
$product
)
->willReturn($collection);
$product->expects($this->once())
->method('getTypeInstance')
->willReturn($typeInstance);
$this->item->expects($this->once())
->method('getProduct')
->willReturn($product);
$this->item->expects($this->at(1))
->method('getOptionByCode')
->with('bundle_option_ids')
->willReturn($itemOption);
$this->item->expects($this->at(2))
->method('getOptionByCode')
->with('bundle_selection_ids')
->willReturn($selectionOption);

$this->assertEquals([], $this->helper->getBundleOptions($this->item));
}
Expand All @@ -201,38 +257,44 @@ public function testGetOptions()
'',
false
);
$priceModel =
$this->getMock(\Magento\Bundle\Model\Product\Price::class, ['getSelectionFinalTotalPrice'], [], '', false);
$selectionQty =
$this->getMock(\Magento\Quote\Model\Quote\Item\Option::class, ['getValue', '__wakeup'], [], '', false);
$bundleOption =
$this->getMock(
\Magento\Bundle\Model\Option::class,
['getSelections',
'getTitle',
'__wakeup'],
[],
'',
false
);
$selectionOption =
$this->getMock(
\Magento\Catalog\Model\Product\Configuration\Item\Option\OptionInterface::class,
['getValue']
);
$collection =
$this->getMock(
\Magento\Bundle\Model\ResourceModel\Option\Collection::class,
['appendSelections'],
[],
'',
false
);
$itemOption =
$this->getMock(
\Magento\Catalog\Model\Product\Configuration\Item\Option\OptionInterface::class,
['getValue']
);
$priceModel = $this->getMock(
\Magento\Bundle\Model\Product\Price::class,
['getSelectionFinalTotalPrice'],
[],
'',
false
);
$selectionQty = $this->getMock(
\Magento\Quote\Model\Quote\Item\Option::class,
['getValue', '__wakeup'],
[],
'',
false
);
$bundleOption = $this->getMock(
\Magento\Bundle\Model\Option::class,
['getSelections',
'getTitle',
'__wakeup'],
[],
'',
false
);
$selectionOption = $this->getMock(
\Magento\Catalog\Model\Product\Configuration\Item\Option\OptionInterface::class,
['getValue']
);
$collection = $this->getMock(
\Magento\Bundle\Model\ResourceModel\Option\Collection::class,
['appendSelections'],
[],
'',
false
);
$itemOption = $this->getMock(
\Magento\Catalog\Model\Product\Configuration\Item\Option\OptionInterface::class,
['getValue']
);
$collection2 = $this->getMock(
\Magento\Bundle\Model\ResourceModel\Selection\Collection::class,
[],
Expand All @@ -241,7 +303,10 @@ public function testGetOptions()
false
);

$this->escaper->expects($this->once())->method('escapeHtml')->with('name')->will($this->returnValue('name'));
$this->escaper->expects($this->once())
->method('escapeHtml')
->with('name')
->willReturn('name');
$this->pricingHelper->expects($this->once())->method('currency')->with(15)
->will($this->returnValue('<span class="price">$15.00</span>'));
$priceModel->expects($this->once())->method('getSelectionFinalTotalPrice')->will($this->returnValue(15));
Expand All @@ -252,8 +317,13 @@ public function testGetOptions()
$collection->expects($this->once())->method('appendSelections')->with($collection2, true)
->will($this->returnValue([$bundleOption]));
$itemOption->expects($this->once())->method('getValue')->will($this->returnValue($optionIds));
$typeInstance->expects($this->once())->method('getOptionsByIds')->with(json_decode($optionIds, true), $product)
->will($this->returnValue($collection));
$typeInstance->expects($this->once())
->method('getOptionsByIds')
->with(
json_decode($optionIds, true),
$product
)
->willReturn($collection);
$typeInstance->expects($this->once())
->method('getSelectionsByIds')
->with(json_decode($selectionIds, true), $product)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2591,7 +2591,7 @@ public function testCheckProductBuyStateEmptyOptionsException()
$this->mockBundleCollection();
$product = $this->getProductMock();
$product->method('getCustomOption')->willReturnMap([
['bundle_selection_ids', new DataObject(['value' => ''])],
['bundle_selection_ids', new DataObject(['value' => '[]'])],
['info_buyRequest', new DataObject(['value' => json_encode(['bundle_option' => ''])])],
]);
$product->setCustomOption(json_encode([]));
Expand Down
Loading

0 comments on commit bdb0464

Please sign in to comment.