Skip to content

Commit bf0d1e9

Browse files
committed
Unit Test Code coverage
1 parent 46ad64c commit bf0d1e9

File tree

2 files changed

+86
-3
lines changed

2 files changed

+86
-3
lines changed

app/code/Magento/Wishlist/Pricing/ConfiguredPrice/ConfigurableProduct.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ public function getValue()
5959
{
6060
$product = $this->getProduct();
6161
$price = $product->getPriceInfo()->getPrice(self::PRICE_CODE)->getValue();
62+
6263
if ($product->getProductOptionsCollection()) {
63-
foreach ($product->getProductOptionsCollection() as $configurableOptions) {
64-
foreach ($configurableOptions->getValues() as $configurableOptionData) {
65-
$price += $configurableOptionData->getData('price');
64+
foreach ($product->getProductOptionsCollection() as $configurationOptionsVal) {
65+
$configurableOptionsData = $configurationOptionsVal->getValues();
66+
foreach($configurableOptionsData as $configurableOptionData) {
67+
$price += $configurableOptionData->getPrice();
6668
}
6769
}
6870
}

app/code/Magento/Wishlist/Test/Unit/Pricing/ConfiguredPrice/ConfigurableProductTest.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\Wishlist\Test\Unit\Pricing\ConfiguredPrice;
99

10+
use Magento\Catalog\Api\Data\ProductInterface;
1011
use Magento\Catalog\Model\Product;
1112
use Magento\Framework\Pricing\Adjustment\CalculatorInterface;
1213
use Magento\Framework\Pricing\Price\PriceInterface;
@@ -46,6 +47,11 @@ class ConfigurableProductTest extends TestCase
4647
*/
4748
private $priceInfoMock;
4849

50+
/**
51+
* @var ProductInterface|MockObject
52+
*/
53+
private $productCustomOption;
54+
4955
protected function setUp(): void
5056
{
5157
$this->priceInfoMock = $this->getMockBuilder(PriceInfoInterface::class)
@@ -65,6 +71,9 @@ protected function setUp(): void
6571
$this->priceCurrency = $this->getMockBuilder(PriceCurrencyInterface::class)
6672
->getMockForAbstractClass();
6773

74+
$this->productCustomOption = $this->getMockBuilder(ProductInterface::class)
75+
->getMockForAbstractClass();
76+
6877
$this->model = new ConfigurableProduct(
6978
$this->saleableItem,
7079
null,
@@ -143,4 +152,76 @@ public function testGetValueWithNoCustomOption()
143152

144153
$this->assertEquals(100, $this->model->getValue());
145154
}
155+
156+
public function testGetValueWithCustomOption() {
157+
$priceValue = 10;
158+
$customOptionPrice = 5;
159+
160+
$priceMock = $this->getMockBuilder(PriceInterface::class)
161+
->getMockForAbstractClass();
162+
163+
$priceMock->expects($this->once())
164+
->method('getValue')
165+
->willReturn($priceValue);
166+
167+
$this->priceInfoMock = $this->getMockBuilder(Base::class)
168+
->disableOriginalConstructor()
169+
->getMock();
170+
$this->priceInfoMock->expects($this->once())
171+
->method('getPrice')
172+
->with(ConfigurableProduct::PRICE_CODE)
173+
->willReturn($priceMock);
174+
175+
$productMock = $this->getMockBuilder(Product::class)
176+
->disableOriginalConstructor()
177+
->getMock();
178+
$productMock->expects($this->once())
179+
->method('getPriceInfo')
180+
->willReturn($this->priceInfoMock);
181+
182+
$wishlistItemOptionMock = $this->getMockBuilder(Option::class)
183+
->disableOriginalConstructor()
184+
->getMock();
185+
$wishlistItemOptionMock->expects($this->once())
186+
->method('getProduct')
187+
->willReturn($productMock);
188+
189+
$this->saleableItem->expects($this->once())
190+
->method('getCustomOption')
191+
->with('simple_product')
192+
->willReturn($wishlistItemOptionMock);
193+
194+
$productOptionMock = $this->getMockBuilder('Magento\Catalog\Model\ResourceModel\Product\Option\Collection')
195+
->disableOriginalConstructor()
196+
->addMethods(['getValues'])
197+
->onlyMethods(['getIterator','getData'])
198+
->getMock();
199+
200+
$productValMock = $this->getMockBuilder('Magento\Catalog\Model\Product\Option\Value')
201+
->disableOriginalConstructor()
202+
->addMethods(['getIterator'])
203+
->onlyMethods(['getPrice'])
204+
->getMock();
205+
206+
$productMock->expects($this->atLeastOnce())
207+
->method('getProductOptionsCollection')
208+
->willReturn($productOptionMock);
209+
210+
$productOptionMock->expects($this->any())->method('getIterator')
211+
->willReturn(new \ArrayIterator([$productOptionMock]));
212+
213+
$productOptionMock->expects($this->any())
214+
->method('getValues')
215+
->willReturn($productValMock);
216+
217+
$productValMock->expects($this->any())->method('getIterator')
218+
->willReturn(new \ArrayIterator([$productValMock]));
219+
220+
$productValMock->expects($this->any())
221+
->method('getPrice')
222+
->willReturn($customOptionPrice);
223+
224+
$totalPrice = $priceValue + $customOptionPrice;
225+
$this->assertEquals($totalPrice, $this->model->getValue());
226+
}
146227
}

0 commit comments

Comments
 (0)