ItemTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Test\Unit\Model\Order\Invoice;
  7. class ItemTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  11. */
  12. protected $objectManager;
  13. /**
  14. * @var \Magento\Sales\Model\Order\Invoice\Item|\PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $item;
  17. /**
  18. * @var \Magento\Sales\Model\Order\ItemFactory|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $orderItemFactoryMock;
  21. /**
  22. * @var \Magento\Sales\Model\Order\Invoice|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $invoiceMock;
  25. /**
  26. * @var \Magento\Sales\Model\Order|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $orderMock;
  29. /**
  30. * @var \Magento\Sales\Model\Order\Item|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $orderItemMock;
  33. protected function setUp()
  34. {
  35. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  36. $this->orderItemFactoryMock = $this->createPartialMock(
  37. \Magento\Sales\Model\Order\ItemFactory::class,
  38. ['create']
  39. );
  40. $this->invoiceMock = $this->createMock(\Magento\Sales\Model\Order\Invoice::class);
  41. $this->orderMock = $this->createMock(\Magento\Sales\Model\Order::class);
  42. $this->orderItemMock = $this->createPartialMock(\Magento\Sales\Model\Order\Item::class, [
  43. 'load', 'isDummy', 'getIsQtyDecimal', 'getQtyToInvoice', 'getQtyInvoiced', 'getTaxInvoiced',
  44. 'getBaseTaxInvoiced', 'getDiscountTaxCompensationInvoiced',
  45. 'getBaseDiscountTaxCompensationInvoiced', 'getDiscountInvoiced',
  46. 'getBaseDiscountInvoiced', 'getRowInvoiced', 'getBaseRowInvoiced', 'setQtyInvoiced', 'setTaxInvoiced',
  47. 'setBaseTaxInvoiced', 'setDiscountTaxCompensationInvoiced',
  48. 'setBaseDiscountTaxCompensationInvoiced', 'setDiscountInvoiced',
  49. 'setBaseDiscountInvoiced', 'setRowInvoiced', 'setBaseRowInvoiced', 'getQtyOrdered', 'getRowTotal',
  50. 'getBaseRowTotal', 'getRowTotalInclTax', 'getBaseRowTotalInclTax'
  51. ]);
  52. $this->item = $this->objectManager->getObject(
  53. \Magento\Sales\Model\Order\Invoice\Item::class,
  54. [
  55. 'orderItemFactory' => $this->orderItemFactoryMock
  56. ]
  57. );
  58. }
  59. public function testGetOrderItemFromOrder()
  60. {
  61. $this->invoiceMock->expects($this->once())->method('getOrder')->willReturn($this->orderMock);
  62. $this->orderMock->expects($this->once())->method('getItemById')->with(1)->willReturn($this->orderItemMock);
  63. $this->item->setInvoice($this->invoiceMock);
  64. $this->item->setOrderItemId(1);
  65. $this->assertEquals($this->orderItemMock, $this->item->getOrderItem());
  66. }
  67. public function testGetOrderItemFromFactory()
  68. {
  69. $this->orderItemFactoryMock->expects($this->once())->method('create')->willReturn($this->orderItemMock);
  70. $this->orderItemMock->expects($this->once())->method('load')->with(1)->willReturn($this->orderItemMock);
  71. $this->item->setOrderItemId(1);
  72. $this->assertEquals($this->orderItemMock, $this->item->getOrderItem());
  73. }
  74. public function testSetQty()
  75. {
  76. $qty = 3;
  77. $this->assertEquals($this->item->setQty($qty), $this->item);
  78. $this->assertEquals($this->item->getQty(), $qty);
  79. }
  80. public function testRegister()
  81. {
  82. $this->orderItemFactoryMock->expects($this->once())->method('create')->willReturn($this->orderItemMock);
  83. $this->orderItemMock->expects($this->once())->method('load')->with(1)->willReturnSelf();
  84. $this->orderItemMock->expects($this->once())->method('getQtyInvoiced')->willReturn(1);
  85. $this->orderItemMock->expects($this->once())->method('getTaxInvoiced')->willReturn(1);
  86. $this->orderItemMock->expects($this->once())->method('getBaseTaxInvoiced')->willReturn(1);
  87. $this->orderItemMock->expects($this->once())->method('getDiscountTaxCompensationInvoiced')->willReturn(1);
  88. $this->orderItemMock->expects($this->once())->method('getBaseDiscountTaxCompensationInvoiced')->willReturn(1);
  89. $this->orderItemMock->expects($this->once())->method('getDiscountInvoiced')->willReturn(1);
  90. $this->orderItemMock->expects($this->once())->method('getBaseDiscountInvoiced')->willReturn(1);
  91. $this->orderItemMock->expects($this->once())->method('getRowInvoiced')->willReturn(1);
  92. $this->orderItemMock->expects($this->once())->method('getBaseRowInvoiced')->willReturn(1);
  93. $this->orderItemMock->expects($this->once())->method('setQtyInvoiced')->with(2)->willReturnSelf();
  94. $this->orderItemMock->expects($this->once())->method('setTaxInvoiced')->with(2)->willReturnSelf();
  95. $this->orderItemMock->expects($this->once())->method('setBaseTaxInvoiced')->with(2)->willReturnSelf();
  96. $this->orderItemMock->expects($this->once())
  97. ->method('setDiscountTaxCompensationInvoiced')->with(2)->willReturnSelf();
  98. $this->orderItemMock->expects($this->once())
  99. ->method('setBaseDiscountTaxCompensationInvoiced')->with(2)->willReturnSelf();
  100. $this->orderItemMock->expects($this->once())->method('setDiscountInvoiced')->with(2)->willReturnSelf();
  101. $this->orderItemMock->expects($this->once())->method('setBaseDiscountInvoiced')->with(2)->willReturnSelf();
  102. $this->orderItemMock->expects($this->once())->method('setRowInvoiced')->with(2)->willReturnSelf();
  103. $this->orderItemMock->expects($this->once())->method('setBaseRowInvoiced')->with(2)->willReturnSelf();
  104. $this->item->setData(
  105. [
  106. 'order_item_id' => 1,
  107. 'qty' => 1,
  108. 'tax_amount' => 1,
  109. 'base_tax_amount' => 1,
  110. 'discount_tax_compensation_amount' => 1,
  111. 'base_discount_tax_compensation_amount' => 1,
  112. 'discount_amount' => 1,
  113. 'base_discount_amount' => 1,
  114. 'row_total' => 1,
  115. 'base_row_total' => 1
  116. ]
  117. );
  118. $this->assertEquals($this->item->register(), $this->item);
  119. }
  120. public function testCancel()
  121. {
  122. $this->orderItemFactoryMock->expects($this->once())->method('create')->willReturn($this->orderItemMock);
  123. $this->orderItemMock->expects($this->once())->method('load')->with(1)->willReturnSelf();
  124. $this->orderItemMock->expects($this->once())->method('getQtyInvoiced')->willReturn(1);
  125. $this->orderItemMock->expects($this->once())->method('getTaxInvoiced')->willReturn(1);
  126. $this->orderItemMock->expects($this->once())->method('getBaseTaxInvoiced')->willReturn(1);
  127. $this->orderItemMock->expects($this->once())->method('getDiscountTaxCompensationInvoiced')->willReturn(1);
  128. $this->orderItemMock->expects($this->once())->method('getBaseDiscountTaxCompensationInvoiced')->willReturn(1);
  129. $this->orderItemMock->expects($this->once())->method('getDiscountInvoiced')->willReturn(1);
  130. $this->orderItemMock->expects($this->once())->method('getBaseDiscountInvoiced')->willReturn(1);
  131. $this->orderItemMock->expects($this->once())->method('getRowInvoiced')->willReturn(1);
  132. $this->orderItemMock->expects($this->once())->method('getBaseRowInvoiced')->willReturn(1);
  133. $this->orderItemMock->expects($this->once())->method('setQtyInvoiced')->with(0)->willReturnSelf();
  134. $this->orderItemMock->expects($this->once())->method('setTaxInvoiced')->with(0)->willReturnSelf();
  135. $this->orderItemMock->expects($this->once())->method('setBaseTaxInvoiced')->with(0)->willReturnSelf();
  136. $this->orderItemMock->expects($this->once())->method('setDiscountTaxCompensationInvoiced')
  137. ->with(0)->willReturnSelf();
  138. $this->orderItemMock->expects($this->once())->method('setBaseDiscountTaxCompensationInvoiced')
  139. ->with(0)->willReturnSelf();
  140. $this->orderItemMock->expects($this->once())->method('setDiscountInvoiced')->with(0)->willReturnSelf();
  141. $this->orderItemMock->expects($this->once())->method('setBaseDiscountInvoiced')->with(0)->willReturnSelf();
  142. $this->orderItemMock->expects($this->once())->method('setRowInvoiced')->with(0)->willReturnSelf();
  143. $this->orderItemMock->expects($this->once())->method('setBaseRowInvoiced')->with(0)->willReturnSelf();
  144. $this->item->setData(
  145. [
  146. 'order_item_id' => 1,
  147. 'qty' => 1,
  148. 'tax_amount' => 1,
  149. 'base_tax_amount' => 1,
  150. 'discount_tax_compensation_amount' => 1,
  151. 'base_discount_tax_compensation_amount' => 1,
  152. 'discount_amount' => 1,
  153. 'base_discount_amount' => 1,
  154. 'row_total' => 1,
  155. 'base_row_total' => 1
  156. ]
  157. );
  158. $this->assertEquals($this->item->cancel(), $this->item);
  159. }
  160. public function testCalcRowTotal()
  161. {
  162. $this->item->setData('order_item_id', 1);
  163. $this->item->setData('qty', 2);
  164. $this->item->setInvoice($this->invoiceMock);
  165. $this->invoiceMock->expects($this->once())->method('getOrder')->willReturn($this->orderMock);
  166. $this->orderMock->expects($this->once())->method('getItemById')->with(1)->willReturn($this->orderItemMock);
  167. $this->orderItemMock->expects($this->once())->method('getQtyOrdered')->willReturn(1);
  168. $this->orderItemMock->expects($this->once())->method('getRowTotal')->willReturn(2);
  169. $this->orderItemMock->expects($this->once())->method('getRowInvoiced')->willReturn(1);
  170. $this->orderItemMock->expects($this->once())->method('getBaseRowTotal')->willReturn(2);
  171. $this->orderItemMock->expects($this->once())->method('getBaseRowInvoiced')->willReturn(1);
  172. $this->orderItemMock->expects($this->once())->method('getRowTotalInclTax')->willReturn(1);
  173. $this->orderItemMock->expects($this->once())->method('getBaseRowTotalInclTax')->willReturn(1);
  174. $this->orderItemMock->expects($this->once())->method('getQtyToInvoice')->willReturn(1);
  175. $this->orderItemMock->expects($this->once())->method('getQtyInvoiced')->willReturn(0);
  176. $this->invoiceMock->expects($this->exactly(4))
  177. ->method('roundPrice')
  178. ->willReturnMap([
  179. [2, 'regular', false, 2],
  180. [2, 'base', false, 2],
  181. [2, 'including', false, 2],
  182. [2, 'including_base', false, 2],
  183. ]);
  184. $this->assertEquals($this->item->calcRowTotal(), $this->item);
  185. }
  186. }