Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractPriceTest.php
Go to the documentation of this file.
1 <?php
8 
9 use \Magento\Framework\Pricing\Price\AbstractPrice;
10 
14 class AbstractPriceTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $price;
20 
24  protected $priceInfoMock;
25 
29  protected $saleableItemMock;
30 
34  protected $calculatorMock;
35 
39  protected $priceCurrencyMock;
40 
44  protected function setUp()
45  {
46  $qty = 1;
47  $this->saleableItemMock = $this->createMock(\Magento\Catalog\Model\Product::class);
48  $this->priceInfoMock = $this->createMock(\Magento\Framework\Pricing\PriceInfo\Base::class);
49  $this->calculatorMock = $this->createMock(\Magento\Framework\Pricing\Adjustment\Calculator::class);
50 
51  $this->saleableItemMock->expects($this->once())
52  ->method('getPriceInfo')
53  ->will($this->returnValue($this->priceInfoMock));
54  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
55 
56  $this->priceCurrencyMock = $this->createMock(\Magento\Framework\Pricing\PriceCurrencyInterface::class);
57 
58  $this->price = $objectManager->getObject(
59  \Magento\Framework\Pricing\Test\Unit\Price\Stub::class,
60  [
61  'saleableItem' => $this->saleableItemMock,
62  'quantity' => $qty,
63  'calculator' => $this->calculatorMock,
64  'priceCurrency' => $this->priceCurrencyMock,
65  ]
66  );
67  }
68 
72  public function testGetAmount()
73  {
74  $priceValue = $this->price->getValue();
75  $amountValue = 88;
76  $this->calculatorMock->expects($this->once())
77  ->method('getAmount')
78  ->with($this->equalTo($priceValue))
79  ->will($this->returnValue($amountValue));
80  $this->assertEquals($amountValue, $this->price->getAmount());
81  }
82 
86  public function testGetPriceCode()
87  {
88  $this->assertEquals(AbstractPrice::PRICE_CODE, $this->price->getPriceCode());
89  }
90 
91  public function testGetCustomAmount()
92  {
93  $exclude = false;
94  $amount = 21.0;
95  $convertedValue = 30.25;
96  $customAmount = 42.0;
97 
98  $this->priceCurrencyMock->expects($this->any())
99  ->method('convertAndRound')
100  ->with($amount)
101  ->will($this->returnValue($convertedValue));
102  $this->calculatorMock->expects($this->once())
103  ->method('getAmount')
104  ->with($convertedValue, $this->saleableItemMock, $exclude)
105  ->will($this->returnValue($customAmount));
106 
107  $this->assertEquals($customAmount, $this->price->getCustomAmount($amount, $exclude));
108  }
109 
110  public function testGetCustomAmountDefault()
111  {
112  $customAmount = 42.0;
113  $this->calculatorMock->expects($this->once())
114  ->method('getAmount')
115  ->with($this->price->getValue(), $this->saleableItemMock, null)
116  ->will($this->returnValue($customAmount));
117 
118  $this->assertEquals($customAmount, $this->price->getCustomAmount());
119  }
120 
121  public function testGetQuantity()
122  {
123  $this->assertEquals(1, $this->price->getQuantity());
124  }
125 }
$objectManager
Definition: bootstrap.php:17
$amount
Definition: order.php:14