OrderInvoiceCreateTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Bundle\Api;
  7. /**
  8. * API test for creation of Invoice for order with bundle product.
  9. */
  10. class OrderInvoiceCreateTest extends \Magento\TestFramework\TestCase\WebapiAbstract
  11. {
  12. const SERVICE_READ_NAME = 'salesInvoiceOrderV1';
  13. const SERVICE_VERSION = 'V1';
  14. /**
  15. * @var \Magento\Framework\ObjectManagerInterface
  16. */
  17. private $objectManager;
  18. /**
  19. * @var \Magento\Sales\Api\InvoiceRepositoryInterface
  20. */
  21. private $invoiceRepository;
  22. /**
  23. * Set up.
  24. *
  25. * @return void
  26. */
  27. protected function setUp()
  28. {
  29. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  30. $this->invoiceRepository = $this->objectManager->get(
  31. \Magento\Sales\Api\InvoiceRepositoryInterface::class
  32. );
  33. }
  34. /**
  35. * Test create a partial invoice for order with bundle and Simple products.
  36. *
  37. * @return void
  38. * @magentoApiDataFixture Magento/Bundle/_files/order_items_simple_and_bundle.php
  39. */
  40. public function testInvoiceWithSimpleAndBundleCreate()
  41. {
  42. /** @var \Magento\Sales\Api\Data\OrderInterface $existingOrder*/
  43. $existingOrder = $this->objectManager->create(\Magento\Sales\Api\Data\OrderInterface::class)
  44. ->loadByIncrementId('100000001');
  45. $serviceInfo = [
  46. 'rest' => [
  47. 'resourcePath' => '/V1/order/' . $existingOrder->getId() . '/invoice',
  48. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
  49. ],
  50. 'soap' => [
  51. 'service' => self::SERVICE_READ_NAME,
  52. 'serviceVersion' => self::SERVICE_VERSION,
  53. 'operation' => self::SERVICE_READ_NAME . 'execute',
  54. ],
  55. ];
  56. $requestData = [
  57. 'orderId' => $existingOrder->getId(),
  58. 'items' => [],
  59. 'comment' => [
  60. 'comment' => 'Test Comment',
  61. 'is_visible_on_front' => 1,
  62. ],
  63. ];
  64. $grantTotal = 0;
  65. foreach ($existingOrder->getAllItems() as $item) {
  66. $requestData['items'] = [];
  67. $requestData['items'][] = [
  68. 'order_item_id' => $item->getItemId(),
  69. 'qty' => $item->getQtyOrdered(),
  70. ];
  71. $result = $this->_webApiCall($serviceInfo, $requestData);
  72. $this->assertNotEmpty($result);
  73. try {
  74. $invoice = $this->invoiceRepository->get($result);
  75. $grantTotal += $invoice->getGrandTotal();
  76. } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
  77. $this->fail('Failed asserting that Invoice was created');
  78. }
  79. }
  80. $this->assertNotEquals(
  81. $existingOrder->getGrandTotal(),
  82. $grantTotal,
  83. 'Failed asserting that invoice is correct.'
  84. );
  85. }
  86. /**
  87. * Test create invoice with Bundle product.
  88. *
  89. * @return void
  90. * @magentoApiDataFixture Magento/Bundle/_files/order_item_with_bundle_and_options.php
  91. */
  92. public function testInvoiceWithBundleCreate()
  93. {
  94. /** @var \Magento\Sales\Api\Data\OrderInterface $existingOrder*/
  95. $existingOrder = $this->objectManager->create(\Magento\Sales\Api\Data\OrderInterface::class)
  96. ->loadByIncrementId('100000001');
  97. $serviceInfo = [
  98. 'rest' => [
  99. 'resourcePath' => '/V1/order/' . $existingOrder->getId() . '/invoice',
  100. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
  101. ],
  102. 'soap' => [
  103. 'service' => self::SERVICE_READ_NAME,
  104. 'serviceVersion' => self::SERVICE_VERSION,
  105. 'operation' => self::SERVICE_READ_NAME . 'execute',
  106. ],
  107. ];
  108. $requestData = [
  109. 'orderId' => $existingOrder->getId(),
  110. 'items' => [],
  111. 'comment' => [
  112. 'comment' => 'Test Comment',
  113. 'is_visible_on_front' => 1,
  114. ],
  115. ];
  116. /** @var \Magento\Sales\Api\Data\OrderItemInterface $item */
  117. foreach ($existingOrder->getAllItems() as $item) {
  118. $requestData['items'][] = [
  119. 'order_item_id' => $item->getItemId(),
  120. 'qty' => $item->getQtyOrdered(),
  121. ];
  122. }
  123. $result = $this->_webApiCall($serviceInfo, $requestData);
  124. $this->assertNotEmpty($result);
  125. $invoice = $this->invoiceRepository->get($result);
  126. $this->assertNotEquals(
  127. $existingOrder->getGrandTotal(),
  128. $invoice->getGrandTotal(),
  129. 'Failed asserting that invoice is correct.'
  130. );
  131. }
  132. }