Skip to content

Commit

Permalink
MAGETWO-56938: CLONE - [Github] API salesOrderRepositoryV1 can't crea…
Browse files Browse the repository at this point in the history
…te shipping address #5544
  • Loading branch information
irenelagno committed Oct 3, 2016
1 parent 32e5b0b commit 8ae9033
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
10 changes: 10 additions & 0 deletions app/code/Magento/Sales/Model/OrderRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ public function deleteById($id)
*/
public function save(\Magento\Sales\Api\Data\OrderInterface $entity)
{
/** @var \Magento\Sales\Api\Data\OrderExtensionInterface $extensionAttributes */
$extensionAttributes = $entity->getExtensionAttributes();
if ($entity->getIsNotVirtual() && $extensionAttributes && $extensionAttributes->getShippingAssignments()) {
$shippingAssignments = $extensionAttributes->getShippingAssignments();
if (count($shippingAssignments) === 1) {
$shipping = $shippingAssignments[0]->getShipping();
$entity->setShippingAddress($shipping->getAddress());
$entity->setShippingMethod($shipping->getMethod());
}
}
$this->metadata->getMapper()->save($entity);
$this->registry[$entity->getEntityId()] = $entity;
return $this->registry[$entity->getEntityId()];
Expand Down
32 changes: 32 additions & 0 deletions app/code/Magento/Sales/Test/Unit/Model/OrderRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,36 @@ public function testGetList()

$this->assertEquals($collectionMock, $this->model->getList($searchCriteriaMock));
}

public function testSave()
{
$mapperMock = $this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order::class)
->disableOriginalConstructor()
->getMock();
$orderEntity = $this->getMock(\Magento\Sales\Model\Order::class, [], [], '', false);
$extensionAttributes = $this->getMock(
\Magento\Sales\Api\Data\OrderExtension::class, ['getShippingAssignments'], [], '', false
);
$shippingAssignment = $this->getMockBuilder(\Magento\Sales\Model\Order\ShippingAssignment::class)
->disableOriginalConstructor()
->setMethods(['getShipping'])
->getMock();
$shippingMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Shipping::class)
->disableOriginalConstructor()
->setMethods(['getAddress', 'getMethod'])
->getMock();
$orderEntity->expects($this->once())->method('getExtensionAttributes')->willReturn($extensionAttributes);
$orderEntity->expects($this->once())->method('getIsNotVirtual')->willReturn(true);
$extensionAttributes
->expects($this->any())
->method('getShippingAssignments')
->willReturn([$shippingAssignment]);
$shippingAssignment->expects($this->once())->method('getShipping')->willReturn($shippingMock);
$shippingMock->expects($this->once())->method('getAddress');
$shippingMock->expects($this->once())->method('getMethod');
$this->metadata->expects($this->once())->method('getMapper')->willReturn($mapperMock);
$mapperMock->expects($this->once())->method('save');
$orderEntity->expects($this->any())->method('getEntityId')->willReturn(1);
$this->model->save($orderEntity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ protected function prepareOrder()
$orderPaymentFactory = $this->objectManager->get(\Magento\Sales\Model\Order\PaymentFactory::class);
/** @var \Magento\Sales\Model\Order\AddressRepository $orderAddressRepository */
$orderAddressRepository = $this->objectManager->get(\Magento\Sales\Model\Order\AddressRepository::class);
/** @var \Magento\Store\Model\StoreManagerInterface $storeManager */
$storeManager = $this->objectManager->get(\Magento\Store\Model\StoreManagerInterface::class);

$order = $orderFactory->create(
['data' => $this->getDataStructure(\Magento\Sales\Api\Data\OrderInterface::class)]
Expand Down Expand Up @@ -68,9 +70,36 @@ protected function prepareOrder()
$order->setCustomerEmail($email);
$order->setBaseGrandTotal(100);
$order->setGrandTotal(100);
$order->setShippingDescription('Flat Rate - Fixed');
$order->setIsVirtual(0);
$order->setStoreId($storeManager->getDefaultStoreView()->getId());
$order->setBaseDiscountAmount(0);
$order->setBaseShippingAmount(5);
$order->setBaseShippingTaxAmount(0);
$order->setBaseSubtotal(100);
$order->setBaseTaxAmount(0);
$order->setBaseToGlobalRate(1);
$order->setBaseToOrderRate(1);
$order->setDiscountAmount(0);
$order->setShippingAmount(0);
$order->setShippingTaxAmount(0);
$order->setStoreToOrderRate(0);
$order->setBaseToOrderRate(0);
$order->setSubtotal(100);
$order->setTaxAmount(0);
$order->setTotalQtyOrdered(1);
$order->setCustomerIsGuest(1);
$order->setCustomerNoteNotify(0);
$order->setCustomerGroupId(0);
$order->setBaseSubtotalInclTax(100);
$order->setWeight(1);
$order->setBaseCurrencyCode('USD');
$order->setShippingInclTax(5);
$order->setBaseShippingInclTax(5);

$this->addProductOption($orderItem);


$order->setItems([$orderItem->getData()]);
$order->setData('payment', $orderPayment->getData());

Expand All @@ -82,12 +111,37 @@ protected function prepareOrder()
$orderAddressBilling->setFirstname('First Name');
$orderAddressBilling->setTelephone('+00(000)-123-45-57');
$orderAddressBilling->setStreet(['Street']);
$orderAddressBilling->setCountryId(1);
$orderAddressBilling->setCountryId('US');
$orderAddressBilling->setRegion('California');
$orderAddressBilling->setAddressType('billing');
$orderAddressBilling->setRegionId(12);

$orderAddressShipping = $orderAddressRepository->create();
$orderAddressShipping->setCity('City2');
$orderAddressShipping->setPostcode('12345');
$orderAddressShipping->setLastname('Last Name2');
$orderAddressShipping->setFirstname('First Name2');
$orderAddressShipping->setTelephone('+00(000)-123-45-57');
$orderAddressShipping->setStreet(['Street']);
$orderAddressShipping->setCountryId('US');
$orderAddressShipping->setRegion('California');
$orderAddressShipping->setAddressType('shipping');
$orderAddressShipping->setRegionId(12);

$orderData = $order->getData();
$orderData['billing_address'] = $orderAddressBilling->getData();
$orderData['billing_address']['street'] = ['Street'];
$address = $orderAddressShipping->getData();
$address['street'] = ['Street'];
$orderData['extension_attributes']['shipping_assignments'] =
[
[
'shipping' => [
'address' => $address,
'method' => 'Flat Rate - Fixed'
]
]
];
return $orderData;
}

Expand Down Expand Up @@ -172,5 +226,8 @@ public function testOrderCreate()
$this->assertTrue((bool)$model->getId());
$this->assertEquals($order['base_grand_total'], $model->getBaseGrandTotal());
$this->assertEquals($order['grand_total'], $model->getGrandTotal());
$this->assertNotNull($model->getShippingAddress());
$this->assertTrue((bool)$model->getShippingAddress()->getId());
$this->assertEquals('Flat Rate - Fixed', $model->getShippingMethod());
}
}

0 comments on commit 8ae9033

Please sign in to comment.