ItemTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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;
  7. use Magento\Framework\Serialize\Serializer\Json;
  8. use Magento\Sales\Model\ResourceModel\OrderFactory;
  9. use \Magento\Sales\Model\Order;
  10. /**
  11. * Class ItemTest
  12. *
  13. * @package Magento\Sales\Model\Order
  14. */
  15. class ItemTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /**
  18. * @var \Magento\Sales\Model\Order\Item
  19. */
  20. protected $model;
  21. /**
  22. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  23. */
  24. protected $objectManager;
  25. /**
  26. * @var OrderFactory |\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $orderFactory;
  29. /**
  30. * @var Json|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. private $serializerMock;
  33. protected function setUp()
  34. {
  35. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  36. $this->orderFactory = $this->createPartialMock(\Magento\Sales\Model\OrderFactory::class, ['create']);
  37. $this->serializerMock = $this->getMockBuilder(Json::class)
  38. ->setMethods(['unserialize'])
  39. ->getMock();
  40. $arguments = [
  41. 'orderFactory' => $this->orderFactory,
  42. 'serializer' => $this->serializerMock
  43. ];
  44. $this->model = $this->objectManager->getObject(\Magento\Sales\Model\Order\Item::class, $arguments);
  45. }
  46. public function testSetParentItemNull()
  47. {
  48. $this->assertEquals($this->model, $this->model->setParentItem(null));
  49. $this->assertNull($this->model->getParentItem());
  50. }
  51. public function testSetParentItem()
  52. {
  53. $item = $this->objectManager->getObject(\Magento\Sales\Model\Order\Item::class, []);
  54. $this->assertEquals($this->model, $this->model->setParentItem($item));
  55. $this->assertEquals($item, $this->model->getParentItem());
  56. $this->assertTrue($item->getHasChildren());
  57. $this->assertCount(1, $item->getChildrenItems());
  58. }
  59. public function testGetPatentItem()
  60. {
  61. $item = $this->objectManager->getObject(\Magento\Sales\Model\Order\Item::class, []);
  62. $this->model->setData(\Magento\Sales\Api\Data\OrderItemInterface::PARENT_ITEM, $item);
  63. $this->assertEquals($item, $this->model->getParentItem());
  64. }
  65. public function testSetOrder()
  66. {
  67. $orderId = 123;
  68. $order = $this->createMock(\Magento\Sales\Model\Order::class);
  69. $order->expects($this->once())
  70. ->method('getId')
  71. ->willReturn($orderId);
  72. $this->assertEquals($this->model, $this->model->setOrder($order));
  73. $this->assertEquals($orderId, $this->model->getOrderId());
  74. }
  75. public function testGetOrder()
  76. {
  77. //order and order_id was not set
  78. $this->assertNull($this->model->getOrder());
  79. //set order_id and get order by id
  80. $orderId = 123;
  81. $order = $this->createMock(\Magento\Sales\Model\Order::class);
  82. $order->expects($this->once())
  83. ->method('load')
  84. ->with($orderId)
  85. ->willReturnSelf();
  86. $this->orderFactory->expects($this->once())
  87. ->method('create')
  88. ->willReturn($order);
  89. $this->model->setOrderId($orderId);
  90. $this->assertEquals($order, $this->model->getOrder());
  91. //get existed order
  92. $this->assertEquals($order, $this->model->getOrder());
  93. }
  94. /**
  95. * @param $qtyBackOrdered
  96. * @param $hasChildren
  97. * @param $qtyCanceled
  98. * @param $qtyInvoiced
  99. * @param $qtyOrdered
  100. * @param $qtyRefunded
  101. * @param $qtyShipped
  102. * @param $expectedStatus
  103. *
  104. * @dataProvider getStatusIdDataProvider
  105. */
  106. public function testGetStatusId(
  107. $qtyBackOrdered,
  108. $qtyCanceled,
  109. $qtyInvoiced,
  110. $qtyOrdered,
  111. $qtyRefunded,
  112. $qtyShipped,
  113. $expectedStatus
  114. ) {
  115. $this->model->setQtyBackordered($qtyBackOrdered);
  116. $this->model->setQtyCanceled($qtyCanceled);
  117. $this->model->setQtyInvoiced($qtyInvoiced);
  118. $this->model->setQtyOrdered($qtyOrdered);
  119. $this->model->setQtyRefunded($qtyRefunded);
  120. $this->model->setQtyShipped($qtyShipped);
  121. $this->assertEquals($expectedStatus, $this->model->getStatusId());
  122. }
  123. /**
  124. * @return array
  125. */
  126. public function getStatusIdDataProvider()
  127. {
  128. return [
  129. [0, 0, 0, null, 0, 0, \Magento\Sales\Model\Order\Item::STATUS_PENDING],
  130. [0, 10, 1, 100, 10, 80, \Magento\Sales\Model\Order\Item::STATUS_SHIPPED],
  131. [1, 10, 1, 100, 10, 80, \Magento\Sales\Model\Order\Item::STATUS_SHIPPED],
  132. [1, 10, 1, 100, 10, 99, \Magento\Sales\Model\Order\Item::STATUS_MIXED],
  133. [0, 10, 80, 100, 10, 0, \Magento\Sales\Model\Order\Item::STATUS_INVOICED],
  134. [1, 10, 80, 100, 10, 0, \Magento\Sales\Model\Order\Item::STATUS_INVOICED],
  135. [1, 10, 99, 100, 10, 0, \Magento\Sales\Model\Order\Item::STATUS_MIXED],
  136. [80, 10, null, 100, 10, null, \Magento\Sales\Model\Order\Item::STATUS_BACKORDERED],
  137. [null, null, null, 9, 9, null, \Magento\Sales\Model\Order\Item::STATUS_REFUNDED],
  138. [null, 9, null, 9, null, null, \Magento\Sales\Model\Order\Item::STATUS_CANCELED],
  139. [1, 10, 70, 100, 10, 79, \Magento\Sales\Model\Order\Item::STATUS_PARTIAL],
  140. [0, 10, 70, 100, 10, 79, \Magento\Sales\Model\Order\Item::STATUS_PARTIAL]
  141. ];
  142. }
  143. public function testGetStatuses()
  144. {
  145. $statuses = [
  146. \Magento\Sales\Model\Order\Item::STATUS_PENDING => 'Ordered',
  147. \Magento\Sales\Model\Order\Item::STATUS_SHIPPED => 'Shipped',
  148. \Magento\Sales\Model\Order\Item::STATUS_INVOICED => 'Invoiced',
  149. \Magento\Sales\Model\Order\Item::STATUS_BACKORDERED => 'Backordered',
  150. \Magento\Sales\Model\Order\Item::STATUS_RETURNED => 'Returned',
  151. \Magento\Sales\Model\Order\Item::STATUS_REFUNDED => 'Refunded',
  152. \Magento\Sales\Model\Order\Item::STATUS_CANCELED => 'Canceled',
  153. \Magento\Sales\Model\Order\Item::STATUS_PARTIAL => 'Partial',
  154. \Magento\Sales\Model\Order\Item::STATUS_MIXED => 'Mixed',
  155. ];
  156. $this->assertEquals($statuses, $this->model->getStatuses());
  157. }
  158. public function testGetOriginalPrice()
  159. {
  160. $price = 9.99;
  161. $this->model->setPrice($price);
  162. $this->assertEquals($price, $this->model->getOriginalPrice());
  163. $originalPrice = 5.55;
  164. $this->model->setData(\Magento\Sales\Api\Data\OrderItemInterface::ORIGINAL_PRICE, $originalPrice);
  165. $this->assertEquals($originalPrice, $this->model->getOriginalPrice());
  166. }
  167. /**
  168. * Test get product options with serialization
  169. *
  170. * @param array|string $options
  171. * @param array $expectedResult
  172. *
  173. * @dataProvider getProductOptionsDataProvider
  174. */
  175. public function testGetProductOptions($options, $expectedResult)
  176. {
  177. if (is_string($options)) {
  178. $this->serializerMock->expects($this->once())
  179. ->method('unserialize')
  180. ->will($this->returnValue($expectedResult));
  181. }
  182. $this->model->setData('product_options', $options);
  183. $result = $this->model->getProductOptions();
  184. $this->assertSame($result, $expectedResult);
  185. }
  186. /**
  187. * Data provider for testGetProductOptions
  188. *
  189. * @return array
  190. */
  191. public function getProductOptionsDataProvider()
  192. {
  193. return [
  194. 'array' => [
  195. 'options' => [
  196. 'option1' => 'option 1 value',
  197. 'option2' => 'option 2 value',
  198. ],
  199. 'expectedResult' => [
  200. 'option1' => 'option 1 value',
  201. 'option2' => 'option 2 value',
  202. ]
  203. ],
  204. 'serialized' => [
  205. 'options' => json_encode([
  206. 'option1' => 'option 1 value',
  207. 'option2' => 'option 2 value',
  208. ]),
  209. 'expectedResult' => [
  210. 'option1' => 'option 1 value',
  211. 'option2' => 'option 2 value',
  212. ]
  213. ]
  214. ];
  215. }
  216. /**
  217. * Test different combinations of item qty setups
  218. *
  219. * @param array $options
  220. * @param float $expectedResult
  221. *
  222. * @dataProvider getItemQtyVariants
  223. */
  224. public function testGetSimpleQtyToMethods(array $options, $expectedResult)
  225. {
  226. $this->model->setData($options);
  227. $this->assertSame($this->model->getSimpleQtyToShip(), $expectedResult['to_ship']);
  228. $this->assertSame($this->model->getQtyToInvoice(), $expectedResult['to_invoice']);
  229. }
  230. /**
  231. * Provides different combinations of qty options for an item and the
  232. * expected qtys pending shipment and invoice
  233. *
  234. * @return array
  235. */
  236. public function getItemQtyVariants()
  237. {
  238. return [
  239. 'empty_item' => [
  240. 'options' => [
  241. 'qty_ordered' => 0, 'qty_invoiced' => 0, 'qty_refunded' => 0, 'qty_shipped' => 0,
  242. 'qty_canceled' => 0
  243. ],
  244. 'expectedResult' => ['to_ship' => 0.0, 'to_invoice' => 0.0]
  245. ],
  246. 'ordered_item' => [
  247. 'options' => [
  248. 'qty_ordered' => 12, 'qty_invoiced' => 0, 'qty_refunded' => 0, 'qty_shipped' => 0,
  249. 'qty_canceled' => 0
  250. ],
  251. 'expectedResult' => ['to_ship' => 12.0, 'to_invoice' => 12.0]
  252. ],
  253. 'partially_invoiced' => [
  254. 'options' => ['qty_ordered' => 12, 'qty_invoiced' => 4, 'qty_refunded' => 0, 'qty_shipped' => 0,
  255. 'qty_canceled' => 0,
  256. ],
  257. 'expectedResult' => ['to_ship' => 12.0, 'to_invoice' => 8.0]
  258. ],
  259. 'completely_invoiced' => [
  260. 'options' => [
  261. 'qty_ordered' => 12, 'qty_invoiced' => 12, 'qty_refunded' => 0, 'qty_shipped' => 0,
  262. 'qty_canceled' => 0,
  263. ],
  264. 'expectedResult' => ['to_ship' => 12.0, 'to_invoice' => 0.0]
  265. ],
  266. 'partially_invoiced_refunded' => [
  267. 'options' => [
  268. 'qty_ordered' => 12, 'qty_invoiced' => 5, 'qty_refunded' => 5, 'qty_shipped' => 0,
  269. 'qty_canceled' => 0,
  270. ],
  271. 'expectedResult' => ['to_ship' => 7.0, 'to_invoice' => 7.0]
  272. ],
  273. 'partially_refunded' => [
  274. 'options' => [
  275. 'qty_ordered' => 12, 'qty_invoiced' => 12, 'qty_refunded' => 5, 'qty_shipped' => 0,
  276. 'qty_canceled' => 0,
  277. ],
  278. 'expectedResult' => ['to_ship' => 7.0, 'to_invoice' => 0.0]
  279. ],
  280. 'partially_shipped' => [
  281. 'options' => [
  282. 'qty_ordered' => 12, 'qty_invoiced' => 0, 'qty_refunded' => 0, 'qty_shipped' => 4,
  283. 'qty_canceled' => 0
  284. ],
  285. 'expectedResult' => ['to_ship' => 8.0, 'to_invoice' => 12.0]
  286. ],
  287. 'partially_refunded_partially_shipped' => [
  288. 'options' => [
  289. 'qty_ordered' => 12, 'qty_invoiced' => 12, 'qty_refunded' => 5, 'qty_shipped' => 4,
  290. 'qty_canceled' => 0
  291. ],
  292. 'expectedResult' => ['to_ship' => 3.0, 'to_invoice' => 0.0]
  293. ],
  294. 'complete' => [
  295. 'options' => [
  296. 'qty_ordered' => 12, 'qty_invoiced' => 12, 'qty_refunded' => 0, 'qty_shipped' => 12,
  297. 'qty_canceled' => 0
  298. ],
  299. 'expectedResult' => ['to_ship' => 0.0, 'to_invoice' => 0.0]
  300. ],
  301. 'canceled' => [
  302. 'options' => [
  303. 'qty_ordered' => 12, 'qty_invoiced' => 0, 'qty_refunded' => 0, 'qty_shipped' => 0,
  304. 'qty_canceled' => 12
  305. ],
  306. 'expectedResult' => ['to_ship' => 0.0, 'to_invoice' => 0.0]
  307. ],
  308. 'completely_shipped_using_decimals' => [
  309. 'options' => [
  310. 'qty_ordered' => 4.4, 'qty_invoiced' => 0.4, 'qty_refunded' => 0.4, 'qty_shipped' => 4,
  311. 'qty_canceled' => 0,
  312. ],
  313. 'expectedResult' => ['to_ship' => 0.0, 'to_invoice' => 4.0]
  314. ],
  315. 'completely_invoiced_using_decimals' => [
  316. 'options' => [
  317. 'qty_ordered' => 4.4, 'qty_invoiced' => 4, 'qty_refunded' => 0, 'qty_shipped' => 4,
  318. 'qty_canceled' => 0.4
  319. ],
  320. 'expectedResult' => ['to_ship' => 0.0, 'to_invoice' => 0.0]
  321. ]
  322. ];
  323. }
  324. }