Skip to content

Commit

Permalink
MAGETWO-44643: [API] Adapt Sales API to be compatible with multishipping
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladyslav Shcherbyna committed Oct 29, 2015
1 parent e3593ae commit 6c1a87a
Show file tree
Hide file tree
Showing 20 changed files with 114 additions and 286 deletions.
8 changes: 4 additions & 4 deletions app/code/Magento/Payment/Gateway/Data/Order/OrderAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@

use Magento\Payment\Gateway\Data\AddressAdapterInterface;
use Magento\Payment\Gateway\Data\OrderAdapterInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Model\Order;

/**
* Class OrderAdapter
*/
class OrderAdapter implements OrderAdapterInterface
{
/**
* @var OrderInterface
* @var Order
*/
private $order;

Expand All @@ -25,11 +25,11 @@ class OrderAdapter implements OrderAdapterInterface
private $addressAdapterFactory;

/**
* @param OrderInterface $order
* @param Order $order
* @param AddressAdapterFactory $addressAdapterFactory
*/
public function __construct(
OrderInterface $order,
Order $order,
AddressAdapterFactory $addressAdapterFactory
) {
$this->order = $order;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ class OrderAdapterTest extends \PHPUnit_Framework_TestCase

protected function setUp()
{
$this->orderMock = $this->getMockBuilder('Magento\Sales\Api\Data\OrderInterface')
->getMockForAbstractClass();
$this->orderMock = $this->getMockBuilder('Magento\Sales\Model\Order')
->disableOriginalConstructor()
->getMock();

$this->addressAdapterFactoryMock =
$this->getMockBuilder('Magento\Payment\Gateway\Data\Order\AddressAdapterFactory')
Expand Down
12 changes: 11 additions & 1 deletion app/code/Magento/Quote/Model/Quote/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Magento\Customer\Api\AddressMetadataInterface;
use Magento\Customer\Api\Data\AddressInterfaceFactory;
use Magento\Customer\Api\Data\RegionInterfaceFactory;
use Magento\Quote\Api\Data\AddressInterface;

/**
* Sales Quote address model
Expand All @@ -26,7 +27,6 @@
* @method Address setFreeShipping(int $value)
* @method int getCollectShippingRates()
* @method Address setCollectShippingRates(int $value)
* @method string getShippingMethod()
* @method Address setShippingMethod(string $value)
* @method string getShippingDescription()
* @method Address setShippingDescription(string $value)
Expand Down Expand Up @@ -1676,6 +1676,16 @@ public function setExtensionAttributes(\Magento\Quote\Api\Data\AddressExtensionI
return $this->_setExtensionAttributes($extensionAttributes);
}

/**
* Shipping method
*
* @return string
*/
public function getShippingMethod()
{
return $this->getData('shipping_method');
}

/**
* {@inheritdoc}
*/
Expand Down
11 changes: 9 additions & 2 deletions app/code/Magento/Quote/Model/Quote/Address/ToOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ public function convert(Address $object, $data = [])
'to_order',
$object
);

/**
* @var $order \Magento\Sales\Model\Order
*/
$order = $this->orderFactory->create();
$this->dataObjectHelper->populateWithArray(
$order,
Expand All @@ -77,7 +79,12 @@ public function convert(Address $object, $data = [])
$order->setStoreId($object->getQuote()->getStoreId())
->setQuoteId($object->getQuote()->getId())
->setIncrementId($object->getQuote()->getReservedOrderId());

if (array_key_exists('shipping_method', $data)) {
$order->setShippingMethod($data['shipping_method']);
}
if ($object->getShippingMethod()) {
$order->setShippingMethod($object->getShippingMethod());
}
$this->objectCopyService->copyFieldsetToTarget('sales_convert_quote', 'to_order', $object->getQuote(), $order);
$this->eventManager->dispatch(
'sales_convert_quote_to_order',
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Quote/Model/QuoteManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ protected function submitQuote(QuoteEntity $quote, $orderData = [])
$addresses[] = $billingAddress;
$order->setBillingAddress($billingAddress);
$order->setAddresses($addresses);
$order->setPayments([$this->quotePaymentToOrderPayment->convert($quote->getPayment())]);
$order->setPayment($this->quotePaymentToOrderPayment->convert($quote->getPayment()));
$order->setItems($this->resolveItems($quote));
if ($quote->getCustomer()) {
$order->setCustomerId($quote->getCustomer()->getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ToOrderTest extends \PHPUnit_Framework_TestCase
/**
* @var \Magento\Sales\Api\Data\OrderInterface | \PHPUnit_Framework_MockObject_MockObject
*/
protected $orderInterfaceMock;
protected $orderMock;

/**
* @var \Magento\Quote\Model\Quote\Address\ToOrder
Expand All @@ -52,15 +52,9 @@ protected function setUp()
false
);
$this->objectCopyMock = $this->getMock('Magento\Framework\DataObject\Copy', [], [], '', false);
$this->orderInterfaceMock = $this->getMockForAbstractClass(
'Magento\Sales\Api\Data\OrderInterface',
[],
'',
false,
true,
true,
['setStoreId', 'setQuoteId']
);
$this->orderMock = $this->getMockBuilder('Magento\Sales\Model\Order')
->disableOriginalConstructor()
->getMock();
$this->eventManagerMock = $this->getMock('Magento\Framework\Event\ManagerInterface', [], [], '', false);
$this->dataObjectHelper = $this->getMock('\Magento\Framework\Api\DataObjectHelper', [], [], '', false);
$objectManager = new ObjectManager($this);
Expand All @@ -81,26 +75,29 @@ public function testConvert()
$data = ['test' => 'beer'];
$quoteId = 1;
$storeId = 777;
$shippingMethod = 'checkmo';

$object = $this->getMock('Magento\Quote\Model\Quote\Address', [], [], '', false);
$quote = $this->getMock('Magento\Quote\Model\Quote', [], [], '', false);
$object->expects($this->exactly(5))->method('getQuote')->willReturn($quote);
$quote->expects($this->once())->method('getId')->willReturn($quoteId);
$quote->expects($this->once())->method('getStoreId')->willReturn($storeId);
$object->expects($this->atLeastOnce())->method('getShippingMethod')->willReturn($shippingMethod);
$this->objectCopyMock->expects($this->once())->method('getDataFromFieldset')->with(
'quote_convert_address',
'to_order',
$object
)->willReturn($orderData);
$this->dataObjectHelper->expects($this->once())->method('populateWithArray')
->with($this->orderInterfaceMock, ['test' => 'beer'], '\Magento\Sales\Api\Data\OrderInterface')
->with($this->orderMock, ['test' => 'beer'], '\Magento\Sales\Api\Data\OrderInterface')
->willReturnSelf();
$this->orderInterfaceMock->expects($this->once())->method('setStoreId')->with($storeId)->willReturnSelf();
$this->orderInterfaceMock->expects($this->once())->method('setQuoteId')->with($quoteId)->willReturnSelf();
$this->orderDataFactoryMock->expects($this->once())->method('create')->willReturn($this->orderInterfaceMock);
$this->orderMock->expects($this->once())->method('setStoreId')->with($storeId)->willReturnSelf();
$this->orderMock->expects($this->once())->method('setQuoteId')->with($quoteId)->willReturnSelf();
$this->orderMock->expects($this->once())->method('setShippingMethod')->willReturnSelf();
$this->orderDataFactoryMock->expects($this->once())->method('create')->willReturn($this->orderMock);
$this->eventManagerMock->expects($this->once())
->method('dispatch')
->with('sales_convert_quote_to_order', ['order' => $this->orderInterfaceMock, 'quote' => $quote]);
$this->assertSame($this->orderInterfaceMock, $this->converter->convert($object, $data));
->with('sales_convert_quote_to_order', ['order' => $this->orderMock, 'quote' => $quote]);
$this->assertSame($this->orderMock, $this->converter->convert($object, $data));
}
}
11 changes: 5 additions & 6 deletions app/code/Magento/Quote/Test/Unit/Model/QuoteManagementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,6 @@ public function testSubmit()
$convertedQuoteItem = $this->getMock('Magento\Sales\Api\Data\OrderItemInterface', [], [], '', false);

$addresses = [$convertedShippingAddress, $convertedBillingAddress];
$payments = [$convertedPayment];
$quoteItems = [$quoteItem];
$convertedItems = [$convertedQuoteItem];

Expand Down Expand Up @@ -612,7 +611,7 @@ public function testSubmit()
$baseOrder,
$convertedBillingAddress,
$addresses,
$payments,
$convertedPayment,
$convertedItems,
$quoteId,
$convertedShippingAddress
Expand Down Expand Up @@ -883,7 +882,7 @@ protected function getQuote(
* @param \Magento\Sales\Api\Data\OrderInterface $baseOrder
* @param \Magento\Sales\Api\Data\OrderAddressInterface $billingAddress
* @param array $addresses
* @param array $payments
* @param $payment
* @param array $items
* @param $quoteId
* @param \Magento\Sales\Api\Data\OrderAddressInterface $shippingAddress
Expand All @@ -893,7 +892,7 @@ protected function prepareOrderFactory(
\Magento\Sales\Api\Data\OrderInterface $baseOrder,
\Magento\Sales\Api\Data\OrderAddressInterface $billingAddress,
array $addresses,
array $payments,
$payment,
array $items,
$quoteId,
\Magento\Sales\Api\Data\OrderAddressInterface $shippingAddress = null,
Expand All @@ -902,7 +901,7 @@ protected function prepareOrderFactory(
$order = $this->getMock(
'Magento\Sales\Model\Order',
['setShippingAddress', 'getAddressesCollection', 'getAddresses', 'getBillingAddress', 'addAddresses',
'setBillingAddress', 'setAddresses', 'setPayments', 'setItems', 'setQuoteId'],
'setBillingAddress', 'setAddresses', 'setPayment', 'setItems', 'setQuoteId'],
[],
'',
false
Expand All @@ -929,7 +928,7 @@ protected function prepareOrderFactory(
$order->expects($this->any())->method('addAddresses')->withAnyParameters()->willReturnSelf();
$order->expects($this->once())->method('setBillingAddress')->with($billingAddress);
$order->expects($this->once())->method('setAddresses')->with($addresses);
$order->expects($this->once())->method('setPayments')->with($payments);
$order->expects($this->once())->method('setPayment')->with($payment);
$order->expects($this->once())->method('setItems')->with($items);
$order->expects($this->once())->method('setQuoteId')->with($quoteId);

Expand Down
19 changes: 0 additions & 19 deletions app/code/Magento/Sales/Api/Data/OrderAddressInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ interface OrderAddressInterface extends \Magento\Framework\Api\ExtensibleDataInt
* Customer address ID.
*/
const CUSTOMER_ADDRESS_ID = 'customer_address_id';
/*
* Quote address ID.
*/
const QUOTE_ADDRESS_ID = 'quote_address_id';
/*
* Region ID.
*/
Expand Down Expand Up @@ -237,13 +233,6 @@ public function getPostcode();
*/
public function getPrefix();

/**
* Gets the quote address ID for the order address.
*
* @return int|null Quote address ID.
*/
public function getQuoteAddressId();

/**
* Gets the region for the order address.
*
Expand Down Expand Up @@ -337,14 +326,6 @@ public function setParentId($id);
*/
public function setCustomerAddressId($id);

/**
* Sets the quote address ID for the order address.
*
* @param int $id
* @return $this
*/
public function setQuoteAddressId($id);

/**
* Sets the region ID for the order address.
*
Expand Down
Loading

0 comments on commit 6c1a87a

Please sign in to comment.