Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DataTest.php
Go to the documentation of this file.
1 <?php
7 
8 use Magento\Weee\Helper\Data as WeeeHelper;
9 
14 class DataTest extends \PHPUnit\Framework\TestCase
15 {
16  const ROW_AMOUNT_INVOICED = '200';
17  const BASE_ROW_AMOUNT_INVOICED = '400';
18  const TAX_AMOUNT_INVOICED = '20';
20  const ROW_AMOUNT_REFUNDED = '100';
21  const BASE_ROW_AMOUNT_REFUNDED = '201';
22  const TAX_AMOUNT_REFUNDED = '10';
24 
28  protected $product;
29 
33  protected $weeeTax;
34 
38  protected $taxData;
39 
43  protected $helperData;
44 
46  private $serializerMock;
47 
48  protected function setUp()
49  {
50  $this->product = $this->createMock(\Magento\Catalog\Model\Product::class);
51  $weeeConfig = $this->createMock(\Magento\Weee\Model\Config::class);
52  $weeeConfig->expects($this->any())->method('isEnabled')->will($this->returnValue(true));
53  $weeeConfig->expects($this->any())->method('getListPriceDisplayType')->will($this->returnValue(1));
54  $this->weeeTax = $this->createMock(\Magento\Weee\Model\Tax::class);
55  $this->weeeTax->expects($this->any())->method('getWeeeAmount')->will($this->returnValue('11.26'));
56  $this->taxData = $this->createPartialMock(
57  \Magento\Tax\Helper\Data::class,
58  ['getPriceDisplayType', 'priceIncludesTax']
59  );
60 
61  $this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)->getMock();
62 
63  $arguments = [
64  'weeeConfig' => $weeeConfig,
65  'weeeTax' => $this->weeeTax,
66  'taxData' => $this->taxData,
67  'serializer' => $this->serializerMock
68  ];
69  $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
70  $this->helperData = $helper->getObject(\Magento\Weee\Helper\Data::class, $arguments);
71  }
72 
73  public function testGetAmount()
74  {
75  $this->product->expects($this->any())->method('hasData')->will($this->returnValue(false));
76  $this->product->expects($this->any())->method('getData')->will($this->returnValue(11.26));
77 
78  $this->assertEquals('11.26', $this->helperData->getAmountExclTax($this->product));
79  }
80 
84  private function setupOrderItem()
85  {
86  $orderItem = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
87  ->disableOriginalConstructor()
88  ->setMethods(['__wakeup'])
89  ->getMock();
90 
91  $weeeTaxApplied = [
92  [
93  WeeeHelper::KEY_WEEE_AMOUNT_INVOICED => self::ROW_AMOUNT_INVOICED,
94  WeeeHelper::KEY_BASE_WEEE_AMOUNT_INVOICED => self::BASE_ROW_AMOUNT_INVOICED,
95  WeeeHelper::KEY_WEEE_TAX_AMOUNT_INVOICED => self::TAX_AMOUNT_INVOICED,
96  WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_INVOICED => self::BASE_TAX_AMOUNT_INVOICED,
97  WeeeHelper::KEY_WEEE_AMOUNT_REFUNDED => self::ROW_AMOUNT_REFUNDED,
98  WeeeHelper::KEY_BASE_WEEE_AMOUNT_REFUNDED => self::BASE_ROW_AMOUNT_REFUNDED,
99  WeeeHelper::KEY_WEEE_TAX_AMOUNT_REFUNDED => self::TAX_AMOUNT_REFUNDED,
100  WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_REFUNDED => self::BASE_TAX_AMOUNT_REFUNDED,
101  ],
102  [
103  WeeeHelper::KEY_WEEE_AMOUNT_INVOICED => self::ROW_AMOUNT_INVOICED,
104  WeeeHelper::KEY_BASE_WEEE_AMOUNT_INVOICED => self::BASE_ROW_AMOUNT_INVOICED,
105  WeeeHelper::KEY_WEEE_TAX_AMOUNT_INVOICED => self::TAX_AMOUNT_INVOICED,
106  WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_INVOICED => self::BASE_TAX_AMOUNT_INVOICED,
107  WeeeHelper::KEY_WEEE_AMOUNT_REFUNDED => self::ROW_AMOUNT_REFUNDED,
108  WeeeHelper::KEY_BASE_WEEE_AMOUNT_REFUNDED => self::BASE_ROW_AMOUNT_REFUNDED,
109  WeeeHelper::KEY_WEEE_TAX_AMOUNT_REFUNDED => self::TAX_AMOUNT_REFUNDED,
110  WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_REFUNDED => self::BASE_TAX_AMOUNT_REFUNDED,
111  ],
112  ];
113 
114  $orderItem->setData(
115  'weee_tax_applied',
116  json_encode($weeeTaxApplied)
117  );
118 
119  $this->serializerMock->expects($this->any())
120  ->method('unserialize')
121  ->will($this->returnValue($weeeTaxApplied));
122 
123  return $orderItem;
124  }
125 
126  public function testGetWeeeAmountInvoiced()
127  {
128  $orderItem = $this->setupOrderItem();
129  $value = $this->helperData->getWeeeAmountInvoiced($orderItem);
130  $this->assertEquals(self::ROW_AMOUNT_INVOICED, $value);
131  }
132 
134  {
135  $orderItem = $this->setupOrderItem();
136  $value = $this->helperData->getBaseWeeeAmountInvoiced($orderItem);
137  $this->assertEquals(self::BASE_ROW_AMOUNT_INVOICED, $value);
138  }
139 
141  {
142  $orderItem = $this->setupOrderItem();
143  $value = $this->helperData->getWeeeTaxAmountInvoiced($orderItem);
144  $this->assertEquals(self::TAX_AMOUNT_INVOICED, $value);
145  }
146 
148  {
149  $orderItem = $this->setupOrderItem();
150  $value = $this->helperData->getBaseWeeeTaxAmountInvoiced($orderItem);
151  $this->assertEquals(self::BASE_TAX_AMOUNT_INVOICED, $value);
152  }
153 
154  public function testGetWeeeAmountRefunded()
155  {
156  $orderItem = $this->setupOrderItem();
157  $value = $this->helperData->getWeeeAmountRefunded($orderItem);
158  $this->assertEquals(self::ROW_AMOUNT_REFUNDED, $value);
159  }
160 
162  {
163  $orderItem = $this->setupOrderItem();
164  $value = $this->helperData->getBaseWeeeAmountRefunded($orderItem);
165  $this->assertEquals(self::BASE_ROW_AMOUNT_REFUNDED, $value);
166  }
167 
169  {
170  $orderItem = $this->setupOrderItem();
171  $value = $this->helperData->getWeeeTaxAmountRefunded($orderItem);
172  $this->assertEquals(self::TAX_AMOUNT_REFUNDED, $value);
173  }
174 
176  {
177  $orderItem = $this->setupOrderItem();
178  $value = $this->helperData->getBaseWeeeTaxAmountRefunded($orderItem);
179  $this->assertEquals(self::BASE_TAX_AMOUNT_REFUNDED, $value);
180  }
181 
188  public function testGetWeeeAttributesForBundle($priceDisplay, $priceIncludesTax, $expectedAmount)
189  {
190  $prodId1 = 1;
191  $prodId2 = 2;
192  $fptCode1 = 'fpt' . $prodId1;
193  $fptCode2 = 'fpt' . $prodId2;
194 
195  $weeeObject1 = new \Magento\Framework\DataObject(
196  [
197  'code' => $fptCode1,
198  'amount' => '15',
199  'amount_excl_tax' => '15.0000',
200  'tax_amount' => '1'
201  ]
202  );
203  $weeeObject2 = new \Magento\Framework\DataObject(
204  [
205  'code' => $fptCode2,
206  'amount' => '10',
207  'amount_excl_tax' => '10.0000',
208  'tax_amount' => '5'
209  ]
210  );
211  $expectedObject1 = new \Magento\Framework\DataObject(
212  [
213  'code' => $fptCode1,
214  'amount' => $expectedAmount[0],
215  'amount_excl_tax' => '15.0000',
216  'tax_amount' => '1'
217  ]
218  );
219  $expectedObject2 = new \Magento\Framework\DataObject(
220  [
221  'code' => $fptCode2,
222  'amount' => $expectedAmount[1],
223  'amount_excl_tax' => '10.0000',
224  'tax_amount' => '5'
225  ]
226  );
227 
228  $expectedArray = [$prodId1 => [$fptCode1 => $expectedObject1], $prodId2 => [$fptCode2 => $expectedObject2]];
229  $this->weeeTax->expects($this->any())
230  ->method('getProductWeeeAttributes')
231  ->will($this->returnValue([$weeeObject1, $weeeObject2]));
232  $this->taxData->expects($this->any())
233  ->method('getPriceDisplayType')
234  ->willReturn($priceDisplay);
235  $this->taxData->expects($this->any())
236  ->method('priceIncludesTax')
237  ->willReturn($priceIncludesTax);
238 
239  $productSimple = $this->createPartialMock(\Magento\Catalog\Model\Product\Type\Simple::class, ['getId']);
240  $productSimple->expects($this->at(0))
241  ->method('getId')
242  ->will($this->returnValue($prodId1));
243  $productSimple->expects($this->at(1))
244  ->method('getId')
245  ->will($this->returnValue($prodId2));
246 
247  $productInstance = $this->createMock(\Magento\Bundle\Model\Product\Type::class);
248  $productInstance->expects($this->any())
249  ->method('getSelectionsCollection')
250  ->will($this->returnValue([$productSimple]));
251 
252  $store=$this->createMock(\Magento\Store\Model\Store::class);
254  $product = $this->createPartialMock(
255  \Magento\Catalog\Model\Product::class,
256  ['getTypeInstance', 'getStoreId', 'getStore', 'getTypeId']
257  );
258  $product->expects($this->any())
259  ->method('getTypeInstance')
260  ->will($this->returnValue($productInstance));
261  $product->expects($this->any())
262  ->method('getStoreId')
263  ->will($this->returnValue(1));
264  $product->expects($this->any())
265  ->method('getStore')
266  ->will($this->returnValue($store));
267  $product->expects($this->any())
268  ->method('getTypeId')
269  ->will($this->returnValue('bundle'));
270 
271  $registry=$this->createMock(\Magento\Framework\Registry::class);
272  $registry->expects($this->any())
273  ->method('registry')
274  ->with('current_product')
275  ->will($this->returnValue($product));
276 
277  $result = $this->helperData->getWeeeAttributesForBundle($product);
278  $this->assertEquals($expectedArray, $result);
279  }
280 
285  {
286  return [
287  [2, false, ["16.00", "15.00"]],
288  [2, true, ["15.00", "10.00"]],
289  [1, false, ["15.00", "10.00"]],
290  [1, true, ["15.00", "10.00"]],
291  [3, false, ["16.00", "15.00"]],
292  [3, true, ["15.00", "10.00"]],
293  ];
294  }
295 
296  public function testGetAppliedSimple()
297  {
298  $testArray = ['key' => 'value'];
299  $itemProductSimple = $this->createPartialMock(
300  \Magento\Quote\Model\Quote\Item::class,
301  ['getWeeeTaxApplied', 'getHasChildren']
302  );
303  $itemProductSimple->expects($this->any())
304  ->method('getHasChildren')
305  ->will($this->returnValue(false));
306 
307  $itemProductSimple->expects($this->any())
308  ->method('getWeeeTaxApplied')
309  ->will($this->returnValue(json_encode($testArray)));
310 
311  $this->serializerMock->expects($this->any())
312  ->method('unserialize')
313  ->will($this->returnValue($testArray));
314 
315  $this->assertEquals($testArray, $this->helperData->getApplied($itemProductSimple));
316  }
317 
318  public function testGetAppliedBundle()
319  {
320  $testArray1 = ['key1' => 'value1'];
321  $testArray2 = ['key2' => 'value2'];
322 
323  $testArray = array_merge($testArray1, $testArray2);
324 
325  $itemProductSimple1=$this->createPartialMock(\Magento\Quote\Model\Quote\Item::class, ['getWeeeTaxApplied']);
326  $itemProductSimple2=$this->createPartialMock(\Magento\Quote\Model\Quote\Item::class, ['getWeeeTaxApplied']);
327 
328  $itemProductSimple1->expects($this->any())
329  ->method('getWeeeTaxApplied')
330  ->will($this->returnValue(json_encode($testArray1)));
331 
332  $itemProductSimple2->expects($this->any())
333  ->method('getWeeeTaxApplied')
334  ->will($this->returnValue(json_encode($testArray2)));
335 
336  $itemProductBundle = $this->createPartialMock(
337  \Magento\Quote\Model\Quote\Item::class,
338  ['getHasChildren', 'isChildrenCalculated', 'getChildren']
339  );
340  $itemProductBundle->expects($this->any())
341  ->method('getHasChildren')
342  ->will($this->returnValue(true));
343  $itemProductBundle->expects($this->any())
344  ->method('isChildrenCalculated')
345  ->will($this->returnValue(true));
346  $itemProductBundle->expects($this->any())
347  ->method('getChildren')
348  ->will($this->returnValue([$itemProductSimple1, $itemProductSimple2]));
349 
350  $this->serializerMock->expects($this->any())
351  ->method('unserialize')
352  ->will($this->returnValue($testArray));
353 
354  $this->assertEquals($testArray, $this->helperData->getApplied($itemProductBundle));
355  }
356 
358  {
359  $testAmountUnit = 2;
360  $testAmountRow = 34;
361 
362  $itemProductSimple = $this->createPartialMock(
363  \Magento\Quote\Model\Quote\Item::class,
364  ['getHasChildren', 'getWeeeTaxAppliedAmount', 'getWeeeTaxAppliedRowAmount']
365  );
366  $itemProductSimple->expects($this->any())
367  ->method('getHasChildren')
368  ->will($this->returnValue(false));
369 
370  $itemProductSimple->expects($this->any())
371  ->method('getWeeeTaxAppliedAmount')
372  ->will($this->returnValue($testAmountUnit));
373  $itemProductSimple->expects($this->any())
374  ->method('getWeeeTaxAppliedRowAmount')
375  ->will($this->returnValue($testAmountRow));
376 
377  $this->assertEquals($testAmountUnit, $this->helperData->getWeeeTaxAppliedAmount($itemProductSimple));
378  $this->assertEquals($testAmountRow, $this->helperData->getWeeeTaxAppliedRowAmount($itemProductSimple));
379  }
380 
382  {
383  $testAmountUnit1 = 1;
384  $testAmountUnit2 = 2;
385  $testTotalUnit = $testAmountUnit1 + $testAmountUnit2;
386 
387  $testAmountRow1 = 33;
388  $testAmountRow2 = 444;
389  $testTotalRow = $testAmountRow1 + $testAmountRow2;
390 
391  $itemProductSimple1 = $this->createPartialMock(
392  \Magento\Quote\Model\Quote\Item::class,
393  ['getWeeeTaxAppliedAmount', 'getWeeeTaxAppliedRowAmount']
394  );
395  $itemProductSimple2 = $this->createPartialMock(
396  \Magento\Quote\Model\Quote\Item::class,
397  ['getWeeeTaxAppliedAmount', 'getWeeeTaxAppliedRowAmount']
398  );
399 
400  $itemProductSimple1->expects($this->any())
401  ->method('getWeeeTaxAppliedAmount')
402  ->will($this->returnValue($testAmountUnit1));
403  $itemProductSimple1->expects($this->any())
404  ->method('getWeeeTaxAppliedRowAmount')
405  ->will($this->returnValue($testAmountRow1));
406 
407  $itemProductSimple2->expects($this->any())
408  ->method('getWeeeTaxAppliedAmount')
409  ->will($this->returnValue($testAmountUnit2));
410  $itemProductSimple2->expects($this->any())
411  ->method('getWeeeTaxAppliedRowAmount')
412  ->will($this->returnValue($testAmountRow2));
413 
414  $itemProductBundle = $this->createPartialMock(
415  \Magento\Quote\Model\Quote\Item::class,
416  ['getHasChildren', 'isChildrenCalculated', 'getChildren']
417  );
418  $itemProductBundle->expects($this->any())
419  ->method('getHasChildren')
420  ->will($this->returnValue(true));
421  $itemProductBundle->expects($this->any())
422  ->method('isChildrenCalculated')
423  ->will($this->returnValue(true));
424  $itemProductBundle->expects($this->any())
425  ->method('getChildren')
426  ->will($this->returnValue([$itemProductSimple1, $itemProductSimple2]));
427 
428  $this->assertEquals($testTotalUnit, $this->helperData->getWeeeTaxAppliedAmount($itemProductBundle));
429  $this->assertEquals($testTotalRow, $this->helperData->getWeeeTaxAppliedRowAmount($itemProductBundle));
430  }
431 
433  {
434  $store = $this->createMock(\Magento\Store\Model\Store::class);
435  $this->product->expects($this->any())
436  ->method('getStore')
437  ->will($this->returnValue($store));
438 
439  $result = $this->helperData->getProductWeeeAttributesForDisplay($this->product);
440  $this->assertNull($result);
441  }
442 
443  public function testGetTaxDisplayConfig()
444  {
445  $expected = 1;
446  $taxData = $this->createPartialMock(\Magento\Tax\Helper\Data::class, ['getPriceDisplayType']);
447  $taxData->expects($this->any())->method('getPriceDisplayType')->will($this->returnValue($expected));
448  $arguments = [
449  'taxData' => $taxData,
450  ];
451  $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
452  $helperData = $helper->getObject(\Magento\Weee\Helper\Data::class, $arguments);
453 
454  $this->assertEquals($expected, $helperData->getTaxDisplayConfig());
455  }
456 
457  public function testGetTotalAmounts()
458  {
459  $item1Weee = 5;
460  $item2Weee = 7;
461  $expected = $item1Weee + $item2Weee;
462  $itemProductSimple1 = $this->createPartialMock(
463  \Magento\Quote\Model\Quote\Item::class,
464  ['getWeeeTaxAppliedRowAmount']
465  );
466  $itemProductSimple2 = $this->createPartialMock(
467  \Magento\Quote\Model\Quote\Item::class,
468  ['getWeeeTaxAppliedRowAmount']
469  );
470  $items = [$itemProductSimple1, $itemProductSimple2];
471 
472  $itemProductSimple1->expects($this->any())
473  ->method('getWeeeTaxAppliedRowAmount')
474  ->willReturn($item1Weee);
475  $itemProductSimple2->expects($this->any())
476  ->method('getWeeeTaxAppliedRowAmount')
477  ->willReturn($item2Weee);
478 
479  $this->assertEquals($expected, $this->helperData->getTotalAmounts($items));
480  }
481 
482  public function testGetBaseTotalAmounts()
483  {
484  $item1BaseWeee = 4;
485  $item2BaseWeee = 3;
486  $expected = $item1BaseWeee + $item2BaseWeee;
487  $itemProductSimple1 = $this->createPartialMock(
488  \Magento\Quote\Model\Quote\Item::class,
489  ['getBaseWeeeTaxAppliedRowAmnt']
490  );
491  $itemProductSimple2 = $this->createPartialMock(
492  \Magento\Quote\Model\Quote\Item::class,
493  ['getBaseWeeeTaxAppliedRowAmnt']
494  );
495  $items = [$itemProductSimple1, $itemProductSimple2];
496 
497  $itemProductSimple1->expects($this->any())
498  ->method('getBaseWeeeTaxAppliedRowAmnt')
499  ->willReturn($item1BaseWeee);
500  $itemProductSimple2->expects($this->any())
501  ->method('getBaseWeeeTaxAppliedRowAmnt')
502  ->willReturn($item2BaseWeee);
503 
504  $this->assertEquals($expected, $this->helperData->getBaseTotalAmounts($items));
505  }
506 }
$helper
Definition: iframe.phtml:13
$orderItem
Definition: order.php:30
$priceDisplay
$value
Definition: gender.phtml:16
$arguments
$items