ShipOrderTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\InventoryShipping\Test\Api;
  8. use Magento\Framework\Api\SearchCriteriaBuilder;
  9. use Magento\Framework\Exception\NoSuchEntityException;
  10. use Magento\Framework\ObjectManagerInterface;
  11. use Magento\Framework\Webapi\Rest\Request;
  12. use Magento\Sales\Api\Data\OrderInterface;
  13. use Magento\Sales\Api\OrderRepositoryInterface;
  14. use Magento\Sales\Api\ShipmentRepositoryInterface;
  15. use Magento\TestFramework\Helper\Bootstrap;
  16. use Magento\TestFramework\TestCase\WebapiAbstract;
  17. class ShipOrderTest extends WebapiAbstract
  18. {
  19. const SERVICE_READ_NAME = 'salesShipOrderV1';
  20. const SERVICE_VERSION = 'V1';
  21. /**
  22. * @var ObjectManagerInterface
  23. */
  24. private $objectManager;
  25. /**
  26. * @var OrderRepositoryInterface
  27. */
  28. private $orderRepository;
  29. /**
  30. * @var SearchCriteriaBuilder
  31. */
  32. private $searchCriteriaBuilder;
  33. /**
  34. * @var ShipmentRepositoryInterface
  35. */
  36. private $shipmentRepository;
  37. /**
  38. * @inheritdoc
  39. */
  40. protected function setUp()
  41. {
  42. $this->objectManager = Bootstrap::getObjectManager();
  43. $this->orderRepository = $this->objectManager->get(OrderRepositoryInterface::class);
  44. $this->searchCriteriaBuilder = $this->objectManager->get(SearchCriteriaBuilder::class);
  45. $this->shipmentRepository = $this->objectManager->get(ShipmentRepositoryInterface::class);
  46. }
  47. /**
  48. * @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
  49. * @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
  50. * @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
  51. * @magentoApiDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/websites_with_stores.php
  52. * @magentoApiDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/stock_website_sales_channels.php
  53. * @magentoApiDataFixture Magento/Checkout/_files/simple_product.php
  54. * @magentoApiDataFixture ../../../../app/code/Magento/InventoryShipping/Test/_files/source_items_for_simple_on_multi_source.php
  55. * @magentoApiDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
  56. * @magentoApiDataFixture ../../../../app/code/Magento/InventoryShipping/Test/_files/create_quote_on_eu_website.php
  57. * @magentoApiDataFixture ../../../../app/code/Magento/InventoryShipping/Test/_files/order_simple_product.php
  58. */
  59. public function testShipOrderWithSimpleProduct()
  60. {
  61. $searchCriteria = $this->searchCriteriaBuilder
  62. ->addFilter('increment_id', 'created_order_for_test')
  63. ->create();
  64. /** @var OrderInterface $order */
  65. $createdOrder = current($this->orderRepository->getList($searchCriteria)->getItems());
  66. $requestData = [
  67. 'arguments' => [
  68. 'extension_attributes' => [
  69. 'source_code' => 'eu-2'
  70. ]
  71. ],
  72. 'orderId' => $createdOrder->getId(),
  73. ];
  74. $result = $this->_webApiCall(
  75. $this->buildServiceInfo((int)$createdOrder->getEntityId()),
  76. $requestData
  77. );
  78. $this->assertNotEmpty($result);
  79. try {
  80. $shipping = $this->shipmentRepository->get($result);
  81. $this->assertNotNull($shipping->getEntityId());
  82. $this->assertEquals('3.0000', $shipping->getTotalQty());
  83. $shipmentExtension = $shipping->getExtensionAttributes();
  84. $this->assertEquals('eu-2', $shipmentExtension->getSourceCode());
  85. } catch (NoSuchEntityException $e) {
  86. $this->fail('Failed asserting that Shipment was created');
  87. }
  88. /** @var OrderInterface $order */
  89. $shippedOrder = current($this->orderRepository->getList($searchCriteria)->getItems());
  90. $this->assertNotEquals(
  91. $createdOrder->getStatus(),
  92. $shippedOrder->getStatus(),
  93. 'Failed asserting that Order status was changed'
  94. );
  95. }
  96. /**
  97. * @magentoApiDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/websites_with_stores.php
  98. * @magentoApiDataFixture Magento/ConfigurableProduct/_files/configurable_attribute.php
  99. * @magentoApiDataFixture ../../../../app/code/Magento/InventoryConfigurableProduct/Test/_files/product_configurable.php
  100. * @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
  101. * @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
  102. * @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
  103. * @magentoApiDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/stock_website_sales_channels.php
  104. * @magentoApiDataFixture ../../../../app/code/Magento/InventoryConfigurableProduct/Test/_files/source_items_configurable.php
  105. * @magentoApiDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
  106. * @magentoApiDataFixture ../../../../app/code/Magento/InventoryShipping/Test/_files/create_quote_on_us_website.php
  107. * @magentoApiDataFixture ../../../../app/code/Magento/InventoryShipping/Test/_files/order_configurable_product.php
  108. */
  109. public function testShipOrderWithConfigurableProduct()
  110. {
  111. $searchCriteria = $this->searchCriteriaBuilder
  112. ->addFilter('increment_id', 'created_order_for_test')
  113. ->create();
  114. /** @var OrderInterface $order */
  115. $createdOrder = current($this->orderRepository->getList($searchCriteria)->getItems());
  116. $requestData = [
  117. 'arguments' => [
  118. 'extension_attributes' => [
  119. 'source_code' => 'us-1'
  120. ]
  121. ],
  122. 'orderId' => $createdOrder->getId(),
  123. ];
  124. $result = $this->_webApiCall(
  125. $this->buildServiceInfo((int)$createdOrder->getEntityId()),
  126. $requestData
  127. );
  128. $this->assertNotEmpty($result);
  129. try {
  130. $shipping = $this->shipmentRepository->get($result);
  131. $this->assertNotNull($shipping->getEntityId());
  132. $this->assertEquals('3.0000', $shipping->getTotalQty());
  133. $shipmentExtension = $shipping->getExtensionAttributes();
  134. $this->assertEquals('us-1', $shipmentExtension->getSourceCode());
  135. } catch (NoSuchEntityException $e) {
  136. $this->fail('Failed asserting that Shipment was created');
  137. }
  138. /** @var OrderInterface $order */
  139. $shippedOrder = current($this->orderRepository->getList($searchCriteria)->getItems());
  140. $this->assertNotEquals(
  141. $createdOrder->getStatus(),
  142. $shippedOrder->getStatus(),
  143. 'Failed asserting that Order status was changed'
  144. );
  145. }
  146. /**
  147. * Build request body
  148. *
  149. * @param int $orderId
  150. * @return array
  151. */
  152. private function buildServiceInfo(int $orderId): array
  153. {
  154. $serviceInfo = [
  155. 'rest' => [
  156. 'resourcePath' => '/V1/order/' . $orderId . '/ship',
  157. 'httpMethod' => Request::HTTP_METHOD_POST,
  158. ],
  159. 'soap' => [
  160. 'service' => self::SERVICE_READ_NAME,
  161. 'serviceVersion' => self::SERVICE_VERSION,
  162. 'operation' => self::SERVICE_READ_NAME . 'execute',
  163. ],
  164. ];
  165. return $serviceInfo;
  166. }
  167. }