Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractAdjustmentTest.php
Go to the documentation of this file.
1 <?php
7 
8 use \Magento\Framework\Pricing\Render\AbstractAdjustment;
9 
13 class AbstractAdjustmentTest extends \PHPUnit\Framework\TestCase
14 {
18  protected $model;
19 
23  protected $priceCurrency;
24 
28  protected $data;
29 
30  protected function setUp()
31  {
32  $this->priceCurrency = $this->createMock(\Magento\Framework\Pricing\PriceCurrencyInterface::class);
33  $this->data = ['argument_one' => 1];
34 
35  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
36  $constructorArgs = $objectManager->getConstructArguments(
37  \Magento\Framework\Pricing\Render\AbstractAdjustment::class,
38  [
39  'priceCurrency' => $this->priceCurrency,
40  'data' => $this->data
41  ]
42  );
43  $this->model = $this->getMockBuilder(\Magento\Framework\Pricing\Render\AbstractAdjustment::class)
44  ->setConstructorArgs($constructorArgs)
45  ->setMethods(['getData', 'setData', 'apply'])
46  ->getMockForAbstractClass();
47  }
48 
49  public function testConvertAndFormatCurrency()
50  {
51  $amount = '100';
52  $includeContainer = true;
54 
55  $result = '100.0 grn';
56 
57  $this->priceCurrency->expects($this->once())
58  ->method('convertAndFormat')
59  ->with($amount, $includeContainer, $precision)
60  ->will($this->returnValue($result));
61 
62  $this->assertEquals($result, $this->model->convertAndFormatCurrency($amount, $includeContainer, $precision));
63  }
64 
65  public function testRender()
66  {
67  $amountRender = $this->createMock(\Magento\Framework\Pricing\Render\Amount::class);
68  $arguments = ['argument_two' => 2];
69  $mergedArguments = ['argument_one' => 1, 'argument_two' => 2];
70  $renderText = 'amount data';
71 
72  $this->model->expects($this->at(0))
73  ->method('getData')
74  ->will($this->returnValue($this->data));
75  $this->model->expects($this->at(1))
76  ->method('setData')
77  ->with($mergedArguments);
78  $this->model->expects($this->at(2))
79  ->method('apply')
80  ->will($this->returnValue($renderText));
81  $this->model->expects($this->at(3))
82  ->method('setData')
83  ->with($this->data);
84 
85  $result = $this->model->render($amountRender, $arguments);
86  $this->assertEquals($renderText, $result);
87  }
88 
89  public function testGetAmountRender()
90  {
91  $amountRender = $this->createMock(\Magento\Framework\Pricing\Render\Amount::class);
92  $this->model->expects($this->at(0))
93  ->method('getData')
94  ->will($this->returnValue($this->data));
95  $this->model->render($amountRender);
96  $this->assertEquals($amountRender, $this->model->getAmountRender());
97  }
98 
99  public function testGetPriceType()
100  {
101  $amountRender = $this->createMock(\Magento\Framework\Pricing\Render\Amount::class);
102  $price = $this->getMockForAbstractClass(\Magento\Framework\Pricing\Price\PriceInterface::class);
103  $sealableItem = $this->getMockForAbstractClass(\Magento\Framework\Pricing\SaleableInterface::class);
104  $priceInfo = $this->createMock(\Magento\Framework\Pricing\PriceInfo\Base::class);
105  $priceCode = 'regular_price';
106 
107  $amountRender->expects($this->once())
108  ->method('getSaleableItem')
109  ->will($this->returnValue($sealableItem));
110  $sealableItem->expects($this->once())
111  ->method('getPriceInfo')
112  ->will($this->returnValue($priceInfo));
113  $priceInfo->expects($this->once())
114  ->method('getPrice')
115  ->with($priceCode)
116  ->will($this->returnValue($price));
117 
118  $this->model->expects($this->at(0))
119  ->method('getData')
120  ->will($this->returnValue($this->data));
121  $this->model->render($amountRender);
122  $this->assertEquals($price, $this->model->getPriceType($priceCode));
123  }
124 
125  public function testGetPrice()
126  {
127  $price = 100;
128  $amountRender = $this->createMock(\Magento\Framework\Pricing\Render\Amount::class);
129  $amountRender->expects($this->once())
130  ->method('getPrice')
131  ->with()
132  ->will($this->returnValue($price));
133 
134  $this->model->expects($this->at(0))
135  ->method('getData')
136  ->will($this->returnValue($this->data));
137  $this->model->render($amountRender);
138  $this->assertEquals($price, $this->model->getPrice());
139  }
140 
141  public function testGetSealableItem()
142  {
143  $sealableItem = $this->getMockForAbstractClass(\Magento\Framework\Pricing\SaleableInterface::class);
144  $amountRender = $this->createMock(\Magento\Framework\Pricing\Render\Amount::class);
145  $amountRender->expects($this->once())
146  ->method('getSaleableItem')
147  ->with()
148  ->will($this->returnValue($sealableItem));
149 
150  $this->model->expects($this->at(0))
151  ->method('getData')
152  ->will($this->returnValue($this->data));
153  $this->model->render($amountRender);
154  $this->assertEquals($sealableItem, $this->model->getSaleableItem());
155  }
156 
157  public function testGetAdjustment()
158  {
159  $amountRender = $this->createMock(\Magento\Framework\Pricing\Render\Amount::class);
160  $adjustment = $this->getMockForAbstractClass(\Magento\Framework\Pricing\Adjustment\AdjustmentInterface::class);
161  $sealableItem = $this->getMockForAbstractClass(\Magento\Framework\Pricing\SaleableInterface::class);
162  $priceInfo = $this->createMock(\Magento\Framework\Pricing\PriceInfo\Base::class);
163  $adjustmentCode = 'tax';
164 
165  $amountRender->expects($this->once())
166  ->method('getSaleableItem')
167  ->will($this->returnValue($sealableItem));
168  $sealableItem->expects($this->once())
169  ->method('getPriceInfo')
170  ->will($this->returnValue($priceInfo));
171  $priceInfo->expects($this->once())
172  ->method('getAdjustment')
173  ->with($adjustmentCode)
174  ->will($this->returnValue($adjustment));
175 
176  $this->model->expects($this->at(0))
177  ->method('getData')
178  ->will($this->returnValue($this->data));
179  $this->model->expects($this->once())
180  ->method('getAdjustmentCode')
181  ->will($this->returnValue($adjustmentCode));
182  $this->model->render($amountRender);
183  $this->assertEquals($adjustment, $this->model->getAdjustment());
184  }
185 
186  public function testFormatCurrency()
187  {
188  $amount = 5.3456;
189  $includeContainer = false;
190  $precision = 3;
191 
192  $expected = 5.346;
193 
194  $this->priceCurrency->expects($this->once())
195  ->method('format')
196  ->with($amount, $includeContainer, $precision)
197  ->will($this->returnValue($expected));
198 
199  $result = $this->model->formatCurrency($amount, $includeContainer, $precision);
200  $this->assertEquals($expected, $result, 'formatCurrent returned unexpected result');
201  }
202 }
$objectManager
Definition: bootstrap.php:17
$price
$amount
Definition: order.php:14
$arguments