OrderInvoiceCreateTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Service\V1;
  7. /**
  8. * API test for creation of Invoice for certain Order.
  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. protected function setUp()
  23. {
  24. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  25. $this->invoiceRepository = $this->objectManager->get(
  26. \Magento\Sales\Api\InvoiceRepositoryInterface::class
  27. );
  28. }
  29. /**
  30. * @magentoApiDataFixture Magento/Sales/_files/order_new.php
  31. */
  32. public function testInvoiceCreate()
  33. {
  34. /** @var \Magento\Sales\Model\Order $existingOrder */
  35. $existingOrder = $this->objectManager->create(\Magento\Sales\Model\Order::class)
  36. ->loadByIncrementId('100000001');
  37. $serviceInfo = [
  38. 'rest' => [
  39. 'resourcePath' => '/V1/order/' . $existingOrder->getId() . '/invoice',
  40. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
  41. ],
  42. 'soap' => [
  43. 'service' => self::SERVICE_READ_NAME,
  44. 'serviceVersion' => self::SERVICE_VERSION,
  45. 'operation' => self::SERVICE_READ_NAME . 'execute',
  46. ],
  47. ];
  48. $requestData = [
  49. 'orderId' => $existingOrder->getId(),
  50. 'items' => [],
  51. 'comment' => [
  52. 'comment' => 'Test Comment',
  53. 'is_visible_on_front' => 1,
  54. ],
  55. ];
  56. /** @var \Magento\Sales\Api\Data\OrderItemInterface $item */
  57. foreach ($existingOrder->getAllItems() as $item) {
  58. $requestData['items'][] = [
  59. 'order_item_id' => $item->getItemId(),
  60. 'qty' => $item->getQtyOrdered(),
  61. ];
  62. }
  63. $result = $this->_webApiCall($serviceInfo, $requestData);
  64. $this->assertNotEmpty($result);
  65. try {
  66. $this->invoiceRepository->get($result);
  67. } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
  68. $this->fail('Failed asserting that Invoice was created');
  69. }
  70. /** @var \Magento\Sales\Model\Order $updatedOrder */
  71. $updatedOrder = $this->objectManager->create(\Magento\Sales\Model\Order::class)
  72. ->loadByIncrementId('100000001');
  73. $this->assertNotEquals(
  74. $existingOrder->getStatus(),
  75. $updatedOrder->getStatus(),
  76. 'Failed asserting that Order status was changed'
  77. );
  78. }
  79. /**
  80. * Tests that MAGETWO-95346 was fixed for bundled products
  81. *
  82. * @expectedException \Exception
  83. * @codingStandardsIgnoreStart
  84. * @expectedExceptionMessageRegExp /Invoice Document Validation Error\(s\):(?:\n|\\n)The invoice can't be created without products. Add products and try again./
  85. * @codingStandardsIgnoreEnd
  86. * @magentoApiDataFixture Magento/Sales/_files/order_with_bundle.php
  87. */
  88. public function testOrderWithBundleInvoicedWithInvalidQuantitiesReturnsError()
  89. {
  90. /** @var \Magento\Sales\Model\Order $existingOrder */
  91. $existingOrder = $this->objectManager->create(\Magento\Sales\Model\Order::class)
  92. ->loadByIncrementId('100000001');
  93. $serviceInfo = [
  94. 'rest' => [
  95. 'resourcePath' => '/V1/order/' . $existingOrder->getId() . '/invoice',
  96. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
  97. ],
  98. 'soap' => [
  99. 'service' => self::SERVICE_READ_NAME,
  100. 'serviceVersion' => self::SERVICE_VERSION,
  101. 'operation' => self::SERVICE_READ_NAME . 'execute',
  102. ],
  103. ];
  104. $requestData = [
  105. 'orderId' => $existingOrder->getId(),
  106. 'notify' => true,
  107. 'appendComment' => true,
  108. 'items' => [
  109. [
  110. 'order_item_id' => -1,
  111. 'qty' => 1
  112. ]
  113. ],
  114. 'comment' => [
  115. 'comment' => 'Test offline',
  116. 'isVisibleOnFront' => 1,
  117. ],
  118. ];
  119. $this->_webApiCall($serviceInfo, $requestData);
  120. }
  121. /**
  122. * Tests that MAGETWO-95346 was fixed for configurable products
  123. *
  124. * @expectedException \Exception
  125. * @codingStandardsIgnoreStart
  126. * @expectedExceptionMessageRegExp /Invoice Document Validation Error\(s\):(?:\n|\\n)The invoice can't be created without products. Add products and try again./
  127. * @codingStandardsIgnoreEnd
  128. * @magentoApiDataFixture Magento/Sales/_files/order_configurable_product.php
  129. */
  130. public function testOrderWithConfigurableProductInvoicedWithInvalidQuantitiesReturnsError()
  131. {
  132. /** @var \Magento\Sales\Model\Order $existingOrder */
  133. $existingOrder = $this->objectManager->create(\Magento\Sales\Model\Order::class)
  134. ->loadByIncrementId('100000001');
  135. $serviceInfo = [
  136. 'rest' => [
  137. 'resourcePath' => '/V1/order/' . $existingOrder->getId() . '/invoice',
  138. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
  139. ],
  140. 'soap' => [
  141. 'service' => self::SERVICE_READ_NAME,
  142. 'serviceVersion' => self::SERVICE_VERSION,
  143. 'operation' => self::SERVICE_READ_NAME . 'execute',
  144. ],
  145. ];
  146. $requestData = [
  147. 'orderId' => $existingOrder->getId(),
  148. 'notify' => true,
  149. 'appendComment' => true,
  150. 'items' => [
  151. [
  152. 'order_item_id' => -1,
  153. 'qty' => 1
  154. ]
  155. ],
  156. 'comment' => [
  157. 'comment' => 'Test offline',
  158. 'isVisibleOnFront' => 1,
  159. ],
  160. ];
  161. $this->_webApiCall($serviceInfo, $requestData);
  162. }
  163. }