Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
OrderCreateTest.php
Go to the documentation of this file.
1 <?php
7 
10 
15 {
16  const RESOURCE_PATH = '/V1/orders';
17 
18  const SERVICE_READ_NAME = 'salesOrderRepositoryV1';
19 
20  const SERVICE_VERSION = 'V1';
21 
22  const ORDER_INCREMENT_ID = '100000001';
23 
27  protected $objectManager;
28 
29  protected function setUp()
30  {
32  }
33 
37  protected function prepareOrder()
38  {
40  $orderFactory = $this->objectManager->get(\Magento\Sales\Model\OrderFactory::class);
42  $orderItemFactory = $this->objectManager->get(\Magento\Sales\Model\Order\ItemFactory::class);
44  $orderPaymentFactory = $this->objectManager->get(\Magento\Sales\Model\Order\PaymentFactory::class);
46  $orderAddressRepository = $this->objectManager->get(\Magento\Sales\Model\Order\AddressRepository::class);
48  $storeManager = $this->objectManager->get(\Magento\Store\Model\StoreManagerInterface::class);
49 
50  $order = $orderFactory->create(
51  ['data' => $this->getDataStructure(\Magento\Sales\Api\Data\OrderInterface::class)]
52  );
53  $orderItem = $orderItemFactory->create(
54  ['data' => $this->getDataStructure(\Magento\Sales\Api\Data\OrderItemInterface::class)]
55  );
56  $orderPayment = $orderPaymentFactory->create(
57  ['data' => $this->getDataStructure(\Magento\Sales\Api\Data\OrderPaymentInterface::class)]
58  );
59 
60  $email = uniqid() . '[email protected]';
61  $orderItem->setSku('sku#1');
62  if (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) {
63  $orderItem->setData('parent_item', $orderItem->getData() + ['parent_item' => null]);
64  $orderItem->setAdditionalData('test');
65  } else {
66  $orderItem->setData('parent_item', ['weight' => 1]);
67  }
68 
69  $orderPayment->setCcLast4('4444');
70  $orderPayment->setMethod('checkmo');
71  $orderPayment->setAccountStatus('ok');
72  $orderPayment->setAdditionalInformation([]);
73  $order->setCustomerEmail($email);
74  $order->setBaseGrandTotal(100);
75  $order->setGrandTotal(100);
76  $order->setShippingDescription('Flat Rate - Fixed');
77  $order->setIsVirtual(0);
78  $order->setStoreId($storeManager->getDefaultStoreView()->getId());
79  $order->setBaseDiscountAmount(0);
80  $order->setBaseShippingAmount(5);
81  $order->setBaseShippingTaxAmount(0);
82  $order->setBaseSubtotal(100);
83  $order->setBaseTaxAmount(0);
84  $order->setBaseToGlobalRate(1);
85  $order->setBaseToOrderRate(1);
86  $order->setDiscountAmount(0);
87  $order->setShippingAmount(0);
88  $order->setShippingTaxAmount(0);
89  $order->setStoreToOrderRate(0);
90  $order->setBaseToOrderRate(0);
91  $order->setSubtotal(100);
92  $order->setTaxAmount(0);
93  $order->setTotalQtyOrdered(1);
94  $order->setCustomerIsGuest(1);
95  $order->setCustomerNoteNotify(0);
96  $order->setCustomerGroupId(0);
97  $order->setBaseSubtotalInclTax(100);
98  $order->setWeight(1);
99  $order->setBaseCurrencyCode('USD');
100  $order->setShippingInclTax(5);
101  $order->setBaseShippingInclTax(5);
102 
103  $this->addProductOption($orderItem);
104 
105  $order->setItems([$orderItem->getData()]);
106  $order->setData('payment', $orderPayment->getData());
107 
108  $orderAddressBilling = $orderAddressRepository->create();
109 
110  $orderAddressBilling->setCity('City');
111  $orderAddressBilling->setPostcode('12345');
112  $orderAddressBilling->setLastname('Last Name');
113  $orderAddressBilling->setFirstname('First Name');
114  $orderAddressBilling->setTelephone('+00(000)-123-45-57');
115  $orderAddressBilling->setStreet(['Street']);
116  $orderAddressBilling->setCountryId('US');
117  $orderAddressBilling->setRegion('California');
118  $orderAddressBilling->setAddressType('billing');
119  $orderAddressBilling->setRegionId(12);
120 
121  $orderAddressShipping = $orderAddressRepository->create();
122  $orderAddressShipping->setCity('City2');
123  $orderAddressShipping->setPostcode('12345');
124  $orderAddressShipping->setLastname('Last Name2');
125  $orderAddressShipping->setFirstname('First Name2');
126  $orderAddressShipping->setTelephone('+00(000)-123-45-57');
127  $orderAddressShipping->setStreet(['Street']);
128  $orderAddressShipping->setCountryId('US');
129  $orderAddressShipping->setRegion('California');
130  $orderAddressShipping->setAddressType('shipping');
131  $orderAddressShipping->setRegionId(12);
132 
133  $orderData = $order->getData();
134  $orderData['billing_address'] = $orderAddressBilling->getData();
135  $orderData['billing_address']['street'] = ['Street'];
136  $address = $orderAddressShipping->getData();
137  $address['street'] = ['Street'];
138  $orderData['extension_attributes']['shipping_assignments'] =
139  [
140  [
141  'shipping' => [
142  'address' => $address,
143  'method' => 'flatrate_flatrate'
144  ],
145  'items' => [$orderItem->getData()],
146  'stock_id' => null,
147  ]
148  ];
149  return $orderData;
150  }
151 
152  protected function getDataStructure($className)
153  {
154  $refClass = new \ReflectionClass($className);
155  $constants = $refClass->getConstants();
156  $data = array_fill_keys($constants, null);
157  unset($data['custom_attributes']);
158  return $data;
159  }
160 
165  protected function addProductOption($orderItem)
166  {
168  $productRepository = $this->objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
169  $product = $productRepository->get('simple');
170  $options = [];
171  foreach ($product->getOptions() as $option) {
172  $options[] = [
173  'option_id' => $option->getId(),
174  'option_value' => $this->getOptionRequestValue($option),
175  ];
176  }
177  $data['extension_attributes']['custom_options'] = $options;
178  $orderItem->setData('product_option', $data);
179  $orderItem->setPrice(10);
180  $orderItem->setBasePrice(10);
181  }
182 
188  {
189  $returnValue = null;
190  switch ($option->getType()) {
191  case 'field':
192  $returnValue = 'Test value';
193  break;
194  case 'date_time':
195  $returnValue = '2015-09-09 07:16:00';
196  break;
197  case 'drop_down':
198  $returnValue = '3-1-select';
199  break;
200  case 'radio':
201  $returnValue = '4-1-radio';
202  break;
203  }
204  return $returnValue;
205  }
206 
210  public function testOrderCreate()
211  {
212  $order = $this->prepareOrder();
213 
214  $serviceInfo = [
215  'rest' => [
216  'resourcePath' => self::RESOURCE_PATH,
218  ],
219  'soap' => [
220  'service' => self::SERVICE_READ_NAME,
221  'serviceVersion' => self::SERVICE_VERSION,
222  'operation' => self::SERVICE_READ_NAME . 'save',
223  ],
224  ];
225  $this->assertNotEmpty($this->_webApiCall($serviceInfo, ['entity' => $order]));
226 
228  $model = $this->objectManager->get(\Magento\Sales\Model\Order::class);
229  $model->load($order['customer_email'], 'customer_email');
230  $this->assertTrue((bool)$model->getId());
231  $this->assertEquals($order['base_grand_total'], $model->getBaseGrandTotal());
232  $this->assertEquals($order['grand_total'], $model->getGrandTotal());
233  $this->assertNotNull($model->getShippingAddress());
234  $this->assertTrue((bool)$model->getShippingAddress()->getId());
235  $this->assertEquals('Flat Rate - Fixed', $model->getShippingDescription());
236  $shippingMethod = $model->getShippingMethod(true);
237  $this->assertEquals('flatrate', $shippingMethod['carrier_code']);
238  $this->assertEquals('flatrate', $shippingMethod['method']);
239  }
240 }
$email
Definition: details.phtml:13
_webApiCall( $serviceInfo, $arguments=[], $webApiAdapterCode=null, $storeCode=null, $integration=null)
$orderItem
Definition: order.php:30
$order
Definition: order.php:55
$storeManager
$address
Definition: customer.php:38
$shippingMethod
Definition: popup.phtml:12
getOptionRequestValue(ProductCustomOptionInterface $option)
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31