Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AmountTest.php
Go to the documentation of this file.
1 <?php
7 
12 
18 class AmountTest extends \PHPUnit\Framework\TestCase
19 {
23  protected $model;
24 
28  protected $priceCurrency;
29 
33  protected $rendererPool;
34 
38  protected $layout;
39 
43  protected $saleableItemMock;
44 
48  protected $amount;
49 
53  protected $priceMock;
54 
55  protected function setUp()
56  {
57  $this->priceCurrency = $this->createMock(\Magento\Framework\Pricing\PriceCurrencyInterface::class);
58  $data = [
59  'default' => [
60  'adjustments' => [
61  'base_price_test' => [
62  'tax' => [
63  'adjustment_render_class' => \Magento\Framework\View\Element\Template::class,
64  'adjustment_render_template' => 'template.phtml',
65  ],
66  ],
67  ],
68  ],
69  ];
70 
71  $this->rendererPool = $this->getMockBuilder(\Magento\Framework\Pricing\Render\RendererPool::class)
72  ->setConstructorArgs(['data' => $data])
73  ->disableOriginalConstructor()
74  ->getMock();
75 
76  $this->layout = $this->createMock(\Magento\Framework\View\Layout::class);
77  $this->amount = $this->getMockForAbstractClass(\Magento\Framework\Pricing\Amount\AmountInterface::class);
78  $this->saleableItemMock = $this->getMockForAbstractClass(\Magento\Framework\Pricing\SaleableInterface::class);
79  $this->priceMock = $this->getMockForAbstractClass(\Magento\Framework\Pricing\Price\PriceInterface::class);
80 
81  $eventManager = $this->createMock(\Magento\Framework\Event\Test\Unit\ManagerStub::class);
82  $scopeConfigMock = $this->getMockForAbstractClass(\Magento\Framework\App\Config\ScopeConfigInterface::class);
83  $context = $this->createMock(\Magento\Framework\View\Element\Template\Context::class);
84  $context->expects($this->any())
85  ->method('getEventManager')
86  ->will($this->returnValue($eventManager));
87  $context->expects($this->any())
88  ->method('getLayout')
89  ->will($this->returnValue($this->layout));
90  $context->expects($this->any())
91  ->method('getScopeConfig')
92  ->will($this->returnValue($scopeConfigMock));
93 
94  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
95  $this->model = $objectManager->getObject(
96  \Magento\Framework\Pricing\Render\Amount::class,
97  [
98  'context' => $context,
99  'priceCurrency' => $this->priceCurrency,
100  'rendererPool' => $this->rendererPool,
101  'amount' => $this->amount,
102  'saleableItem' => $this->saleableItemMock,
103  'price' => $this->priceMock
104  ]
105  );
106  }
107 
108  public function testFormatCurrency()
109  {
110  $amount = '100';
111  $includeContainer = true;
113 
114  $result = '100.0 grn';
115 
116  $this->priceCurrency->expects($this->once())
117  ->method('format')
118  ->with($amount, $includeContainer, $precision)
119  ->will($this->returnValue($result));
120 
121  $this->assertEquals($result, $this->model->formatCurrency($amount, $includeContainer, $precision));
122  }
123 
125  {
126  $currencySymbol = '$';
127  $this->priceCurrency->expects($this->once())
128  ->method('getCurrencySymbol')
129  ->willReturn($currencySymbol);
130  $this->assertEquals($currencySymbol, $this->model->getDisplayCurrencySymbol());
131  }
132 
141  public function testToHtmlSkipAdjustments($hasSkipAdjustments, $skipAdjustments, $expected)
142  {
143  if ($hasSkipAdjustments) {
144  $this->model->setData('skip_adjustments', $skipAdjustments);
145  $expectedData = [
146  'key1' => 'data1',
147  'css_classes' => 'class1 class2',
148  'module_name' => null,
149  'adjustment_css_classes' => 'class1 class2 render1 render2',
150  'skip_adjustments' => $skipAdjustments
151  ];
152  } else {
153  $expectedData = [
154  'key1' => 'data1',
155  'css_classes' => 'class1 class2',
156  'module_name' => null,
157  'adjustment_css_classes' => 'class1 class2 render1 render2',
158  ];
159  }
160 
161  $this->model->setData('key1', 'data1');
162  $this->model->setData('css_classes', 'class1 class2');
163 
164  $adjustmentRender1 = $this->getAdjustmentRenderMock($expectedData, 'html');
165  $adjustmentRender2 = $this->getAdjustmentRenderMock($expectedData);
166  $adjustmentRenders = ['render1' => $adjustmentRender1, 'render2' => $adjustmentRender2];
167  $this->rendererPool->expects($this->once())
168  ->method('getAdjustmentRenders')
169  ->will($this->returnValue($adjustmentRenders));
170 
171  $this->model->toHtml();
172  $this->assertEquals($expected, $this->model->getAdjustmentsHtml());
173  }
174 
179  {
180  return [
181  [false, null, 'html'],
182  [false, null, 'html'],
183  [true, false, 'html'],
184  [true, true, ''],
185  ];
186  }
187 
192  {
193  $data = ['key1' => 'data1', 'css_classes' => 'class1 class2'];
194  $expectedData = [
195  'key1' => 'data1',
196  'css_classes' => 'class1 class2',
197  'module_name' => null,
198  'adjustment_css_classes' => 'class1 class2 render1 render2',
199  ];
200 
201  $this->model->setData($data);
202 
203  $adjustmentRender1 = $this->getAdjustmentRenderMock($expectedData);
204  $adjustmentRender2 = $this->getAdjustmentRenderMock($expectedData);
205  $adjustmentRenders = ['render1' => $adjustmentRender1, 'render2' => $adjustmentRender2];
206  $this->rendererPool->expects($this->once())
207  ->method('getAdjustmentRenders')
208  ->will($this->returnValue($adjustmentRenders));
209  $this->amount->expects($this->atLeastOnce())
210  ->method('getAdjustmentAmount')
211  ->willReturn(true);
212 
213  $this->model->toHtml();
214  }
215 
216  public function testGetDisplayValueExiting()
217  {
218  $displayValue = 5.99;
219  $this->model->setDisplayValue($displayValue);
220  $this->assertEquals($displayValue, $this->model->getDisplayValue());
221  }
222 
223  public function testGetDisplayValue()
224  {
225  $amountValue = 100.99;
226  $this->amount->expects($this->once())
227  ->method('getValue')
228  ->will($this->returnValue($amountValue));
229  $this->assertEquals($amountValue, $this->model->getDisplayValue());
230  }
231 
232  public function testGetAmount()
233  {
234  $this->assertEquals($this->amount, $this->model->getAmount());
235  }
236 
237  public function testGetSealableItem()
238  {
239  $this->assertEquals($this->saleableItemMock, $this->model->getSaleableItem());
240  }
241 
242  public function testGetPrice()
243  {
244  $this->assertEquals($this->priceMock, $this->model->getPrice());
245  }
246 
247  public function testAdjustmentsHtml()
248  {
249  $adjustmentHtml1 = 'adjustment_1_html';
250  $adjustmentHtml2 = 'adjustment_2_html';
251  $data = ['key1' => 'data1', 'css_classes' => 'class1 class2'];
252  $expectedData = [
253  'key1' => 'data1',
254  'css_classes' => 'class1 class2',
255  'module_name' => null,
256  'adjustment_css_classes' => 'class1 class2 render1 render2',
257  ];
258 
259  $this->model->setData($data);
260 
261  $this->assertFalse($this->model->hasAdjustmentsHtml());
262 
263  $adjustmentRender1 = $this->getAdjustmentRenderMock($expectedData, $adjustmentHtml1, 'adjustment_code1');
264  $adjustmentRender2 = $this->getAdjustmentRenderMock($expectedData, $adjustmentHtml2, 'adjustment_code2');
265  $adjustmentRenders = ['render1' => $adjustmentRender1, 'render2' => $adjustmentRender2];
266  $this->rendererPool->expects($this->once())
267  ->method('getAdjustmentRenders')
268  ->will($this->returnValue($adjustmentRenders));
269  $this->amount->expects($this->atLeastOnce())
270  ->method('getAdjustmentAmount')
271  ->willReturn(true);
272 
273  $this->model->toHtml();
274 
275  $this->assertTrue($this->model->hasAdjustmentsHtml());
276 
277  $this->assertEquals($adjustmentHtml1 . $adjustmentHtml2, $this->model->getAdjustmentsHtml());
278  }
279 
286  protected function getAdjustmentRenderMock($data = [], $html = '', $code = 'adjustment_code')
287  {
288  $adjustmentRender = $this->getMockForAbstractClass(
289  \Magento\Framework\Pricing\Render\AdjustmentRenderInterface::class
290  );
291  $adjustmentRender->expects($this->once())
292  ->method('render')
293  ->with($this->model, $data)
294  ->will($this->returnValue($html));
295  $adjustmentRender->expects($this->any())
296  ->method('getAdjustmentCode')
297  ->will($this->returnValue($code));
298  return $adjustmentRender;
299  }
300 }
if( $block->displayPriceExclTax()||$block->displayBothPrices())(__('Excl. Tax')) ?>"> <?php if ($block -> displayPriceWithWeeeDetails()): ?> <span class="cart-tax-total" data-mage-init=' Magento Weee Helper Data Magento Weee Helper Data title amount
Definition: unit.phtml:68
$objectManager
Definition: bootstrap.php:17
$currencySymbol
Definition: matrix.phtml:14
getAdjustmentRenderMock($data=[], $html='', $code='adjustment_code')
Definition: AmountTest.php:286
testToHtmlSkipAdjustments($hasSkipAdjustments, $skipAdjustments, $expected)
Definition: AmountTest.php:141
$code
Definition: info.phtml:12