Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
use Magento\Store\Model\StoreManagerInterface;
use PHPUnit_Framework_MockObject_MockObject as MockObject;

/**
* Test for Magento\OfflineShipping\Model\Quote\Address\FreeShipping class.
*/
class FreeShippingTest extends \PHPUnit\Framework\TestCase
{
private static $websiteId = 1;
Expand Down Expand Up @@ -77,12 +80,14 @@ public function testIsFreeShipping(int $addressFree, int $fItemFree, int $sItemF
[$fItem],
[$sItem]
)
->willReturnCallback(function () use ($fItem, $sItem, $addressFree, $fItemFree, $sItemFree) {
// emulate behavior of cart rule calculator
$fItem->getAddress()->setFreeShipping($addressFree);
$fItem->setFreeShipping($fItemFree);
$sItem->setFreeShipping($sItemFree);
});
->willReturnCallback(
function () use ($fItem, $sItem, $addressFree, $fItemFree, $sItemFree) {
// emulate behavior of cart rule calculator
$fItem->getAddress()->setFreeShipping($addressFree);
$fItem->setFreeShipping($fItemFree);
$sItem->setFreeShipping($sItemFree);
}
);

$actual = $this->model->isFreeShipping($quote, $items);
self::assertEquals($expected, $actual);
Expand Down Expand Up @@ -133,8 +138,11 @@ private function getQuote(Address $address): Quote
->disableOriginalConstructor()
->setMethods(
[
'getCouponCode', 'getCustomerGroupId', 'getShippingAddress', 'getStoreId', 'getItemsQty',
'getVirtualItemsQty'
'getCouponCode',
'getCustomerGroupId',
'getShippingAddress',
'getStoreId',
'isVirtual'
]
)
->getMock();
Expand All @@ -147,10 +155,8 @@ private function getQuote(Address $address): Quote
->willReturn(self::$couponCode);
$quote->method('getShippingAddress')
->willReturn($address);
$quote->method('getItemsQty')
->willReturn(2);
$quote->method('getVirtualItemsQty')
->willReturn(0);
$quote->method('isVirtual')
->willReturn(false);

return $quote;
}
Expand Down
41 changes: 22 additions & 19 deletions app/code/Magento/Quote/Model/Quote/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public function beforeSave()
*/
public function getAddress()
{
if ($this->getQuote()->getItemsQty() == $this->getQuote()->getVirtualItemsQty()) {
if ($this->getQuote()->isVirtual()) {
$address = $this->getQuote()->getBillingAddress();
} else {
$address = $this->getQuote()->getShippingAddress();
Expand All @@ -282,7 +282,7 @@ public function getAddress()
/**
* Declare quote model object
*
* @param \Magento\Quote\Model\Quote $quote
* @param \Magento\Quote\Model\Quote $quote
* @return $this
*/
public function setQuote(\Magento\Quote\Model\Quote $quote)
Expand Down Expand Up @@ -719,6 +719,7 @@ public function getOptionByCode($code)

/**
* Checks that item model has data changes.
*
* Call save item options if model isn't need to save in DB
*
* @return boolean
Expand Down Expand Up @@ -813,8 +814,9 @@ public function __clone()
}

/**
* Returns formatted buy request - object, holding request received from
* product view page with keys and options for configured product
* Get formatted buy request.
*
* Returns object, holding request received from product view page with keys and options for configured product.
*
* @return \Magento\Framework\DataObject
*/
Expand Down Expand Up @@ -863,6 +865,7 @@ public function setHasError($flag)

/**
* Clears list of errors, associated with this quote item.
*
* Also automatically removes error-flag from oneself.
*
* @return $this
Expand All @@ -876,6 +879,7 @@ protected function _clearErrorInfo()

/**
* Adds error information to the quote item.
*
* Automatically sets error flag.
*
* @param string|null $origin Usually a name of module, that embeds error
Expand Down Expand Up @@ -930,97 +934,96 @@ public function removeErrorInfosByParams($params)
}

/**
* @inheritdoc
* @codeCoverageIgnoreStart
*
* {@inheritdoc}
*/
public function getItemId()
{
return $this->getData(self::KEY_ITEM_ID);
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function setItemId($itemID)
{
return $this->setData(self::KEY_ITEM_ID, $itemID);
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getSku()
{
return $this->getData(self::KEY_SKU);
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function setSku($sku)
{
return $this->setData(self::KEY_SKU, $sku);
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getQty()
{
return $this->getData(self::KEY_QTY);
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getName()
{
return $this->getData(self::KEY_NAME);
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function setName($name)
{
return $this->setData(self::KEY_NAME, $name);
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getPrice()
{
return $this->getData(self::KEY_PRICE);
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function setPrice($price)
{
return $this->setData(self::KEY_PRICE, $price);
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function setProductType($productType)
{
return $this->setData(self::KEY_PRODUCT_TYPE, $productType);
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getQuoteId()
{
return $this->getData(self::KEY_QUOTE_ID);
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function setQuoteId($quoteId)
{
Expand Down Expand Up @@ -1051,7 +1054,7 @@ public function setProductOption(\Magento\Quote\Api\Data\ProductOptionInterface
//@codeCoverageIgnoreEnd

/**
* {@inheritdoc}
* @inheritdoc
*
* @return \Magento\Quote\Api\Data\CartItemExtensionInterface|null
*/
Expand All @@ -1061,7 +1064,7 @@ public function getExtensionAttributes()
}

/**
* {@inheritdoc}
* @inheritdoc
*
* @param \Magento\Quote\Api\Data\CartItemExtensionInterface $extensionAttributes
* @return $this
Expand Down
43 changes: 21 additions & 22 deletions app/code/Magento/Quote/Test/Unit/Model/Quote/ItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,27 +135,24 @@ protected function setUp()
public function testGetAddress()
{
$quote = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
->setMethods(['getShippingAddress', 'getBillingAddress', 'getStoreId', '__wakeup'])
->setMethods(['getShippingAddress', 'getBillingAddress', 'getStoreId', '__wakeup', 'isVirtual'])
->disableOriginalConstructor()
->getMock();
$quote->expects($this->once())
->method('getShippingAddress')
->will($this->returnValue('shipping'));
->willReturn('shipping');
$quote->expects($this->once())
->method('getBillingAddress')
->will($this->returnValue('billing'));
->willReturn('billing');
$quote->expects($this->any())
->method('getStoreId')
->will($this->returnValue(1));
->willReturn(1);
$quote->expects($this->exactly(2))
->method('isVirtual')
->willReturnOnConsecutiveCalls(false, true);

$this->model->setQuote($quote);

$quote->setItemsQty(2);
$quote->setVirtualItemsQty(1);
$this->assertEquals('shipping', $this->model->getAddress(), 'Wrong shipping address');

$quote->setItemsQty(2);
$quote->setVirtualItemsQty(2);
$this->assertEquals('billing', $this->model->getAddress(), 'Wrong billing address');
}

Expand Down Expand Up @@ -855,18 +852,20 @@ public function testSetOptionsWithNull()
private function createOptionMock($optionCode, $optionData = [])
{
$optionMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item\Option::class)
->setMethods([
'setData',
'setItem',
'getItem',
'getCode',
'__wakeup',
'isDeleted',
'delete',
'getValue',
'getProduct',
'save'
])
->setMethods(
[
'setData',
'setItem',
'getItem',
'getCode',
'__wakeup',
'isDeleted',
'delete',
'getValue',
'getProduct',
'save'
]
)
->disableOriginalConstructor()
->getMock();
$optionMock->expects($this->any())
Expand Down
41 changes: 41 additions & 0 deletions dev/tests/integration/testsuite/Magento/Quote/Model/QuoteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
use Magento\Customer\Model\Data\Customer;
use Magento\Framework\Exception\LocalizedException;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\ObjectManager;
use Magento\Framework\Api\SearchCriteriaBuilder;
Expand Down Expand Up @@ -72,6 +73,46 @@ public function testCollectTotalsWithVirtual(): void
$this->assertEquals(20, $quote->getBaseGrandTotal());
}

/**
* @magentoDataFixture Magento/Catalog/_files/product_virtual.php
* @magentoDataFixture Magento/Quote/_files/empty_quote.php
* @return void
*/
public function testGetAddressWithVirtualProduct()
{
/** @var Quote $quote */
$quote = $this->objectManager->create(Quote::class);
$quote->load('reserved_order_id_1', 'reserved_order_id');
$billingAddress = $this->objectManager->create(AddressInterface::class);
$billingAddress->setFirstname('Joe')
->setLastname('Doe')
->setCountryId('US')
->setRegion('TX')
->setCity('Austin')
->setStreet('1000 West Parmer Line')
->setPostcode('11501')
->setTelephone('123456789');
$quote->setBillingAddress($billingAddress);
$shippingAddress = $this->objectManager->create(AddressInterface::class);
$shippingAddress->setFirstname('Joe')
->setLastname('Doe')
->setCountryId('US')
->setRegion('NJ')
->setCity('Newark')
->setStreet('2775 Granville Lane')
->setPostcode('07102')
->setTelephone('9734685221');
$quote->setShippingAddress($shippingAddress);
$productRepository = $this->objectManager->create(
ProductRepositoryInterface::class
);
$product = $productRepository->get('virtual-product', false, null, true);
$quote->addProduct($product);
$quote->save();
$expectedAddress = $quote->getBillingAddress();
$this->assertEquals($expectedAddress, $quote->getAllItems()[0]->getAddress());
}

/**
* @return void
*/
Expand Down