Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StockManagementTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
19 
23 class StockManagementTest extends \PHPUnit\Framework\TestCase
24 {
28  private $stockManagement;
29 
33  private $stockResourceMock;
34 
38  private $stockRegistryProviderMock;
39 
43  private $stockStateMock;
44 
48  private $stockConfigurationMock;
49 
53  private $productRepositoryMock;
54 
58  private $qtyCounterMock;
59 
63  private $stockRegistryStorageMock;
64 
68  private $stockItemInterfaceMock;
69 
73  private $websiteId = 0;
74 
75  protected function setUp()
76  {
77  $this->stockResourceMock = $this->getMockBuilder(ResourceStock::class)
78  ->disableOriginalConstructor()
79  ->getMock();
80  $this->stockRegistryProviderMock = $this->getMockBuilder(StockRegistryProviderInterface::class)
81  ->disableOriginalConstructor()
82  ->getMockForAbstractClass();
83  $this->stockStateMock = $this->getMockBuilder(StockState::class)
84  ->disableOriginalConstructor()
85  ->getMock();
86  $this->stockConfigurationMock = $this->getMockBuilder(StockConfigurationInterface::class)
87  ->disableOriginalConstructor()
88  ->getMockForAbstractClass();
89  $this->productRepositoryMock = $this->getMockBuilder(ProductRepositoryInterface::class)
90  ->disableOriginalConstructor()
91  ->getMockForAbstractClass();
92  $this->qtyCounterMock = $this->getMockBuilder(QtyCounterInterface::class)
93  ->disableOriginalConstructor()
94  ->getMockForAbstractClass();
95  $this->stockRegistryStorageMock = $this->getMockBuilder(StockRegistryStorage::class)
96  ->disableOriginalConstructor()
97  ->getMock();
98  $this->stockItemInterfaceMock = $this->getMockBuilder(StockItemInterface::class)
99  ->setMethods(['hasAdminArea','getWebsiteId'])
100  ->disableOriginalConstructor()
101  ->getMockForAbstractClass();
102  $this->stockManagement = $this->getMockBuilder(StockManagement::class)
103  ->setMethods(['getResource', 'canSubtractQty'])
104  ->setConstructorArgs(
105  [
106  'stockResource' => $this->stockResourceMock,
107  'stockRegistryProvider' => $this->stockRegistryProviderMock,
108  'stockState' => $this->stockStateMock,
109  'stockConfiguration' => $this->stockConfigurationMock,
110  'productRepository' => $this->productRepositoryMock,
111  'qtyCounter' => $this->qtyCounterMock,
112  'stockRegistryStorage' => $this->stockRegistryStorageMock,
113  ]
114  )->getMock();
115 
116  $this->stockConfigurationMock
117  ->expects($this->once())
118  ->method('getDefaultScopeId')
119  ->willReturn($this->websiteId);
120  $this->stockManagement
121  ->expects($this->any())
122  ->method('getResource')
123  ->willReturn($this->stockResourceMock);
124  $this->stockRegistryProviderMock
125  ->expects($this->any())
126  ->method('getStockItem')
127  ->willReturn($this->stockItemInterfaceMock);
128  $this->stockItemInterfaceMock
129  ->expects($this->any())
130  ->method('hasAdminArea')
131  ->willReturn(false);
132  }
133 
145  public function testRegisterProductsSale(
146  array $items,
147  array $lockedItems,
148  bool $canSubtract,
149  bool $isQty,
150  bool $verifyStock = true
151  ) {
152  $this->stockResourceMock
153  ->expects($this->once())
154  ->method('beginTransaction');
155  $this->stockResourceMock
156  ->expects($this->once())
157  ->method('lockProductsStock')
158  ->willReturn([$lockedItems]);
159  $this->stockItemInterfaceMock
160  ->expects($this->any())
161  ->method('getItemId')
162  ->willReturn($lockedItems['product_id']);
163  $this->stockManagement
164  ->expects($this->any())
165  ->method('canSubtractQty')
166  ->willReturn($canSubtract);
167  $this->stockConfigurationMock
168  ->expects($this->any())
169  ->method('isQty')
170  ->willReturn($isQty);
171  $this->stockItemInterfaceMock
172  ->expects($this->any())
173  ->method('getWebsiteId')
174  ->willReturn($this->websiteId);
175  $this->stockStateMock
176  ->expects($this->any())
177  ->method('checkQty')
178  ->willReturn(true);
179  $this->stockStateMock
180  ->expects($this->any())
181  ->method('verifyStock')
182  ->willReturn($verifyStock);
183  $this->stockStateMock
184  ->expects($this->any())
185  ->method('verifyNotification')
186  ->willReturn(false);
187  $this->stockResourceMock
188  ->expects($this->once())
189  ->method('commit');
190 
191  $this->stockManagement->registerProductsSale($items, $this->websiteId);
192  }
193 
204  public function testRegisterProductsSaleException(array $items, array $lockedItems)
205  {
206  $this->stockResourceMock
207  ->expects($this->once())
208  ->method('beginTransaction');
209  $this->stockResourceMock
210  ->expects($this->once())
211  ->method('lockProductsStock')
212  ->willReturn([$lockedItems]);
213  $this->stockItemInterfaceMock
214  ->expects($this->any())
215  ->method('getItemId')
216  ->willReturn($lockedItems['product_id']);
217  $this->stockManagement
218  ->expects($this->any())
219  ->method('canSubtractQty')
220  ->willReturn(true);
221  $this->stockConfigurationMock
222  ->expects($this->any())
223  ->method('isQty')
224  ->willReturn(true);
225  $this->stockStateMock
226  ->expects($this->any())
227  ->method('checkQty')
228  ->willReturn(false);
229  $this->stockResourceMock
230  ->expects($this->once())
231  ->method('commit');
232 
233  $this->stockManagement->registerProductsSale($items, $this->websiteId);
234  }
235 
239  public function productsWithCorrectQtyDataProvider(): array
240  {
241  return [
242  [
243  [1 => 3],
244  [
245  'product_id' => 1,
246  'qty' => 10,
247  'type_id' => 'simple',
248  ],
249  false,
250  false,
251  ],
252  [
253  [2 => 4],
254  [
255  'product_id' => 2,
256  'qty' => 10,
257  'type_id' => 'simple',
258  ],
259  true,
260  true,
261  ],
262  [
263  [3 => 5],
264  [
265  'product_id' => 3,
266  'qty' => 10,
267  'type_id' => 'simple',
268  ],
269  true,
270  true,
271  false,
272  ],
273  ];
274  }
275 
279  public function productsWithIncorrectQtyDataProvider(): array
280  {
281  return [
282  [
283  [2 => 4],
284  [
285  'product_id' => 2,
286  'qty' => 2,
287  'type_id' => 'simple',
288  ],
289  ],
290  ];
291  }
292 }
testRegisterProductsSale(array $items, array $lockedItems, bool $canSubtract, bool $isQty, bool $verifyStock=true)
$items