diff --git a/app/code/Magento/Checkout/Block/Cart/AbstractCart.php b/app/code/Magento/Checkout/Block/Cart/AbstractCart.php
index 95b5cda03e38a..b4f14959357fb 100644
--- a/app/code/Magento/Checkout/Block/Cart/AbstractCart.php
+++ b/app/code/Magento/Checkout/Block/Cart/AbstractCart.php
@@ -149,7 +149,11 @@ public function getTotals()
public function getTotalsCache()
{
if (empty($this->_totals)) {
- $this->_totals = $this->getQuote()->getTotals();
+ if ($this->getQuote()->isVirtual()) {
+ $this->_totals = $this->getQuote()->getBillingAddress()->getTotals();
+ } else {
+ $this->_totals = $this->getQuote()->getShippingAddress()->getTotals();
+ }
}
return $this->_totals;
}
diff --git a/app/code/Magento/Checkout/Model/DefaultConfigProvider.php b/app/code/Magento/Checkout/Model/DefaultConfigProvider.php
index a02455a1068aa..d0cc39ceaafe1 100644
--- a/app/code/Magento/Checkout/Model/DefaultConfigProvider.php
+++ b/app/code/Magento/Checkout/Model/DefaultConfigProvider.php
@@ -562,7 +562,11 @@ private function getTotalsData()
$totalSegmentsData = [];
/** @var \Magento\Quote\Model\Cart\TotalSegment $totalSegment */
foreach ($totals->getTotalSegments() as $totalSegment) {
- $totalSegmentsData[] = $totalSegment->toArray();
+ $totalSegmentArray = $totalSegment->toArray();
+ if (is_object($totalSegment->getExtensionAttributes())) {
+ $totalSegmentArray['extension_attributes'] = $totalSegment->getExtensionAttributes()->__toArray();
+ }
+ $totalSegmentsData[] = $totalSegmentArray;
}
$totals->setItems($items);
$totals->setTotalSegments($totalSegmentsData);
diff --git a/app/code/Magento/Checkout/Model/ShippingInformationManagement.php b/app/code/Magento/Checkout/Model/ShippingInformationManagement.php
index 338a8e053e344..2858cc787163d 100644
--- a/app/code/Magento/Checkout/Model/ShippingInformationManagement.php
+++ b/app/code/Magento/Checkout/Model/ShippingInformationManagement.php
@@ -62,6 +62,11 @@ class ShippingInformationManagement implements \Magento\Checkout\Api\ShippingInf
*/
protected $scopeConfig;
+ /**
+ * @var \Magento\Quote\Model\Quote\TotalsCollector
+ */
+ protected $totalsCollector;
+
/**
* @param \Magento\Quote\Api\PaymentMethodManagementInterface $paymentMethodManagement
* @param \Magento\Checkout\Model\PaymentDetailsFactory $paymentDetailsFactory
@@ -71,6 +76,7 @@ class ShippingInformationManagement implements \Magento\Checkout\Api\ShippingInf
* @param Logger $logger
* @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
+ * @param \Magento\Quote\Model\Quote\TotalsCollector $totalsCollector
* @codeCoverageIgnore
*/
public function __construct(
@@ -81,7 +87,8 @@ public function __construct(
QuoteAddressValidator $addressValidator,
Logger $logger,
\Magento\Customer\Api\AddressRepositoryInterface $addressRepository,
- \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
+ \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
+ \Magento\Quote\Model\Quote\TotalsCollector $totalsCollector
) {
$this->paymentMethodManagement = $paymentMethodManagement;
$this->paymentDetailsFactory = $paymentDetailsFactory;
@@ -91,6 +98,7 @@ public function __construct(
$this->logger = $logger;
$this->addressRepository = $addressRepository;
$this->scopeConfig = $scopeConfig;
+ $this->totalsCollector = $totalsCollector;
}
/**
@@ -132,9 +140,7 @@ public function saveAddressInformation(
$address->setShippingMethod($carrierCode . '_' . $methodCode);
try {
- /** TODO: refactor this code. Eliminate save operation */
- $address->save();
- $address->collectTotals();
+ $this->totalsCollector->collectAddressTotals($quote, $address);
} catch (\Exception $e) {
$this->logger->critical($e);
throw new InputException(__('Unable to save address. Please, check input data.'));
diff --git a/app/code/Magento/Checkout/Model/Type/Onepage.php b/app/code/Magento/Checkout/Model/Type/Onepage.php
index e3c114f9c2209..775c0dcb63232 100644
--- a/app/code/Magento/Checkout/Model/Type/Onepage.php
+++ b/app/code/Magento/Checkout/Model/Type/Onepage.php
@@ -165,6 +165,11 @@ class Onepage
*/
protected $dataObjectHelper;
+ /**
+ * @var \Magento\Quote\Model\Quote\TotalsCollector
+ */
+ protected $totalsCollector;
+
/**
* @param \Magento\Framework\Event\ManagerInterface $eventManager
* @param \Magento\Checkout\Helper\Data $helper
@@ -192,6 +197,7 @@ class Onepage
* @param \Magento\Framework\Api\ExtensibleDataObjectConverter $extensibleDataObjectConverter
* @param \Magento\Quote\Model\QuoteManagement $quoteManagement
* @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
+ * @param \Magento\Quote\Model\Quote\TotalsCollector $totalsCollector
* @codeCoverageIgnore
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
@@ -221,7 +227,8 @@ public function __construct(
\Magento\Quote\Model\QuoteRepository $quoteRepository,
\Magento\Framework\Api\ExtensibleDataObjectConverter $extensibleDataObjectConverter,
\Magento\Quote\Model\QuoteManagement $quoteManagement,
- \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
+ \Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
+ \Magento\Quote\Model\Quote\TotalsCollector $totalsCollector
) {
$this->_eventManager = $eventManager;
$this->_customerUrl = $customerUrl;
@@ -249,6 +256,7 @@ public function __construct(
$this->extensibleDataObjectConverter = $extensibleDataObjectConverter;
$this->quoteManagement = $quoteManagement;
$this->dataObjectHelper = $dataObjectHelper;
+ $this->totalsCollector = $totalsCollector;
}
/**
@@ -490,8 +498,8 @@ public function saveBilling($data, $customerAddressId)
->setSameAsBilling(1)
->setSaveInAddressBook(0)
->setShippingMethod($shippingMethod)
- ->setCollectShippingRates(true)
- ->collectTotals();
+ ->setCollectShippingRates(true);
+ $this->totalsCollector->collectAddressTotals($this->getQuote(), $shipping);
if (!$this->isCheckoutMethodRegister()) {
$shipping->save();
@@ -690,7 +698,8 @@ public function saveShipping($data, $customerAddressId)
return ['error' => 1, 'message' => $validateRes];
}
- $address->collectTotals()->save();
+ $this->totalsCollector->collectAddressTotals($this->getQuote(), $address);
+ $address->save();
$this->getCheckout()->setStepData('shipping', 'complete', true)->setStepData('shipping_method', 'allow', true);
diff --git a/app/code/Magento/Checkout/Test/Unit/Block/Cart/AbstractCartTest.php b/app/code/Magento/Checkout/Test/Unit/Block/Cart/AbstractCartTest.php
index 56984a205d19c..c900c384d0d8d 100644
--- a/app/code/Magento/Checkout/Test/Unit/Block/Cart/AbstractCartTest.php
+++ b/app/code/Magento/Checkout/Test/Unit/Block/Cart/AbstractCartTest.php
@@ -109,4 +109,38 @@ public function testGetItemRendererThrowsExceptionForNonexistentRenderer()
$block->getItemRenderer('some-type');
}
+
+ /**
+ * @param array $expectedResult
+ * @param bool $isVirtual
+ * @dataProvider getTotalsCacheDataProvider
+ */
+ public function testGetTotalsCache($expectedResult, $isVirtual)
+ {
+ $totals = $isVirtual ? ['billing_totals'] : ['shipping_totals'];
+ $addressMock = $this->getMock('Magento\Quote\Model\Quote\Address', [], [], '', false);
+ $checkoutSessionMock = $this->getMock('Magento\Checkout\Model\Session', [], [], '', false);
+ $quoteMock = $this->getMock('Magento\Quote\Model\Quote', [], [], '', false);
+ $checkoutSessionMock->expects($this->once())->method('getQuote')->willReturn($quoteMock);
+
+ $quoteMock->expects($this->once())->method('isVirtual')->willReturn($isVirtual);
+ $quoteMock->expects($this->any())->method('getShippingAddress')->willReturn($addressMock);
+ $quoteMock->expects($this->any())->method('getBillingAddress')->willReturn($addressMock);
+ $addressMock->expects($this->once())->method('getTotals')->willReturn($totals);
+
+ /** @var \Magento\Checkout\Block\Cart\AbstractCart $model */
+ $model = $this->_objectManager->getObject(
+ 'Magento\Checkout\Block\Cart\AbstractCart',
+ ['checkoutSession' => $checkoutSessionMock]
+ );
+ $this->assertEquals($expectedResult, $model->getTotalsCache());
+ }
+
+ public function getTotalsCacheDataProvider()
+ {
+ return [
+ [['billing_totals'], true],
+ [['shipping_totals'], false]
+ ];
+ }
}
diff --git a/app/code/Magento/Checkout/Test/Unit/Model/ShippingInformationManagementTest.php b/app/code/Magento/Checkout/Test/Unit/Model/ShippingInformationManagementTest.php
index 8021921e17336..552acbb01097b 100644
--- a/app/code/Magento/Checkout/Test/Unit/Model/ShippingInformationManagementTest.php
+++ b/app/code/Magento/Checkout/Test/Unit/Model/ShippingInformationManagementTest.php
@@ -57,6 +57,11 @@ class ShippingInformationManagementTest extends \PHPUnit_Framework_TestCase
*/
protected $quoteMock;
+ /**
+ * @var \PHPUnit_Framework_MockObject_MockObject
+ */
+ protected $totalsCollectorMock;
+
/**
* @var \Magento\Checkout\Model\ShippingInformationManagement
*/
@@ -80,7 +85,8 @@ protected function setUp()
$this->loggerMock = $this->getMock('\Psr\Log\LoggerInterface');
$this->addressRepositoryMock = $this->getMock('\Magento\Customer\Api\AddressRepositoryInterface');
$this->scopeConfigMock = $this->getMock('\Magento\Framework\App\Config\ScopeConfigInterface');
-
+ $this->totalsCollectorMock =
+ $this->getMock('Magento\Quote\Model\Quote\TotalsCollector', [], [], '', false);
$this->model = $objectManager->getObject(
'\Magento\Checkout\Model\ShippingInformationManagement',
[
@@ -91,7 +97,8 @@ protected function setUp()
'addressValidator' => $this->addressValidatorMock,
'logger' => $this->loggerMock,
'addressRepository' => $this->addressRepositoryMock,
- 'scopeConfig' => $this->scopeConfigMock
+ 'scopeConfig' => $this->scopeConfigMock,
+ 'totalsCollector' => $this->totalsCollectorMock
]
);
@@ -109,7 +116,6 @@ protected function setUp()
'getCountryId',
'importCustomerAddressData',
'save',
- 'collectTotals',
'getShippingRateByCode',
'getShippingMethod'
],
@@ -294,8 +300,10 @@ public function testSaveAddressInformationThrowExceptionWhileAddressSaving()
->with(true)
->willReturnSelf();
$this->shippingAddressMock->expects($this->once())->method('getCountryId')->willReturn(1);
- $this->shippingAddressMock->expects($this->once())->method('save')->willThrowException($exception);
-
+ $this->totalsCollectorMock
+ ->expects($this->once())
+ ->method('collectAddressTotals')
+ ->willThrowException($exception);
$this->addressValidatorMock->expects($this->once())
->method('validate')
->with($this->shippingAddressMock)
@@ -386,8 +394,10 @@ public function testSaveAddressInformationIfCarrierCodeIsInvalid()
->with(true)
->willReturnSelf();
$this->shippingAddressMock->expects($this->once())->method('getCountryId')->willReturn(1);
- $this->shippingAddressMock->expects($this->once())->method('save')->willReturnSelf();
- $this->shippingAddressMock->expects($this->once())->method('collectTotals')->willReturnSelf();
+ $this->totalsCollectorMock
+ ->expects($this->once())
+ ->method('collectAddressTotals')
+ ->with($this->quoteMock, $this->shippingAddressMock);
$this->shippingAddressMock->expects($this->once())->method('getShippingMethod')->willReturn($shippingMethod);
$this->shippingAddressMock->expects($this->once())
->method('getShippingRateByCode')
@@ -464,8 +474,10 @@ public function testSaveAddressInformationIfMinimumAmountIsNotValid()
->with(true)
->willReturnSelf();
$this->shippingAddressMock->expects($this->once())->method('getCountryId')->willReturn(1);
- $this->shippingAddressMock->expects($this->once())->method('save')->willReturnSelf();
- $this->shippingAddressMock->expects($this->once())->method('collectTotals')->willReturnSelf();
+ $this->totalsCollectorMock
+ ->expects($this->once())
+ ->method('collectAddressTotals')
+ ->with($this->quoteMock, $this->shippingAddressMock);
$this->shippingAddressMock->expects($this->once())->method('getShippingMethod')->willReturn($shippingMethod);
$this->shippingAddressMock->expects($this->once())
->method('getShippingRateByCode')
@@ -506,7 +518,6 @@ public function testSaveAddressInformationIfCanNotSaveQuote()
->method('save')
->with($this->quoteMock)
->willThrowException($exception);
-
$addressInformationMock = $this->getMock('\Magento\Checkout\Api\Data\ShippingInformationInterface');
$addressInformationMock->expects($this->once())
->method('getShippingAddress')
@@ -550,8 +561,11 @@ public function testSaveAddressInformationIfCanNotSaveQuote()
->with(true)
->willReturnSelf();
$this->shippingAddressMock->expects($this->once())->method('getCountryId')->willReturn(1);
- $this->shippingAddressMock->expects($this->exactly(2))->method('save')->willReturnSelf();
- $this->shippingAddressMock->expects($this->once())->method('collectTotals')->willReturnSelf();
+ $this->shippingAddressMock->expects($this->once())->method('save')->willReturnSelf();
+ $this->totalsCollectorMock
+ ->expects($this->once())
+ ->method('collectAddressTotals')
+ ->with($this->quoteMock, $this->shippingAddressMock);
$this->shippingAddressMock->expects($this->once())->method('getShippingMethod')->willReturn($shippingMethod);
$this->shippingAddressMock->expects($this->once())
->method('getShippingRateByCode')
@@ -626,8 +640,10 @@ public function testSaveAddressInformation()
->with(true)
->willReturnSelf();
$this->shippingAddressMock->expects($this->once())->method('getCountryId')->willReturn(1);
- $this->shippingAddressMock->expects($this->exactly(2))->method('save')->willReturnSelf();
- $this->shippingAddressMock->expects($this->once())->method('collectTotals')->willReturnSelf();
+ $this->totalsCollectorMock
+ ->expects($this->once())
+ ->method('collectAddressTotals')
+ ->with($this->quoteMock, $this->shippingAddressMock);
$this->shippingAddressMock->expects($this->once())->method('getShippingMethod')->willReturn($shippingMethod);
$this->shippingAddressMock->expects($this->once())
->method('getShippingRateByCode')
diff --git a/app/code/Magento/Checkout/Test/Unit/Model/Type/OnepageTest.php b/app/code/Magento/Checkout/Test/Unit/Model/Type/OnepageTest.php
index 52d80ff6c3494..0c319e19e1f37 100644
--- a/app/code/Magento/Checkout/Test/Unit/Model/Type/OnepageTest.php
+++ b/app/code/Magento/Checkout/Test/Unit/Model/Type/OnepageTest.php
@@ -98,6 +98,9 @@ class OnepageTest extends \PHPUnit_Framework_TestCase
/** @var \Magento\Framework\Api\ExtensibleDataObjectConverter|\PHPUnit_Framework_MockObject_MockObject */
protected $extensibleDataObjectConverterMock;
+ /** @var \PHPUnit_Framework_MockObject_MockObject */
+ protected $totalsCollectorMock;
+
/**
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
@@ -195,6 +198,7 @@ protected function setUp()
->method('toFlatArray')
->will($this->returnValue([]));
$this->objectManagerHelper = new ObjectManagerHelper($this);
+ $this->totalsCollectorMock = $this->getMock('Magento\Quote\Model\Quote\TotalsCollector', [], [], '', false);
$this->onepage = $this->objectManagerHelper->getObject(
'Magento\Checkout\Model\Type\Onepage',
[
@@ -222,7 +226,8 @@ protected function setUp()
'customerRepository' => $this->customerRepositoryMock,
'extensibleDataObjectConverter' => $this->extensibleDataObjectConverterMock,
'quoteRepository' => $this->quoteRepositoryMock,
- 'quoteManagement' => $this->quoteManagementMock
+ 'quoteManagement' => $this->quoteManagementMock,
+ 'totalsCollector' => $this->totalsCollectorMock
]
);
}
@@ -492,8 +497,17 @@ public function testSaveBilling(
->method('setCollectShippingRates')
->will($this->returnSelf());
- $shippingAddressMock->expects($useForShipping ? $this->once() : $this->never())
- ->method('collectTotals');
+ if ($useForShipping === \Magento\Checkout\Model\Type\Onepage::USE_FOR_SHIPPING) {
+ $this->totalsCollectorMock
+ ->expects($this->once())
+ ->method('collectAddressTotals')
+ ->with($quoteMock, $shippingAddressMock);
+ } else {
+ $this->totalsCollectorMock
+ ->expects($this->never())
+ ->method('collectAddressTotals')
+ ->with($quoteMock, $shippingAddressMock);
+ }
$quoteMock->expects($this->any())->method('setPasswordHash')->with($passwordHash);
$quoteMock->expects($this->any())->method('getCheckoutMethod')->will($this->returnValue($checkoutMethod));
diff --git a/app/code/Magento/Msrp/Block/Total.php b/app/code/Magento/Msrp/Block/Total.php
index d2775a0ccde6c..9e2ba73829e41 100644
--- a/app/code/Magento/Msrp/Block/Total.php
+++ b/app/code/Magento/Msrp/Block/Total.php
@@ -10,21 +10,31 @@
*/
class Total extends \Magento\Framework\View\Element\Template
{
- /** @var \Magento\Msrp\Model\Config */
+ /**
+ * @var \Magento\Msrp\Model\Config
+ */
protected $config;
+ /**
+ * @var \Magento\Msrp\Model\Quote\Msrp
+ */
+ protected $msrp;
+
/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Msrp\Model\Config $config
+ * @param \Magento\Msrp\Model\Quote\Msrp $msrp
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Msrp\Model\Config $config,
+ \Magento\Msrp\Model\Quote\Msrp $msrp,
array $data = []
) {
parent::__construct($context, $data);
$this->config = $config;
+ $this->msrp = $msrp;
}
/**
@@ -35,10 +45,10 @@ protected function _toHtml()
/** @var \Magento\Checkout\Block\Cart\AbstractCart $originalBlock */
$originalBlock = $this->getLayout()->getBlock($this->getOriginalBlockName());
$quote = $originalBlock->getQuote();
- if (!$quote->hasCanApplyMsrp() && $this->config->isEnabled()) {
+ if (!$this->msrp->getCanApplyMsrp($quote->getId()) && $this->config->isEnabled()) {
$quote->collectTotals();
}
- if ($quote->getCanApplyMsrp()) {
+ if ($this->msrp->getCanApplyMsrp($quote->getId())) {
$originalBlock->setTemplate('');
return parent::_toHtml();
} else {
diff --git a/app/code/Magento/Msrp/Model/Quote/Address/CanApplyMsrp.php b/app/code/Magento/Msrp/Model/Quote/Address/CanApplyMsrp.php
new file mode 100644
index 0000000000000..07ac9c0144f8b
--- /dev/null
+++ b/app/code/Magento/Msrp/Model/Quote/Address/CanApplyMsrp.php
@@ -0,0 +1,41 @@
+msrpHelper = $msrpHelper;
+ }
+
+ /**
+ * @param \Magento\Quote\Model\Quote\Address $address
+ * @return bool
+ */
+ public function isCanApplyMsrp($address)
+ {
+ $canApplyMsrp = false;
+ foreach ($address->getAllItems() as $item) {
+ if (!$item->getParentItemId()
+ && $this->msrpHelper->isShowBeforeOrderConfirm($item->getProductId())
+ && $this->msrpHelper->isMinimalPriceLessMsrp($item->getProductId())
+ ) {
+ $canApplyMsrp = true;
+ break;
+ }
+ }
+ return $canApplyMsrp;
+ }
+}
diff --git a/app/code/Magento/Msrp/Model/Quote/Address/Total.php b/app/code/Magento/Msrp/Model/Quote/Address/Total.php
deleted file mode 100644
index 9af7b08e1174e..0000000000000
--- a/app/code/Magento/Msrp/Model/Quote/Address/Total.php
+++ /dev/null
@@ -1,57 +0,0 @@
-msrpData = $msrpData;
- }
-
- /**
- * Collect information about MSRP price enabled
- *
- * @param \Magento\Quote\Model\Quote\Address $address
- * @return $this
- * @api
- */
- public function collect(\Magento\Quote\Model\Quote\Address $address)
- {
- parent::collect($address);
-
- $items = $this->_getAddressItems($address);
- if (!count($items)) {
- return $this;
- }
-
- $canApplyMsrp = false;
- foreach ($items as $item) {
- if (!$item->getParentItemId()
- && $this->msrpData->isShowBeforeOrderConfirm($item->getProductId())
- && $this->msrpData->isMinimalPriceLessMsrp($item->getProductId())
- ) {
- $canApplyMsrp = true;
- break;
- }
- }
-
- $address->setCanApplyMsrp($canApplyMsrp);
-
- return $this;
- }
-}
diff --git a/app/code/Magento/Msrp/Model/Quote/Msrp.php b/app/code/Magento/Msrp/Model/Quote/Msrp.php
new file mode 100644
index 0000000000000..3e1cf94baae92
--- /dev/null
+++ b/app/code/Magento/Msrp/Model/Quote/Msrp.php
@@ -0,0 +1,41 @@
+canApplyMsrpData[$quoteId] = (bool)$canApply;
+ return $this;
+ }
+
+ /**
+ * @param int $quoteId
+ * @return bool
+ * @SuppressWarnings(PHPMD.BooleanGetMethodName)
+ */
+ public function getCanApplyMsrp($quoteId)
+ {
+ if (isset($this->canApplyMsrpData[$quoteId])) {
+ return (bool)$this->canApplyMsrpData[$quoteId];
+ }
+ return false;
+ }
+}
diff --git a/app/code/Magento/Msrp/Observer/Frontend/Quote/SetCanApplyMsrpObserver.php b/app/code/Magento/Msrp/Observer/Frontend/Quote/SetCanApplyMsrpObserver.php
index c91b321d05160..0e1bf4ea27180 100644
--- a/app/code/Magento/Msrp/Observer/Frontend/Quote/SetCanApplyMsrpObserver.php
+++ b/app/code/Magento/Msrp/Observer/Frontend/Quote/SetCanApplyMsrpObserver.php
@@ -5,7 +5,6 @@
*/
namespace Magento\Msrp\Observer\Frontend\Quote;
-use Magento\Msrp\Model\Config;
use Magento\Framework\Event\ObserverInterface;
/**
@@ -13,15 +12,34 @@
*/
class SetCanApplyMsrpObserver implements ObserverInterface
{
- /** @var Config */
+ /**
+ * @var \Magento\Msrp\Model\Config
+ */
protected $config;
/**
- * @param Config $config
+ * @var \Magento\Msrp\Model\Quote\Address\CanApplyMsrp
*/
- public function __construct(Config $config)
- {
+ protected $canApplyMsrp;
+
+ /**
+ * @var \Magento\Msrp\Model\Quote\Msrp
+ */
+ protected $msrp;
+
+ /**
+ * @param \Magento\Msrp\Model\Config $config
+ * @param \Magento\Msrp\Model\Quote\Address\CanApplyMsrp $canApplyMsrp
+ * @param \Magento\Msrp\Model\Quote\Msrp $msrp
+ */
+ public function __construct(
+ \Magento\Msrp\Model\Config $config,
+ \Magento\Msrp\Model\Quote\Address\CanApplyMsrp $canApplyMsrp,
+ \Magento\Msrp\Model\Quote\Msrp $msrp
+ ) {
$this->config = $config;
+ $this->canApplyMsrp = $canApplyMsrp;
+ $this->msrp = $msrp;
}
/**
@@ -38,13 +56,12 @@ public function execute(\Magento\Framework\Event\Observer $observer)
$canApplyMsrp = false;
if ($this->config->isEnabled()) {
foreach ($quote->getAllAddresses() as $address) {
- if ($address->getCanApplyMsrp()) {
+ if ($this->canApplyMsrp->isCanApplyMsrp($address)) {
$canApplyMsrp = true;
break;
}
}
}
-
- $quote->setCanApplyMsrp($canApplyMsrp);
+ $this->msrp->setCanApplyMsrp($quote->getId(), $canApplyMsrp);
}
}
diff --git a/app/code/Magento/Msrp/Test/Unit/Observer/Frontend/Quote/SetCanApplyMsrpObserverTest.php b/app/code/Magento/Msrp/Test/Unit/Observer/Frontend/Quote/SetCanApplyMsrpObserverTest.php
index c94a8303b0436..37f016065901a 100644
--- a/app/code/Magento/Msrp/Test/Unit/Observer/Frontend/Quote/SetCanApplyMsrpObserverTest.php
+++ b/app/code/Magento/Msrp/Test/Unit/Observer/Frontend/Quote/SetCanApplyMsrpObserverTest.php
@@ -6,7 +6,6 @@
namespace Magento\Msrp\Test\Unit\Observer\Frontend\Quote;
use Magento\Quote\Model\Quote\Address;
-use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
/**
* Tests Magento\Msrp\Model\Observer\Frontend\Quote\SetCanApplyMsrp
@@ -23,72 +22,77 @@ class SetCanApplyMsrpObserverTest extends \PHPUnit_Framework_TestCase
*/
protected $configMock;
+ /** @var \PHPUnit_Framework_MockObject_MockObject */
+ protected $canApplyMsrpMock;
+
+ /** @var \PHPUnit_Framework_MockObject_MockObject */
+ protected $msrpMock;
+
protected function setUp()
{
- $this->configMock = $this->getMockBuilder('Magento\Msrp\Model\Config')
- ->disableOriginalConstructor()
- ->getMock();
- $this->observer = (new ObjectManager($this))->getObject(
- 'Magento\Msrp\Observer\Frontend\Quote\SetCanApplyMsrpObserver',
- ['config' => $this->configMock]
+ $this->configMock = $this->getMock('\Magento\Msrp\Model\Config', [], [], '', false);
+ $this->canApplyMsrpMock = $this->getMock('\Magento\Msrp\Model\Quote\Address\CanApplyMsrp', [], [], '', false);
+ $this->msrpMock = $this->getMock('\Magento\Msrp\Model\Quote\Msrp', [], [], '', false);
+
+ $this->observer = new \Magento\Msrp\Observer\Frontend\Quote\SetCanApplyMsrpObserver(
+ $this->configMock,
+ $this->canApplyMsrpMock,
+ $this->msrpMock
);
}
- /**
- * @param bool $isMsrpEnabled
- * @param bool $canApplyMsrp
- * @dataProvider setQuoteCanApplyMsrpDataProvider
- */
- public function testSetQuoteCanApplyMsrp($isMsrpEnabled, $canApplyMsrp)
+ public function testSetQuoteCanApplyMsrpIfMsrpCanApply()
{
- $eventMock = $this->getMockBuilder('Magento\Framework\Event')
- ->disableOriginalConstructor()
- ->setMethods(['getQuote'])
- ->getMock();
- $quoteMock = $this->getMockBuilder('Magento\Quote\Model\Quote')
- ->disableOriginalConstructor()
- ->setMethods(['__wakeup', 'setCanApplyMsrp', 'getAllAddresses'])
- ->getMock();
- $observerMock = $this->getMockBuilder('Magento\Framework\Event\Observer')
- ->disableOriginalConstructor()
- ->getMock();
- $observerMock->expects($this->once())
- ->method('getEvent')
- ->will($this->returnValue($eventMock));
- $eventMock->expects($this->once())
- ->method('getQuote')
- ->will($this->returnValue($quoteMock));
- $this->configMock->expects($this->once())
- ->method('isEnabled')
- ->will($this->returnValue($isMsrpEnabled));
- $quoteMock->expects($this->once())
- ->method('setCanApplyMsrp')
- ->with($canApplyMsrp);
- $addressMock1 = $this->getMockBuilder('Magento\Customer\Model\Address\AbstractAddress')
- ->disableOriginalConstructor()
- ->setMethods(['__wakeup'])
- ->getMockForAbstractClass();
- $addressMock1->setCanApplyMsrp($canApplyMsrp);
- $addressMock2 = $this->getMockBuilder('Magento\Customer\Model\Address\AbstractAddress')
- ->disableOriginalConstructor()
- ->setMethods(['__wakeup'])
- ->getMockForAbstractClass();
- $addressMock2->setCanApplyMsrp(false);
- $quoteMock->expects($this->any())
- ->method('getAllAddresses')
- ->will($this->returnValue([$addressMock1, $addressMock2]));
+ $quoteId = 100;
+ $eventMock = $this->getMock('\Magento\Framework\Event', ['getQuote'], [], '', false);
+ $quoteMock = $this->getMock('\Magento\Quote\Model\Quote', ['getAllAddresses', 'getId'], [], '', false);
+ $observerMock = $this->getMock('\Magento\Framework\Event\Observer', [], [], '', false);
+
+ $observerMock->expects($this->once())->method('getEvent')->willReturn($eventMock);
+ $eventMock->expects($this->once())->method('getQuote')->willReturn($quoteMock);
+ $this->configMock->expects($this->once())->method('isEnabled')->willReturn(true);
+ $this->msrpMock->expects($this->once())->method('setCanApplyMsrp')->with($quoteId, true);
+
+ $addressMock = $this->getMock('\Magento\Customer\Model\Address\AbstractAddress', ['__wakeup'], [], '', false);
+ $this->canApplyMsrpMock->expects($this->once())->method('isCanApplyMsrp')->willReturn(true);
+
+ $quoteMock->expects($this->once())->method('getAllAddresses')->willReturn([$addressMock]);
+ $quoteMock->expects($this->once())->method('getId')->willReturn($quoteId);
$this->observer->execute($observerMock);
}
- /**
- * @return array
- */
public function setQuoteCanApplyMsrpDataProvider()
{
- return [
- [false, false],
- [true, true],
- [true, false]
- ];
+ $quoteId = 100;
+ $eventMock = $this->getMock('\Magento\Framework\Event', ['getQuote'], [], '', false);
+ $quoteMock = $this->getMock('\Magento\Quote\Model\Quote', ['getAllAddresses', 'getId'], [], '', false);
+ $observerMock = $this->getMock('\Magento\Framework\Event\Observer', [], [], '', false);
+
+ $observerMock->expects($this->once())->method('getEvent')->willReturn($eventMock);
+ $eventMock->expects($this->once())->method('getQuote')->willReturn($quoteMock);
+ $this->configMock->expects($this->once())->method('isEnabled')->willReturn(true);
+ $this->msrpMock->expects($this->once())->method('setCanApplyMsrp')->with($quoteId, false);
+
+ $addressMock = $this->getMock('\Magento\Customer\Model\Address\AbstractAddress', ['__wakeup'], [], '', false);
+ $this->canApplyMsrpMock->expects($this->once())->method('isCanApplyMsrp')->willReturn(false);
+
+ $quoteMock->expects($this->once())->method('getAllAddresses')->willReturn([$addressMock]);
+ $quoteMock->expects($this->once())->method('getId')->willReturn($quoteId);
+ $this->observer->execute($observerMock);
+ }
+
+ public function testSetQuoteCanApplyMsrpIfMsrpDisabled()
+ {
+ $quoteId = 100;
+ $eventMock = $this->getMock('\Magento\Framework\Event', ['getQuote'], [], '', false);
+ $quoteMock = $this->getMock('\Magento\Quote\Model\Quote', ['getAllAddresses', 'getId'], [], '', false);
+ $observerMock = $this->getMock('\Magento\Framework\Event\Observer', [], [], '', false);
+
+ $observerMock->expects($this->once())->method('getEvent')->willReturn($eventMock);
+ $eventMock->expects($this->once())->method('getQuote')->willReturn($quoteMock);
+ $this->configMock->expects($this->once())->method('isEnabled')->willReturn(false);
+ $this->msrpMock->expects($this->once())->method('setCanApplyMsrp')->with($quoteId, false);
+ $quoteMock->expects($this->once())->method('getId')->willReturn($quoteId);
+ $this->observer->execute($observerMock);
}
}
diff --git a/app/code/Magento/Msrp/composer.json b/app/code/Magento/Msrp/composer.json
index b5d603e700e32..efc09c43975ec 100644
--- a/app/code/Magento/Msrp/composer.json
+++ b/app/code/Magento/Msrp/composer.json
@@ -9,7 +9,6 @@
"magento/module-eav": "1.0.0-beta",
"magento/module-grouped-product": "1.0.0-beta",
"magento/module-tax": "1.0.0-beta",
- "magento/module-quote": "1.0.0-beta",
"magento/framework": "1.0.0-beta",
"magento/magento-composer-installer": "*"
},
diff --git a/app/code/Magento/Msrp/etc/sales.xml b/app/code/Magento/Msrp/etc/sales.xml
deleted file mode 100644
index ca96eacc284ff..0000000000000
--- a/app/code/Magento/Msrp/etc/sales.xml
+++ /dev/null
@@ -1,14 +0,0 @@
-
-
-