Skip to content

Commit fda138b

Browse files
committed
Add unit test for changes
1 parent b16890b commit fda138b

File tree

2 files changed

+136
-52
lines changed

2 files changed

+136
-52
lines changed

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

Lines changed: 9 additions & 4 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
@@ -81,10 +86,10 @@ public function __construct(
8186
* Get cart total repository
8287
*
8388
* @param int $cartId
84-
* @return Api\Data\TotalsInterface
89+
* @return QuoteTotalsInterface
8590
* @throws \Magento\Framework\Exception\NoSuchEntityException
8691
*/
87-
public function get($cartId)
92+
public function get($cartId): QuoteTotalsInterface
8893
{
8994
/** @var \Magento\Quote\Model\Quote $quote */
9095
$quote = $this->quoteRepository->getActive($cartId);
@@ -97,12 +102,12 @@ public function get($cartId)
97102
}
98103
unset($addressTotalsData[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]);
99104

100-
/** @var \Magento\Quote\Api\Data\TotalsInterface $quoteTotals */
105+
/** @var QuoteTotalsInterface $quoteTotals */
101106
$quoteTotals = $this->totalsFactory->create();
102107
$this->dataObjectHelper->populateWithArray(
103108
$quoteTotals,
104109
$addressTotalsData,
105-
\Magento\Quote\Api\Data\TotalsInterface::class
110+
QuoteTotalsInterface::class
106111
);
107112
$items = [];
108113
foreach ($quote->getAllVisibleItems() as $index => $item) {

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

Lines changed: 127 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,73 +4,110 @@
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 PHPUnit\Framework\TestCase;
24+
use PHPUnit\Framework\MockObject\MockObject;
1025

1126
/**
27+
* Cart total Repository Test
28+
*
1229
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1330
*/
14-
class CartTotalRepositoryTest extends \PHPUnit\Framework\TestCase
31+
class CartTotalRepositoryTest extends TestCase
1532
{
1633
/**
17-
* @var ObjectManager
34+
* @var int
35+
*/
36+
private const STUB_CART_ID = 12;
37+
38+
/**
39+
* @var int
40+
*/
41+
private const STUB_ITEMS_QTY = 100;
42+
43+
/**
44+
* @var string
45+
*/
46+
private const STUB_CURRENCY_CODE = 'en_US';
47+
48+
/**
49+
* @var string
50+
*/
51+
private const STUB_COUPON = 'coupon';
52+
53+
/**
54+
* @var ObjectManagerHelper
1855
*/
1956
protected $objectManager;
2057

2158
/**
22-
* @var \PHPUnit_Framework_MockObject_MockObject
59+
* @var ItemConverter|MockObject
2360
*/
2461
protected $converterMock;
2562

2663
/**
27-
* @var \Magento\Quote\Model\Cart\CartTotalRepository
64+
* @var CartTotalRepository
2865
*/
2966
protected $model;
3067

3168
/**
32-
* @var \PHPUnit_Framework_MockObject_MockObject
69+
* @var CartRepositoryInterface|MockObject
3370
*/
3471
private $quoteRepositoryMock;
3572

3673
/**
37-
* @var \PHPUnit_Framework_MockObject_MockObject
74+
* @var MockObject
3875
*/
3976
private $quoteMock;
4077

4178
/**
42-
* @var \PHPUnit_Framework_MockObject_MockObject
79+
* @var \Magento\Quote\Api\Data\TotalsInterfaceFactory|MockObject
4380
*/
4481
private $totalsFactoryMock;
4582

4683
/**
47-
* @var \PHPUnit_Framework_MockObject_MockObject
84+
* @var MockObject
4885
*/
4986
protected $addressMock;
5087

5188
/**
52-
* @var \Magento\Framework\Api\DataObjectHelper|\PHPUnit_Framework_MockObject_MockObject
89+
* @var DataObjectHelper|MockObject
5390
*/
5491
protected $dataObjectHelperMock;
5592

5693
/**
57-
* @var \PHPUnit_Framework_MockObject_MockObject
94+
* @var CouponManagementInterface|MockObject
5895
*/
5996
protected $couponServiceMock;
6097

6198
/**
62-
* @var \PHPUnit_Framework_MockObject_MockObject
99+
* @var TotalsConverter|MockObject
63100
*/
64101
protected $totalsConverterMock;
65102

66103
protected function setUp()
67104
{
68-
$this->objectManager = new ObjectManager($this);
105+
$this->objectManager = new ObjectManagerHelper($this);
69106
$this->totalsFactoryMock = $this->createPartialMock(
70107
\Magento\Quote\Api\Data\TotalsInterfaceFactory::class,
71108
['create']
72109
);
73-
$this->quoteMock = $this->createPartialMock(\Magento\Quote\Model\Quote::class, [
110+
$this->quoteMock = $this->createPartialMock(Quote::class, [
74111
'isVirtual',
75112
'getShippingAddress',
76113
'getBillingAddress',
@@ -80,20 +117,20 @@ protected function setUp()
80117
'getItemsQty',
81118
'collectTotals'
82119
]);
83-
$this->quoteRepositoryMock = $this->createMock(\Magento\Quote\Api\CartRepositoryInterface::class);
120+
$this->quoteRepositoryMock = $this->createMock(CartRepositoryInterface::class);
84121
$this->addressMock = $this->createPartialMock(
85-
\Magento\Quote\Model\Quote\Address::class,
122+
Address::class,
86123
['getData', 'getTotals']
87124
);
88-
$this->dataObjectHelperMock = $this->getMockBuilder(\Magento\Framework\Api\DataObjectHelper::class)
125+
$this->dataObjectHelperMock = $this->getMockBuilder(DataObjectHelper::class)
89126
->disableOriginalConstructor()
90127
->getMock();
91-
$this->converterMock = $this->createMock(\Magento\Quote\Model\Cart\Totals\ItemConverter::class);
128+
$this->converterMock = $this->createMock(ItemConverter::class);
92129

93-
$this->couponServiceMock = $this->createMock(\Magento\Quote\Api\CouponManagementInterface::class);
94-
$this->totalsConverterMock = $this->createMock(\Magento\Quote\Model\Cart\TotalsConverter::class);
130+
$this->couponServiceMock = $this->createMock(CouponManagementInterface::class);
131+
$this->totalsConverterMock = $this->createMock(TotalsConverter::class);
95132

96-
$this->model = new \Magento\Quote\Model\Cart\CartTotalRepository(
133+
$this->model = new CartTotalRepository(
97134
$this->totalsFactoryMock,
98135
$this->quoteRepositoryMock,
99136
$this->dataObjectHelperMock,
@@ -104,38 +141,53 @@ protected function setUp()
104141
}
105142

106143
/**
144+
* Test get cart total
145+
*
107146
* @param bool $isVirtual
108147
* @param string $getAddressType
109148
* @dataProvider getDataProvider
149+
*
150+
* @return void
110151
*/
111-
public function testGet($isVirtual, $getAddressType)
152+
public function testGetCartTotal($isVirtual, $getAddressType): void
112153
{
113-
$cartId = 12;
114-
$itemsQty = 100;
115-
$coupon = 'coupon';
116154
$addressTotals = ['address' => 'totals'];
117-
$itemMock = $this->createMock(\Magento\Quote\Model\Quote\Item::class);
155+
$itemMock = $this->createMock(QuoteItem::class);
118156
$visibleItems = [
119157
11 => $itemMock,
120158
];
121159
$itemArray = [
122160
'name' => 'item',
123161
'options' => [ 4 => ['label' => 'justLabel']],
124162
];
125-
$currencyCode = 'US';
126-
127163
$this->quoteRepositoryMock->expects($this->once())
128164
->method('getActive')
129-
->with($cartId)
165+
->with(self::STUB_CART_ID)
130166
->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);
167+
$this->quoteMock->expects($this->once())
168+
->method('isVirtual')
169+
->willReturn($isVirtual);
170+
$this->quoteMock->expects($this->exactly(2))
171+
->method($getAddressType)
172+
->willReturn($this->addressMock);
173+
$this->quoteMock->expects($this->once())
174+
->method('getAllVisibleItems')
175+
->willReturn($visibleItems);
176+
$this->quoteMock->expects($this->once())
177+
->method('getBaseCurrencyCode')
178+
->willReturn(self::STUB_CURRENCY_CODE);
179+
$this->quoteMock->expects($this->once())
180+
->method('getQuoteCurrencyCode')
181+
->willReturn(self::STUB_CURRENCY_CODE);
182+
$this->quoteMock->expects($this->once())
183+
->method('getItemsQty')
184+
->willReturn(self::STUB_ITEMS_QTY);
185+
$this->addressMock->expects($this->any())
186+
->method('getData')
187+
->willReturn($addressTotals);
188+
$this->addressMock->expects($this->once())
189+
->method('getTotals')
190+
->willReturn($addressTotals);
139191

140192
$totalsMock = $this->createMock(\Magento\Quote\Api\Data\TotalsInterface::class);
141193
$this->totalsFactoryMock->expects($this->once())->method('create')->willReturn($totalsMock);
@@ -145,29 +197,56 @@ public function testGet($isVirtual, $getAddressType)
145197
->with($itemMock)
146198
->willReturn($itemArray);
147199

148-
$totalSegmentsMock = $this->createMock(\Magento\Quote\Api\Data\TotalSegmentInterface::class);
200+
$totalSegmentsMock = $this->createMock(TotalSegmentInterface::class);
149201
$this->totalsConverterMock->expects($this->once())
150202
->method('process')
151203
->with($addressTotals)
152204
->willReturn($totalSegmentsMock);
153205

154-
$this->couponServiceMock->expects($this->once())->method('get')->with($cartId)->willReturn($coupon);
206+
$this->couponServiceMock
207+
->expects($this->once())
208+
->method('get')
209+
->with(self::STUB_CART_ID)
210+
->willReturn(self::STUB_COUPON);
155211

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();
212+
$totalsMock->expects($this->once())
213+
->method('setItems')
214+
->with([11 => $itemArray])
215+
->willReturnSelf();
216+
$totalsMock->expects($this->once())
217+
->method('setTotalSegments')
218+
->with($totalSegmentsMock)
219+
->willReturnSelf();
220+
$totalsMock->expects($this->once())
221+
->method('setCouponCode')
222+
->with(self::STUB_COUPON)
223+
->willReturnSelf();
224+
$totalsMock->expects($this->once())
225+
->method('setGrandTotal')
226+
->willReturnSelf();
227+
$totalsMock->expects($this->once())
228+
->method('setItemsQty')
229+
->with(self::STUB_ITEMS_QTY)
230+
->willReturnSelf();
231+
$totalsMock->expects($this->once())
232+
->method('setBaseCurrencyCode')
233+
->with(self::STUB_CURRENCY_CODE)
234+
->willReturnSelf();
235+
$totalsMock->expects($this->once())
236+
->method('setQuoteCurrencyCode')
237+
->with(self::STUB_CURRENCY_CODE)
238+
->willReturnSelf();
163239

164-
$this->assertEquals($totalsMock, $this->model->get($cartId));
240+
$this->assertEquals($totalsMock, $this->model->get(self::STUB_CART_ID));
165241
}
166242

167243
/**
244+
* Provide data for test different cases
245+
*
246+
* @param void
168247
* @return array
169248
*/
170-
public function getDataProvider()
249+
public function getDataProvider(): array
171250
{
172251
return [
173252
'Virtual Quote' => [

0 commit comments

Comments
 (0)