Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StockStateTest.php
Go to the documentation of this file.
1 <?php
7 
9 
15 class StockStateTest extends \PHPUnit\Framework\TestCase
16 {
19 
23  protected $stockState;
24 
29 
34 
38  protected $stock;
39 
43  protected $stockItem;
44 
48  protected $stockStatus;
49 
53  protected $objectResult;
54 
55  protected $productId = 111;
56  protected $websiteId = 111;
57  protected $qty = 111;
58 
59  protected function setUp()
60  {
61  $this->objectManagerHelper = new ObjectManagerHelper($this);
62 
63  $this->stock = $this->createMock(\Magento\CatalogInventory\Api\Data\StockInterface::class);
64  $this->stockItem = $this->createMock(\Magento\CatalogInventory\Api\Data\StockItemInterface::class);
65  $this->stockStatus = $this->createMock(\Magento\CatalogInventory\Api\Data\StockStatusInterface::class);
66  $this->objectResult = $this->createMock(\Magento\Framework\DataObject::class);
67 
68  $this->stockStateProvider = $this->createPartialMock(
69  \Magento\CatalogInventory\Model\Spi\StockStateProviderInterface::class,
70  [
71  'verifyStock',
72  'verifyNotification',
73  'checkQty',
74  'suggestQty',
75  'getStockQty',
76  'checkQtyIncrements',
77  'checkQuoteItemQty'
78  ]
79  );
80  $this->stockStateProvider->expects($this->any())->method('verifyStock')->willReturn(true);
81  $this->stockStateProvider->expects($this->any())->method('verifyNotification')->willReturn(true);
82  $this->stockStateProvider->expects($this->any())->method('checkQty')->willReturn(true);
83  $this->stockStateProvider->expects($this->any())->method('suggestQty')->willReturn($this->qty);
84  $this->stockStateProvider->expects($this->any())->method('getStockQty')->willReturn($this->qty);
85  $this->stockStateProvider->expects($this->any())->method('checkQtyIncrements')->willReturn($this->objectResult);
86  $this->stockStateProvider->expects($this->any())->method('checkQuoteItemQty')->willReturn($this->objectResult);
87 
88  $this->stockRegistryProvider = $this->createPartialMock(
89  \Magento\CatalogInventory\Model\Spi\StockRegistryProviderInterface::class,
90  ['getStock', 'getStockItem', 'getStockStatus']
91  );
92  $this->stockRegistryProvider->expects($this->any())
93  ->method('getStock')
94  ->will($this->returnValue($this->stock));
95  $this->stockRegistryProvider->expects($this->any())
96  ->method('getStockItem')
97  ->will($this->returnValue($this->stockItem));
98  $this->stockRegistryProvider->expects($this->any())
99  ->method('getStockStatus')
100  ->will($this->returnValue($this->stockStatus));
101 
102  $this->stockState = $this->objectManagerHelper->getObject(
103  \Magento\CatalogInventory\Model\StockState::class,
104  [
105  'stockStateProvider' => $this->stockStateProvider,
106  'stockRegistryProvider' => $this->stockRegistryProvider
107  ]
108  );
109  }
110 
111  protected function tearDown()
112  {
113  $this->stockState = null;
114  }
115 
116  public function testVerifyStock()
117  {
118  $this->assertEquals(
119  true,
120  $this->stockState->verifyStock($this->productId, $this->websiteId)
121  );
122  }
123 
124  public function testVerifyNotification()
125  {
126  $this->assertEquals(
127  true,
128  $this->stockState->verifyNotification($this->productId, $this->websiteId)
129  );
130  }
131 
132  public function testCheckQty()
133  {
134  $this->assertEquals(
135  true,
136  $this->stockState->checkQty($this->productId, $this->qty, $this->websiteId)
137  );
138  }
139 
140  public function testSuggestQty()
141  {
142  $this->assertEquals(
143  $this->qty,
144  $this->stockState->suggestQty($this->productId, $this->qty, $this->websiteId)
145  );
146  }
147 
148  public function testGetStockQty()
149  {
150  $this->assertEquals(
151  $this->qty,
152  $this->stockState->getStockQty($this->productId, $this->websiteId)
153  );
154  }
155 
156  public function testCheckQtyIncrements()
157  {
158  $this->assertEquals(
159  $this->objectResult,
160  $this->stockState->checkQtyIncrements($this->productId, $this->qty, $this->websiteId)
161  );
162  }
163 
164  public function testCheckQuoteItemQty()
165  {
166  $this->assertEquals(
167  $this->objectResult,
168  $this->stockState->checkQuoteItemQty(
169  $this->productId,
170  $this->qty,
171  $this->qty,
172  $this->qty,
173  $this->websiteId
174  )
175  );
176  }
177 }