ItemTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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\Creditmemo;
  7. use Magento\Sales\Api\Data\CreditmemoItemInterface;
  8. class ItemTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \PHPUnit_Framework_MockObject_MockObject
  12. */
  13. protected $orderItemFactoryMock;
  14. /** @var \Magento\Sales\Model\Order\Creditmemo\Item */
  15. protected $item;
  16. protected function setUp()
  17. {
  18. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  19. $this->orderItemFactoryMock = $this->getMockBuilder(\Magento\Sales\Model\Order\ItemFactory::class)
  20. ->disableOriginalConstructor()
  21. ->setMethods(['create'])
  22. ->getMock();
  23. $this->item = $objectManager->getObject(
  24. \Magento\Sales\Model\Order\Creditmemo\Item::class,
  25. [
  26. 'orderItemFactory' => $this->orderItemFactoryMock
  27. ]
  28. );
  29. }
  30. public function testGetOrderItemExist()
  31. {
  32. $orderItemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
  33. ->disableOriginalConstructor()
  34. ->getMock();
  35. $this->item->setOrderItem($orderItemMock);
  36. $result = $this->item->getOrderItem();
  37. $this->assertInstanceOf(\Magento\Sales\Model\Order\Item::class, $result);
  38. }
  39. public function testGetOrderItemFromCreditmemo()
  40. {
  41. $orderItemId = 1;
  42. $orderItemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
  43. ->disableOriginalConstructor()
  44. ->getMock();
  45. $orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
  46. ->disableOriginalConstructor()
  47. ->getMock();
  48. $orderMock->expects($this->once())
  49. ->method('getItemById')
  50. ->with($orderItemId)
  51. ->willReturn($orderItemMock);
  52. $creditmemoMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Creditmemo::class)
  53. ->disableOriginalConstructor()
  54. ->getMock();
  55. $creditmemoMock->expects($this->once())
  56. ->method('getOrder')
  57. ->willReturn($orderMock);
  58. $this->item->setData(CreditmemoItemInterface::ORDER_ITEM_ID, $orderItemId);
  59. $this->item->setCreditmemo($creditmemoMock);
  60. $result = $this->item->getOrderItem();
  61. $this->assertInstanceOf(\Magento\Sales\Model\Order\Item::class, $result);
  62. }
  63. public function testGetOrderItemFromFactory()
  64. {
  65. $orderItemId = 1;
  66. $orderItemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
  67. ->disableOriginalConstructor()
  68. ->getMock();
  69. $orderItemMock->expects($this->once())
  70. ->method('load')
  71. ->with($orderItemId)
  72. ->willReturnSelf();
  73. $this->orderItemFactoryMock->expects($this->once())
  74. ->method('create')
  75. ->willReturn($orderItemMock);
  76. $this->item->setData(CreditmemoItemInterface::ORDER_ITEM_ID, $orderItemId);
  77. $result = $this->item->getOrderItem();
  78. $this->assertInstanceOf(\Magento\Sales\Model\Order\Item::class, $result);
  79. }
  80. public function testSetQty()
  81. {
  82. $qty = 10;
  83. $this->item->setQty($qty);
  84. $this->assertEquals($qty, $this->item->getQty());
  85. }
  86. public function testRegister()
  87. {
  88. $orderItemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
  89. ->disableOriginalConstructor()
  90. ->getMock();
  91. $orderItemMock->expects($this->once())
  92. ->method('getQtyRefunded')
  93. ->willReturn(1);
  94. $orderItemMock->expects($this->once())
  95. ->method('getTaxRefunded')
  96. ->willReturn(1);
  97. $orderItemMock->expects($this->once())
  98. ->method('getBaseTaxRefunded')
  99. ->willReturn(1);
  100. $orderItemMock->expects($this->once())
  101. ->method('getDiscountTaxCompensationRefunded')
  102. ->willReturn(1);
  103. $orderItemMock->expects($this->once())
  104. ->method('getBaseDiscountTaxCompensationRefunded')
  105. ->willReturn(1);
  106. $orderItemMock->expects($this->once())
  107. ->method('getAmountRefunded')
  108. ->willReturn(1);
  109. $orderItemMock->expects($this->once())
  110. ->method('getBaseAmountRefunded')
  111. ->willReturn(1);
  112. $orderItemMock->expects($this->once())
  113. ->method('getDiscountRefunded')
  114. ->willReturn(1);
  115. $orderItemMock->expects($this->once())
  116. ->method('getBaseDiscountRefunded')
  117. ->willReturn(1);
  118. $orderItemMock->expects($this->once())
  119. ->method('getQtyToRefund')
  120. ->willReturn(1);
  121. $this->item->setQty(1);
  122. $this->item->setTaxAmount(1);
  123. $this->item->setBaseTaxAmount(1);
  124. $this->item->setDiscountTaxCompensationAmount(1);
  125. $this->item->setBaseDiscountTaxCompensationAmount(1);
  126. $this->item->setRowTotal(1);
  127. $this->item->setBaseRowTotal(1);
  128. $this->item->setDiscountAmount(1);
  129. $this->item->setBaseDiscountAmount(1);
  130. $this->item->setOrderItem($orderItemMock);
  131. $result = $this->item->register();
  132. $this->assertInstanceOf(\Magento\Sales\Model\Order\Creditmemo\Item::class, $result);
  133. }
  134. /**
  135. * @expectedException \Magento\Framework\Exception\LocalizedException
  136. * @expectedExceptionMessage We found an invalid quantity to refund item "test".
  137. */
  138. public function testRegisterWithException()
  139. {
  140. $orderItemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
  141. ->disableOriginalConstructor()
  142. ->setMethods(['getQtyRefunded'])
  143. ->getMock();
  144. $orderItemMock->expects($this->once())
  145. ->method('getQtyRefunded')
  146. ->willReturn(1);
  147. $this->item->setQty(2);
  148. $this->item->setOrderItem($orderItemMock);
  149. $this->item->setName('test');
  150. $result = $this->item->register();
  151. $this->assertInstanceOf(\Magento\Sales\Model\Order\Creditmemo\Item::class, $result);
  152. }
  153. public function testCancel()
  154. {
  155. $orderItemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
  156. ->disableOriginalConstructor()
  157. ->getMock();
  158. $orderItemMock->expects($this->once())
  159. ->method('getQtyRefunded')
  160. ->willReturn(1);
  161. $orderItemMock->expects($this->once())
  162. ->method('setQtyRefunded')
  163. ->with(0);
  164. $orderItemMock->expects($this->once())
  165. ->method('getTaxRefunded')
  166. ->willReturn(10);
  167. $orderItemMock->expects($this->once())
  168. ->method('getBaseTaxAmount')
  169. ->willReturn(5);
  170. $orderItemMock->expects($this->exactly(2))
  171. ->method('getQtyOrdered')
  172. ->willReturn(1);
  173. $orderItemMock->expects($this->once())
  174. ->method('setTaxRefunded')
  175. ->with(5);
  176. $orderItemMock->expects($this->once())
  177. ->method('setDiscountTaxCompensationRefunded')
  178. ->with(0);
  179. $orderItemMock->expects($this->once())
  180. ->method('getDiscountTaxCompensationRefunded')
  181. ->willReturn(10);
  182. $orderItemMock->expects($this->once())
  183. ->method('getDiscountTaxCompensationAmount')
  184. ->willReturn(10);
  185. $orderItemMock->expects($this->once())
  186. ->method('getQtyToRefund')
  187. ->willReturn(1);
  188. $this->item->setQty(1);
  189. $this->item->setOrderItem($orderItemMock);
  190. $result = $this->item->cancel();
  191. $this->assertInstanceOf(\Magento\Sales\Model\Order\Creditmemo\Item::class, $result);
  192. }
  193. /**
  194. * @dataProvider calcRowTotalDataProvider
  195. */
  196. public function testCalcRowTotal($qty)
  197. {
  198. $creditmemoMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Creditmemo::class)
  199. ->disableOriginalConstructor()
  200. ->getMock();
  201. $creditmemoMock->expects($this->exactly(4))
  202. ->method('roundPrice')
  203. ->will($this->returnCallback(
  204. function ($arg) {
  205. return round($arg, 2);
  206. }
  207. ));
  208. $qtyInvoiced = 10;
  209. $qtyRefunded = 2;
  210. $qtyAvailable = $qtyInvoiced - $qtyRefunded;
  211. $rowInvoiced = 5;
  212. $amountRefunded = 2;
  213. $expectedRowTotal = ($rowInvoiced - $amountRefunded) / $qtyAvailable * $qty;
  214. $expectedRowTotal = round($expectedRowTotal, 2);
  215. $orderItemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
  216. ->disableOriginalConstructor()
  217. ->getMock();
  218. $orderItemMock->expects($this->once())
  219. ->method('getQtyInvoiced')
  220. ->willReturn($qtyInvoiced);
  221. $orderItemMock->expects($this->once())
  222. ->method('getQtyRefunded')
  223. ->willReturn($qtyRefunded);
  224. $orderItemMock->expects($this->once())
  225. ->method('getRowInvoiced')
  226. ->willReturn($rowInvoiced);
  227. $orderItemMock->expects($this->once())
  228. ->method('getAmountRefunded')
  229. ->willReturn($amountRefunded);
  230. $orderItemMock->expects($this->once())
  231. ->method('getBaseRowInvoiced')
  232. ->willReturn($rowInvoiced);
  233. $orderItemMock->expects($this->once())
  234. ->method('getBaseAmountRefunded')
  235. ->willReturn($amountRefunded);
  236. $orderItemMock->expects($this->once())
  237. ->method('getRowTotalInclTax')
  238. ->willReturn(1);
  239. $orderItemMock->expects($this->once())
  240. ->method('getBaseRowTotalInclTax')
  241. ->willReturn(1);
  242. $orderItemMock->expects($this->once())
  243. ->method('getQtyRefunded')
  244. ->willReturn(1);
  245. $orderItemMock->expects($this->once())
  246. ->method('getQtyOrdered')
  247. ->willReturn(1);
  248. $orderItemMock->expects($this->any())
  249. ->method('getQtyToRefund')
  250. ->willReturn($qtyAvailable);
  251. $this->item->setQty($qty);
  252. $this->item->setCreditmemo($creditmemoMock);
  253. $this->item->setOrderItem($orderItemMock);
  254. $result = $this->item->calcRowTotal();
  255. $this->assertInstanceOf(\Magento\Sales\Model\Order\Creditmemo\Item::class, $result);
  256. $this->assertEquals($expectedRowTotal, $this->item->getData('row_total'));
  257. $this->assertEquals($expectedRowTotal, $this->item->getData('base_row_total'));
  258. }
  259. /**
  260. * @return array
  261. */
  262. public function calcRowTotalDataProvider()
  263. {
  264. return [
  265. 'qty 1' => [1],
  266. 'qty 0' => [0],
  267. ];
  268. }
  269. }