Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/code/Magento/Catalog/Pricing/Render/FinalPriceBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ protected function wrapResult($html)
{
return '<div class="price-box ' . $this->getData('css_classes') . '" ' .
'data-role="priceBox" ' .
'data-product-id="' . $this->getSaleableItem()->getId() . '"' .
'data-product-id="' . $this->getSaleableItem()->getId() . '" ' .
'data-price-box="product-id-' . $this->getSaleableItem()->getId() . '"' .
'>' . $html . '</div>';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ public function testRenderMsrpEnabled()

//assert price wrapper
$this->assertEquals(
'<div class="price-box price-final_price" data-role="priceBox" data-product-id="">test</div>',
'<div class="price-box price-final_price" data-role="priceBox" data-product-id="" ' .
'data-price-box="product-id-">test</div>',
$result
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ class Configurable extends \Magento\Catalog\Block\Product\View\AbstractView
*/
private $customerSession;

/**
* @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Variations\Prices
*/
private $variationPrices;

/**
* @param \Magento\Catalog\Block\Product\Context $context
* @param \Magento\Framework\Stdlib\ArrayUtils $arrayUtils
Expand All @@ -86,6 +91,7 @@ class Configurable extends \Magento\Catalog\Block\Product\View\AbstractView
* @param array $data
* @param Format|null $localeFormat
* @param Session|null $customerSession
* @param \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Variations\Prices|null $variationPrices
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand All @@ -99,7 +105,8 @@ public function __construct(
ConfigurableAttributeData $configurableAttributeData,
array $data = [],
Format $localeFormat = null,
Session $customerSession = null
Session $customerSession = null,
\Magento\ConfigurableProduct\Model\Product\Type\Configurable\Variations\Prices $variationPrices = null
) {
$this->priceCurrency = $priceCurrency;
$this->helper = $helper;
Expand All @@ -109,6 +116,9 @@ public function __construct(
$this->configurableAttributeData = $configurableAttributeData;
$this->localeFormat = $localeFormat ?: ObjectManager::getInstance()->get(Format::class);
$this->customerSession = $customerSession ?: ObjectManager::getInstance()->get(Session::class);
$this->variationPrices = $variationPrices ?: ObjectManager::getInstance()->get(
\Magento\ConfigurableProduct\Model\Product\Type\Configurable\Variations\Prices::class
);

parent::__construct(
$context,
Expand Down Expand Up @@ -211,9 +221,6 @@ public function getJsonConfig()
$store = $this->getCurrentStore();
$currentProduct = $this->getProduct();

$regularPrice = $currentProduct->getPriceInfo()->getPrice('regular_price');
$finalPrice = $currentProduct->getPriceInfo()->getPrice('final_price');

$options = $this->helper->getOptions($currentProduct, $this->getAllowProducts());
$attributesData = $this->configurableAttributeData->getAttributesData($currentProduct, $options);

Expand All @@ -223,17 +230,7 @@ public function getJsonConfig()
'currencyFormat' => $store->getCurrentCurrency()->getOutputFormat(),
'optionPrices' => $this->getOptionPrices(),
'priceFormat' => $this->localeFormat->getPriceFormat(),
'prices' => [
'oldPrice' => [
'amount' => $this->localeFormat->getNumber($regularPrice->getAmount()->getValue()),
],
'basePrice' => [
'amount' => $this->localeFormat->getNumber($finalPrice->getAmount()->getBaseAmount()),
],
'finalPrice' => [
'amount' => $this->localeFormat->getNumber($finalPrice->getAmount()->getValue()),
],
],
'prices' => $this->variationPrices->getFormattedPrices($this->getProduct()->getPriceInfo()),
'productId' => $currentProduct->getId(),
'chooseText' => __('Choose an Option...'),
'images' => $this->getOptionImages(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\ConfigurableProduct\Model\Product\Type\Configurable\Variations;

/**
* Configurable product variation prices.
*/
class Prices
{
/**
* @var \Magento\Framework\Locale\Format
*/
private $localeFormat;

/**
* Prices constructor.
* @param \Magento\Framework\Locale\Format $localeFormat
*/
public function __construct(\Magento\Framework\Locale\Format $localeFormat)
{
$this->localeFormat = $localeFormat;
}

/**
* Get product prices for configurable variations
*
* @param \Magento\Framework\Pricing\PriceInfo\Base $priceInfo
* @return array
*/
public function getFormattedPrices(\Magento\Framework\Pricing\PriceInfo\Base $priceInfo)
{
$regularPrice = $priceInfo->getPrice('regular_price');
$finalPrice = $priceInfo->getPrice('final_price');

return [
'oldPrice' => [
'amount' => $this->localeFormat->getNumber($regularPrice->getAmount()->getValue()),
],
'basePrice' => [
'amount' => $this->localeFormat->getNumber($finalPrice->getAmount()->getBaseAmount()),
],
'finalPrice' => [
'amount' => $this->localeFormat->getNumber($finalPrice->getAmount()->getValue()),
],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ class ConfigurableTest extends \PHPUnit\Framework\TestCase
*/
private $customerSession;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $variationPricesMock;

protected function setUp()
{
$this->mockContextObject();
Expand Down Expand Up @@ -144,6 +149,10 @@ protected function setUp()
->disableOriginalConstructor()
->getMock();

$this->variationPricesMock = $this->createMock(
\Magento\ConfigurableProduct\Model\Product\Type\Configurable\Variations\Prices::class
);

$this->block = new \Magento\ConfigurableProduct\Block\Product\View\Type\Configurable(
$this->context,
$this->arrayUtils,
Expand All @@ -155,7 +164,8 @@ protected function setUp()
$this->configurableAttributeData,
[],
$this->localeFormat,
$this->customerSession
$this->customerSession,
$this->variationPricesMock
);
}

Expand Down Expand Up @@ -260,12 +270,8 @@ public function testGetJsonConfig()
'getAmount',
])
->getMockForAbstractClass();
$priceMock->expects($this->any())
->method('getAmount')
->willReturn($amountMock);

$priceMock->expects($this->any())->method('getAmount')->willReturn($amountMock);
$tierPriceMock = $this->getTierPriceMock($amountMock, $priceQty, $percentage);

$productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
->disableOriginalConstructor()
->getMock();
Expand All @@ -283,27 +289,16 @@ public function testGetJsonConfig()
['tier_price', $tierPriceMock],
]);

$productMock->expects($this->any())
->method('getTypeInstance')
->willReturn($productTypeMock);
$productMock->expects($this->any())
->method('getPriceInfo')
->willReturn($priceInfoMock);
$productMock->expects($this->any())
->method('isSaleable')
->willReturn(true);
$productMock->expects($this->any())
->method('getId')
->willReturn($productId);
$productMock->expects($this->any())->method('getTypeInstance')->willReturn($productTypeMock);
$productMock->expects($this->any())->method('getPriceInfo')->willReturn($priceInfoMock);
$productMock->expects($this->any())->method('isSaleable')->willReturn(true);
$productMock->expects($this->any())->method('getId')->willReturn($productId);

$this->helper->expects($this->any())
->method('getOptions')
->with($productMock, [$productMock])
->willReturn([]);

$this->product->expects($this->any())
->method('getSkipSaleableCheck')
->willReturn(true);
$this->product->expects($this->any())->method('getSkipSaleableCheck')->willReturn(true);

$attributesData = [
'attributes' => [],
Expand All @@ -315,9 +310,7 @@ public function testGetJsonConfig()
->with($productMock, [])
->willReturn($attributesData);

$this->localeFormat->expects($this->any())
->method('getPriceFormat')
->willReturn([]);
$this->localeFormat->expects($this->atLeastOnce())->method('getPriceFormat')->willReturn([]);
$this->localeFormat->expects($this->any())
->method('getNumber')
->willReturnMap([
Expand All @@ -326,16 +319,29 @@ public function testGetJsonConfig()
[$percentage, $percentage],
]);

$this->variationPricesMock->expects($this->once())
->method('getFormattedPrices')
->with($priceInfoMock)
->willReturn(
[
'oldPrice' => [
'amount' => $amount,
],
'basePrice' => [
'amount' => $amount,
],
'finalPrice' => [
'amount' => $amount,
],
]
);

$expectedArray = $this->getExpectedArray($productId, $amount, $priceQty, $percentage);
$expectedJson = json_encode($expectedArray);

$this->jsonEncoder->expects($this->once())
->method('encode')
->with($expectedArray)
->willReturn($expectedJson);
$this->jsonEncoder->expects($this->once())->method('encode')->with($expectedArray)->willReturn($expectedJson);

$this->block->setData('product', $productMock);

$result = $this->block->getJsonConfig();
$this->assertEquals($expectedJson, $result);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\ConfigurableProduct\Test\Unit\Model\Product\Type\Configurable\Variations;

use PHPUnit\Framework\TestCase;

class PricesTest extends TestCase
{
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $localeFormatMock;

/**
* @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Variations\Prices
*/
private $model;

protected function setUp()
{
$this->localeFormatMock = $this->createMock(\Magento\Framework\Locale\Format::class);
$this->model = new \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Variations\Prices(
$this->localeFormatMock
);
}

public function testGetFormattedPrices()
{
$expected = [
'oldPrice' => [
'amount' => 500
],
'basePrice' => [
'amount' => 1000
],
'finalPrice' => [
'amount' => 500
]
];
$priceInfoMock = $this->createMock(\Magento\Framework\Pricing\PriceInfo\Base::class);
$priceMock = $this->createMock(\Magento\Framework\Pricing\Price\PriceInterface::class);
$priceInfoMock->expects($this->atLeastOnce())->method('getPrice')->willReturn($priceMock);

$amountMock = $this->createMock(\Magento\Framework\Pricing\Amount\AmountInterface::class);
$amountMock->expects($this->atLeastOnce())->method('getValue')->willReturn(500);
$amountMock->expects($this->atLeastOnce())->method('getBaseAmount')->willReturn(1000);
$priceMock->expects($this->atLeastOnce())->method('getAmount')->willReturn($amountMock);

$this->localeFormatMock->expects($this->atLeastOnce())
->method('getNumber')
->withConsecutive([500], [1000], [500])
->will($this->onConsecutiveCalls(500, 1000, 500));

$this->assertEquals($expected, $this->model->getFormattedPrices($priceInfoMock));
}
}
Loading