Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
StockStateProviderTest.php
Go to the documentation of this file.
1 <?php
7 
10 
16 class StockStateProviderTest extends \PHPUnit\Framework\TestCase
17 {
20 
25 
29  protected $productFactory;
30 
34  protected $product;
35 
39  protected $mathDivision;
40 
44  protected $localeFormat;
45 
49  protected $objectFactory;
50 
54  protected $object;
55 
59  protected $qty = 50.5;
60 
64  protected $qtyCheckApplicable = true;
65 
69  protected $stockItemMethods = [
70  'getId',
71  'getProductId',
72  'getWebsiteId',
73  'getStockId',
74  'getQty',
75  'getIsInStock',
76  'getIsQtyDecimal',
77  'getShowDefaultNotificationMessage',
78  'getUseConfigMinQty',
79  'getMinQty',
80  'getUseConfigMinSaleQty',
81  'getMinSaleQty',
82  'getUseConfigMaxSaleQty',
83  'getMaxSaleQty',
84  'getUseConfigBackorders',
85  'getBackorders',
86  'getUseConfigNotifyStockQty',
87  'getNotifyStockQty',
88  'getUseConfigQtyIncrements',
89  'getQtyIncrements',
90  'getUseConfigEnableQtyInc',
91  'getEnableQtyIncrements',
92  'getUseConfigManageStock',
93  'getManageStock',
94  'getLowStockDate',
95  'getIsDecimalDivided',
96  'getStockStatusChangedAuto',
97  'hasStockQty',
98  'setStockQty',
99  'getData',
100  'getSuppressCheckQtyIncrements',
101  'getIsChildItem',
102  'getIsSaleable',
103  'getOrderedItems',
104  'setOrderedItems',
105  'getProductName',
106  ];
107 
108  protected function setUp()
109  {
110  $this->objectManagerHelper = new ObjectManagerHelper($this);
111 
112  $this->mathDivision = $this->createPartialMock(\Magento\Framework\Math\Division::class, ['getExactDivision']);
113 
114  $this->localeFormat = $this->getMockForAbstractClass(
115  \Magento\Framework\Locale\FormatInterface::class,
116  ['getNumber']
117  );
118  $this->localeFormat->expects($this->any())
119  ->method('getNumber')
120  ->willReturn($this->qty);
121 
122  $this->object = $this->objectManagerHelper->getObject(\Magento\Framework\DataObject::class);
123  $this->objectFactory = $this->createPartialMock(\Magento\Framework\DataObject\Factory::class, ['create']);
124  $this->objectFactory->expects($this->any())->method('create')->willReturn($this->object);
125 
126  $this->product = $this->createPartialMock(
127  \Magento\Catalog\Model\Product::class,
128  ['load', 'isComposite', '__wakeup', 'isSaleable']
129  );
130  $this->productFactory = $this->createPartialMock(\Magento\Catalog\Model\ProductFactory::class, ['create']);
131  $this->productFactory->expects($this->any())->method('create')->willReturn($this->product);
132 
133  $this->stockStateProvider = $this->objectManagerHelper->getObject(
134  \Magento\CatalogInventory\Model\StockStateProvider::class,
135  [
136  'mathDivision' => $this->mathDivision,
137  'localeFormat' => $this->localeFormat,
138  'objectFactory' => $this->objectFactory,
139  'productFactory' => $this->productFactory,
140  'qtyCheckApplicable' => $this->qtyCheckApplicable
141  ]
142  );
143  }
144 
145  protected function tearDown()
146  {
147  $this->stockStateProvider = null;
148  }
149 
155  public function testVerifyStock(StockItemInterface $stockItem, $expectedResult)
156  {
157  $this->assertEquals(
158  $expectedResult,
159  $this->stockStateProvider->verifyStock($stockItem)
160  );
161  }
162 
168  public function testVerifyNotification(StockItemInterface $stockItem, $expectedResult)
169  {
170  $this->assertEquals(
171  $expectedResult,
172  $this->stockStateProvider->verifyNotification($stockItem)
173  );
174  }
175 
181  public function testCheckQty(StockItemInterface $stockItem, $expectedResult)
182  {
183  $this->assertEquals(
184  $expectedResult,
185  $this->stockStateProvider->checkQty($stockItem, $this->qty)
186  );
187  }
188 
194  public function testSuggestQty(StockItemInterface $stockItem, $expectedResult)
195  {
196  $this->assertEquals(
197  $expectedResult,
198  $this->stockStateProvider->suggestQty($stockItem, $this->qty)
199  );
200  }
201 
207  public function testGetStockQty(StockItemInterface $stockItem, $expectedResult)
208  {
209  $this->assertEquals(
210  $expectedResult,
211  $this->stockStateProvider->getStockQty($stockItem)
212  );
213  }
214 
220  public function testCheckQtyIncrements(StockItemInterface $stockItem, $expectedResult)
221  {
222  $this->assertEquals(
223  $expectedResult,
224  $this->stockStateProvider->checkQtyIncrements($stockItem, $this->qty)->getHasError()
225  );
226  }
227 
233  public function testCheckQuoteItemQty(StockItemInterface $stockItem, $expectedResult)
234  {
235  $this->assertEquals(
236  $expectedResult,
237  $this->stockStateProvider->checkQuoteItemQty(
238  $stockItem,
239  $this->qty,
240  $this->qty,
241  $this->qty
242  )->getHasError()
243  );
244  }
245 
249  public function verifyStockDataProvider()
250  {
251  return $this->prepareDataForMethod('verifyStock');
252  }
253 
258  {
259  return $this->prepareDataForMethod('verifyNotification');
260  }
261 
265  public function checkQtyDataProvider()
266  {
267  return $this->prepareDataForMethod('checkQty');
268  }
269 
273  public function suggestQtyDataProvider()
274  {
275  return $this->prepareDataForMethod('suggestQty');
276  }
277 
281  public function getStockQtyDataProvider()
282  {
283  return $this->prepareDataForMethod('getStockQty');
284  }
285 
290  {
291  return $this->prepareDataForMethod('checkQtyIncrements');
292  }
293 
298  {
299  return $this->prepareDataForMethod('checkQuoteItemQty');
300  }
301 
306  protected function prepareDataForMethod($methodName)
307  {
308  $variations = [];
309  foreach ($this->getVariations() as $variation) {
310  $stockItem = $this->getMockBuilder(\Magento\CatalogInventory\Api\Data\StockItemInterface::class)
311  ->disableOriginalConstructor()
312  ->setMethods($this->stockItemMethods)
313  ->getMockForAbstractClass();
314  $stockItem->expects($this->any())->method('getSuppressCheckQtyIncrements')->willReturn(
315  $variation['values']['_suppress_check_qty_increments_']
316  );
317  $stockItem->expects($this->any())->method('getIsSaleable')->willReturn(
318  $variation['values']['_is_saleable_']
319  );
320  $stockItem->expects($this->any())->method('getOrderedItems')->willReturn(
321  $variation['values']['_ordered_items_']
322  );
323 
324  $stockItem->expects($this->any())->method('getProductName')->willReturn($variation['values']['_product_']);
325  $stockItem->expects($this->any())->method('getIsChildItem')->willReturn(false);
326  $stockItem->expects($this->any())->method('hasStockQty')->willReturn(false);
327  $stockItem->expects($this->any())->method('setStockQty')->willReturnSelf();
328  $stockItem->expects($this->any())->method('setOrderedItems')->willReturnSelf();
329  $stockItem->expects($this->any())->method('getData')
330  ->with('stock_qty')
331  ->willReturn($variation['values']['_stock_qty_']);
332 
333  foreach ($this->stockItemMethods as $method) {
334  $value = isset($variation['values'][$method]) ? $variation['values'][$method] : null;
335  $stockItem->expects($this->any())->method($method)->willReturn($value);
336  }
337  $expectedResult = isset($variation['results'][$methodName]) ? $variation['results'][$methodName] : null;
338  $variations[] = [
339  'stockItem' => $stockItem,
340  'expectedResult' => $expectedResult,
341  ];
342  }
343  return $variations;
344  }
345 
349  protected function getVariations()
350  {
351  $stockQty = 100;
352  return [
353  [
354  'values' => [
355  'getIsInStock' => true,
356  'getQty' => $stockQty,
357  'getMinQty' => 0,
358  'getMinSaleQty' => 0,
359  'getMaxSaleQty' => 99,
360  'getNotifyStockQty' => 10,
361  'getManageStock' => true,
362  'getBackorders' => 1,
363  'getQtyIncrements' => 3,
364  '_stock_qty_' => $stockQty,
365  '_suppress_check_qty_increments_' => false,
366  '_is_saleable_' => true,
367  '_ordered_items_' => 0,
368  '_product_' => 'Test product Name',
369  ],
370  'results' => [
371  'verifyStock' => true,
372  'verifyNotification' => false,
373  'checkQty' => true,
374  'suggestQty' => 51,
375  'getStockQty' => $stockQty,
376  'checkQtyIncrements' => false,
377  'checkQuoteItemQty' => false,
378  ],
379  ],
380  [
381  'values' => [
382  'getIsInStock' => true,
383  'getQty' => $stockQty,
384  'getMinQty' => 60,
385  'getMinSaleQty' => 0,
386  'getMaxSaleQty' => 99,
387  'getNotifyStockQty' => 101,
388  'getManageStock' => true,
389  'getBackorders' => 3,
390  'getQtyIncrements' => 1,
391  '_stock_qty_' => $stockQty,
392  '_suppress_check_qty_increments_' => false,
393  '_is_saleable_' => true,
394  '_ordered_items_' => 0,
395  '_product_' => 'Test product Name',
396  ],
397  'results' => [
398  'verifyStock' => true,
399  'verifyNotification' => true,
400  'checkQty' => false,
401  'suggestQty' => 50.5,
402  'getStockQty' => $stockQty,
403  'checkQtyIncrements' => false,
404  'checkQuoteItemQty' => true,
405  ]
406  ],
407  [
408  'values' => [
409  'getIsInStock' => true,
410  'getQty' => null,
411  'getMinQty' => 60,
412  'getMinSaleQty' => 1,
413  'getMaxSaleQty' => 99,
414  'getNotifyStockQty' => 101,
415  'getManageStock' => true,
416  'getBackorders' => 0,
417  'getQtyIncrements' => 1,
418  '_stock_qty_' => null,
419  '_suppress_check_qty_increments_' => false,
420  '_is_saleable_' => true,
421  '_ordered_items_' => 0,
422  '_product_' => 'Test product Name',
423  ],
424  'results' => [
425  'verifyStock' => false,
426  'verifyNotification' => true,
427  'checkQty' => false,
428  'suggestQty' => 50.5,
429  'getStockQty' => null,
430  'checkQtyIncrements' => false,
431  'checkQuoteItemQty' => true,
432  ]
433  ]
434  ];
435  }
436 
442  public function testCheckQtyIncrementsMsg($isChildItem, $expectedMsg)
443  {
444  $qty = 1;
445  $qtyIncrements = 5;
446  $stockItem = $this->getMockBuilder(\Magento\CatalogInventory\Api\Data\StockItemInterface::class)
447  ->disableOriginalConstructor()
448  ->setMethods($this->stockItemMethods)
449  ->getMockForAbstractClass();
450  $stockItem->expects($this->any())->method('getSuppressCheckQtyIncrements')->willReturn(false);
451  $stockItem->expects($this->any())->method('getQtyIncrements')->willReturn($qtyIncrements);
452  $stockItem->expects($this->any())->method('getIsChildItem')->willReturn($isChildItem);
453  $stockItem->expects($this->any())->method('getProductName')->willReturn('Simple Product');
454  $this->mathDivision->expects($this->any())->method('getExactDivision')->willReturn(1);
455 
456  $result = $this->stockStateProvider->checkQtyIncrements($stockItem, $qty);
457  $this->assertTrue($result->getHasError());
458  $this->assertEquals($expectedMsg, $result->getMessage()->render());
459  }
460 
465  {
466  return [
467  [true, 'You can buy Simple Product only in quantities of 5 at a time.'],
468  [false, 'You can buy this product only in quantities of 5 at a time.'],
469  ];
470  }
471 }
testVerifyNotification(StockItemInterface $stockItem, $expectedResult)
testCheckQtyIncrements(StockItemInterface $stockItem, $expectedResult)
$value
Definition: gender.phtml:16
$method
Definition: info.phtml:13
testCheckQuoteItemQty(StockItemInterface $stockItem, $expectedResult)