Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BasePriceTest.php
Go to the documentation of this file.
1 <?php
8 
12 class BasePriceTest extends \PHPUnit\Framework\TestCase
13 {
17  protected $basePrice;
18 
22  protected $priceInfoMock;
23 
27  protected $saleableItemMock;
28 
32  protected $calculatorMock;
33 
37  protected $regularPriceMock;
38 
42  protected $tearPriceMock;
43 
47  protected $specialPriceMock;
48 
52  protected $prices;
53 
57  protected function setUp()
58  {
59  $qty = 1;
60  $this->saleableItemMock = $this->createMock(\Magento\Catalog\Model\Product::class);
61  $this->priceInfoMock = $this->createMock(\Magento\Framework\Pricing\PriceInfo\Base::class);
62  $this->regularPriceMock = $this->createMock(\Magento\Catalog\Pricing\Price\RegularPrice::class);
63  $this->tearPriceMock = $this->createMock(\Magento\Catalog\Pricing\Price\TierPrice::class);
64  $this->specialPriceMock = $this->createMock(\Magento\Catalog\Pricing\Price\SpecialPrice::class);
65  $this->calculatorMock = $this->createMock(\Magento\Framework\Pricing\Adjustment\Calculator::class);
66 
67  $this->saleableItemMock->expects($this->once())
68  ->method('getPriceInfo')
69  ->will($this->returnValue($this->priceInfoMock));
70  $this->prices = [
71  'regular_price' => $this->regularPriceMock,
72  'tear_price' => $this->tearPriceMock,
73  'special_price' => $this->specialPriceMock,
74  ];
75 
76  $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
77  $this->basePrice = $helper->getObject(
78  \Magento\Catalog\Pricing\Price\BasePrice::class,
79  [
80  'saleableItem' => $this->saleableItemMock,
81  'quantity' => $qty,
82  'calculator' => $this->calculatorMock
83  ]
84  );
85  }
86 
92  public function testGetValue($specialPriceValue, $expectedResult)
93  {
94  $this->priceInfoMock->expects($this->once())
95  ->method('getPrices')
96  ->will($this->returnValue($this->prices));
97  $this->regularPriceMock->expects($this->exactly(3))
98  ->method('getValue')
99  ->will($this->returnValue(100));
100  $this->tearPriceMock->expects($this->exactly(2))
101  ->method('getValue')
102  ->will($this->returnValue(99));
103  $this->specialPriceMock->expects($this->any())
104  ->method('getValue')
105  ->will($this->returnValue($specialPriceValue));
106  $this->assertSame($expectedResult, $this->basePrice->getValue());
107  }
108 
112  public function getValueDataProvider()
113  {
114  return [[77, 77], [0, 0], [false, 99]];
115  }
116 
117  public function testGetAmount()
118  {
119  $amount = 20.;
120 
121  $priceMock = $this->getMockBuilder(\Magento\Framework\Pricing\Price\PriceInterface::class)
122  ->getMockForAbstractClass();
123 
124  $this->priceInfoMock->expects($this->once())
125  ->method('getPrices')
126  ->willReturn([$priceMock]);
127 
128  $this->calculatorMock->expects($this->once())
129  ->method('getAmount')
130  ->with(false, $this->saleableItemMock)
131  ->willReturn($amount);
132 
133  $this->assertEquals($amount, $this->basePrice->getAmount());
134  }
135 }
testGetValue($specialPriceValue, $expectedResult)
$helper
Definition: iframe.phtml:13
$amount
Definition: order.php:14