Skip to content

Commit

Permalink
Merge pull request #337 from magento-fearless-kiwis/develop
Browse files Browse the repository at this point in the history
[FearlessKiwis] Bug Fixes for Pricing and FPT
  • Loading branch information
Tang, Yu(ytang1) committed Jun 3, 2015
2 parents aeaaec5 + 2891271 commit c4002b6
Show file tree
Hide file tree
Showing 23 changed files with 721 additions and 88 deletions.
18 changes: 16 additions & 2 deletions app/code/Magento/Bundle/Pricing/Price/BundleRegularPrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Magento\Bundle\Pricing\Adjustment\BundleCalculatorInterface;
use Magento\Catalog\Model\Product;
use Magento\Framework\Pricing\Amount\AmountInterface;
use Magento\Catalog\Pricing\Price\CustomOptionPrice;
use Magento\Bundle\Model\Product\Price;

/**
* Bundle product regular price model
Expand Down Expand Up @@ -48,7 +50,13 @@ public function __construct(
public function getAmount()
{
if (null === $this->amount) {
$this->amount = $this->calculator->getMinRegularAmount($this->getValue(), $this->product);
$price = $this->getValue();
if ($this->product->getPriceType() == Price::PRICE_TYPE_FIXED) {
/** @var \Magento\Catalog\Pricing\Price\CustomOptionPrice $customOptionPrice */
$customOptionPrice = $this->priceInfo->getPrice(CustomOptionPrice::PRICE_CODE);
$price += $customOptionPrice->getCustomOptionRange(true);
}
$this->amount = $this->calculator->getMinRegularAmount($price, $this->product);
}
return $this->amount;
}
Expand All @@ -61,7 +69,13 @@ public function getAmount()
public function getMaximalPrice()
{
if (null === $this->maximalPrice) {
$this->maximalPrice = $this->calculator->getMaxRegularAmount($this->getValue(), $this->product);
$price = $this->getValue();
if ($this->product->getPriceType() == Price::PRICE_TYPE_FIXED) {
/** @var \Magento\Catalog\Pricing\Price\CustomOptionPrice $customOptionPrice */
$customOptionPrice = $this->priceInfo->getPrice(CustomOptionPrice::PRICE_CODE);
$price += $customOptionPrice->getCustomOptionRange(false);
}
$this->maximalPrice = $this->calculator->getMaxRegularAmount($price, $this->product);
}
return $this->maximalPrice;
}
Expand Down
18 changes: 16 additions & 2 deletions app/code/Magento/Bundle/Pricing/Price/FinalPrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

use Magento\Catalog\Model\Product;
use Magento\Framework\Pricing\Adjustment\CalculatorInterface;
use Magento\Catalog\Pricing\Price\CustomOptionPrice;
use Magento\Bundle\Model\Product\Price;

/**
* Final price model
Expand Down Expand Up @@ -68,7 +70,13 @@ public function getValue()
public function getMaximalPrice()
{
if (!$this->maximalPrice) {
$this->maximalPrice = $this->calculator->getMaxAmount($this->getBasePrice()->getValue(), $this->product);
$price = $this->getBasePrice()->getValue();
if ($this->product->getPriceType() == Price::PRICE_TYPE_FIXED) {
/** @var \Magento\Catalog\Pricing\Price\CustomOptionPrice $customOptionPrice */
$customOptionPrice = $this->priceInfo->getPrice(CustomOptionPrice::PRICE_CODE);
$price += $customOptionPrice->getCustomOptionRange(false);
}
$this->maximalPrice = $this->calculator->getMaxAmount($price, $this->product);
}
return $this->maximalPrice;
}
Expand All @@ -91,7 +99,13 @@ public function getMinimalPrice()
public function getAmount()
{
if (!$this->minimalPrice) {
$this->minimalPrice = $this->calculator->getAmount(parent::getValue(), $this->product);
$price = parent::getValue();
if ($this->product->getPriceType() == Price::PRICE_TYPE_FIXED) {
/** @var \Magento\Catalog\Pricing\Price\CustomOptionPrice $customOptionPrice */
$customOptionPrice = $this->priceInfo->getPrice(CustomOptionPrice::PRICE_CODE);
$price += $customOptionPrice->getCustomOptionRange(true);
}
$this->minimalPrice = $this->calculator->getAmount($price, $this->product);
}
return $this->minimalPrice;
}
Expand Down
10 changes: 10 additions & 0 deletions app/code/Magento/Bundle/Pricing/Price/TierPrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Magento\Bundle\Pricing\Price;

use Magento\Catalog\Pricing\Price\RegularPrice;
use Magento\Framework\Pricing\Amount\AmountInterface;

/**
* Bundle tier prices model
Expand Down Expand Up @@ -89,4 +90,13 @@ public function isPercentageDiscount()
{
return true;
}

/**
* @param AmountInterface $amount
* @return float
*/
public function getSavePercent(AmountInterface $amount)
{
return round($amount->getBaseAmount());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
namespace Magento\Bundle\Test\Unit\Pricing\Price;

use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
use Magento\Catalog\Pricing\Price\CustomOptionPrice;
use Magento\Bundle\Model\Product\Price;

class BundleRegularPriceTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -25,6 +27,9 @@ class BundleRegularPriceTest extends \PHPUnit_Framework_TestCase
/** @var \Magento\Framework\Pricing\PriceInfo\Base |\PHPUnit_Framework_MockObject_MockObject */
protected $priceInfoMock;

/** @var CustomOptionPrice|\PHPUnit_Framework_MockObject_MockObject */
protected $customOptionPriceMock;

/**
* @var int
*/
Expand All @@ -40,11 +45,18 @@ class BundleRegularPriceTest extends \PHPUnit_Framework_TestCase
*/
protected function setUp()
{
$this->saleableInterfaceMock = $this->getMock('Magento\Catalog\Model\Product', [], [], '', false);
$this->saleableInterfaceMock = $this->getMockBuilder('\Magento\Catalog\Model\Product')
->disableOriginalConstructor()
->setMethods(['getPriceInfo', 'getPriceType', 'getPrice'])
->getMock();
$this->bundleCalculatorMock = $this->getMock('Magento\Bundle\Pricing\Adjustment\BundleCalculatorInterface');

$this->priceInfoMock = $this->getMock('Magento\Framework\Pricing\PriceInfo\Base', [], [], '', false);

$this->customOptionPriceMock = $this->getMockBuilder('\Magento\Catalog\Pricing\Price\CustomOptionPrice')
->disableOriginalConstructor()
->getMock();

$this->saleableInterfaceMock->expects($this->once())
->method('getPriceInfo')
->will($this->returnValue($this->priceInfoMock));
Expand Down Expand Up @@ -110,6 +122,48 @@ public function testGetMaximalPrice()
$this->assertEquals($expectedResult, $result, 'Incorrect amount the second time');
}

public function testGetMaximalPriceForFixedPriceBundleWithOption()
{
$price = 5;
$maxOptionPrice = 2;

$expectedPrice = $price + $maxOptionPrice;

$this->priceInfoMock->expects($this->atLeastOnce())
->method('getPrice')
->with(CustomOptionPrice::PRICE_CODE)
->willReturn($this->customOptionPriceMock);

$this->customOptionPriceMock->expects($this->once())
->method('getCustomOptionRange')
->with(false)
->willReturn($maxOptionPrice);

$this->saleableInterfaceMock->expects($this->once())
->method('getPriceType')
->willReturn(Price::PRICE_TYPE_FIXED);

$this->saleableInterfaceMock->expects($this->once())
->method('getPrice')
->will($this->returnValue($price));

$this->bundleCalculatorMock->expects($this->once())
->method('getMaxRegularAmount')
->with($expectedPrice, $this->saleableInterfaceMock)
->will($this->returnValue($expectedPrice));

$this->priceCurrencyMock->expects($this->once())
->method('convertAndRound')
->will($this->returnArgument(0));

$result = $this->regularPrice->getMaximalPrice();
$this->assertEquals($expectedPrice, $result, 'Incorrect amount');

//Calling a second time, should use cached value
$result = $this->regularPrice->getMaximalPrice();
$this->assertEquals($expectedPrice, $result, 'Incorrect amount the second time');
}

public function testGetMinimalPrice()
{
$expectedResult = 5;
Expand All @@ -134,4 +188,45 @@ public function testGetMinimalPrice()
$result = $this->regularPrice->getMinimalPrice();
$this->assertEquals($expectedResult, $result, 'Incorrect amount the second time');
}

public function testGetMinimalPriceForFixedPricedBundleWithOptions()
{
$price = 5;
$minOptionPrice = 1;
$expectedValue = $price + $minOptionPrice;

$this->saleableInterfaceMock->expects($this->once())
->method('getPrice')
->will($this->returnValue($price));

$this->saleableInterfaceMock->expects($this->once())
->method('getPriceType')
->willReturn(Price::PRICE_TYPE_FIXED);

$this->priceInfoMock->expects($this->atLeastOnce())
->method('getPrice')
->with(CustomOptionPrice::PRICE_CODE)
->willReturn($this->customOptionPriceMock);

$this->customOptionPriceMock->expects($this->once())
->method('getCustomOptionRange')
->with(true)
->willReturn($minOptionPrice);

$this->priceCurrencyMock->expects($this->once())
->method('convertAndRound')
->will($this->returnArgument(0));

$this->bundleCalculatorMock->expects($this->once())
->method('getMinRegularAmount')
->with($expectedValue, $this->saleableInterfaceMock)
->will($this->returnValue($expectedValue));

$result = $this->regularPrice->getMinimalPrice();
$this->assertEquals($expectedValue, $result, 'Incorrect amount');

//Calling a second time, should use cached value
$result = $this->regularPrice->getMinimalPrice();
$this->assertEquals($expectedValue, $result, 'Incorrect amount the second time');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
*/

namespace Magento\Bundle\Test\Unit\Pricing\Price;

use Magento\Bundle\Pricing\Price\BundleOptionPrice;
use Magento\Catalog\Pricing\Price\CustomOptionPrice;
use Magento\Bundle\Model\Product\Price;

use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;

Expand Down Expand Up @@ -38,6 +41,9 @@ class FinalPriceTest extends \PHPUnit_Framework_TestCase
/** @var BundleOptionPrice|\PHPUnit_Framework_MockObject_MockObject */
protected $bundleOptionMock;

/** @var CustomOptionPrice|\PHPUnit_Framework_MockObject_MockObject */
protected $customOptionPriceMock;

/**
* @var \Magento\Framework\Pricing\PriceCurrencyInterface|\PHPUnit_Framework_MockObject_MockObject
*/
Expand All @@ -48,7 +54,10 @@ class FinalPriceTest extends \PHPUnit_Framework_TestCase
*/
protected function prepareMock()
{
$this->saleableInterfaceMock = $this->getMock('Magento\Catalog\Model\Product', [], [], '', false);
$this->saleableInterfaceMock = $this->getMockBuilder('\Magento\Catalog\Model\Product')
->disableOriginalConstructor()
->setMethods(['getPriceType', 'getPriceInfo'])
->getMock();
$this->bundleCalculatorMock = $this->getMock('Magento\Bundle\Pricing\Adjustment\BundleCalculatorInterface');

$this->basePriceMock = $this->getMock('Magento\Catalog\Pricing\Price\BasePrice', [], [], '', false);
Expand All @@ -60,13 +69,18 @@ protected function prepareMock()
->disableOriginalConstructor()
->getMock();

$this->customOptionPriceMock = $this->getMockBuilder('\Magento\Catalog\Pricing\Price\CustomOptionPrice')
->disableOriginalConstructor()
->getMock();

$this->priceInfoMock = $this->getMock('Magento\Framework\Pricing\PriceInfo\Base', [], [], '', false);

$this->priceInfoMock->expects($this->atLeastOnce())
->method('getPrice')
->will($this->returnValueMap([
[\Magento\Catalog\Pricing\Price\BasePrice::PRICE_CODE, $this->basePriceMock],
[BundleOptionPrice::PRICE_CODE, $this->bundleOptionMock],
[CustomOptionPrice::PRICE_CODE, $this->customOptionPriceMock],
]));

$this->saleableInterfaceMock->expects($this->once())
Expand Down Expand Up @@ -128,6 +142,54 @@ public function testGetMaximalPrice($baseAmount)
$this->assertSame($result, $this->finalPrice->getMaximalPrice());
}

public function testGetMaximalPriceFixedBundleWithOption()
{
$optionMaxPrice = 2;
$this->baseAmount = 5;
$result = 7;
$this->prepareMock();

$this->saleableInterfaceMock->expects($this->once())
->method('getPriceType')
->willReturn(Price::PRICE_TYPE_FIXED);
$this->customOptionPriceMock->expects($this->once())
->method('getCustomOptionRange')
->with(false)
->willReturn($optionMaxPrice);

$this->bundleCalculatorMock->expects($this->once())
->method('getMaxAmount')
->with($this->equalTo($this->baseAmount + $optionMaxPrice), $this->equalTo($this->saleableInterfaceMock))
->will($this->returnValue($result));
$this->assertSame($result, $this->finalPrice->getMaximalPrice());
//The second call should use cached value
$this->assertSame($result, $this->finalPrice->getMaximalPrice());
}

public function testGetMinimalPriceFixedBundleWithOption()
{
$optionMaxPrice = 2;
$this->baseAmount = 5;
$result = 7;
$this->prepareMock();

$this->saleableInterfaceMock->expects($this->once())
->method('getPriceType')
->willReturn(Price::PRICE_TYPE_FIXED);
$this->customOptionPriceMock->expects($this->once())
->method('getCustomOptionRange')
->with(true)
->willReturn($optionMaxPrice);

$this->bundleCalculatorMock->expects($this->once())
->method('getAmount')
->with($this->equalTo($this->baseAmount + $optionMaxPrice), $this->equalTo($this->saleableInterfaceMock))
->will($this->returnValue($result));
$this->assertSame($result, $this->finalPrice->getMinimalPrice());
//The second call should use cached value
$this->assertSame($result, $this->finalPrice->getMinimalPrice());
}

/**
* @dataProvider getValueDataProvider
*/
Expand Down
24 changes: 24 additions & 0 deletions app/code/Magento/Bundle/Test/Unit/Pricing/Price/TierPriceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,28 @@ public function providerForGetterTierPriceList()
]
];
}

/**
* @dataProvider providerForTestGetSavePercent
*/
public function testGetSavePercent($baseAmount, $savePercent)
{
$amount = $this->getMockForAbstractClass('Magento\Framework\Pricing\Amount\AmountInterface');
$amount->expects($this->once())->method('getBaseAmount')->willReturn($baseAmount);

$this->assertEquals($savePercent, $this->model->getSavePercent($amount));
}

/**
* @return array
*/
public function providerForTestGetSavePercent()
{
return [
'no fraction' => [10.0000, 10],
'lower half' => [10.1234, 10],
'half way' => [10.5000, 11],
'upper half' => [10.6789, 11],
];
}
}
1 change: 1 addition & 0 deletions app/code/Magento/Bundle/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<item name="base_price" xsi:type="string">Magento\Catalog\Pricing\Price\BasePrice</item>
<item name="configured_price" xsi:type="string">Magento\Bundle\Pricing\Price\ConfiguredPrice</item>
<item name="bundle_option" xsi:type="string">Magento\Bundle\Pricing\Price\BundleOptionPrice</item>
<item name="catalog_rule_price" xsi:type="string">Magento\CatalogRule\Pricing\Price\CatalogRulePrice</item>
</argument>
</arguments>
</virtualType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ $tierPrices = $tierPriceModel->getTierPriceList();
<?php echo __(
'Buy %1 with %2 discount each',
$price['price_qty'],
'<strong class="benefit">' . $price['price']->getBaseAmount() . '%</strong>'
'<strong class="benefit">' . $tierPriceModel->getSavePercent($price['price']) . '%</strong>'
); ?>
</li>
<?php endforeach; ?>
Expand Down
Loading

0 comments on commit c4002b6

Please sign in to comment.