123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Bundle\Test\Unit\Pricing\Price;
- /**
- * Class DiscountCalculatorTest
- */
- class DiscountCalculatorTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Bundle\Pricing\Price\DiscountCalculator
- */
- protected $calculator;
- /**
- * @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $productMock;
- /**
- * @var \Magento\Framework\Pricing\PriceInfo\Base |\PHPUnit_Framework_MockObject_MockObject
- */
- protected $priceInfoMock;
- /**
- * @var \Magento\Catalog\Pricing\Price\FinalPrice|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $finalPriceMock;
- /**
- * @var \Magento\Bundle\Pricing\Price\DiscountProviderInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $priceMock;
- /**
- * Test setUp
- */
- protected function setUp()
- {
- $this->productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
- $this->priceInfoMock = $this->createPartialMock(
- \Magento\Framework\Pricing\PriceInfo\Base::class,
- ['getPrice', 'getPrices']
- );
- $this->finalPriceMock = $this->createMock(\Magento\Catalog\Pricing\Price\FinalPrice::class);
- $this->priceMock = $this->getMockForAbstractClass(
- \Magento\Bundle\Pricing\Price\DiscountProviderInterface::class
- );
- $this->calculator = new \Magento\Bundle\Pricing\Price\DiscountCalculator();
- }
- /**
- * Returns price mock with specified %
- *
- * @param int $value
- * @return \PHPUnit_Framework_MockObject_MockObject
- */
- protected function getPriceMock($value)
- {
- $price = clone $this->priceMock;
- $price->expects($this->exactly(3))
- ->method('getDiscountPercent')
- ->will($this->returnValue($value));
- return $price;
- }
- /**
- * test method calculateDiscount with default price amount
- */
- public function testCalculateDiscountWithDefaultAmount()
- {
- $this->productMock->expects($this->exactly(2))
- ->method('getPriceInfo')
- ->will($this->returnValue($this->priceInfoMock));
- $this->priceInfoMock->expects($this->once())
- ->method('getPrice')
- ->with($this->equalTo(\Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE))
- ->will($this->returnValue($this->finalPriceMock));
- $this->finalPriceMock->expects($this->once())
- ->method('getValue')
- ->will($this->returnValue(100));
- $this->priceInfoMock->expects($this->once())
- ->method('getPrices')
- ->will(
- $this->returnValue(
- [
- $this->getPriceMock(30),
- $this->getPriceMock(20),
- $this->getPriceMock(40),
- ]
- )
- );
- $this->assertEquals(20, $this->calculator->calculateDiscount($this->productMock));
- }
- /**
- * test method calculateDiscount with custom price amount
- */
- public function testCalculateDiscountWithCustomAmount()
- {
- $this->productMock->expects($this->once())
- ->method('getPriceInfo')
- ->will($this->returnValue($this->priceInfoMock));
- $this->priceInfoMock->expects($this->once())
- ->method('getPrices')
- ->will(
- $this->returnValue(
- [
- $this->getPriceMock(30),
- $this->getPriceMock(20),
- $this->getPriceMock(40),
- ]
- )
- );
- $this->assertEquals(10, $this->calculator->calculateDiscount($this->productMock, 50));
- }
- }
|