5
5
*/
6
6
namespace Magento \Quote \Test \Unit \Model \Quote \Address \Total ;
7
7
8
+ use Magento \Catalog \Api \Data \ProductExtensionInterface ;
9
+ use Magento \Catalog \Model \Product ;
10
+ use Magento \Catalog \Model \Product \Type \Price ;
11
+ use Magento \CatalogInventory \Model \StockRegistry ;
12
+ use Magento \Framework \Pricing \PriceCurrencyInterface ;
13
+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
14
+ use Magento \Quote \Api \Data \ShippingAssignmentInterface ;
15
+ use Magento \Quote \Api \Data \ShippingInterface ;
16
+ use Magento \Quote \Model \Quote ;
17
+ use Magento \Quote \Model \Quote \Address ;
18
+ use Magento \Quote \Model \Quote \Address \Item as AddressItem ;
19
+ use Magento \Quote \Model \Quote \Address \Total ;
20
+ use Magento \Quote \Model \Quote \Address \Total \Subtotal ;
21
+ use Magento \Quote \Model \Quote \Item ;
22
+ use Magento \Store \Model \Store ;
23
+ use PHPUnit \Framework \MockObject \MockObject ;
24
+ use PHPUnit \Framework \TestCase ;
25
+ use PHPUnit_Framework_MockObject_MockObject ;
26
+
8
27
/**
9
- * Class SubtotalTest
10
- * @package Magento\Quote\Model\Quote\Address\Total
11
- * TODO refactor me
28
+ * Test address total collector model.
12
29
*
13
30
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
14
31
*/
15
- class SubtotalTest extends \ PHPUnit \ Framework \ TestCase
32
+ class SubtotalTest extends TestCase
16
33
{
17
34
/**
18
- * @var \Magento\Framework\TestFramework\Unit\Helper\ ObjectManager
35
+ * @var ObjectManager
19
36
*/
20
37
protected $ objectManager ;
21
38
22
39
/**
23
- * @var \Magento\Quote\Model\Quote\Address\Total\ Subtotal
40
+ * @var Subtotal
24
41
*/
25
42
protected $ subtotalModel ;
26
43
27
- /** @var \PHPUnit_Framework_MockObject_MockObject */
44
+ /** @var MockObject */
28
45
protected $ stockItemMock ;
29
46
30
47
/**
31
- * @var \PHPUnit_Framework_MockObject_MockObject
48
+ * @var MockObject
32
49
*/
33
50
protected $ stockRegistry ;
34
51
35
52
protected function setUp ()
36
53
{
37
- $ this ->objectManager = new \ Magento \ Framework \ TestFramework \ Unit \ Helper \ ObjectManager ($ this );
54
+ $ this ->objectManager = new ObjectManager ($ this );
38
55
$ this ->subtotalModel = $ this ->objectManager ->getObject (
39
- \ Magento \ Quote \ Model \ Quote \ Address \ Total \ Subtotal::class
56
+ Subtotal::class
40
57
);
41
58
42
59
$ this ->stockRegistry = $ this ->createPartialMock (
43
- \ Magento \ CatalogInventory \ Model \ StockRegistry::class,
60
+ StockRegistry::class,
44
61
['getStockItem ' , '__wakeup ' ]
45
62
);
46
63
$ this ->stockItemMock = $ this ->createPartialMock (
@@ -78,39 +95,39 @@ public function testCollect($price, $originalPrice, $itemHasParent, $expectedPri
78
95
{
79
96
$ this ->stockRegistry ->expects ($ this ->any ())->method ('getStockItem ' )->willReturn ($ this ->stockItemMock );
80
97
81
- $ priceCurrency = $ this ->getMockBuilder (\ Magento \ Framework \ Pricing \ PriceCurrencyInterface::class)->getMock ();
98
+ $ priceCurrency = $ this ->getMockBuilder (PriceCurrencyInterface::class)->getMock ();
82
99
$ convertedPrice = 1231313 ;
83
100
// @TODO this is a wrong test and it does not check methods. Any digital value will be correct
84
101
$ priceCurrency ->expects ($ this ->any ())->method ('convert ' )->willReturn (1231313 );
85
102
86
- /** @var \Magento\Quote\Model\Quote\ Item|\ PHPUnit_Framework_MockObject_MockObject $quoteItem */
103
+ /** @var Item|PHPUnit_Framework_MockObject_MockObject $quoteItem */
87
104
$ quoteItem = $ this ->objectManager ->getObject (
88
- \ Magento \ Quote \ Model \ Quote \ Item::class,
105
+ Item::class,
89
106
[
90
107
'stockRegistry ' => $ this ->stockRegistry ,
91
108
'priceCurrency ' => $ priceCurrency ,
92
109
]
93
110
);
94
- /** @var \Magento\Quote\Model\Quote\ Address|\PHPUnit_Framework_MockObject_MockObject $address */
111
+ /** @var Address|MockObject $address */
95
112
$ address = $ this ->createPartialMock (
96
- \ Magento \ Quote \ Model \ Quote \ Address::class,
113
+ Address::class,
97
114
['setTotalQty ' , 'getTotalQty ' , 'removeItem ' , 'getQuote ' ]
98
115
);
99
116
100
- /** @var \Magento\Catalog\Model\ Product|\PHPUnit_Framework_MockObject_MockObject $product */
101
- $ product = $ this ->createMock (\ Magento \ Catalog \ Model \ Product::class);
117
+ /** @var Product|MockObject $product */
118
+ $ product = $ this ->createMock (Product::class);
102
119
$ product ->expects ($ this ->any ())->method ('getPrice ' )->will ($ this ->returnValue ($ originalPrice ));
103
120
104
- /** @var \Magento\ Quote\Model\Quote|\PHPUnit_Framework_MockObject_MockObject $quote */
105
- $ quote = $ this ->createMock (\ Magento \ Quote \ Model \ Quote::class);
106
- $ store = $ this ->objectManager ->getObject (\ Magento \ Store \ Model \ Store::class);
121
+ /** @var Quote|MockObject $quote */
122
+ $ quote = $ this ->createMock (Quote::class);
123
+ $ store = $ this ->objectManager ->getObject (Store::class);
107
124
$ store ->setCurrentCurrency ('' );
108
125
109
- $ store = $ this ->createPartialMock (\ Magento \ Store \ Model \ Store::class, ['getWebsiteId ' ]);
126
+ $ store = $ this ->createPartialMock (Store::class, ['getWebsiteId ' ]);
110
127
$ store ->expects ($ this ->any ())->method ('getWebsiteId ' )->willReturn (10 );
111
128
$ product ->expects ($ this ->any ())->method ('getStore ' )->willReturn ($ store );
112
129
$ product ->expects ($ this ->any ())->method ('isVisibleInCatalog ' )->will ($ this ->returnValue (true ));
113
- $ extensionAttribute = $ this ->getMockBuilder (\ Magento \ Catalog \ Api \ Data \ ProductExtensionInterface::class)
130
+ $ extensionAttribute = $ this ->getMockBuilder (ProductExtensionInterface::class)
114
131
->setMethods (['getStockItem ' ])
115
132
->disableOriginalConstructor ()
116
133
->getMockForAbstractClass ();
@@ -123,28 +140,28 @@ public function testCollect($price, $originalPrice, $itemHasParent, $expectedPri
123
140
124
141
$ parentQuoteItem = false ;
125
142
if ($ itemHasParent ) {
126
- $ parentQuoteItem = $ this ->createMock (\ Magento \ Quote \ Model \ Quote \ Item::class);
143
+ $ parentQuoteItem = $ this ->createMock (Item::class);
127
144
$ parentQuoteItem ->expects ($ this ->any ())->method ('getProduct ' )->will ($ this ->returnValue ($ product ));
128
145
}
129
146
$ quoteItem ->setParentItem ($ parentQuoteItem );
130
147
//This value will be overwritten
131
148
$ quoteItem ->setConvertedPrice (10 );
132
149
133
- $ priceModel = $ this ->createMock (\ Magento \ Catalog \ Model \ Product \ Type \ Price::class);
150
+ $ priceModel = $ this ->createMock (Price::class);
134
151
$ priceModel ->expects ($ this ->any ())->method ('getChildFinalPrice ' )->willReturn ($ price );
135
152
$ product ->expects ($ this ->any ())->method ('getPriceModel ' )->willReturn ($ priceModel );
136
153
$ product ->expects ($ this ->any ())->method ('getFinalPrice ' )->willReturn ($ price );
137
154
138
- $ shipping = $ this ->createMock (\ Magento \ Quote \ Api \ Data \ ShippingInterface::class);
155
+ $ shipping = $ this ->createMock (ShippingInterface::class);
139
156
$ shipping ->expects ($ this ->exactly (2 ))->method ('getAddress ' )->willReturn ($ address );
140
157
$ address ->expects ($ this ->at (0 ))->method ('setTotalQty ' )->with (0 );
141
158
$ address ->expects ($ this ->any ())->method ('getTotalQty ' )->willReturn (0 );
142
- $ shippingAssignmentMock = $ this ->createMock (\ Magento \ Quote \ Api \ Data \ ShippingAssignmentInterface::class);
159
+ $ shippingAssignmentMock = $ this ->createMock (ShippingAssignmentInterface::class);
143
160
$ shippingAssignmentMock ->expects ($ this ->exactly (2 ))->method ('getShipping ' )->willReturn ($ shipping );
144
161
$ shippingAssignmentMock ->expects ($ this ->once ())->method ('getItems ' )->willReturn ([$ quoteItem ]);
145
162
146
163
$ total = $ this ->createPartialMock (
147
- \ Magento \ Quote \ Model \ Quote \ Address \ Total::class,
164
+ Total::class,
148
165
['setBaseVirtualAmount ' , 'setVirtualAmount ' ]
149
166
);
150
167
$ total ->expects ($ this ->once ())->method ('setBaseVirtualAmount ' )->willReturnSelf ();
@@ -166,10 +183,75 @@ public function testFetch()
166
183
'value ' => 100
167
184
];
168
185
169
- $ quoteMock = $ this ->createMock (\ Magento \ Quote \ Model \ Quote::class);
170
- $ totalMock = $ this ->createPartialMock (\ Magento \ Quote \ Model \ Quote \ Address \ Total::class, ['getSubtotal ' ]);
186
+ $ quoteMock = $ this ->createMock (Quote::class);
187
+ $ totalMock = $ this ->createPartialMock (Total::class, ['getSubtotal ' ]);
171
188
$ totalMock ->expects ($ this ->once ())->method ('getSubtotal ' )->willReturn (100 );
172
189
173
190
$ this ->assertEquals ($ expectedResult , $ this ->subtotalModel ->fetch ($ quoteMock , $ totalMock ));
174
191
}
192
+
193
+ /**
194
+ * Test that invalid items are not collected
195
+ */
196
+ public function testCollectWithInvalidItems ()
197
+ {
198
+ $ addressItemId = 38203 ;
199
+ $ addressQuoteItemId = 7643 ;
200
+ $ storeId = 1 ;
201
+ $ quote = $ this ->createPartialMock (
202
+ Quote::class,
203
+ [
204
+ 'getItemsCollection ' ,
205
+ ]
206
+ );
207
+ $ quote ->setData (
208
+ [
209
+ 'store_id ' => $ storeId
210
+ ]
211
+ );
212
+ $ quoteItem = $ this ->createPartialMock (
213
+ Item::class,
214
+ []
215
+ );
216
+ $ quoteItem ->setQuote ($ quote );
217
+ $ quote ->method ('getItemsCollection ' )
218
+ ->willReturn ([$ quoteItem ]);
219
+ $ address = $ this ->createPartialMock (
220
+ Address::class,
221
+ [
222
+ 'removeItem ' ,
223
+ 'getQuote '
224
+ ]
225
+ );
226
+ $ address ->method ('getQuote ' )
227
+ ->willReturn ($ quote );
228
+ $ address ->expects ($ this ->once ())
229
+ ->method ('removeItem ' )
230
+ ->with ($ addressItemId );
231
+ $ addressItem = $ this ->createPartialMock (
232
+ AddressItem::class,
233
+ [
234
+ 'getId ' ,
235
+ 'getQuoteItemId '
236
+ ]
237
+ );
238
+ $ addressItem ->setAddress ($ address );
239
+ $ addressItem ->method ('getId ' )
240
+ ->willReturn ($ addressItemId );
241
+ $ addressItem ->method ('getQuoteItemId ' )
242
+ ->willReturn ($ addressQuoteItemId );
243
+ $ shipping = $ this ->createMock (ShippingInterface::class);
244
+ $ shipping ->method ('getAddress ' )
245
+ ->willReturn ($ address );
246
+ $ shippingAssignmentMock = $ this ->createMock (ShippingAssignmentInterface::class);
247
+ $ shippingAssignmentMock ->method ('getShipping ' )
248
+ ->willReturn ($ shipping );
249
+ $ shippingAssignmentMock ->method ('getItems ' )
250
+ ->willReturn ([$ addressItem ]);
251
+ $ total = $ this ->createPartialMock (
252
+ Total::class,
253
+ []
254
+ );
255
+ $ this ->subtotalModel ->collect ($ quote , $ shippingAssignmentMock , $ total );
256
+ }
175
257
}
0 commit comments