Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
DiscountCalculatorTest.php
Go to the documentation of this file.
1 <?php
8 
12 class DiscountCalculatorTest extends \PHPUnit\Framework\TestCase
13 {
17  protected $calculator;
18 
22  protected $productMock;
23 
27  protected $priceInfoMock;
28 
32  protected $finalPriceMock;
33 
37  protected $priceMock;
38 
42  protected function setUp()
43  {
44  $this->productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
45  $this->priceInfoMock = $this->createPartialMock(
46  \Magento\Framework\Pricing\PriceInfo\Base::class,
47  ['getPrice', 'getPrices']
48  );
49  $this->finalPriceMock = $this->createMock(\Magento\Catalog\Pricing\Price\FinalPrice::class);
50  $this->priceMock = $this->getMockForAbstractClass(
51  \Magento\Bundle\Pricing\Price\DiscountProviderInterface::class
52  );
53  $this->calculator = new \Magento\Bundle\Pricing\Price\DiscountCalculator();
54  }
55 
62  protected function getPriceMock($value)
63  {
64  $price = clone $this->priceMock;
65  $price->expects($this->exactly(3))
66  ->method('getDiscountPercent')
67  ->will($this->returnValue($value));
68  return $price;
69  }
70 
75  {
76  $this->productMock->expects($this->exactly(2))
77  ->method('getPriceInfo')
78  ->will($this->returnValue($this->priceInfoMock));
79  $this->priceInfoMock->expects($this->once())
80  ->method('getPrice')
81  ->with($this->equalTo(\Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE))
82  ->will($this->returnValue($this->finalPriceMock));
83  $this->finalPriceMock->expects($this->once())
84  ->method('getValue')
85  ->will($this->returnValue(100));
86  $this->priceInfoMock->expects($this->once())
87  ->method('getPrices')
88  ->will(
89  $this->returnValue(
90  [
91  $this->getPriceMock(30),
92  $this->getPriceMock(20),
93  $this->getPriceMock(40),
94  ]
95  )
96  );
97  $this->assertEquals(20, $this->calculator->calculateDiscount($this->productMock));
98  }
99 
104  {
105  $this->productMock->expects($this->once())
106  ->method('getPriceInfo')
107  ->will($this->returnValue($this->priceInfoMock));
108  $this->priceInfoMock->expects($this->once())
109  ->method('getPrices')
110  ->will(
111  $this->returnValue(
112  [
113  $this->getPriceMock(30),
114  $this->getPriceMock(20),
115  $this->getPriceMock(40),
116  ]
117  )
118  );
119  $this->assertEquals(10, $this->calculator->calculateDiscount($this->productMock, 50));
120  }
121 }
$price
$value
Definition: gender.phtml:16