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