16 class CartTest extends \PHPUnit\Framework\TestCase
63 private $storeManagerMock;
73 private $productRepository;
78 private $requestInfoFilterMock;
82 $this->checkoutSessionMock = $this->createMock(\
Magento\Checkout\Model\Session::class);
83 $this->customerSessionMock = $this->createMock(\
Magento\Customer\Model\Session::class);
84 $this->scopeConfigMock = $this->createMock(\
Magento\Framework\
App\
Config\ScopeConfigInterface::class);
85 $this->quoteMock = $this->createMock(\
Magento\
Quote\Model\Quote::class);
86 $this->eventManagerMock = $this->createMock(\
Magento\Framework\Event\ManagerInterface::class);
87 $this->storeManagerMock = $this->createMock(\
Magento\
Store\Model\StoreManagerInterface::class);
88 $this->productRepository = $this->createMock(\
Magento\Catalog\Api\ProductRepositoryInterface::class);
89 $this->stockRegistry = $this->getMockBuilder(\
Magento\CatalogInventory\Model\StockRegistry::class)
90 ->disableOriginalConstructor()
91 ->setMethods([
'getStockItem',
'__wakeup'])
93 $this->stockItemMock = $this->createPartialMock(
94 \
Magento\CatalogInventory\Model\Stock\Item::class,
95 [
'getMinSaleQty',
'__wakeup']
97 $this->stockState = $this->createPartialMock(
98 \
Magento\CatalogInventory\Model\StockState::class,
99 [
'suggestQty',
'__wakeup']
102 $this->createPartialMock(\
Magento\
Store\Model\Store::class, [
'getWebsiteId',
'getId',
'__wakeup']);
103 $this->requestInfoFilterMock = $this->createMock(
104 \
Magento\Checkout\Model\Cart\RequestInfoFilterInterface::class
107 $this->stockRegistry->expects($this->any())
108 ->method(
'getStockItem')
109 ->will($this->returnValue($this->stockItemMock));
110 $this->storeMock->expects($this->any())
111 ->method(
'getWebsiteId')
112 ->will($this->returnValue(10));
113 $this->storeMock->expects($this->any())
115 ->will($this->returnValue(10));
116 $this->storeManagerMock->expects($this->any())
118 ->will($this->returnValue($this->storeMock));
120 $this->objectManagerHelper =
new ObjectManagerHelper($this);
121 $this->cart = $this->objectManagerHelper->getObject(
122 \
Magento\Checkout\Model\Cart::class,
124 'scopeConfig' => $this->scopeConfigMock,
125 'checkoutSession' => $this->checkoutSessionMock,
126 'stockRegistry' => $this->stockRegistry,
127 'stockState' => $this->stockState,
128 'customerSession' => $this->customerSessionMock,
129 'eventManager' => $this->eventManagerMock,
130 'storeManager' => $this->storeManagerMock,
131 'productRepository' => $this->productRepository
135 $this->objectManagerHelper
136 ->setBackwardCompatibleProperty($this->cart,
'requestInfoFilter', $this->requestInfoFilterMock);
141 $data = [[] , [
'qty' => -2], [
'qty' => 3], [
'qty' => 3.5], [
'qty' => 5], [
'qty' => 4]];
142 $this->quoteMock->expects($this->any())
143 ->method(
'getItemById')
144 ->will($this->returnValueMap([
151 $this->stockState->expects($this->at(0))
152 ->method(
'suggestQty')
153 ->will($this->returnValue(3.0));
154 $this->stockState->expects($this->at(1))
155 ->method(
'suggestQty')
156 ->will($this->returnValue(3.5));
158 $this->checkoutSessionMock->expects($this->any())
160 ->will($this->returnValue($this->quoteMock));
166 [
'qty' => 3.,
'before_suggest_qty' => 3.],
167 [
'qty' => 3.5,
'before_suggest_qty' => 3.5],
171 $this->cart->suggestItemsQty(
$data)
177 $data = [[
'qty' => 5.5,
'before_suggest_qty' => 5.5]];
178 $infoDataObject = $this->objectManagerHelper->getObject(
179 \
Magento\Framework\DataObject::class,
183 $this->checkoutSessionMock->expects($this->once())
185 ->will($this->returnValue($this->quoteMock));
186 $this->eventManagerMock->expects($this->at(0))->method(
'dispatch')->with(
187 'checkout_cart_update_items_before',
188 [
'cart' => $this->cart,
'info' => $infoDataObject]
190 $this->eventManagerMock->expects($this->at(1))->method(
'dispatch')->with(
191 'checkout_cart_update_items_after',
192 [
'cart' => $this->cart,
'info' => $infoDataObject]
196 $this->assertSame($this->cart,
$result);
205 $store = $this->createPartialMock(\
Magento\
Store\Model\Store::class, [
'getId',
'__wakeup',
'getWebsiteId']);
206 $store->expects($this->any())
207 ->method(
'getWebsiteId')
208 ->will($this->returnValue(10));
209 $store->expects($this->any())
211 ->will($this->returnValue(10));
212 $this->storeManagerMock->expects($this->any())
214 ->will($this->returnValue(
$store));
218 $product = $this->createPartialMock(
219 \
Magento\Catalog\Model\Product::class,
220 [
'getStore',
'getId',
'__wakeup']
224 ->will($this->returnValue(4));
227 ->will($this->returnValue(
$store));
230 $product = $this->createPartialMock(
231 \
Magento\Catalog\Model\Product::class,
232 [
'getStore',
'getId',
'__wakeup']
236 ->will($this->returnValue(5));
239 ->will($this->returnValue(
$store));
250 ->method(
'getProduct')
251 ->will($this->returnValue(
$product));
265 [
'getItemsCount',
'getItemsQty',
'__wakeup']
268 $this->checkoutSessionMock->expects($this->any())->method(
'getQuote')->will($this->returnValue(
$quoteMock));
269 $this->checkoutSessionMock->expects($this->at(2))->method(
'getQuoteId')->will($this->returnValue(
$quoteId));
270 $this->customerSessionMock->expects($this->any())->method(
'isLoggedIn')->will($this->returnValue(
true));
272 $this->scopeConfigMock->expects($this->once())->method(
'getValue')
274 ->will($this->returnValue($useQty));
276 $qtyMethodName = ($useQty) ?
'getItemsQty' :
'getItemsCount';
277 $quoteMock->expects($this->once())->method($qtyMethodName)->will($this->returnValue($itemsCount));
279 $this->assertEquals($itemsCount, $this->cart->getSummaryQty());
302 $product = $this->createPartialMock(
303 \
Magento\Catalog\Model\Product::class,
304 [
'getStore',
'getWebsiteIds',
'getProductUrl',
'getId',
'__wakeup']
308 ->will($this->returnValue(4));
311 ->will($this->returnValue($this->storeMock));
313 ->method(
'getWebsiteIds')
314 ->will($this->returnValue([10]));
316 ->method(
'getProductUrl')
317 ->will($this->returnValue(
'url'));
318 $this->productRepository->expects($this->any())
320 ->will($this->returnValue(
$product));
321 $this->quoteMock->expects($this->once())
322 ->method(
'addProduct')
323 ->will($this->returnValue(1));
324 $this->checkoutSessionMock->expects($this->once())
326 ->will($this->returnValue($this->quoteMock));
328 $this->eventManagerMock->expects($this->at(0))->method(
'dispatch')->with(
329 'checkout_cart_product_add_after',
330 [
'quote_item' => 1,
'product' =>
$product]
337 $this->assertSame($this->cart,
$result);
347 $product = $this->createPartialMock(
348 \
Magento\Catalog\Model\Product::class,
349 [
'getStore',
'getWebsiteIds',
'getProductUrl',
'getId',
'__wakeup']
353 ->will($this->returnValue(4));
356 ->will($this->returnValue($this->storeMock));
358 ->method(
'getWebsiteIds')
359 ->will($this->returnValue([10]));
361 ->method(
'getProductUrl')
362 ->will($this->returnValue(
'url'));
363 $this->productRepository->expects($this->any())
365 ->will($this->returnValue(
$product));
366 $this->quoteMock->expects($this->once())
367 ->method(
'addProduct')
368 ->will($this->returnValue(
'error'));
369 $this->checkoutSessionMock->expects($this->once())
371 ->will($this->returnValue($this->quoteMock));
373 $this->eventManagerMock->expects($this->never())->method(
'dispatch')->with(
374 'checkout_cart_product_add_after',
375 [
'quote_item' => 1,
'product' =>
$product]
377 $this->expectException(\
Magento\Framework\
Exception\LocalizedException::class);
378 $this->cart->addProduct(4, 4);
388 $product = $this->createPartialMock(
389 \
Magento\Catalog\Model\Product::class,
390 [
'getWebsiteIds',
'getId',
'__wakeup']
394 ->will($this->returnValue(4));
396 ->method(
'getWebsiteIds')
397 ->will($this->returnValue([10]));
398 $this->productRepository->expects($this->any())
400 ->will($this->returnValue(
$product));
402 $this->eventManagerMock->expects($this->never())->method(
'dispatch')->with(
403 'checkout_cart_product_add_after',
404 [
'quote_item' => 1,
'product' =>
$product]
406 $this->expectException(\
Magento\Framework\
Exception\LocalizedException::class);
407 $this->cart->addProduct(4,
'bad');
417 $obj =
new ObjectManagerHelper($this);
418 $data = [
'qty' => 5.5,
'sku' =>
'prod'];
421 'prod_int_info_int' => [4, 4],
422 'prod_int_info_array' => [ 4,
$data],
423 'prod_int_info_object' => [
426 \
Magento\Framework\DataObject::class,
430 'prod_obj_info_int' => [
null, 4],
431 'prod_obj_info_array' => [
null,
$data],
432 'prod_obj_info_object' => [
435 \
Magento\Framework\DataObject::class,
testAddProductExceptionBadParams()
testAddProduct($productInfo, $requestInfo)
testAddProductException()
testGetSummaryQty($useQty)
prepareQuoteItemMock($itemId)
foreach($optionCollection as $option) $requestInfo