Skip to content

Commit ad8fea6

Browse files
committed
Cover CartTotalRepositoryPlugin by unit test and correct docblock
Fix static tests Update commit with meaningful test name Remove unnecessary assignment stub in test code
1 parent e2c403f commit ad8fea6

File tree

2 files changed

+120
-51
lines changed

2 files changed

+120
-51
lines changed

app/code/Magento/Multishipping/Model/Cart/CartTotalRepositoryPlugin.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,21 @@ public function __construct(
3333
}
3434

3535
/**
36-
* Overwrite the CartTotalRepository quoteTotal and update the shipping price
36+
* Check multishipping update shipping price after get cart total
3737
*
38-
* @param CartTotalRepository $subject
39-
* @param Totals $quoteTotals
40-
* @param String $cartId
41-
* @return Totals
42-
* @throws \Magento\Framework\Exception\NoSuchEntityException
38+
* @param CartTotalRepository $subject
39+
* @param Totals $quoteTotals
40+
* @param int $cartId
41+
* @return Totals
42+
* @throws \Magento\Framework\Exception\NoSuchEntityException
4343
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
4444
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
4545
*/
4646
public function afterGet(
4747
CartTotalRepository $subject,
4848
Totals $quoteTotals,
49-
String $cartId
50-
) {
49+
$cartId
50+
) : Totals {
5151
$quote = $this->quoteRepository->getActive($cartId);
5252
if ($quote->getIsMultiShipping()) {
5353
$shippingMethod = $quote->getShippingAddress()->getShippingMethod();

app/code/Magento/Multishipping/Test/Unit/Model/Cart/CartTotalRepositoryPluginTest.php

Lines changed: 112 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,76 +6,145 @@
66

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

9+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
10+
use Magento\Quote\Api\CartRepositoryInterface;
11+
use Magento\Quote\Model\Cart\CartTotalRepository;
12+
use Magento\Quote\Model\Cart\Totals as QuoteTotals;
13+
use Magento\Quote\Model\Quote\Address as QuoteAddress;
14+
use Magento\Quote\Model\Quote\Address\Rate as QuoteAddressRate;
15+
use Magento\Multishipping\Model\Cart\CartTotalRepositoryPlugin;
16+
use Magento\Store\Model\Store;
17+
use PHPUnit\Framework\MockObject\MockObject;
18+
919
class CartTotalRepositoryPluginTest extends \PHPUnit\Framework\TestCase
1020
{
1121
/**
12-
* @var \Magento\Multishipping\Model\Cart\CartTotalRepositoryPlugin
22+
* Stub cart id
23+
*/
24+
private const STUB_CART_ID = 10;
25+
26+
/**
27+
* Stub shipping method
28+
*/
29+
private const STUB_SHIPPING_METHOD = 'flatrate_flatrate';
30+
31+
/**
32+
* Stub shipping price
33+
*/
34+
private const STUB_SHIPPING_PRICE = '10.00';
35+
36+
/**
37+
* @var CartTotalRepositoryPlugin
1338
*/
1439
private $modelRepository;
1540

1641
/**
17-
* @var \PHPUnit_Framework_MockObject_MockObject
42+
* @var CartTotalRepository|MockObject
43+
*/
44+
private $quoteTotalRepositoryMock;
45+
46+
/**
47+
* @var CartRepositoryInterface|MockObject
1848
*/
1949
private $quoteRepositoryMock;
2050

21-
protected function setUp()
22-
{
23-
$this->quoteRepositoryMock = $this->createMock(\Magento\Quote\Api\CartRepositoryInterface::class);
24-
$this->modelRepository = new \Magento\Multishipping\Model\Cart\CartTotalRepositoryPlugin(
25-
$this->quoteRepositoryMock
26-
);
27-
}
51+
/**
52+
* @var QuoteTotals|MockObject
53+
*/
54+
private $quoteTotalsMock;
2855

2956
/**
30-
* Test quotTotal from cartRepository after get($cartId) function is called
57+
* @var QuoteAddress|MockObject
3158
*/
32-
public function testAfterGet()
59+
private $shippingAddressMock;
60+
61+
/**
62+
* @var QuoteAddressRate|MockObject
63+
*/
64+
private $shippingRateMock;
65+
66+
/**
67+
* @var Store|MockObject
68+
*/
69+
private $storeMock;
70+
71+
protected function setUp()
3372
{
34-
$cartId = "10";
35-
$shippingMethod = 'flatrate_flatrate';
36-
$shippingPrice = '10.00';
37-
$quoteMock = $this->createPartialMock(
38-
\Magento\Quote\Model\Cart\Totals::class,
73+
$objectManager = new ObjectManager($this);
74+
$this->quoteTotalsMock = $this->createPartialMock(
75+
QuoteTotals::class,
3976
[
40-
'getStore',
41-
'getShippingAddress',
42-
'getIsMultiShipping'
77+
'getStore',
78+
'getShippingAddress',
79+
'getIsMultiShipping'
4380
]
4481
);
45-
$this->quoteRepositoryMock->expects($this->once())->method('getActive')->with($cartId)->willReturn($quoteMock);
46-
$quoteMock->expects($this->once())->method('getIsMultiShipping')->willReturn(true);
47-
$shippingAddressMock = $this->createPartialMock(
48-
\Magento\Quote\Model\Quote\Address::class,
82+
$this->shippingAddressMock = $this->createPartialMock(
83+
QuoteAddress::class,
4984
[
50-
'getShippingMethod',
51-
'getShippingRateByCode',
52-
'getShippingAmount'
85+
'getShippingMethod',
86+
'getShippingRateByCode',
87+
'getShippingAmount'
5388
]
5489
);
55-
$quoteMock->expects($this->any())->method('getShippingAddress')->willReturn($shippingAddressMock);
56-
57-
$shippingAddressMock->expects($this->once())->method('getShippingMethod')->willReturn($shippingMethod);
58-
$shippingAddressMock->expects($this->any())->method('getShippingAmount')->willReturn($shippingPrice);
59-
$shippingRateMock = $this->createPartialMock(
60-
\Magento\Quote\Model\Quote\Address\Rate::class,
90+
$this->shippingRateMock = $this->createPartialMock(
91+
QuoteAddressRate::class,
6192
[
62-
'getPrice'
93+
'getPrice'
6394
]
6495
);
65-
$shippingAddressMock->expects($this->once())->method('getShippingRateByCode')->willReturn($shippingRateMock);
66-
67-
$shippingRateMock->expects($this->once())->method('getPrice')->willReturn($shippingPrice);
68-
69-
$storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
96+
$this->storeMock = $this->getMockBuilder(Store::class)
7097
->disableOriginalConstructor()
7198
->getMock();
72-
$quoteMock->expects($this->any())->method('getStore')->willReturn($storeMock);
73-
$storeMock->expects($this->any())->method('getBaseCurrency')->willReturnSelf();
99+
$this->quoteRepositoryMock = $this->createMock(CartRepositoryInterface::class);
100+
$this->quoteTotalRepositoryMock = $this->createMock(CartTotalRepository::class);
101+
$this->modelRepository = $objectManager->getObject(CartTotalRepositoryPlugin::class, [
102+
'quoteRepository' => $this->quoteRepositoryMock
103+
]);
104+
}
105+
106+
/**
107+
* Test quoteTotal from cartRepository after get($cartId) function is called
108+
*/
109+
public function testAfterGetQuoteTotalAddedShippingPrice()
110+
{
111+
$this->quoteRepositoryMock->expects($this->once())
112+
->method('getActive')
113+
->with(self::STUB_CART_ID)
114+
->willReturn($this->quoteTotalsMock);
115+
$this->quoteTotalsMock->expects($this->once())
116+
->method('getIsMultiShipping')
117+
->willReturn(true);
118+
$this->quoteTotalsMock->expects($this->any())
119+
->method('getShippingAddress')
120+
->willReturn($this->shippingAddressMock);
121+
122+
$this->shippingAddressMock->expects($this->once())
123+
->method('getShippingMethod')
124+
->willReturn(self::STUB_SHIPPING_METHOD);
125+
$this->shippingAddressMock->expects($this->any())
126+
->method('getShippingAmount')
127+
->willReturn(self::STUB_SHIPPING_PRICE);
128+
129+
$this->shippingAddressMock->expects($this->once())
130+
->method('getShippingRateByCode')
131+
->willReturn($this->shippingRateMock);
132+
133+
$this->shippingRateMock->expects($this->once())
134+
->method('getPrice')
135+
->willReturn(self::STUB_SHIPPING_PRICE);
136+
137+
$this->quoteTotalsMock->expects($this->any())
138+
->method('getStore')
139+
->willReturn($this->storeMock);
140+
$this->storeMock->expects($this->any())
141+
->method('getBaseCurrency')
142+
->willReturnSelf();
74143

75144
$this->modelRepository->afterGet(
76-
$this->createMock(\Magento\Quote\Model\Cart\CartTotalRepository::class),
77-
$quoteMock,
78-
$cartId
145+
$this->quoteTotalRepositoryMock,
146+
$this->quoteTotalsMock,
147+
self::STUB_CART_ID
79148
);
80149
}
81150
}

0 commit comments

Comments
 (0)