Skip to content

Commit

Permalink
ENGCOM-7009: Cover CartTotalRepositoryPlugin by unit test and correct…
Browse files Browse the repository at this point in the history
… docblock #26619

 - Merge Pull Request #26619 from mrtuvn/magento2:coverage-test-multishipping-cart-totals-plugin
 - Merged commits:
   1. e2c403f
   2. ad8fea6
   3. 083aed1
   4. 5611339
   5. 386898a
   6. 4ba9113
   7. d61764f
  • Loading branch information
magento-engcom-team committed Mar 2, 2020
2 parents 690500f + d61764f commit 2cbe34f
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ public function __construct(
}

/**
* Overwrite the CartTotalRepository quoteTotal and update the shipping price
* Check multishipping update shipping price after get cart total
*
* @param CartTotalRepository $subject
* @param Totals $quoteTotals
* @param String $cartId
* @return Totals
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @param CartTotalRepository $subject
* @param Totals $quoteTotals
* @param int $cartId
* @return Totals
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterGet(
CartTotalRepository $subject,
Totals $quoteTotals,
String $cartId
) {
$cartId
) : Totals {
$quote = $this->quoteRepository->getActive($cartId);
if ($quote->getIsMultiShipping()) {
$shippingMethod = $quote->getShippingAddress()->getShippingMethod();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,76 +6,145 @@

namespace Magento\Multishipping\Test\Unit\Model\Cart;

use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Model\Cart\CartTotalRepository;
use Magento\Quote\Model\Cart\Totals as QuoteTotals;
use Magento\Quote\Model\Quote\Address as QuoteAddress;
use Magento\Quote\Model\Quote\Address\Rate as QuoteAddressRate;
use Magento\Multishipping\Model\Cart\CartTotalRepositoryPlugin;
use Magento\Store\Model\Store;
use PHPUnit\Framework\MockObject\MockObject;

class CartTotalRepositoryPluginTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Multishipping\Model\Cart\CartTotalRepositoryPlugin
* Stub cart id
*/
private const STUB_CART_ID = 10;

/**
* Stub shipping method
*/
private const STUB_SHIPPING_METHOD = 'flatrate_flatrate';

/**
* Stub shipping price
*/
private const STUB_SHIPPING_PRICE = '10.00';

/**
* @var CartTotalRepositoryPlugin
*/
private $modelRepository;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
* @var CartTotalRepository|MockObject
*/
private $quoteTotalRepositoryMock;

/**
* @var CartRepositoryInterface|MockObject
*/
private $quoteRepositoryMock;

protected function setUp()
{
$this->quoteRepositoryMock = $this->createMock(\Magento\Quote\Api\CartRepositoryInterface::class);
$this->modelRepository = new \Magento\Multishipping\Model\Cart\CartTotalRepositoryPlugin(
$this->quoteRepositoryMock
);
}
/**
* @var QuoteTotals|MockObject
*/
private $quoteTotalsMock;

/**
* Test quotTotal from cartRepository after get($cartId) function is called
* @var QuoteAddress|MockObject
*/
public function testAfterGet()
private $shippingAddressMock;

/**
* @var QuoteAddressRate|MockObject
*/
private $shippingRateMock;

/**
* @var Store|MockObject
*/
private $storeMock;

protected function setUp()
{
$cartId = "10";
$shippingMethod = 'flatrate_flatrate';
$shippingPrice = '10.00';
$quoteMock = $this->createPartialMock(
\Magento\Quote\Model\Cart\Totals::class,
$objectManager = new ObjectManager($this);
$this->quoteTotalsMock = $this->createPartialMock(
QuoteTotals::class,
[
'getStore',
'getShippingAddress',
'getIsMultiShipping'
'getStore',
'getShippingAddress',
'getIsMultiShipping'
]
);
$this->quoteRepositoryMock->expects($this->once())->method('getActive')->with($cartId)->willReturn($quoteMock);
$quoteMock->expects($this->once())->method('getIsMultiShipping')->willReturn(true);
$shippingAddressMock = $this->createPartialMock(
\Magento\Quote\Model\Quote\Address::class,
$this->shippingAddressMock = $this->createPartialMock(
QuoteAddress::class,
[
'getShippingMethod',
'getShippingRateByCode',
'getShippingAmount'
'getShippingMethod',
'getShippingRateByCode',
'getShippingAmount'
]
);
$quoteMock->expects($this->any())->method('getShippingAddress')->willReturn($shippingAddressMock);

$shippingAddressMock->expects($this->once())->method('getShippingMethod')->willReturn($shippingMethod);
$shippingAddressMock->expects($this->any())->method('getShippingAmount')->willReturn($shippingPrice);
$shippingRateMock = $this->createPartialMock(
\Magento\Quote\Model\Quote\Address\Rate::class,
$this->shippingRateMock = $this->createPartialMock(
QuoteAddressRate::class,
[
'getPrice'
'getPrice'
]
);
$shippingAddressMock->expects($this->once())->method('getShippingRateByCode')->willReturn($shippingRateMock);

$shippingRateMock->expects($this->once())->method('getPrice')->willReturn($shippingPrice);

$storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
$this->storeMock = $this->getMockBuilder(Store::class)
->disableOriginalConstructor()
->getMock();
$quoteMock->expects($this->any())->method('getStore')->willReturn($storeMock);
$storeMock->expects($this->any())->method('getBaseCurrency')->willReturnSelf();
$this->quoteRepositoryMock = $this->createMock(CartRepositoryInterface::class);
$this->quoteTotalRepositoryMock = $this->createMock(CartTotalRepository::class);
$this->modelRepository = $objectManager->getObject(CartTotalRepositoryPlugin::class, [
'quoteRepository' => $this->quoteRepositoryMock
]);
}

/**
* Test quoteTotal from cartRepository after get($cartId) function is called
*/
public function testAfterGetQuoteTotalAddedShippingPrice()
{
$this->quoteRepositoryMock->expects($this->once())
->method('getActive')
->with(self::STUB_CART_ID)
->willReturn($this->quoteTotalsMock);
$this->quoteTotalsMock->expects($this->once())
->method('getIsMultiShipping')
->willReturn(true);
$this->quoteTotalsMock->expects($this->any())
->method('getShippingAddress')
->willReturn($this->shippingAddressMock);

$this->shippingAddressMock->expects($this->once())
->method('getShippingMethod')
->willReturn(self::STUB_SHIPPING_METHOD);
$this->shippingAddressMock->expects($this->any())
->method('getShippingAmount')
->willReturn(self::STUB_SHIPPING_PRICE);

$this->shippingAddressMock->expects($this->once())
->method('getShippingRateByCode')
->willReturn($this->shippingRateMock);

$this->shippingRateMock->expects($this->once())
->method('getPrice')
->willReturn(self::STUB_SHIPPING_PRICE);

$this->quoteTotalsMock->expects($this->any())
->method('getStore')
->willReturn($this->storeMock);
$this->storeMock->expects($this->any())
->method('getBaseCurrency')
->willReturnSelf();

$this->modelRepository->afterGet(
$this->createMock(\Magento\Quote\Model\Cart\CartTotalRepository::class),
$quoteMock,
$cartId
$this->quoteTotalRepositoryMock,
$this->quoteTotalsMock,
self::STUB_CART_ID
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<arguments>
<argument name="attribute_data" xsi:type="object">Magento\Customer\Block\DataProviders\AddressAttributeData</argument>
<argument name="post_code_config" xsi:type="object">Magento\Customer\Block\DataProviders\PostCodesPatternsAttributeData</argument>
<argument name="view_model" xsi:type="object">Magento\Customer\ViewModel\Address</argument>
</arguments>
</block>
</referenceContainer>
Expand Down

0 comments on commit 2cbe34f

Please sign in to comment.