Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DefaultStockqtyTest.php
Go to the documentation of this file.
1 <?php
7 
11 class DefaultStockqtyTest extends \PHPUnit\Framework\TestCase
12 {
16  protected $block;
17 
21  protected $registryMock;
22 
26  protected $stockRegistryMock;
27 
31  protected $scopeConfigMock;
32 
33  protected function setUp()
34  {
35  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
36  $this->registryMock = $this->createMock(\Magento\Framework\Registry::class);
37  $this->stockRegistryMock = $this->getMockBuilder(\Magento\CatalogInventory\Api\StockRegistryInterface::class)
38  ->disableOriginalConstructor()
39  ->getMock();
40  $this->scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
41  ->disableOriginalConstructor()
42  ->getMock();
43  $this->block = $objectManager->getObject(
44  \Magento\CatalogInventory\Block\Stockqty\DefaultStockqty::class,
45  [
46  'registry' => $this->registryMock,
47  'stockRegistry' => $this->stockRegistryMock,
48  'scopeConfig' => $this->scopeConfigMock
49  ]
50  );
51  }
52 
53  protected function tearDown()
54  {
55  $this->block = null;
56  }
57 
58  public function testGetIdentities()
59  {
60  $productTags = ['catalog_product_1'];
61  $product = $this->createMock(\Magento\Catalog\Model\Product::class);
62  $product->expects($this->once())->method('getIdentities')->will($this->returnValue($productTags));
63  $this->registryMock->expects($this->once())
64  ->method('registry')
65  ->with('current_product')
66  ->will($this->returnValue($product));
67  $this->assertEquals($productTags, $this->block->getIdentities());
68  }
69 
78  public function testGetStockQty($productStockQty, $productId, $websiteId, $dataQty, $expectedQty)
79  {
80  $this->assertNull($this->block->getData('product_stock_qty'));
81  if ($dataQty) {
82  $this->setDataArrayValue('product_stock_qty', $dataQty);
83  } else {
84  $product = $this->createPartialMock(
85  \Magento\Catalog\Model\Product::class,
86  ['getId', 'getStore', '__wakeup']
87  );
88  $product->expects($this->any())->method('getId')->will($this->returnValue($productId));
89  $store = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getWebsiteId', '__wakeup']);
90  $store->expects($this->any())->method('getWebsiteId')->willReturn($websiteId);
91  $product->expects($this->any())->method('getStore')->will($this->returnValue($store));
92 
93  $this->registryMock->expects($this->any())
94  ->method('registry')
95  ->with('current_product')
96  ->will($this->returnValue($product));
97 
98  if ($productId) {
99  $stockStatus = $this->getMockBuilder(\Magento\CatalogInventory\Api\Data\StockStatusInterface::class)
100  ->getMockForAbstractClass();
101  $stockStatus->expects($this->any())->method('getQty')->willReturn($productStockQty);
102  $this->stockRegistryMock->expects($this->once())
103  ->method('getStockStatus')
104  ->with($this->equalTo($productId), $this->equalTo($websiteId))
105  ->will($this->returnValue($stockStatus));
106  }
107  }
108  $this->assertSame($expectedQty, $this->block->getStockQty());
109  $this->assertSame($expectedQty, $this->block->getData('product_stock_qty'));
110  }
111 
112  public function te1stGetStockQtyLeft()
113  {
114  $productId = 1;
115  $minQty = 0;
116  $websiteId = 1;
117  $stockQty = 2;
118 
119  $storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
120  ->disableOriginalConstructor()
121  ->getMock();
122  $storeMock->expects($this->once())
123  ->method('getWebsiteId')
124  ->willReturn($websiteId);
125  $product = $this->createMock(\Magento\Catalog\Model\Product::class);
126  $product->expects($this->any())
127  ->method('getId')
128  ->willReturn($productId);
129  $product->expects($this->once())
130  ->method('getStore')
131  ->willReturn($storeMock);
132  $this->registryMock->expects($this->once())
133  ->method('registry')
134  ->with('current_product')
135  ->will($this->returnValue($product));
136 
137  $stockItemMock = $this->getMockBuilder(\Magento\CatalogInventory\Api\Data\StockItemInterface::class)
138  ->disableOriginalConstructor()
139  ->getMock();
140  $stockItemMock->expects($this->once())
141  ->method('getMinQty')
142  ->willReturn($minQty);
143  $this->stockRegistryMock->expects($this->once())
144  ->method('getStockItem')
145  ->with($productId)
146  ->willReturn($stockItemMock);
147 
148  $this->assertEquals($stockQty, $this->block->getStockQtyLeft());
149  }
150 
154  public function getStockQtyDataProvider()
155  {
156  return [
157  [
158  'product qty' => 100,
159  'product id' => 5,
160  'website id' => 0,
161  'default qty' => null,
162  'expected qty' => 100,
163  ],
164  [
165  'product qty' => 100,
166  'product id' => null,
167  'website id' => null,
168  'default qty' => null,
169  'expected qty' => 0
170  ],
171  [
172  'product qty' => null,
173  'product id' => null,
174  'website id' => null,
175  'default qty' => 50,
176  'expected qty' => 50
177  ],
178  ];
179  }
180 
185  protected function setDataArrayValue($key, $value)
186  {
187  $property = new \ReflectionProperty($this->block, '_data');
188  $property->setAccessible(true);
189  $dataArray = $property->getValue($this->block);
190  $dataArray[$key] = $value;
191  $property->setValue($this->block, $dataArray);
192  }
193 
194  public function testGetThresholdQty()
195  {
196  $this->scopeConfigMock->expects($this->once())->method('getValue')->willReturn(5);
197  $this->assertEquals(5, $this->block->getThresholdQty());
198  }
199 }
$objectManager
Definition: bootstrap.php:17
$value
Definition: gender.phtml:16
testGetStockQty($productStockQty, $productId, $websiteId, $dataQty, $expectedQty)