Skip to content

Commit 8ef9334

Browse files
authored
ENGCOM-7183: Correct docblock CartTotalRepository get method #26618
2 parents 527d91f + 79deeeb commit 8ef9334

File tree

2 files changed

+153
-67
lines changed

2 files changed

+153
-67
lines changed

app/code/Magento/Quote/Model/Cart/CartTotalRepository.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
7+
declare(strict_types=1);
8+
69
namespace Magento\Quote\Model\Cart;
710

811
use Magento\Quote\Api;
@@ -12,9 +15,11 @@
1215
use Magento\Framework\Api\ExtensibleDataInterface;
1316
use Magento\Quote\Model\Cart\Totals\ItemConverter;
1417
use Magento\Quote\Api\CouponManagementInterface;
18+
use Magento\Quote\Api\Data\TotalsInterface as QuoteTotalsInterface;
1519

1620
/**
1721
* Cart totals data object.
22+
*
1823
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1924
*/
2025
class CartTotalRepository implements CartTotalRepositoryInterface
@@ -79,11 +84,8 @@ public function __construct(
7984

8085
/**
8186
* @inheritdoc
82-
*
83-
* @param int $cartId The cart ID.
84-
* @return Totals Quote totals data.
8587
*/
86-
public function get($cartId)
88+
public function get($cartId): QuoteTotalsInterface
8789
{
8890
/** @var \Magento\Quote\Model\Quote $quote */
8991
$quote = $this->quoteRepository->getActive($cartId);
@@ -96,17 +98,14 @@ public function get($cartId)
9698
}
9799
unset($addressTotalsData[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]);
98100

99-
/** @var \Magento\Quote\Api\Data\TotalsInterface $quoteTotals */
101+
/** @var QuoteTotalsInterface $quoteTotals */
100102
$quoteTotals = $this->totalsFactory->create();
101103
$this->dataObjectHelper->populateWithArray(
102104
$quoteTotals,
103105
$addressTotalsData,
104-
\Magento\Quote\Api\Data\TotalsInterface::class
106+
QuoteTotalsInterface::class
105107
);
106-
$items = [];
107-
foreach ($quote->getAllVisibleItems() as $index => $item) {
108-
$items[$index] = $this->itemConverter->modelToDataObject($item);
109-
}
108+
$items = array_map([$this->itemConverter, 'modelToDataObject'], $quote->getAllVisibleItems());
110109
$calculatedTotals = $this->totalsConverter->process($addressTotals);
111110
$quoteTotals->setTotalSegments($calculatedTotals);
112111

app/code/Magento/Quote/Test/Unit/Model/Cart/CartTotalRepositoryTest.php

Lines changed: 144 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,73 +4,104 @@
44
* Copyright © Magento, Inc. All rights reserved.
55
* See COPYING.txt for license details.
66
*/
7+
8+
declare(strict_types=1);
9+
710
namespace Magento\Quote\Test\Unit\Model\Cart;
811

9-
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
12+
use Magento\Framework\Api\DataObjectHelper;
13+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
14+
use Magento\Quote\Api\CartRepositoryInterface;
15+
use Magento\Quote\Api\CouponManagementInterface;
16+
use Magento\Quote\Api\Data\TotalSegmentInterface;
17+
use Magento\Quote\Model\Cart\CartTotalRepository;
18+
use Magento\Quote\Model\Cart\Totals\ItemConverter;
19+
use Magento\Quote\Model\Quote;
20+
use Magento\Quote\Model\Quote\Address;
21+
use Magento\Quote\Model\Quote\Item as QuoteItem;
22+
use Magento\Quote\Model\Cart\TotalsConverter;
23+
use Magento\Quote\Api\Data\TotalsInterfaceFactory;
24+
use Magento\Quote\Api\Data\TotalsInterface as QuoteTotalsInterface;
25+
use PHPUnit\Framework\TestCase;
26+
use PHPUnit\Framework\MockObject\MockObject;
1027

1128
/**
29+
* Test Cart totals object for class \Magento\Quote\Model\Cart\CartTotalRepository
30+
*
1231
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1332
*/
14-
class CartTotalRepositoryTest extends \PHPUnit\Framework\TestCase
33+
class CartTotalRepositoryTest extends TestCase
1534
{
35+
private const STUB_CART_ID = 12;
36+
37+
private const STUB_ITEMS_QTY = 100;
38+
39+
private const STUB_CURRENCY_CODE = 'en_US';
40+
41+
private const STUB_COUPON = 'coupon';
42+
1643
/**
17-
* @var ObjectManager
44+
* @var ObjectManagerHelper
1845
*/
1946
protected $objectManager;
2047

2148
/**
22-
* @var \PHPUnit_Framework_MockObject_MockObject
49+
* @var ItemConverter|MockObject
2350
*/
2451
protected $converterMock;
2552

2653
/**
27-
* @var \Magento\Quote\Model\Cart\CartTotalRepository
54+
* @var CartTotalRepository
2855
*/
2956
protected $model;
3057

3158
/**
32-
* @var \PHPUnit_Framework_MockObject_MockObject
59+
* @var CartRepositoryInterface|MockObject
3360
*/
3461
private $quoteRepositoryMock;
3562

3663
/**
37-
* @var \PHPUnit_Framework_MockObject_MockObject
64+
* @var MockObject
3865
*/
3966
private $quoteMock;
4067

4168
/**
42-
* @var \PHPUnit_Framework_MockObject_MockObject
69+
* @var TotalsInterfaceFactory|MockObject
4370
*/
4471
private $totalsFactoryMock;
4572

4673
/**
47-
* @var \PHPUnit_Framework_MockObject_MockObject
74+
* @var MockObject
4875
*/
4976
protected $addressMock;
5077

5178
/**
52-
* @var \Magento\Framework\Api\DataObjectHelper|\PHPUnit_Framework_MockObject_MockObject
79+
* @var DataObjectHelper|MockObject
5380
*/
5481
protected $dataObjectHelperMock;
5582

5683
/**
57-
* @var \PHPUnit_Framework_MockObject_MockObject
84+
* @var CouponManagementInterface|MockObject
5885
*/
5986
protected $couponServiceMock;
6087

6188
/**
62-
* @var \PHPUnit_Framework_MockObject_MockObject
89+
* @var TotalsConverter|MockObject
6390
*/
6491
protected $totalsConverterMock;
6592

6693
protected function setUp()
6794
{
68-
$this->objectManager = new ObjectManager($this);
95+
$this->objectManager = new ObjectManagerHelper($this);
6996
$this->totalsFactoryMock = $this->createPartialMock(
70-
\Magento\Quote\Api\Data\TotalsInterfaceFactory::class,
71-
['create']
97+
TotalsInterfaceFactory::class,
98+
[
99+
'create'
100+
]
72101
);
73-
$this->quoteMock = $this->createPartialMock(\Magento\Quote\Model\Quote::class, [
102+
$this->quoteMock = $this->createPartialMock(
103+
Quote::class,
104+
[
74105
'isVirtual',
75106
'getShippingAddress',
76107
'getBillingAddress',
@@ -79,21 +110,33 @@ protected function setUp()
79110
'getQuoteCurrencyCode',
80111
'getItemsQty',
81112
'collectTotals'
82-
]);
83-
$this->quoteRepositoryMock = $this->createMock(\Magento\Quote\Api\CartRepositoryInterface::class);
113+
]
114+
);
115+
$this->quoteRepositoryMock = $this->createMock(
116+
CartRepositoryInterface::class
117+
);
84118
$this->addressMock = $this->createPartialMock(
85-
\Magento\Quote\Model\Quote\Address::class,
86-
['getData', 'getTotals']
119+
Address::class,
120+
[
121+
'getData',
122+
'getTotals'
123+
]
124+
);
125+
$this->dataObjectHelperMock = $this->getMockBuilder(
126+
DataObjectHelper::class
127+
)->disableOriginalConstructor()->getMock();
128+
$this->converterMock = $this->createMock(
129+
ItemConverter::class
87130
);
88-
$this->dataObjectHelperMock = $this->getMockBuilder(\Magento\Framework\Api\DataObjectHelper::class)
89-
->disableOriginalConstructor()
90-
->getMock();
91-
$this->converterMock = $this->createMock(\Magento\Quote\Model\Cart\Totals\ItemConverter::class);
92131

93-
$this->couponServiceMock = $this->createMock(\Magento\Quote\Api\CouponManagementInterface::class);
94-
$this->totalsConverterMock = $this->createMock(\Magento\Quote\Model\Cart\TotalsConverter::class);
132+
$this->couponServiceMock = $this->createMock(
133+
CouponManagementInterface::class
134+
);
135+
$this->totalsConverterMock = $this->createMock(
136+
TotalsConverter::class
137+
);
95138

96-
$this->model = new \Magento\Quote\Model\Cart\CartTotalRepository(
139+
$this->model = new CartTotalRepository(
97140
$this->totalsFactoryMock,
98141
$this->quoteRepositoryMock,
99142
$this->dataObjectHelperMock,
@@ -104,70 +147,114 @@ protected function setUp()
104147
}
105148

106149
/**
150+
* Test get cart total
151+
*
107152
* @param bool $isVirtual
108153
* @param string $getAddressType
109154
* @dataProvider getDataProvider
155+
*
156+
* @return void
110157
*/
111-
public function testGet($isVirtual, $getAddressType)
158+
public function testGetCartTotal($isVirtual, $getAddressType): void
112159
{
113-
$cartId = 12;
114-
$itemsQty = 100;
115-
$coupon = 'coupon';
116160
$addressTotals = ['address' => 'totals'];
117-
$itemMock = $this->createMock(\Magento\Quote\Model\Quote\Item::class);
161+
$itemMock = $this->createMock(QuoteItem::class);
118162
$visibleItems = [
119163
11 => $itemMock,
120164
];
121165
$itemArray = [
122166
'name' => 'item',
123167
'options' => [ 4 => ['label' => 'justLabel']],
124168
];
125-
$currencyCode = 'US';
126-
127169
$this->quoteRepositoryMock->expects($this->once())
128170
->method('getActive')
129-
->with($cartId)
171+
->with(self::STUB_CART_ID)
130172
->willReturn($this->quoteMock);
131-
$this->quoteMock->expects($this->once())->method('isVirtual')->willReturn($isVirtual);
132-
$this->quoteMock->expects($this->exactly(2))->method($getAddressType)->willReturn($this->addressMock);
133-
$this->quoteMock->expects($this->once())->method('getAllVisibleItems')->willReturn($visibleItems);
134-
$this->quoteMock->expects($this->once())->method('getBaseCurrencyCode')->willReturn($currencyCode);
135-
$this->quoteMock->expects($this->once())->method('getQuoteCurrencyCode')->willReturn($currencyCode);
136-
$this->quoteMock->expects($this->once())->method('getItemsQty')->willReturn($itemsQty);
137-
$this->addressMock->expects($this->any())->method('getData')->willReturn($addressTotals);
138-
$this->addressMock->expects($this->once())->method('getTotals')->willReturn($addressTotals);
139-
140-
$totalsMock = $this->createMock(\Magento\Quote\Api\Data\TotalsInterface::class);
141-
$this->totalsFactoryMock->expects($this->once())->method('create')->willReturn($totalsMock);
173+
$this->quoteMock->expects($this->once())
174+
->method('isVirtual')
175+
->willReturn($isVirtual);
176+
$this->quoteMock->expects($this->exactly(2))
177+
->method($getAddressType)
178+
->willReturn($this->addressMock);
179+
$this->quoteMock->expects($this->once())
180+
->method('getAllVisibleItems')
181+
->willReturn($visibleItems);
182+
$this->quoteMock->expects($this->once())
183+
->method('getBaseCurrencyCode')
184+
->willReturn(self::STUB_CURRENCY_CODE);
185+
$this->quoteMock->expects($this->once())
186+
->method('getQuoteCurrencyCode')
187+
->willReturn(self::STUB_CURRENCY_CODE);
188+
$this->quoteMock->expects($this->once())
189+
->method('getItemsQty')
190+
->willReturn(self::STUB_ITEMS_QTY);
191+
$this->addressMock->expects($this->any())
192+
->method('getData')
193+
->willReturn($addressTotals);
194+
$this->addressMock->expects($this->once())
195+
->method('getTotals')
196+
->willReturn($addressTotals);
197+
198+
$totalsMock = $this->createMock(QuoteTotalsInterface::class);
199+
$this->totalsFactoryMock->expects($this->once())
200+
->method('create')
201+
->willReturn($totalsMock);
142202
$this->dataObjectHelperMock->expects($this->once())->method('populateWithArray');
143203
$this->converterMock->expects($this->once())
144204
->method('modelToDataObject')
145205
->with($itemMock)
146206
->willReturn($itemArray);
147207

148-
$totalSegmentsMock = $this->createMock(\Magento\Quote\Api\Data\TotalSegmentInterface::class);
208+
$totalSegmentsMock = $this->createMock(TotalSegmentInterface::class);
149209
$this->totalsConverterMock->expects($this->once())
150210
->method('process')
151211
->with($addressTotals)
152212
->willReturn($totalSegmentsMock);
153213

154-
$this->couponServiceMock->expects($this->once())->method('get')->with($cartId)->willReturn($coupon);
214+
$this->couponServiceMock
215+
->expects($this->once())
216+
->method('get')
217+
->with(self::STUB_CART_ID)
218+
->willReturn(self::STUB_COUPON);
155219

156-
$totalsMock->expects($this->once())->method('setItems')->with([11 => $itemArray])->willReturnSelf();
157-
$totalsMock->expects($this->once())->method('setTotalSegments')->with($totalSegmentsMock)->willReturnSelf();
158-
$totalsMock->expects($this->once())->method('setCouponCode')->with($coupon)->willReturnSelf();
159-
$totalsMock->expects($this->once())->method('setGrandTotal')->willReturnSelf();
160-
$totalsMock->expects($this->once())->method('setItemsQty')->with($itemsQty)->willReturnSelf();
161-
$totalsMock->expects($this->once())->method('setBaseCurrencyCode')->with($currencyCode)->willReturnSelf();
162-
$totalsMock->expects($this->once())->method('setQuoteCurrencyCode')->with($currencyCode)->willReturnSelf();
220+
$totalsMock->expects($this->once())
221+
->method('setItems')
222+
->with([11 => $itemArray])
223+
->willReturnSelf();
224+
$totalsMock->expects($this->once())
225+
->method('setTotalSegments')
226+
->with($totalSegmentsMock)
227+
->willReturnSelf();
228+
$totalsMock->expects($this->once())
229+
->method('setCouponCode')
230+
->with(self::STUB_COUPON)
231+
->willReturnSelf();
232+
$totalsMock->expects($this->once())
233+
->method('setGrandTotal')
234+
->willReturnSelf();
235+
$totalsMock->expects($this->once())
236+
->method('setItemsQty')
237+
->with(self::STUB_ITEMS_QTY)
238+
->willReturnSelf();
239+
$totalsMock->expects($this->once())
240+
->method('setBaseCurrencyCode')
241+
->with(self::STUB_CURRENCY_CODE)
242+
->willReturnSelf();
243+
$totalsMock->expects($this->once())
244+
->method('setQuoteCurrencyCode')
245+
->with(self::STUB_CURRENCY_CODE)
246+
->willReturnSelf();
163247

164-
$this->assertEquals($totalsMock, $this->model->get($cartId));
248+
$this->assertEquals($totalsMock, $this->model->get(self::STUB_CART_ID));
165249
}
166250

167251
/**
252+
* Provide data for test different cases
253+
*
254+
* @param void
168255
* @return array
169256
*/
170-
public function getDataProvider()
257+
public function getDataProvider(): array
171258
{
172259
return [
173260
'Virtual Quote' => [

0 commit comments

Comments
 (0)