Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CartTest.php
Go to the documentation of this file.
1 <?php
8 
10 
16 class CartTest extends \PHPUnit\Framework\TestCase
17 {
19  protected $cart;
20 
23 
26 
31 
33  protected $stockItemMock;
34 
38  protected $scopeConfigMock;
39 
43  protected $quoteMock;
44 
48  protected $eventManagerMock;
49 
53  protected $stockRegistry;
54 
58  protected $stockState;
59 
63  private $storeManagerMock;
64 
68  private $storeMock;
69 
73  private $productRepository;
74 
78  private $requestInfoFilterMock;
79 
80  protected function setUp()
81  {
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'])
92  ->getMock();
93  $this->stockItemMock = $this->createPartialMock(
94  \Magento\CatalogInventory\Model\Stock\Item::class,
95  ['getMinSaleQty', '__wakeup']
96  );
97  $this->stockState = $this->createPartialMock(
98  \Magento\CatalogInventory\Model\StockState::class,
99  ['suggestQty', '__wakeup']
100  );
101  $this->storeMock =
102  $this->createPartialMock(\Magento\Store\Model\Store::class, ['getWebsiteId', 'getId', '__wakeup']);
103  $this->requestInfoFilterMock = $this->createMock(
104  \Magento\Checkout\Model\Cart\RequestInfoFilterInterface::class
105  );
106 
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())
114  ->method('getId')
115  ->will($this->returnValue(10));
116  $this->storeManagerMock->expects($this->any())
117  ->method('getStore')
118  ->will($this->returnValue($this->storeMock));
119 
120  $this->objectManagerHelper = new ObjectManagerHelper($this);
121  $this->cart = $this->objectManagerHelper->getObject(
122  \Magento\Checkout\Model\Cart::class,
123  [
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
132  ]
133  );
134 
135  $this->objectManagerHelper
136  ->setBackwardCompatibleProperty($this->cart, 'requestInfoFilter', $this->requestInfoFilterMock);
137  }
138 
139  public function testSuggestItemsQty()
140  {
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([
145  [2, $this->prepareQuoteItemMock(2)],
146  [3, $this->prepareQuoteItemMock(3)],
147  [4, $this->prepareQuoteItemMock(4)],
148  [5, $this->prepareQuoteItemMock(5)],
149  ]));
150 
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));
157 
158  $this->checkoutSessionMock->expects($this->any())
159  ->method('getQuote')
160  ->will($this->returnValue($this->quoteMock));
161 
162  $this->assertSame(
163  [
164  [],
165  ['qty' => -2],
166  ['qty' => 3., 'before_suggest_qty' => 3.],
167  ['qty' => 3.5, 'before_suggest_qty' => 3.5],
168  ['qty' => 5],
169  ['qty' => 4],
170  ],
171  $this->cart->suggestItemsQty($data)
172  );
173  }
174 
175  public function testUpdateItems()
176  {
177  $data = [['qty' => 5.5, 'before_suggest_qty' => 5.5]];
178  $infoDataObject = $this->objectManagerHelper->getObject(
179  \Magento\Framework\DataObject::class,
180  ['data' => $data]
181  );
182 
183  $this->checkoutSessionMock->expects($this->once())
184  ->method('getQuote')
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]
189  );
190  $this->eventManagerMock->expects($this->at(1))->method('dispatch')->with(
191  'checkout_cart_update_items_after',
192  ['cart' => $this->cart, 'info' => $infoDataObject]
193  );
194 
195  $result = $this->cart->updateItems($data);
196  $this->assertSame($this->cart, $result);
197  }
198 
203  public function prepareQuoteItemMock($itemId)
204  {
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())
210  ->method('getId')
211  ->will($this->returnValue(10));
212  $this->storeManagerMock->expects($this->any())
213  ->method('getStore')
214  ->will($this->returnValue($store));
215 
216  switch ($itemId) {
217  case 2:
218  $product = $this->createPartialMock(
219  \Magento\Catalog\Model\Product::class,
220  ['getStore', 'getId', '__wakeup']
221  );
222  $product->expects($this->once())
223  ->method('getId')
224  ->will($this->returnValue(4));
225  $product->expects($this->once())
226  ->method('getStore')
227  ->will($this->returnValue($store));
228  break;
229  case 3:
230  $product = $this->createPartialMock(
231  \Magento\Catalog\Model\Product::class,
232  ['getStore', 'getId', '__wakeup']
233  );
234  $product->expects($this->once())
235  ->method('getId')
236  ->will($this->returnValue(5));
237  $product->expects($this->once())
238  ->method('getStore')
239  ->will($this->returnValue($store));
240  break;
241  case 4:
242  $product = false;
243  break;
244  default:
245  return false;
246  }
247 
248  $quoteItem = $this->createMock(\Magento\Quote\Model\Quote\Item::class);
249  $quoteItem->expects($this->once())
250  ->method('getProduct')
251  ->will($this->returnValue($product));
252  return $quoteItem;
253  }
254 
259  public function testGetSummaryQty($useQty)
260  {
261  $quoteId = 1;
262  $itemsCount = 1;
263  $quoteMock = $this->createPartialMock(
264  \Magento\Quote\Model\Quote::class,
265  ['getItemsCount', 'getItemsQty', '__wakeup']
266  );
267 
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));
271 
272  $this->scopeConfigMock->expects($this->once())->method('getValue')
273  ->with('checkout/cart_link/use_qty', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
274  ->will($this->returnValue($useQty));
275 
276  $qtyMethodName = ($useQty) ? 'getItemsQty' : 'getItemsCount';
277  $quoteMock->expects($this->once())->method($qtyMethodName)->will($this->returnValue($itemsCount));
278 
279  $this->assertEquals($itemsCount, $this->cart->getSummaryQty());
280  }
281 
285  public function useQtyDataProvider()
286  {
287  return [
288  ['useQty' => true],
289  ['useQty' => false]
290  ];
291  }
292 
300  public function testAddProduct($productInfo, $requestInfo)
301  {
302  $product = $this->createPartialMock(
303  \Magento\Catalog\Model\Product::class,
304  ['getStore', 'getWebsiteIds', 'getProductUrl', 'getId', '__wakeup']
305  );
306  $product->expects($this->any())
307  ->method('getId')
308  ->will($this->returnValue(4));
309  $product->expects($this->once())
310  ->method('getStore')
311  ->will($this->returnValue($this->storeMock));
312  $product->expects($this->any())
313  ->method('getWebsiteIds')
314  ->will($this->returnValue([10]));
315  $product->expects($this->any())
316  ->method('getProductUrl')
317  ->will($this->returnValue('url'));
318  $this->productRepository->expects($this->any())
319  ->method('getById')
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())
325  ->method('getQuote')
326  ->will($this->returnValue($this->quoteMock));
327 
328  $this->eventManagerMock->expects($this->at(0))->method('dispatch')->with(
329  'checkout_cart_product_add_after',
330  ['quote_item' => 1, 'product' => $product]
331  );
332 
333  if (!$productInfo) {
334  $productInfo = $product;
335  }
336  $result = $this->cart->addProduct($productInfo, $requestInfo);
337  $this->assertSame($this->cart, $result);
338  }
339 
345  public function testAddProductException()
346  {
347  $product = $this->createPartialMock(
348  \Magento\Catalog\Model\Product::class,
349  ['getStore', 'getWebsiteIds', 'getProductUrl', 'getId', '__wakeup']
350  );
351  $product->expects($this->any())
352  ->method('getId')
353  ->will($this->returnValue(4));
354  $product->expects($this->once())
355  ->method('getStore')
356  ->will($this->returnValue($this->storeMock));
357  $product->expects($this->any())
358  ->method('getWebsiteIds')
359  ->will($this->returnValue([10]));
360  $product->expects($this->any())
361  ->method('getProductUrl')
362  ->will($this->returnValue('url'));
363  $this->productRepository->expects($this->any())
364  ->method('getById')
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())
370  ->method('getQuote')
371  ->will($this->returnValue($this->quoteMock));
372 
373  $this->eventManagerMock->expects($this->never())->method('dispatch')->with(
374  'checkout_cart_product_add_after',
375  ['quote_item' => 1, 'product' => $product]
376  );
377  $this->expectException(\Magento\Framework\Exception\LocalizedException::class);
378  $this->cart->addProduct(4, 4);
379  }
380 
387  {
388  $product = $this->createPartialMock(
389  \Magento\Catalog\Model\Product::class,
390  ['getWebsiteIds', 'getId', '__wakeup']
391  );
392  $product->expects($this->any())
393  ->method('getId')
394  ->will($this->returnValue(4));
395  $product->expects($this->any())
396  ->method('getWebsiteIds')
397  ->will($this->returnValue([10]));
398  $this->productRepository->expects($this->any())
399  ->method('getById')
400  ->will($this->returnValue($product));
401 
402  $this->eventManagerMock->expects($this->never())->method('dispatch')->with(
403  'checkout_cart_product_add_after',
404  ['quote_item' => 1, 'product' => $product]
405  );
406  $this->expectException(\Magento\Framework\Exception\LocalizedException::class);
407  $this->cart->addProduct(4, 'bad');
408  }
409 
415  public function addProductDataProvider()
416  {
417  $obj = new ObjectManagerHelper($this);
418  $data = ['qty' => 5.5, 'sku' => 'prod'];
419 
420  return [
421  'prod_int_info_int' => [4, 4],
422  'prod_int_info_array' => [ 4, $data],
423  'prod_int_info_object' => [
424  4,
425  $obj->getObject(
426  \Magento\Framework\DataObject::class,
427  ['data' => $data]
428  )
429  ],
430  'prod_obj_info_int' => [null, 4],
431  'prod_obj_info_array' => [ null, $data],
432  'prod_obj_info_object' => [
433  null,
434  $obj->getObject(
435  \Magento\Framework\DataObject::class,
436  ['data' => $data]
437  )
438  ]
439  ];
440  }
441 }
testAddProduct($productInfo, $requestInfo)
Definition: CartTest.php:300
$quoteItem
Definition: quote.php:38
foreach($optionCollection as $option) $requestInfo