-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MAGETWO-58269: [GitHub] Error in 'useForShipping'-parameter when savi…
…ng billing address via API #6557
- Loading branch information
1 parent
6af30d6
commit a1d4a1d
Showing
6 changed files
with
227 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
app/code/Magento/Quote/Model/ShippingAddressAssignment.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
/** | ||
* Copyright © 2016 Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\Quote\Model; | ||
|
||
class ShippingAddressAssignment | ||
{ | ||
/** | ||
* @var \Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentProcessor | ||
*/ | ||
private $shippingAssignmentProcessor; | ||
|
||
/** | ||
* @var \Magento\Quote\Api\Data\CartExtensionFactory | ||
*/ | ||
private $cartExtensionFactory; | ||
|
||
/** | ||
* ShippingAddressAssignment constructor. | ||
* @param \Magento\Quote\Api\Data\CartExtensionFactory $cartExtensionFactory | ||
* @param Quote\ShippingAssignment\ShippingAssignmentProcessor $shippingAssignmentProcessor | ||
*/ | ||
public function __construct( | ||
\Magento\Quote\Api\Data\CartExtensionFactory $cartExtensionFactory, | ||
\Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentProcessor $shippingAssignmentProcessor | ||
) { | ||
$this->shippingAssignmentProcessor = $shippingAssignmentProcessor;; | ||
$this->cartExtensionFactory = $cartExtensionFactory; | ||
} | ||
|
||
/** | ||
* @param \Magento\Quote\Api\Data\CartInterface $quote | ||
* @param \Magento\Quote\Api\Data\AddressInterface $address | ||
* @param bool $useForShipping | ||
* @return void | ||
*/ | ||
public function setAddress( | ||
\Magento\Quote\Api\Data\CartInterface $quote, | ||
\Magento\Quote\Api\Data\AddressInterface $address, | ||
$useForShipping = false | ||
) { | ||
if ($useForShipping) { | ||
$quote->removeAddress($quote->getShippingAddress()->getId()); | ||
$address->setSameAsBilling(1); | ||
$address->setCollectShippingRates(true); | ||
} else { | ||
$address = $quote->getShippingAddress()->setSameAsBilling(0); | ||
} | ||
|
||
$quote->setShippingAddress($address); | ||
$cartExtension = $quote->getExtensionAttributes(); | ||
if ($cartExtension === null) { | ||
$cartExtension = $this->cartExtensionFactory->create(); | ||
} | ||
/** @var \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment */ | ||
$shippingAssignment = $this->shippingAssignmentProcessor->create($quote); | ||
$cartExtension->setShippingAssignments([$shippingAssignment]); | ||
$quote->setExtensionAttributes($cartExtension); | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
app/code/Magento/Quote/Test/Unit/Model/ShippingAddressAssignmentTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php | ||
/** | ||
* Copyright © 2016 Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\Quote\Test\Unit\Model; | ||
|
||
class ShippingAddressAssignmentTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var \Magento\Quote\Model\ShippingAddressAssignment | ||
*/ | ||
private $model; | ||
|
||
/** | ||
* @var \PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private $shippingAssignmentProcessorMock; | ||
|
||
/** | ||
* @var \PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private $cartExtensionFactoryMock; | ||
|
||
/** | ||
* @var \PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private $quoteMock; | ||
|
||
/** | ||
* @var \PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private $addressMock; | ||
|
||
/** | ||
* @var \PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private $extensionAttributeMock; | ||
|
||
/** | ||
* @var \PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private $shippingAssignmentMock; | ||
|
||
public function setUp() | ||
{ | ||
$this->cartExtensionFactoryMock = $this->getMock( | ||
\Magento\Quote\Api\Data\CartExtensionFactory::class, | ||
['create'], | ||
[], | ||
'', | ||
false | ||
); | ||
$this->shippingAssignmentProcessorMock = $this->getMock( | ||
\Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentProcessor::class, | ||
[], | ||
[], | ||
'', | ||
false | ||
); | ||
$this->quoteMock = $this->getMock(\Magento\Quote\Model\Quote::class, [], [], '', false); | ||
$this->addressMock = $this->getMock(\Magento\Quote\Model\Quote\Address::class, [], [], '', false); | ||
$this->extensionAttributeMock = $this->getMock( | ||
\Magento\Quote\Api\Data\CartExtension::class, | ||
['setShippingAssignments'], | ||
[], | ||
'', | ||
false | ||
); | ||
|
||
$this->shippingAssignmentMock = $this->getMock(\Magento\Quote\Api\Data\ShippingAssignmentInterface::class); | ||
//shipping assignment processing | ||
$this->quoteMock->expects($this->once())->method('getExtensionAttributes')->willReturn(null); | ||
$this->cartExtensionFactoryMock | ||
->expects($this->once()) | ||
->method('create') | ||
->willReturn($this->extensionAttributeMock); | ||
$this->shippingAssignmentProcessorMock | ||
->expects($this->once()) | ||
->method('create') | ||
->willReturn($this->shippingAssignmentMock); | ||
$this->extensionAttributeMock | ||
->expects($this->once()) | ||
->method('setShippingAssignments') | ||
->with([$this->shippingAssignmentMock]) | ||
->willReturnSelf(); | ||
$this->quoteMock->expects($this->once())->method('setExtensionAttributes')->with($this->extensionAttributeMock); | ||
$this->model = new \Magento\Quote\Model\ShippingAddressAssignment( | ||
$this->cartExtensionFactoryMock, | ||
$this->shippingAssignmentProcessorMock | ||
); | ||
} | ||
|
||
public function testSetAddressUseForShippingTrue() | ||
{ | ||
$addressId = 1; | ||
$addressMock = $this->getMock(\Magento\Quote\Api\Data\AddressInterface::class); | ||
$this->quoteMock->expects($this->once())->method('getShippingAddress')->willReturn($addressMock); | ||
$addressMock->expects($this->once())->method('getId')->willReturn($addressId); | ||
$this->addressMock->expects($this->once())->method('setSameAsBilling')->with(1); | ||
$this->quoteMock->expects($this->once())->method('removeAddress')->with($addressId); | ||
$this->quoteMock->expects($this->once())->method('setShippingAddress')->with($this->addressMock); | ||
$this->model->setAddress($this->quoteMock, $this->addressMock, true); | ||
} | ||
|
||
public function testSetAddressUseForShippingFalse() | ||
{ | ||
$addressMock = $this->getMock(\Magento\Quote\Api\Data\AddressInterface::class); | ||
$this->quoteMock->expects($this->once())->method('getShippingAddress')->willReturn($addressMock); | ||
$addressMock->expects($this->once())->method('setSameAsBilling')->with(0)->willReturnSelf(); | ||
$this->quoteMock->expects($this->once())->method('setShippingAddress')->with($addressMock); | ||
$this->model->setAddress($this->quoteMock, $this->addressMock, false); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters