OrderCreateTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. use Magento\TestFramework\TestCase\WebapiAbstract;
  8. use Magento\Catalog\Api\Data\ProductCustomOptionInterface;
  9. /**
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class OrderCreateTest extends WebapiAbstract
  13. {
  14. const RESOURCE_PATH = '/V1/orders';
  15. const SERVICE_READ_NAME = 'salesOrderRepositoryV1';
  16. const SERVICE_VERSION = 'V1';
  17. const ORDER_INCREMENT_ID = '100000001';
  18. /**
  19. * @var \Magento\Framework\ObjectManagerInterface
  20. */
  21. protected $objectManager;
  22. protected function setUp()
  23. {
  24. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  25. }
  26. /**
  27. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  28. */
  29. protected function prepareOrder()
  30. {
  31. /** @var \Magento\Sales\Model\Order $orderBuilder */
  32. $orderFactory = $this->objectManager->get(\Magento\Sales\Model\OrderFactory::class);
  33. /** @var \Magento\Sales\Api\Data\OrderItemFactory $orderItemFactory */
  34. $orderItemFactory = $this->objectManager->get(\Magento\Sales\Model\Order\ItemFactory::class);
  35. /** @var \Magento\Sales\Api\Data\OrderPaymentFactory $orderPaymentFactory */
  36. $orderPaymentFactory = $this->objectManager->get(\Magento\Sales\Model\Order\PaymentFactory::class);
  37. /** @var \Magento\Sales\Model\Order\AddressRepository $orderAddressRepository */
  38. $orderAddressRepository = $this->objectManager->get(\Magento\Sales\Model\Order\AddressRepository::class);
  39. /** @var \Magento\Store\Model\StoreManagerInterface $storeManager */
  40. $storeManager = $this->objectManager->get(\Magento\Store\Model\StoreManagerInterface::class);
  41. $order = $orderFactory->create(
  42. ['data' => $this->getDataStructure(\Magento\Sales\Api\Data\OrderInterface::class)]
  43. );
  44. $orderItem = $orderItemFactory->create(
  45. ['data' => $this->getDataStructure(\Magento\Sales\Api\Data\OrderItemInterface::class)]
  46. );
  47. $orderPayment = $orderPaymentFactory->create(
  48. ['data' => $this->getDataStructure(\Magento\Sales\Api\Data\OrderPaymentInterface::class)]
  49. );
  50. $email = uniqid() . 'email@example.com';
  51. $orderItem->setSku('sku#1');
  52. if (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) {
  53. $orderItem->setData('parent_item', $orderItem->getData() + ['parent_item' => null]);
  54. $orderItem->setAdditionalData('test');
  55. } else {
  56. $orderItem->setData('parent_item', ['weight' => 1]);
  57. }
  58. $orderPayment->setCcLast4('4444');
  59. $orderPayment->setMethod('checkmo');
  60. $orderPayment->setAccountStatus('ok');
  61. $orderPayment->setAdditionalInformation([]);
  62. $order->setCustomerEmail($email);
  63. $order->setBaseGrandTotal(100);
  64. $order->setGrandTotal(100);
  65. $order->setShippingDescription('Flat Rate - Fixed');
  66. $order->setIsVirtual(0);
  67. $order->setStoreId($storeManager->getDefaultStoreView()->getId());
  68. $order->setBaseDiscountAmount(0);
  69. $order->setBaseShippingAmount(5);
  70. $order->setBaseShippingTaxAmount(0);
  71. $order->setBaseSubtotal(100);
  72. $order->setBaseTaxAmount(0);
  73. $order->setBaseToGlobalRate(1);
  74. $order->setBaseToOrderRate(1);
  75. $order->setDiscountAmount(0);
  76. $order->setShippingAmount(0);
  77. $order->setShippingTaxAmount(0);
  78. $order->setStoreToOrderRate(0);
  79. $order->setBaseToOrderRate(0);
  80. $order->setSubtotal(100);
  81. $order->setTaxAmount(0);
  82. $order->setTotalQtyOrdered(1);
  83. $order->setCustomerIsGuest(1);
  84. $order->setCustomerNoteNotify(0);
  85. $order->setCustomerGroupId(0);
  86. $order->setBaseSubtotalInclTax(100);
  87. $order->setWeight(1);
  88. $order->setBaseCurrencyCode('USD');
  89. $order->setShippingInclTax(5);
  90. $order->setBaseShippingInclTax(5);
  91. $this->addProductOption($orderItem);
  92. $order->setItems([$orderItem->getData()]);
  93. $order->setData('payment', $orderPayment->getData());
  94. $orderAddressBilling = $orderAddressRepository->create();
  95. $orderAddressBilling->setCity('City');
  96. $orderAddressBilling->setPostcode('12345');
  97. $orderAddressBilling->setLastname('Last Name');
  98. $orderAddressBilling->setFirstname('First Name');
  99. $orderAddressBilling->setTelephone('+00(000)-123-45-57');
  100. $orderAddressBilling->setStreet(['Street']);
  101. $orderAddressBilling->setCountryId('US');
  102. $orderAddressBilling->setRegion('California');
  103. $orderAddressBilling->setAddressType('billing');
  104. $orderAddressBilling->setRegionId(12);
  105. $orderAddressShipping = $orderAddressRepository->create();
  106. $orderAddressShipping->setCity('City2');
  107. $orderAddressShipping->setPostcode('12345');
  108. $orderAddressShipping->setLastname('Last Name2');
  109. $orderAddressShipping->setFirstname('First Name2');
  110. $orderAddressShipping->setTelephone('+00(000)-123-45-57');
  111. $orderAddressShipping->setStreet(['Street']);
  112. $orderAddressShipping->setCountryId('US');
  113. $orderAddressShipping->setRegion('California');
  114. $orderAddressShipping->setAddressType('shipping');
  115. $orderAddressShipping->setRegionId(12);
  116. $orderData = $order->getData();
  117. $orderData['billing_address'] = $orderAddressBilling->getData();
  118. $orderData['billing_address']['street'] = ['Street'];
  119. $address = $orderAddressShipping->getData();
  120. $address['street'] = ['Street'];
  121. $orderData['extension_attributes']['shipping_assignments'] =
  122. [
  123. [
  124. 'shipping' => [
  125. 'address' => $address,
  126. 'method' => 'flatrate_flatrate'
  127. ],
  128. 'items' => [$orderItem->getData()],
  129. 'stock_id' => null,
  130. ]
  131. ];
  132. return $orderData;
  133. }
  134. protected function getDataStructure($className)
  135. {
  136. $refClass = new \ReflectionClass($className);
  137. $constants = $refClass->getConstants();
  138. $data = array_fill_keys($constants, null);
  139. unset($data['custom_attributes']);
  140. return $data;
  141. }
  142. /**
  143. * @param array $orderItem
  144. * @return array
  145. */
  146. protected function addProductOption($orderItem)
  147. {
  148. /** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
  149. $productRepository = $this->objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
  150. $product = $productRepository->get('simple');
  151. $options = [];
  152. foreach ($product->getOptions() as $option) {
  153. $options[] = [
  154. 'option_id' => $option->getId(),
  155. 'option_value' => $this->getOptionRequestValue($option),
  156. ];
  157. }
  158. $data['extension_attributes']['custom_options'] = $options;
  159. $orderItem->setData('product_option', $data);
  160. $orderItem->setPrice(10);
  161. $orderItem->setBasePrice(10);
  162. }
  163. /**
  164. * @param ProductCustomOptionInterface $option
  165. * @return null|string
  166. */
  167. protected function getOptionRequestValue(ProductCustomOptionInterface $option)
  168. {
  169. $returnValue = null;
  170. switch ($option->getType()) {
  171. case 'field':
  172. $returnValue = 'Test value';
  173. break;
  174. case 'date_time':
  175. $returnValue = '2015-09-09 07:16:00';
  176. break;
  177. case 'drop_down':
  178. $returnValue = '3-1-select';
  179. break;
  180. case 'radio':
  181. $returnValue = '4-1-radio';
  182. break;
  183. }
  184. return $returnValue;
  185. }
  186. /**
  187. * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
  188. */
  189. public function testOrderCreate()
  190. {
  191. $order = $this->prepareOrder();
  192. $serviceInfo = [
  193. 'rest' => [
  194. 'resourcePath' => self::RESOURCE_PATH,
  195. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
  196. ],
  197. 'soap' => [
  198. 'service' => self::SERVICE_READ_NAME,
  199. 'serviceVersion' => self::SERVICE_VERSION,
  200. 'operation' => self::SERVICE_READ_NAME . 'save',
  201. ],
  202. ];
  203. $this->assertNotEmpty($this->_webApiCall($serviceInfo, ['entity' => $order]));
  204. /** @var \Magento\Sales\Model\Order $model */
  205. $model = $this->objectManager->get(\Magento\Sales\Model\Order::class);
  206. $model->load($order['customer_email'], 'customer_email');
  207. $this->assertTrue((bool)$model->getId());
  208. $this->assertEquals($order['base_grand_total'], $model->getBaseGrandTotal());
  209. $this->assertEquals($order['grand_total'], $model->getGrandTotal());
  210. $this->assertNotNull($model->getShippingAddress());
  211. $this->assertTrue((bool)$model->getShippingAddress()->getId());
  212. $this->assertEquals('Flat Rate - Fixed', $model->getShippingDescription());
  213. $shippingMethod = $model->getShippingMethod(true);
  214. $this->assertEquals('flatrate', $shippingMethod['carrier_code']);
  215. $this->assertEquals('flatrate', $shippingMethod['method']);
  216. }
  217. }