4
4
* Copyright © Magento, Inc. All rights reserved.
5
5
* See COPYING.txt for license details.
6
6
*/
7
+
8
+ declare (strict_types=1 );
9
+
7
10
namespace Magento \Quote \Test \Unit \Model \Cart ;
8
11
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 ;
10
25
11
26
/**
27
+ * Cart total Repository Test
28
+ *
12
29
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
13
30
*/
14
- class CartTotalRepositoryTest extends \ PHPUnit \ Framework \ TestCase
31
+ class CartTotalRepositoryTest extends TestCase
15
32
{
16
33
/**
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
18
55
*/
19
56
protected $ objectManager ;
20
57
21
58
/**
22
- * @var \PHPUnit_Framework_MockObject_MockObject
59
+ * @var ItemConverter|MockObject
23
60
*/
24
61
protected $ converterMock ;
25
62
26
63
/**
27
- * @var \Magento\Quote\Model\Cart\ CartTotalRepository
64
+ * @var CartTotalRepository
28
65
*/
29
66
protected $ model ;
30
67
31
68
/**
32
- * @var \PHPUnit_Framework_MockObject_MockObject
69
+ * @var CartRepositoryInterface|MockObject
33
70
*/
34
71
private $ quoteRepositoryMock ;
35
72
36
73
/**
37
- * @var \PHPUnit_Framework_MockObject_MockObject
74
+ * @var MockObject
38
75
*/
39
76
private $ quoteMock ;
40
77
41
78
/**
42
- * @var \PHPUnit_Framework_MockObject_MockObject
79
+ * @var \Magento\Quote\Api\Data\TotalsInterfaceFactory|MockObject
43
80
*/
44
81
private $ totalsFactoryMock ;
45
82
46
83
/**
47
- * @var \PHPUnit_Framework_MockObject_MockObject
84
+ * @var MockObject
48
85
*/
49
86
protected $ addressMock ;
50
87
51
88
/**
52
- * @var \Magento\Framework\Api\ DataObjectHelper|\PHPUnit_Framework_MockObject_MockObject
89
+ * @var DataObjectHelper|MockObject
53
90
*/
54
91
protected $ dataObjectHelperMock ;
55
92
56
93
/**
57
- * @var \PHPUnit_Framework_MockObject_MockObject
94
+ * @var CouponManagementInterface|MockObject
58
95
*/
59
96
protected $ couponServiceMock ;
60
97
61
98
/**
62
- * @var \PHPUnit_Framework_MockObject_MockObject
99
+ * @var TotalsConverter|MockObject
63
100
*/
64
101
protected $ totalsConverterMock ;
65
102
66
103
protected function setUp ()
67
104
{
68
- $ this ->objectManager = new ObjectManager ($ this );
105
+ $ this ->objectManager = new ObjectManagerHelper ($ this );
69
106
$ this ->totalsFactoryMock = $ this ->createPartialMock (
70
107
\Magento \Quote \Api \Data \TotalsInterfaceFactory::class,
71
108
['create ' ]
72
109
);
73
- $ this ->quoteMock = $ this ->createPartialMock (\ Magento \ Quote \ Model \ Quote::class, [
110
+ $ this ->quoteMock = $ this ->createPartialMock (Quote::class, [
74
111
'isVirtual ' ,
75
112
'getShippingAddress ' ,
76
113
'getBillingAddress ' ,
@@ -80,20 +117,20 @@ protected function setUp()
80
117
'getItemsQty ' ,
81
118
'collectTotals '
82
119
]);
83
- $ this ->quoteRepositoryMock = $ this ->createMock (\ Magento \ Quote \ Api \ CartRepositoryInterface::class);
120
+ $ this ->quoteRepositoryMock = $ this ->createMock (CartRepositoryInterface::class);
84
121
$ this ->addressMock = $ this ->createPartialMock (
85
- \ Magento \ Quote \ Model \ Quote \ Address::class,
122
+ Address::class,
86
123
['getData ' , 'getTotals ' ]
87
124
);
88
- $ this ->dataObjectHelperMock = $ this ->getMockBuilder (\ Magento \ Framework \ Api \ DataObjectHelper::class)
125
+ $ this ->dataObjectHelperMock = $ this ->getMockBuilder (DataObjectHelper::class)
89
126
->disableOriginalConstructor ()
90
127
->getMock ();
91
- $ this ->converterMock = $ this ->createMock (\ Magento \ Quote \ Model \ Cart \ Totals \ ItemConverter::class);
128
+ $ this ->converterMock = $ this ->createMock (ItemConverter::class);
92
129
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);
95
132
96
- $ this ->model = new \ Magento \ Quote \ Model \ Cart \ CartTotalRepository (
133
+ $ this ->model = new CartTotalRepository (
97
134
$ this ->totalsFactoryMock ,
98
135
$ this ->quoteRepositoryMock ,
99
136
$ this ->dataObjectHelperMock ,
@@ -104,38 +141,53 @@ protected function setUp()
104
141
}
105
142
106
143
/**
144
+ * Test get cart total
145
+ *
107
146
* @param bool $isVirtual
108
147
* @param string $getAddressType
109
148
* @dataProvider getDataProvider
149
+ *
150
+ * @return void
110
151
*/
111
- public function testGet ($ isVirtual , $ getAddressType )
152
+ public function testGetCartTotal ($ isVirtual , $ getAddressType ): void
112
153
{
113
- $ cartId = 12 ;
114
- $ itemsQty = 100 ;
115
- $ coupon = 'coupon ' ;
116
154
$ addressTotals = ['address ' => 'totals ' ];
117
- $ itemMock = $ this ->createMock (\ Magento \ Quote \ Model \ Quote \Item ::class);
155
+ $ itemMock = $ this ->createMock (QuoteItem ::class);
118
156
$ visibleItems = [
119
157
11 => $ itemMock ,
120
158
];
121
159
$ itemArray = [
122
160
'name ' => 'item ' ,
123
161
'options ' => [ 4 => ['label ' => 'justLabel ' ]],
124
162
];
125
- $ currencyCode = 'US ' ;
126
-
127
163
$ this ->quoteRepositoryMock ->expects ($ this ->once ())
128
164
->method ('getActive ' )
129
- ->with ($ cartId )
165
+ ->with (self :: STUB_CART_ID )
130
166
->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 );
139
191
140
192
$ totalsMock = $ this ->createMock (\Magento \Quote \Api \Data \TotalsInterface::class);
141
193
$ this ->totalsFactoryMock ->expects ($ this ->once ())->method ('create ' )->willReturn ($ totalsMock );
@@ -145,29 +197,56 @@ public function testGet($isVirtual, $getAddressType)
145
197
->with ($ itemMock )
146
198
->willReturn ($ itemArray );
147
199
148
- $ totalSegmentsMock = $ this ->createMock (\ Magento \ Quote \ Api \ Data \ TotalSegmentInterface::class);
200
+ $ totalSegmentsMock = $ this ->createMock (TotalSegmentInterface::class);
149
201
$ this ->totalsConverterMock ->expects ($ this ->once ())
150
202
->method ('process ' )
151
203
->with ($ addressTotals )
152
204
->willReturn ($ totalSegmentsMock );
153
205
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 );
155
211
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 ();
163
239
164
- $ this ->assertEquals ($ totalsMock , $ this ->model ->get ($ cartId ));
240
+ $ this ->assertEquals ($ totalsMock , $ this ->model ->get (self :: STUB_CART_ID ));
165
241
}
166
242
167
243
/**
244
+ * Provide data for test different cases
245
+ *
246
+ * @param void
168
247
* @return array
169
248
*/
170
- public function getDataProvider ()
249
+ public function getDataProvider (): array
171
250
{
172
251
return [
173
252
'Virtual Quote ' => [
0 commit comments