Skip to content

Commit bdb0464

Browse files
authored
Merge pull request #1125 from magento-jackalopes/MAGETWO-67538-error-handling
- MAGETWO-67538 Add error handling to implementations of SerializerInterface - MAGETWO-63844 Shipping issue in Cart Price Rule
2 parents f63b0a1 + f542e70 commit bdb0464

File tree

56 files changed

+1877
-391
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1877
-391
lines changed

app/code/Magento/Braintree/Gateway/Config/Config.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,12 @@ public function __construct(
7272
*/
7373
public function getCountrySpecificCardTypeConfig()
7474
{
75-
$countriesCardTypes = $this->serializer->unserialize($this->getValue(self::KEY_COUNTRY_CREDIT_CARD));
76-
77-
return is_array($countriesCardTypes) ? $countriesCardTypes : [];
75+
$countryCardTypes = $this->getValue(self::KEY_COUNTRY_CREDIT_CARD);
76+
if (!$countryCardTypes) {
77+
return [];
78+
}
79+
$countryCardTypes = $this->serializer->unserialize($countryCardTypes);
80+
return is_array($countryCardTypes) ? $countryCardTypes : [];
7881
}
7982

8083
/**

app/code/Magento/Braintree/Model/Adminhtml/System/Config/CountryCreditCard.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,11 @@ public function beforeSave()
8989
*/
9090
public function afterLoad()
9191
{
92-
$value = $this->serializer->unserialize($this->getValue());
93-
if (is_array($value)) {
94-
$value = $this->encodeArrayFieldValue($value);
95-
$this->setValue($value);
92+
if ($this->getValue()) {
93+
$value = $this->serializer->unserialize($this->getValue());
94+
if (is_array($value)) {
95+
$this->setValue($this->encodeArrayFieldValue($value));
96+
}
9697
}
9798
return $this;
9899
}

app/code/Magento/Braintree/Test/Unit/Model/Adminhtml/System/Config/CountryCreditCardTest.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,24 +150,30 @@ public function beforeSaveDataProvider()
150150
}
151151

152152
/**
153-
* @dataProvider afterLoadDataProvider
154153
* @param string $encodedValue
155154
* @param array|null $value
156155
* @param array $hashData
157156
* @param array|null $expected
157+
* @param int $unserializeCalledNum
158+
* @dataProvider afterLoadDataProvider
158159
*/
159-
public function testAfterLoad($encodedValue, $value, array $hashData, $expected)
160-
{
160+
public function testAfterLoad(
161+
$encodedValue,
162+
$value,
163+
array $hashData,
164+
$expected,
165+
$unserializeCalledNum = 1
166+
) {
161167
$this->model->setValue($encodedValue);
162168
$index = 0;
163169
foreach ($hashData as $hash) {
164-
$this->mathRandomMock->expects(static::at($index))
170+
$this->mathRandomMock->expects($this->at($index))
165171
->method('getUniqueHash')
166172
->willReturn($hash);
167173
$index++;
168174
}
169175

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

179185
/**
180186
* Get data to test saved credit cards types
187+
*
181188
* @return array
182189
*/
183190
public function afterLoadDataProvider()
@@ -193,7 +200,8 @@ public function afterLoadDataProvider()
193200
'encoded' => '',
194201
'value' => null,
195202
'randomHash' => [],
196-
'expected' => null
203+
'expected' => null,
204+
0
197205
],
198206
'valid data' => [
199207
'encoded' => '{"US":["AE","VI","MA"],"AF":["AE","MA"]}',

app/code/Magento/Bundle/Model/Product/Price.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ protected function getBundleSelectionIds(\Magento\Catalog\Model\Product $product
166166
$customOption = $product->getCustomOption('bundle_selection_ids');
167167
if ($customOption) {
168168
$selectionIds = $this->serializer->unserialize($customOption->getValue());
169-
if (!empty($selectionIds) && is_array($selectionIds)) {
169+
if (is_array($selectionIds) && !empty($selectionIds)) {
170170
return $selectionIds;
171171
}
172172
}

app/code/Magento/Bundle/Test/Unit/Helper/Catalog/Product/ConfigurationTest.php

Lines changed: 146 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,30 @@
1212
*/
1313
class ConfigurationTest extends \PHPUnit_Framework_TestCase
1414
{
15-
/** @var \Magento\Framework\Pricing\Helper\Data|\PHPUnit_Framework_MockObject_MockObject */
16-
protected $pricingHelper;
15+
/**
16+
* @var \Magento\Framework\Pricing\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
17+
*/
18+
private $pricingHelper;
1719

18-
/** @var \Magento\Catalog\Helper\Product\Configuration|\PHPUnit_Framework_MockObject_MockObject */
19-
protected $productConfiguration;
20+
/**
21+
* @var \Magento\Catalog\Helper\Product\Configuration|\PHPUnit_Framework_MockObject_MockObject
22+
*/
23+
private $productConfiguration;
2024

21-
/** @var \Magento\Framework\Escaper|\PHPUnit_Framework_MockObject_MockObject */
22-
protected $escaper;
25+
/**
26+
* @var \Magento\Framework\Escaper|\PHPUnit_Framework_MockObject_MockObject
27+
*/
28+
private $escaper;
2329

24-
/** @var \Magento\Bundle\Helper\Catalog\Product\Configuration */
25-
protected $helper;
30+
/**
31+
* @var \Magento\Bundle\Helper\Catalog\Product\Configuration
32+
*/
33+
private $helper;
2634

27-
/** @var \Magento\Catalog\Model\Product\Configuration\Item\ItemInterface|\PHPUnit_Framework_MockObject_MockObject */
28-
protected $item;
35+
/**
36+
* @var \Magento\Catalog\Model\Product\Configuration\Item\ItemInterface|\PHPUnit_Framework_MockObject_MockObject
37+
*/
38+
private $item;
2939

3040
/**
3141
* @var \Magento\Framework\Serialize\Serializer\Json
@@ -48,7 +58,13 @@ protected function setUp()
4858
'',
4959
false
5060
);
51-
$this->escaper = $this->getMock(\Magento\Framework\Escaper::class, ['escapeHtml'], [], '', false);
61+
$this->escaper = $this->getMock(
62+
\Magento\Framework\Escaper::class,
63+
['escapeHtml'],
64+
[],
65+
'',
66+
false
67+
);
5268
$this->item = $this->getMock(
5369
\Magento\Catalog\Model\Product\Configuration\Item\ItemInterface::class,
5470
['getQty', 'getProduct', 'getOptionByCode', 'getFileDownloadParams']
@@ -79,12 +95,28 @@ public function testGetSelectionQty()
7995
{
8096
$selectionId = 15;
8197
$selectionQty = 35;
82-
$product = $this->getMock(\Magento\Catalog\Model\Product::class, [], [], '', false);
83-
$option = $this->getMock(\Magento\Catalog\Model\Product\Option::class, ['__wakeup', 'getValue'], [], '', false);
98+
$product = $this->getMock(
99+
\Magento\Catalog\Model\Product::class,
100+
[],
101+
[],
102+
'',
103+
false
104+
);
105+
$option = $this->getMock(
106+
\Magento\Catalog\Model\Product\Option::class,
107+
['__wakeup', 'getValue'],
108+
[],
109+
'',
110+
false
111+
);
84112

85-
$product->expects($this->once())->method('getCustomOption')->with('selection_qty_' . $selectionId)
86-
->will($this->returnValue($option));
87-
$option->expects($this->once())->method('getValue')->will($this->returnValue($selectionQty));
113+
$product->expects($this->once())
114+
->method('getCustomOption')
115+
->with('selection_qty_' . $selectionId)
116+
->willReturn($option);
117+
$option->expects($this->once())
118+
->method('getValue')
119+
->willReturn($selectionQty);
88120

89121
$this->assertEquals($selectionQty, $this->helper->getSelectionQty($product, $selectionId));
90122
}
@@ -100,9 +132,6 @@ public function testGetSelectionQtyIfCustomOptionIsNotSet()
100132
$this->assertEquals(0, $this->helper->getSelectionQty($product, $selectionId));
101133
}
102134

103-
/**
104-
* @covers \Magento\Bundle\Helper\Catalog\Product\Configuration::getSelectionFinalPrice
105-
*/
106135
public function testGetSelectionFinalPrice()
107136
{
108137
$itemQty = 2;
@@ -144,7 +173,13 @@ public function testGetBundleOptionsEmptyBundleOptionsIds()
144173
public function testGetBundleOptionsEmptyBundleSelectionIds()
145174
{
146175
$optionIds = '{"0":"1"}';
147-
$collection = $this->getMock(\Magento\Bundle\Model\ResourceModel\Option\Collection::class, [], [], '', false);
176+
$collection = $this->getMock(
177+
\Magento\Bundle\Model\ResourceModel\Option\Collection::class,
178+
[],
179+
[],
180+
'',
181+
false
182+
);
148183
$product = $this->getMock(
149184
\Magento\Catalog\Model\Product::class,
150185
['getTypeInstance',
@@ -153,28 +188,49 @@ public function testGetBundleOptionsEmptyBundleSelectionIds()
153188
'',
154189
false
155190
);
156-
$typeInstance = $this->getMock(\Magento\Bundle\Model\Product\Type::class, ['getOptionsByIds'], [], '', false);
157-
$selectionOption =
158-
$this->getMock(
159-
\Magento\Catalog\Model\Product\Configuration\Item\Option\OptionInterface::class,
160-
['getValue']
161-
);
162-
$itemOption =
163-
$this->getMock(
164-
\Magento\Catalog\Model\Product\Configuration\Item\Option\OptionInterface::class,
165-
['getValue']
166-
);
191+
$typeInstance = $this->getMock(
192+
\Magento\Bundle\Model\Product\Type::class,
193+
['getOptionsByIds'],
194+
[],
195+
'',
196+
false
197+
);
198+
$selectionOption = $this->getMock(
199+
\Magento\Catalog\Model\Product\Configuration\Item\Option\OptionInterface::class,
200+
['getValue']
201+
);
202+
$itemOption = $this->getMock(
203+
\Magento\Catalog\Model\Product\Configuration\Item\Option\OptionInterface::class,
204+
['getValue']
205+
);
167206

168-
$selectionOption->expects($this->once())->method('getValue')->will($this->returnValue(''));
169-
$itemOption->expects($this->once())->method('getValue')->will($this->returnValue($optionIds));
170-
$typeInstance->expects($this->once())->method('getOptionsByIds')->with(json_decode($optionIds, true), $product)
171-
->will($this->returnValue($collection));
172-
$product->expects($this->once())->method('getTypeInstance')->will($this->returnValue($typeInstance));
173-
$this->item->expects($this->once())->method('getProduct')->will($this->returnValue($product));
174-
$this->item->expects($this->at(1))->method('getOptionByCode')->with('bundle_option_ids')
175-
->will($this->returnValue($itemOption));
176-
$this->item->expects($this->at(2))->method('getOptionByCode')->with('bundle_selection_ids')
177-
->will($this->returnValue($selectionOption));
207+
$selectionOption->expects($this->once())
208+
->method('getValue')
209+
->willReturn('[]');
210+
$itemOption->expects($this->once())
211+
->method('getValue')
212+
->willReturn($optionIds);
213+
$typeInstance->expects($this->once())
214+
->method('getOptionsByIds')
215+
->with(
216+
json_decode($optionIds, true),
217+
$product
218+
)
219+
->willReturn($collection);
220+
$product->expects($this->once())
221+
->method('getTypeInstance')
222+
->willReturn($typeInstance);
223+
$this->item->expects($this->once())
224+
->method('getProduct')
225+
->willReturn($product);
226+
$this->item->expects($this->at(1))
227+
->method('getOptionByCode')
228+
->with('bundle_option_ids')
229+
->willReturn($itemOption);
230+
$this->item->expects($this->at(2))
231+
->method('getOptionByCode')
232+
->with('bundle_selection_ids')
233+
->willReturn($selectionOption);
178234

179235
$this->assertEquals([], $this->helper->getBundleOptions($this->item));
180236
}
@@ -201,38 +257,44 @@ public function testGetOptions()
201257
'',
202258
false
203259
);
204-
$priceModel =
205-
$this->getMock(\Magento\Bundle\Model\Product\Price::class, ['getSelectionFinalTotalPrice'], [], '', false);
206-
$selectionQty =
207-
$this->getMock(\Magento\Quote\Model\Quote\Item\Option::class, ['getValue', '__wakeup'], [], '', false);
208-
$bundleOption =
209-
$this->getMock(
210-
\Magento\Bundle\Model\Option::class,
211-
['getSelections',
212-
'getTitle',
213-
'__wakeup'],
214-
[],
215-
'',
216-
false
217-
);
218-
$selectionOption =
219-
$this->getMock(
220-
\Magento\Catalog\Model\Product\Configuration\Item\Option\OptionInterface::class,
221-
['getValue']
222-
);
223-
$collection =
224-
$this->getMock(
225-
\Magento\Bundle\Model\ResourceModel\Option\Collection::class,
226-
['appendSelections'],
227-
[],
228-
'',
229-
false
230-
);
231-
$itemOption =
232-
$this->getMock(
233-
\Magento\Catalog\Model\Product\Configuration\Item\Option\OptionInterface::class,
234-
['getValue']
235-
);
260+
$priceModel = $this->getMock(
261+
\Magento\Bundle\Model\Product\Price::class,
262+
['getSelectionFinalTotalPrice'],
263+
[],
264+
'',
265+
false
266+
);
267+
$selectionQty = $this->getMock(
268+
\Magento\Quote\Model\Quote\Item\Option::class,
269+
['getValue', '__wakeup'],
270+
[],
271+
'',
272+
false
273+
);
274+
$bundleOption = $this->getMock(
275+
\Magento\Bundle\Model\Option::class,
276+
['getSelections',
277+
'getTitle',
278+
'__wakeup'],
279+
[],
280+
'',
281+
false
282+
);
283+
$selectionOption = $this->getMock(
284+
\Magento\Catalog\Model\Product\Configuration\Item\Option\OptionInterface::class,
285+
['getValue']
286+
);
287+
$collection = $this->getMock(
288+
\Magento\Bundle\Model\ResourceModel\Option\Collection::class,
289+
['appendSelections'],
290+
[],
291+
'',
292+
false
293+
);
294+
$itemOption = $this->getMock(
295+
\Magento\Catalog\Model\Product\Configuration\Item\Option\OptionInterface::class,
296+
['getValue']
297+
);
236298
$collection2 = $this->getMock(
237299
\Magento\Bundle\Model\ResourceModel\Selection\Collection::class,
238300
[],
@@ -241,7 +303,10 @@ public function testGetOptions()
241303
false
242304
);
243305

244-
$this->escaper->expects($this->once())->method('escapeHtml')->with('name')->will($this->returnValue('name'));
306+
$this->escaper->expects($this->once())
307+
->method('escapeHtml')
308+
->with('name')
309+
->willReturn('name');
245310
$this->pricingHelper->expects($this->once())->method('currency')->with(15)
246311
->will($this->returnValue('<span class="price">$15.00</span>'));
247312
$priceModel->expects($this->once())->method('getSelectionFinalTotalPrice')->will($this->returnValue(15));
@@ -252,8 +317,13 @@ public function testGetOptions()
252317
$collection->expects($this->once())->method('appendSelections')->with($collection2, true)
253318
->will($this->returnValue([$bundleOption]));
254319
$itemOption->expects($this->once())->method('getValue')->will($this->returnValue($optionIds));
255-
$typeInstance->expects($this->once())->method('getOptionsByIds')->with(json_decode($optionIds, true), $product)
256-
->will($this->returnValue($collection));
320+
$typeInstance->expects($this->once())
321+
->method('getOptionsByIds')
322+
->with(
323+
json_decode($optionIds, true),
324+
$product
325+
)
326+
->willReturn($collection);
257327
$typeInstance->expects($this->once())
258328
->method('getSelectionsByIds')
259329
->with(json_decode($selectionIds, true), $product)

app/code/Magento/Bundle/Test/Unit/Model/Product/TypeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2591,7 +2591,7 @@ public function testCheckProductBuyStateEmptyOptionsException()
25912591
$this->mockBundleCollection();
25922592
$product = $this->getProductMock();
25932593
$product->method('getCustomOption')->willReturnMap([
2594-
['bundle_selection_ids', new DataObject(['value' => ''])],
2594+
['bundle_selection_ids', new DataObject(['value' => '[]'])],
25952595
['info_buyRequest', new DataObject(['value' => json_encode(['bundle_option' => ''])])],
25962596
]);
25972597
$product->setCustomOption(json_encode([]));

0 commit comments

Comments
 (0)