OrderSaveTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Test\Unit\Model\Plugin;
  7. use \Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. class OrderSaveTest extends \PHPUnit\Framework\TestCase
  9. {
  10. const ORDERID = 123;
  11. const ITEMID = 151;
  12. const ORDER_ITEM_ID = 116;
  13. /**
  14. * @var \Magento\Tax\Model\Sales\Order\TaxFactory|\PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $orderTaxFactoryMock;
  17. /**
  18. * @var \Magento\Sales\Model\Order\Tax\ItemFactory|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $taxItemFactoryMock;
  21. /**
  22. * @var \Magento\Sales\Api\OrderRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $subjectMock;
  25. /**
  26. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  27. */
  28. protected $objectManagerHelper;
  29. /**
  30. * @var \Magento\Tax\Model\Plugin\OrderSave
  31. */
  32. protected $model;
  33. protected function setUp()
  34. {
  35. $this->orderTaxFactoryMock = $this->getMockBuilder(
  36. \Magento\Tax\Model\Sales\Order\TaxFactory::class
  37. )->disableOriginalConstructor()
  38. ->setMethods(['create'])
  39. ->getMock();
  40. $this->taxItemFactoryMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Tax\ItemFactory::class)
  41. ->disableOriginalConstructor()
  42. ->setMethods(['create'])
  43. ->getMock();
  44. $this->subjectMock = $this->getMockForAbstractClass(\Magento\Sales\Api\OrderRepositoryInterface::class);
  45. $this->objectManagerHelper = new ObjectManager($this);
  46. $this->model = $this->objectManagerHelper->getObject(
  47. \Magento\Tax\Model\Plugin\OrderSave::class,
  48. [
  49. 'orderTaxFactory' => $this->orderTaxFactoryMock,
  50. 'taxItemFactory' => $this->taxItemFactoryMock,
  51. ]
  52. );
  53. }
  54. /**
  55. * @return \PHPUnit_Framework_MockObject_MockObject
  56. */
  57. protected function setupOrderMock()
  58. {
  59. $orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
  60. ->disableOriginalConstructor()
  61. ->setMethods(
  62. [
  63. 'getExtensionAttributes',
  64. 'getAppliedTaxIsSaved',
  65. 'getItemByQuoteItemId',
  66. 'setAppliedTaxIsSaved',
  67. 'getEntityId',
  68. ]
  69. )->getMock();
  70. return $orderMock;
  71. }
  72. /**
  73. * @return \PHPUnit_Framework_MockObject_MockObject
  74. */
  75. protected function setupExtensionAttributeMock()
  76. {
  77. $orderExtensionAttributeMock = $this->getMockBuilder(\Magento\Sales\Api\Data\OrderExtensionInterface::class)
  78. ->disableOriginalConstructor()
  79. ->setMethods(
  80. [
  81. 'getAppliedTaxes',
  82. 'getConvertingFromQuote',
  83. 'getItemAppliedTaxes',
  84. ]
  85. )->getMockForAbstractClass();
  86. return $orderExtensionAttributeMock;
  87. }
  88. /**
  89. * @param $expectedTaxes
  90. */
  91. protected function verifyOrderTaxes($expectedTaxes)
  92. {
  93. $index = 0;
  94. foreach ($expectedTaxes as $orderTaxId => $orderTaxData) {
  95. $orderTaxMock = $this->getMockBuilder(\Magento\Tax\Model\Sales\Order\Tax::class)
  96. ->disableOriginalConstructor()
  97. ->setMethods(
  98. [
  99. 'getTaxId',
  100. 'setData',
  101. 'save',
  102. ]
  103. )->getMock();
  104. $orderTaxMock->expects($this->once())
  105. ->method('setData')
  106. ->with($orderTaxData)
  107. ->willReturnSelf();
  108. $orderTaxMock->expects($this->once())
  109. ->method('save')
  110. ->willReturnSelf();
  111. $orderTaxMock->expects($this->atLeastOnce())
  112. ->method('getTaxId')
  113. ->willReturn($orderTaxId);
  114. $this->orderTaxFactoryMock->expects($this->at($index))
  115. ->method('create')
  116. ->willReturn($orderTaxMock);
  117. $index++;
  118. }
  119. }
  120. /**
  121. * @param $expectedItemTaxes
  122. */
  123. public function verifyItemTaxes($expectedItemTaxes)
  124. {
  125. $index = 0;
  126. foreach ($expectedItemTaxes as $itemTax) {
  127. $itemTaxMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Tax\Item::class)
  128. ->disableOriginalConstructor()
  129. ->setMethods(
  130. [
  131. 'setData',
  132. 'save',
  133. ]
  134. )->getMock();
  135. $itemTaxMock->expects($this->once())
  136. ->method('setData')
  137. ->with($itemTax)
  138. ->willReturnSelf();
  139. $itemTaxMock->expects($this->once())
  140. ->method('save')
  141. ->willReturnSelf();
  142. $this->taxItemFactoryMock->expects($this->at($index))
  143. ->method('create')
  144. ->willReturn($itemTaxMock);
  145. $index++;
  146. }
  147. }
  148. /**
  149. * @dataProvider afterSaveDataProvider
  150. */
  151. public function testAfterSave(
  152. $appliedTaxes,
  153. $itemAppliedTaxes,
  154. $expectedTaxes,
  155. $expectedItemTaxes
  156. ) {
  157. $orderMock = $this->setupOrderMock();
  158. $extensionAttributeMock = $this->setupExtensionAttributeMock();
  159. $extensionAttributeMock->expects($this->any())
  160. ->method('getConvertingFromQuote')
  161. ->willReturn(true);
  162. $extensionAttributeMock->expects($this->any())
  163. ->method('getAppliedTaxes')
  164. ->willReturn($appliedTaxes);
  165. $extensionAttributeMock->expects($this->any())
  166. ->method('getItemAppliedTaxes')
  167. ->willReturn($itemAppliedTaxes);
  168. $orderItemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
  169. ->disableOriginalConstructor()
  170. ->setMethods(['getId'])
  171. ->getMock();
  172. $orderItemMock->expects($this->atLeastOnce())
  173. ->method('getId')
  174. ->willReturn(self::ORDER_ITEM_ID);
  175. $orderMock->expects($this->once())
  176. ->method('getAppliedTaxIsSaved')
  177. ->willReturn(false);
  178. $orderMock->expects($this->once())
  179. ->method('getExtensionAttributes')
  180. ->willReturn($extensionAttributeMock);
  181. $orderMock->expects($this->atLeastOnce())
  182. ->method('getItemByQuoteItemId')
  183. ->with(self::ITEMID)
  184. ->willReturn($orderItemMock);
  185. $orderMock->expects($this->atLeastOnce())
  186. ->method('getEntityId')
  187. ->willReturn(self::ORDERID);
  188. $orderMock->expects($this->once())
  189. ->method('setAppliedTaxIsSaved')
  190. ->with(true);
  191. $this->verifyOrderTaxes($expectedTaxes);
  192. $this->verifyItemTaxes($expectedItemTaxes);
  193. $this->assertEquals($orderMock, $this->model->afterSave($this->subjectMock, $orderMock));
  194. }
  195. /**
  196. * @return array
  197. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  198. */
  199. public function afterSaveDataProvider()
  200. {
  201. return [
  202. //one item with shipping
  203. //three tax rates: state and national tax rates of 6 and 5 percent with priority 0
  204. //city tax rate of 3 percent with priority 1
  205. 'item_with_shipping_three_tax' => [
  206. 'applied_taxes' => [
  207. [
  208. 'amount' => 0.66,
  209. 'base_amount' => 0.66,
  210. 'percent' => 11,
  211. 'id' => 'ILUS',
  212. 'extension_attributes' => [
  213. 'rates' => [
  214. [
  215. 'percent' => 6,
  216. 'code' => 'IL',
  217. 'title' => 'IL',
  218. ],
  219. [
  220. 'percent' => 5,
  221. 'code' => 'US',
  222. 'title' => 'US',
  223. ],
  224. ]
  225. ],
  226. ],
  227. [
  228. 'amount' => 0.2,
  229. 'base_amount' => 0.2,
  230. 'percent' => 3.33,
  231. 'id' => 'CityTax',
  232. 'extension_attributes' => [
  233. 'rates' => [
  234. [
  235. 'percent' => 3,
  236. 'code' => 'CityTax',
  237. 'title' => 'CityTax',
  238. ],
  239. ]
  240. ],
  241. ],
  242. ],
  243. 'item_applied_taxes' => [
  244. //item tax, three tax rates
  245. [
  246. //first two taxes are combined
  247. 'item_id' => self::ITEMID,
  248. 'type' => 'product',
  249. 'associated_item_id' => null,
  250. 'applied_taxes' => [
  251. [
  252. 'amount' => 0.11,
  253. 'base_amount' => 0.11,
  254. 'percent' => 11,
  255. 'id' => 'ILUS',
  256. 'extension_attributes' => [
  257. 'rates' => [
  258. [
  259. 'percent' => 6,
  260. 'code' => 'IL',
  261. 'title' => 'IL',
  262. ],
  263. [
  264. 'percent' => 5,
  265. 'code' => 'US',
  266. 'title' => 'US',
  267. ],
  268. ]
  269. ],
  270. ],
  271. //city tax
  272. [
  273. 'amount' => 0.03,
  274. 'base_amount' => 0.03,
  275. 'percent' => 3.33,
  276. 'id' => 'CityTax',
  277. 'extension_attributes' => [
  278. 'rates' => [
  279. [
  280. 'percent' => 3,
  281. 'code' => 'CityTax',
  282. 'title' => 'CityTax',
  283. ],
  284. ]
  285. ],
  286. ],
  287. ],
  288. ],
  289. //shipping tax
  290. [
  291. //first two taxes are combined
  292. 'item_id' => null,
  293. 'type' => 'shipping',
  294. 'associated_item_id' => null,
  295. 'applied_taxes' => [
  296. [
  297. 'amount' => 0.55,
  298. 'base_amount' => 0.55,
  299. 'percent' => 11,
  300. 'id' => 'ILUS',
  301. 'extension_attributes' => [
  302. 'rates' => [
  303. [
  304. 'percent' => 6,
  305. 'code' => 'IL',
  306. 'title' => 'IL',
  307. ],
  308. [
  309. 'percent' => 5,
  310. 'code' => 'US',
  311. 'title' => 'US',
  312. ],
  313. ]
  314. ],
  315. ],
  316. //city tax
  317. [
  318. 'amount' => 0.17,
  319. 'base_amount' => 0.17,
  320. 'percent' => 3.33,
  321. 'id' => 'CityTax',
  322. 'extension_attributes' => [
  323. 'rates' => [
  324. [
  325. 'percent' => 3,
  326. 'code' => 'CityTax',
  327. 'title' => 'CityTax',
  328. ],
  329. ]
  330. ],
  331. ],
  332. ],
  333. ],
  334. ],
  335. 'expected_order_taxes' => [
  336. //state tax
  337. '35' => [
  338. 'order_id' => self::ORDERID,
  339. 'code' => 'IL',
  340. 'title' => 'IL',
  341. 'hidden' => 0,
  342. 'percent' => 6,
  343. 'priority' => 0,
  344. 'position' => 0,
  345. 'amount' => 0.66,
  346. 'base_amount' => 0.66,
  347. 'process' => 0,
  348. 'base_real_amount' => 0.36,
  349. ],
  350. //federal tax
  351. '36' => [
  352. 'order_id' => self::ORDERID,
  353. 'code' => 'US',
  354. 'title' => 'US',
  355. 'hidden' => 0,
  356. 'percent' => 5,
  357. 'priority' => 0,
  358. 'position' => 0,
  359. 'amount' => 0.66, //combined amount
  360. 'base_amount' => 0.66,
  361. 'process' => 0,
  362. 'base_real_amount' => 0.3, //portion for specific rate
  363. ],
  364. //city tax
  365. '37' => [
  366. 'order_id' => self::ORDERID,
  367. 'code' => 'CityTax',
  368. 'title' => 'CityTax',
  369. 'hidden' => 0,
  370. 'percent' => 3,
  371. 'priority' => 0,
  372. 'position' => 0,
  373. 'amount' => 0.2, //combined amount
  374. 'base_amount' => 0.2,
  375. 'process' => 0,
  376. 'base_real_amount' => 0.18018018018018, //this number is meaningless since this is single rate
  377. ],
  378. ],
  379. 'expected_item_taxes' => [
  380. [
  381. //state tax for item
  382. 'item_id' => self::ORDER_ITEM_ID,
  383. 'tax_id' => '35',
  384. 'tax_percent' => 6,
  385. 'associated_item_id' => null,
  386. 'amount' => 0.11,
  387. 'base_amount' => 0.11,
  388. 'real_amount' => 0.06,
  389. 'real_base_amount' => 0.06,
  390. 'taxable_item_type' => 'product',
  391. ],
  392. [
  393. //state tax for shipping
  394. 'item_id' => null,
  395. 'tax_id' => '35',
  396. 'tax_percent' => 6,
  397. 'associated_item_id' => null,
  398. 'amount' => 0.55,
  399. 'base_amount' => 0.55,
  400. 'real_amount' => 0.3,
  401. 'real_base_amount' => 0.3,
  402. 'taxable_item_type' => 'shipping',
  403. ],
  404. [
  405. //federal tax for item
  406. 'item_id' => self::ORDER_ITEM_ID,
  407. 'tax_id' => '36',
  408. 'tax_percent' => 5,
  409. 'associated_item_id' => null,
  410. 'amount' => 0.11,
  411. 'base_amount' => 0.11,
  412. 'real_amount' => 0.05,
  413. 'real_base_amount' => 0.05,
  414. 'taxable_item_type' => 'product',
  415. ],
  416. [
  417. //federal tax for shipping
  418. 'item_id' => null,
  419. 'tax_id' => '36',
  420. 'tax_percent' => 5,
  421. 'associated_item_id' => null,
  422. 'amount' => 0.55,
  423. 'base_amount' => 0.55,
  424. 'real_amount' => 0.25,
  425. 'real_base_amount' => 0.25,
  426. 'taxable_item_type' => 'shipping',
  427. ],
  428. [
  429. //city tax for item
  430. 'item_id' => self::ORDER_ITEM_ID,
  431. 'tax_id' => '37',
  432. 'tax_percent' => 3.33,
  433. 'associated_item_id' => null,
  434. 'amount' => 0.03,
  435. 'base_amount' => 0.03,
  436. 'real_amount' => 0.03,
  437. 'real_base_amount' => 0.03,
  438. 'taxable_item_type' => 'product',
  439. ],
  440. [
  441. //city tax for shipping
  442. 'item_id' => null,
  443. 'tax_id' => '37',
  444. 'tax_percent' => 3.33,
  445. 'associated_item_id' => null,
  446. 'amount' => 0.17,
  447. 'base_amount' => 0.17,
  448. 'real_amount' => 0.17,
  449. 'real_base_amount' => 0.17,
  450. 'taxable_item_type' => 'shipping',
  451. ],
  452. ],
  453. ],
  454. ];
  455. }
  456. }