Skip to content

Commit 74e9299

Browse files
committed
MAGETWO-32001: Refactor and cover by unit tests Magento classes.
- MAGETWO-32003: Refactor and cover: Magento\Bundle\Model\Product\Type::_prepareProduct - Added test testPrepareForCartAdvancedSelectRequiredOptions
1 parent c27da27 commit 74e9299

File tree

2 files changed

+91
-35
lines changed
  • app/code/Magento/Bundle/Model/Product
  • dev/tests/unit/testsuite/Magento/Bundle/Model/Product

2 files changed

+91
-35
lines changed

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

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ protected function _prepareProduct(\Magento\Framework\Object $buyRequest, $produ
683683

684684
$options = $buyRequest->getBundleOption();
685685
if (is_array($options)) {
686-
$options = array_filter($options, 'intval');
686+
$options = $this->recursiveIntval($options);
687687
$optionIds = array_keys($options);
688688

689689
if (empty($optionIds) && $isStrictProcessMode) {
@@ -700,21 +700,8 @@ protected function _prepareProduct(\Magento\Framework\Object $buyRequest, $produ
700700
}
701701
}
702702
}
703-
$selectionIds = [];
704703

705-
foreach ($options as $selectionId) {
706-
if (!is_array($selectionId)) {
707-
if ($selectionId != '') {
708-
$selectionIds[] = (int)$selectionId;
709-
}
710-
} else {
711-
foreach ($selectionId as $id) {
712-
if ($id != '') {
713-
$selectionIds[] = (int)$id;
714-
}
715-
}
716-
}
717-
}
704+
$selectionIds = $this->multiToOFlatArray($options);
718705
// If product has not been configured yet then $selections array should be empty
719706
if (!empty($selectionIds)) {
720707
$selections = $this->getSelectionsByIds($selectionIds, $product);
@@ -723,13 +710,14 @@ protected function _prepareProduct(\Magento\Framework\Object $buyRequest, $produ
723710
foreach ($selections->getItems() as $selection) {
724711
if (!$selection->isSalable() && !$skipSaleableCheck) {
725712
$_option = $optionsCollection->getItemById($selection->getOptionId());
726-
if (is_array($options[$_option->getId()]) && count($options[$_option->getId()]) > 1) {
713+
$optionId = $_option->getId();
714+
if (is_array($options[$optionId]) && count($options[$optionId]) > 1) {
727715
$moreSelections = true;
728716
} else {
729717
$moreSelections = false;
730718
}
731-
if ($_option->getRequired() && (!$_option->isMultiSelection() ||
732-
$_option->isMultiSelection() && !$moreSelections)
719+
$isMultiSelection = $_option->isMultiSelection();
720+
if ($_option->getRequired() && (!$isMultiSelection || !$moreSelections)
733721
) {
734722
throw new \Magento\Framework\Model\Exception(
735723
__('The required options you selected are not available.')
@@ -816,7 +804,9 @@ protected function _prepareProduct(\Magento\Framework\Object $buyRequest, $produ
816804
}
817805

818806
if (!isset($_result[0])) {
819-
throw new \Magento\Framework\Model\Exception(__('We cannot add this item to your shopping cart.'));
807+
throw new \Magento\Framework\Model\Exception(
808+
__('We cannot add this item to your shopping cart.')
809+
);
820810
}
821811

822812
$result[] = $_result[0]->setParentProductId($product->getId())
@@ -849,6 +839,41 @@ protected function _prepareProduct(\Magento\Framework\Object $buyRequest, $produ
849839
return $this->getSpecifyOptionMessage();
850840
}
851841

842+
/**
843+
* @param array $array
844+
* @return int[]|int[][]
845+
*/
846+
public function recursiveIntval(array $array)
847+
{
848+
foreach($array as $key => $value) {
849+
if (is_array($value)) {
850+
$array[$key] = $this->recursiveIntval($value);
851+
} elseif (is_numeric($value) && (int)$value != 0) {
852+
$array[$key] = (int) $value;
853+
} else {
854+
unset($array[$key]);
855+
}
856+
}
857+
return $array;
858+
}
859+
860+
/**
861+
* @param array $array
862+
* @return int[]
863+
*/
864+
public function multiToOFlatArray(array $array)
865+
{
866+
$flatArray = [];
867+
foreach($array as $key => $value) {
868+
if (is_array($value)) {
869+
$flatArray = array_merge($flatArray, $this->multiToOFlatArray($value));
870+
} else {
871+
$flatArray[$key] = $value;
872+
}
873+
}
874+
return $flatArray;
875+
}
876+
852877
/**
853878
* Retrieve message for specify option(s)
854879
*
@@ -873,7 +898,7 @@ public function getSelectionsByIds($selectionIds, $product)
873898
$usedSelections = $product->getData($this->_keyUsedSelections);
874899
$usedSelectionsIds = $product->getData($this->_keyUsedSelectionsIds);
875900

876-
if (!$usedSelections || serialize($usedSelectionsIds) != serialize($selectionIds)) {
901+
if (!$usedSelections || $usedSelectionsIds !== $selectionIds) {
877902
$storeId = $product->getStoreId();
878903
$usedSelections = $this->_bundleCollection
879904
->create()

dev/tests/unit/testsuite/Magento/Bundle/Model/Product/TypeTest.php

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Magento\Bundle\Model\Product;
66

77
use Magento\Bundle\Model\Resource\Option\Collection;
8+
use Magento\Bundle\Model\Resource\Selection\Collection as SelectionCollection;
89
use Magento\Catalog\Model\Product\Option\Type\DefaultType;
910
use Magento\Framework\Model\Exception;
1011

@@ -120,7 +121,17 @@ public function testPrepareForCartAdvancedSelectRequiredOptions()
120121
->getMock();
121122
/* @var $option \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Model\Product\Option */
122123
$option = $this->getMockBuilder('Magento\Catalog\Model\Product\Option')
123-
->setMethods(['groupFactory', 'getType', 'getId', 'getRequired'])
124+
->setMethods(['groupFactory', 'getType', 'getId', 'getRequired', 'isMultiSelection'])
125+
->disableOriginalConstructor()
126+
->getMock();
127+
/** @var \PHPUnit_Framework_MockObject_MockObject|SelectionCollection $selectionCollection */
128+
$selectionCollection = $this->getMockBuilder('Magento\Bundle\Model\Resource\Selection\Collection')
129+
->setMethods(['getItems'])
130+
->disableOriginalConstructor()
131+
->getMock();
132+
/** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Object $buyRequest */
133+
$selection = $this->getMockBuilder('Magento\Framework\Object')
134+
->setMethods(['__wakeup', 'isSalable', 'getOptionId'])
124135
->disableOriginalConstructor()
125136
->getMock();
126137
/** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Model\Product $product */
@@ -148,44 +159,64 @@ public function testPrepareForCartAdvancedSelectRequiredOptions()
148159
->getMock();
149160
/** @var \PHPUnit_Framework_MockObject_MockObject|Collection $optionCollection */
150161
$optionCollection = $this->getMockBuilder('Magento\Bundle\Model\Resource\Option\Collection')
151-
->setMethods(['getItems'])
162+
->setMethods(['getItems', 'getItemById'])
152163
->disableOriginalConstructor()
153164
->getMock();
154165

155166
$this->parentClass($group, $option, $buyRequest, $product);
156167

157-
$product->expects($this->at(0))
168+
$product->expects($this->any())
158169
->method('getSkipCheckRequiredOption')
159170
->willReturn(true);
160-
$product->expects($this->at(1))
161-
->method('getSkipCheckRequiredOption')
162-
->willReturn(false);
163171
$product->expects($this->once())
164172
->method('getTypeInstance')
165173
->willReturn($productType);
166174
$product->expects($this->once())
167175
->method('hasData')
168176
->willReturn(true);
169-
$product->expects($this->once())
177+
$product->expects($this->any())
170178
->method('getData')
171-
->willReturn($optionCollection);
179+
->willReturnCallback(function($key) use ($optionCollection, $selectionCollection){
180+
$resultValue = null;
181+
switch($key) {
182+
case '_cache_instance_options_collection':
183+
$resultValue = $optionCollection;
184+
break;
185+
case '_cache_instance_used_selections':
186+
$resultValue = $selectionCollection;
187+
break;
188+
case '_cache_instance_used_selections_ids':
189+
$resultValue = [0 => 5];
190+
break;
191+
}
192+
return $resultValue;
193+
});
194+
$optionCollection->expects($this->once())
195+
->method('getItemById')
196+
->willReturn($option);
172197
$productType->expects($this->once())
173198
->method('setStoreFilter');
174199
$buyRequest->expects($this->once())
175200
->method('getBundleOption')
176-
->willReturn([1]);
177-
$optionCollection->expects($this->once())
201+
->willReturn([3 => 5]);
202+
$selectionCollection->expects($this->once())
178203
->method('getItems')
179-
->willReturn([$option]);
204+
->willReturn([$selection]);
205+
$selection->expects($this->once())
206+
->method('isSalable')
207+
->willReturn(false);
208+
$option->expects($this->at(3))
209+
->method('getId')
210+
->willReturn(3);
180211
$option->expects($this->once())
181212
->method('getRequired')
182213
->willReturn(true);
183-
$option->expects($this->at(1))
184-
->method('getId')
185-
->willReturn(3);
214+
$option->expects($this->once())
215+
->method('isMultiSelection')
216+
->willReturn(true);
186217

187218
$result = $this->model->prepareForCartAdvanced($buyRequest, $product);
188-
$this->assertEquals('Please select all required options.', $result);
219+
$this->assertEquals('The required options you selected are not available.', $result);
189220
}
190221

191222
public function testPrepareForCartAdvancedParentClassReturnString()

0 commit comments

Comments
 (0)