Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
InventoryTest.php
Go to the documentation of this file.
1 <?php
7 
13 class InventoryTest extends \PHPUnit\Framework\TestCase
14 {
18  protected $moduleManager;
19 
23  protected $coreRegistryMock;
24 
28  protected $stockMock;
29 
33  protected $backordersMock;
34 
38  protected $stockRegistryMock;
39 
44 
48  protected $contextMock;
49 
53  protected $storeManagerMock;
54 
58  protected $inventory;
59 
65  protected function setUp()
66  {
67  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
68 
69  $this->contextMock = $this->createPartialMock(
70  \Magento\Backend\Block\Template\Context::class,
71  ['getRequest', 'getStoreManager']
72  );
73  $this->stockConfigurationMock = $this->getMockForAbstractClass(
74  \Magento\CatalogInventory\Api\StockConfigurationInterface::class,
75  [],
76  '',
77  false
78  );
79  $this->stockRegistryMock = $this->getMockForAbstractClass(
80  \Magento\CatalogInventory\Api\StockRegistryInterface::class,
81  [],
82  '',
83  false
84  );
85  $this->backordersMock = $this->createMock(\Magento\CatalogInventory\Model\Source\Backorders::class);
86  $this->stockMock = $this->createMock(\Magento\CatalogInventory\Model\Source\Stock::class);
87  $this->coreRegistryMock = $this->createMock(\Magento\Framework\Registry::class);
88  $this->moduleManager = $this->createMock(\Magento\Framework\Module\Manager::class);
89  $this->storeManagerMock = $this->getMockForAbstractClass(
90  \Magento\Store\Model\StoreManagerInterface::class,
91  [],
92  '',
93  false
94  );
95 
96  $this->contextMock->expects($this->once())
97  ->method('getStoreManager')
98  ->will($this->returnValue($this->storeManagerMock));
99 
100  $this->inventory = $objectManager->getObject(
101  \Magento\Catalog\Block\Adminhtml\Product\Edit\Tab\Inventory::class,
102  [
103  'context' => $this->contextMock,
104  'backorders' => $this->backordersMock,
105  'stock' => $this->stockMock,
106  'moduleManager' => $this->moduleManager,
107  'coreRegistry' => $this->coreRegistryMock,
108  'stockRegistry' => $this->stockRegistryMock,
109  'stockConfiguration' => $this->stockConfigurationMock,
110  ]
111  );
112  }
113 
122  public function testGetBackordersOption($moduleEnabled)
123  {
124  $this->moduleManager->expects($this->once())
125  ->method('isEnabled')
126  ->with('Magento_CatalogInventory')
127  ->will($this->returnValue($moduleEnabled));
128  if ($moduleEnabled) {
129  $this->backordersMock->expects($this->once())
130  ->method('toOptionArray')
131  ->will($this->returnValue(['test-value', 'test-value']));
132  }
133 
134  $result = $this->inventory->getBackordersOption();
135  $this->assertEquals($moduleEnabled, !empty($result));
136  }
137 
146  public function testGetStockOption($moduleEnabled)
147  {
148  $this->moduleManager->expects($this->once())
149  ->method('isEnabled')
150  ->with('Magento_CatalogInventory')
151  ->will($this->returnValue($moduleEnabled));
152  if ($moduleEnabled) {
153  $this->stockMock->expects($this->once())
154  ->method('toOptionArray')
155  ->will($this->returnValue(['test-value', 'test-value']));
156  }
157 
158  $result = $this->inventory->getStockOption();
159  $this->assertEquals($moduleEnabled, !empty($result));
160  }
161 
167  public function testGetProduct()
168  {
169  $this->coreRegistryMock->expects($this->once())
170  ->method('registry')
171  ->with('product')
172  ->will($this->returnValue('return-value'));
173 
174  $result = $this->inventory->getProduct();
175  $this->assertEquals('return-value', $result);
176  }
177 
183  public function testGetStockItem()
184  {
185  $productId = 10;
186  $websiteId = 15;
187  $productMock = $this->createPartialMock(\Magento\Catalog\Model\Product::class, ['getId', 'getStore']);
188  $storeMock = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getWebsiteId']);
189  $productMock->expects($this->once())
190  ->method('getId')
191  ->will($this->returnValue($productId));
192  $productMock->expects($this->once())
193  ->method('getStore')
194  ->will($this->returnValue($storeMock));
195  $storeMock->expects($this->once())
196  ->method('getWebsiteId')
197  ->will($this->returnValue($websiteId));
198  $this->coreRegistryMock->expects($this->any())
199  ->method('registry')
200  ->with('product')
201  ->will($this->returnValue($productMock));
202  $this->stockRegistryMock->expects($this->once())
203  ->method('getStockItem')
204  ->with($productId, $websiteId)
205  ->will($this->returnValue('return-value'));
206 
207  $resultItem = $this->inventory->getStockItem();
208  $this->assertEquals('return-value', $resultItem);
209  }
210 
221  public function testGetFieldValue($stockId, $methods, $result)
222  {
223  $productId = 10;
224  $websiteId = 15;
225  $fieldName = 'field';
226 
227  $stockItemMock = $this->getMockForAbstractClass(
228  \Magento\CatalogInventory\Api\Data\StockItemInterface::class,
229  [],
230  '',
231  false,
232  false,
233  false,
234  $methods
235  );
236  $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
237  $storeMock = $this->createMock(\Magento\Store\Model\Store::class);
238  $productMock->expects($this->once())
239  ->method('getId')
240  ->will($this->returnValue($productId));
241  $productMock->expects($this->once())
242  ->method('getStore')
243  ->will($this->returnValue($storeMock));
244  $storeMock->expects($this->once())
245  ->method('getWebsiteId')
246  ->will($this->returnValue($websiteId));
247  $this->coreRegistryMock->expects($this->any())
248  ->method('registry')
249  ->with('product')
250  ->will($this->returnValue($productMock));
251  $this->stockRegistryMock->expects($this->once())
252  ->method('getStockItem')
253  ->with($productId, $websiteId)
254  ->will($this->returnValue($stockItemMock));
255  $stockItemMock->expects($this->once())
256  ->method('getItemId')
257  ->will($this->returnValue($stockId));
258 
259  if (!empty($methods)) {
260  $stockItemMock->expects($this->once())
261  ->method(reset($methods))
262  ->will($this->returnValue('call-method'));
263  }
264  if (empty($methods) || empty($stockId)) {
265  $this->stockConfigurationMock->expects($this->once())
266  ->method('getDefaultConfigValue')
267  ->will($this->returnValue('default-result'));
268  }
269 
270  $resultValue = $this->inventory->getFieldValue($fieldName);
271  $this->assertEquals($result, $resultValue);
272  }
273 
284  public function testGetConfigFieldValue($stockId, $methods, $result)
285  {
286  $productId = 10;
287  $websiteId = 15;
288  $fieldName = 'field';
289 
290  $stockItemMock = $this->getMockForAbstractClass(
291  \Magento\CatalogInventory\Api\Data\StockItemInterface::class,
292  [],
293  '',
294  false,
295  false,
296  false,
297  $methods
298  );
299  $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
300  $storeMock = $this->createMock(\Magento\Store\Model\Store::class);
301  $productMock->expects($this->once())
302  ->method('getId')
303  ->will($this->returnValue($productId));
304  $productMock->expects($this->once())
305  ->method('getStore')
306  ->will($this->returnValue($storeMock));
307  $storeMock->expects($this->once())
308  ->method('getWebsiteId')
309  ->will($this->returnValue($websiteId));
310  $this->coreRegistryMock->expects($this->any())
311  ->method('registry')
312  ->with('product')
313  ->will($this->returnValue($productMock));
314  $this->stockRegistryMock->expects($this->once())
315  ->method('getStockItem')
316  ->with($productId, $websiteId)
317  ->will($this->returnValue($stockItemMock));
318  $stockItemMock->expects($this->once())
319  ->method('getItemId')
320  ->will($this->returnValue($stockId));
321 
322  if (!empty($methods)) {
323  $stockItemMock->expects($this->once())
324  ->method(reset($methods))
325  ->will($this->returnValue('call-method'));
326  }
327  if (empty($methods) || empty($stockId)) {
328  $this->stockConfigurationMock->expects($this->once())
329  ->method('getDefaultConfigValue')
330  ->will($this->returnValue('default-result'));
331  }
332 
333  $resultField = $this->inventory->getConfigFieldValue($fieldName);
334  $this->assertEquals($result, $resultField);
335  }
336 
342  public function testGetDefaultConfigValue()
343  {
344  $field = 'filed-name';
345  $this->stockConfigurationMock->expects($this->once())
346  ->method('getDefaultConfigValue')
347  ->will($this->returnValue('return-value'));
348 
349  $result = $this->inventory->getDefaultConfigValue($field);
350  $this->assertEquals('return-value', $result);
351  }
352 
358  public function testIsReadonly()
359  {
360  $productMock = $this->createPartialMock(\Magento\Catalog\Model\Product::class, ['getInventoryReadonly']);
361  $this->coreRegistryMock->expects($this->once())
362  ->method('registry')
363  ->with('product')
364  ->will($this->returnValue($productMock));
365 
366  $productMock->expects($this->once())
367  ->method('getInventoryReadonly')
368  ->will($this->returnValue('return-value'));
369 
370  $result = $this->inventory->isReadonly();
371  $this->assertEquals('return-value', $result);
372  }
373 
383  public function testIsNew($id, $result)
384  {
385  $productMock = $this->createPartialMock(\Magento\Catalog\Model\Product::class, ['getId']);
386  $this->coreRegistryMock->expects($this->once())
387  ->method('registry')
388  ->with('product')
389  ->will($this->returnValue($productMock));
390  $productMock->expects($this->once())
391  ->method('getId')
392  ->will($this->returnValue($id));
393 
394  $methodResult = $this->inventory->isNew();
395  $this->assertEquals($result, $methodResult);
396  }
397 
403  public function testGetFieldSuffix()
404  {
405  $result = $this->inventory->getFieldSuffix();
406  $this->assertEquals('product', $result);
407  }
408 
414  public function testCanUseQtyDecimals()
415  {
416  $productMock = $this->createPartialMock(\Magento\Catalog\Model\Product::class, ['getTypeInstance']);
417  $typeMock = $this->getMockForAbstractClass(
418  \Magento\Catalog\Model\Product\Type\AbstractType::class,
419  [],
420  '',
421  false,
422  true,
423  true,
424  ['canUseQtyDecimals']
425  );
426  $this->coreRegistryMock->expects($this->once())
427  ->method('registry')
428  ->with('product')
429  ->will($this->returnValue($productMock));
430  $productMock->expects($this->once())
431  ->method('getTypeInstance')
432  ->will($this->returnValue($typeMock));
433  $typeMock->expects($this->once())
434  ->method('canUseQtyDecimals')
435  ->will($this->returnValue('return-value'));
436 
437  $result = $this->inventory->canUseQtyDecimals();
438  $this->assertEquals('return-value', $result);
439  }
440 
446  public function testIsVirtual()
447  {
448  $productMock = $this->createPartialMock(\Magento\Catalog\Model\Product::class, ['getIsVirtual']);
449  $this->coreRegistryMock->expects($this->once())
450  ->method('registry')
451  ->with('product')
452  ->will($this->returnValue($productMock));
453  $productMock->expects($this->once())
454  ->method('getIsVirtual')
455  ->will($this->returnValue('return-value'));
456 
457  $result = $this->inventory->isVirtual();
458  $this->assertEquals('return-value', $result);
459  }
460 
466  public function testIsSingleStoreMode()
467  {
468  $this->storeManagerMock->expects($this->once())
469  ->method('isSingleStoreMode')
470  ->will($this->returnValue('return-value'));
471 
472  $result = $this->inventory->isSingleStoreMode();
473  $this->assertEquals('return-value', $result);
474  }
475 
481  public function dataProviderModuleEnabled()
482  {
483  return [
484  [
485  'ModuleEnabled' => true,
486  ],
487  [
488  'ModuleEnabled' => false
489  ]
490  ];
491  }
492 
498  public function dataProviderGetFieldValue()
499  {
500  return [
501  [
502  'stockId' => 99,
503  'methods' => ['getField'],
504  'result' => 'call-method',
505  ],
506  [
507  'stockId' => null,
508  'methods' => [],
509  'result' => 'default-result'
510  ],
511  [
512  'stockId' => 99,
513  'methods' => [],
514  'result' => 'default-result'
515  ]
516  ];
517  }
518 
525  {
526  return [
527  [
528  'stockId' => 99,
529  'methods' => ['getUseConfigField'],
530  'result' => 'call-method',
531  ],
532  [
533  'stockId' => null,
534  'methods' => [],
535  'result' => 'default-result'
536  ],
537  [
538  'stockId' => 99,
539  'methods' => [],
540  'result' => 'default-result'
541  ]
542  ];
543  }
544 
550  public function dataProviderGetId()
551  {
552  return [
553  [
554  'id' => 99,
555  'result' => false,
556  ],
557  [
558  'id' => null,
559  'result' => true
560  ]
561  ];
562  }
563 }
$objectManager
Definition: bootstrap.php:17
return false
Definition: gallery.phtml:36
$id
Definition: fieldset.phtml:14
$methods
Definition: billing.phtml:71