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