Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
IsCorrectQtyConditionTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
15 use PHPUnit\Framework\TestCase;
16 
20 class IsCorrectQtyConditionTest extends TestCase
21 {
25  private $getStockItemConfig;
26 
30  private $saveStockItemConfig;
31 
35  private $isProductSalableForRequestedQty;
36 
40  private $getStockItemConfiguration;
41 
45  private $saveStockItemConfiguration;
46 
50  protected function setUp()
51  {
52  parent::setUp();
53 
54  $this->getStockItemConfig = Bootstrap::getObjectManager()->get(GetStockItemConfigurationInterface::class);
55  $this->saveStockItemConfig = Bootstrap::getObjectManager()->get(SaveStockItemConfigurationInterface::class);
56  $this->isProductSalableForRequestedQty
57  = Bootstrap::getObjectManager()->get(IsProductSalableForRequestedQtyInterface::class);
58  $this->getStockItemConfiguration = Bootstrap::getObjectManager()->get(
59  GetStockItemConfigurationInterface::class
60  );
61  $this->saveStockItemConfiguration = Bootstrap::getObjectManager()->get(
62  SaveStockItemConfigurationInterface::class
63  );
64  }
65 
87  string $sku,
88  int $stockId,
89  int $requestedQty,
90  bool $expectedResult
91  ) {
92  $result = $this->isProductSalableForRequestedQty->execute($sku, $stockId, $requestedQty);
93  $this->assertEquals($expectedResult, $result->isSalable());
94  }
95 
100  {
101  return [
102  ['SKU-2', 10, 1, false],
103  ];
104  }
105 
126  public function testExecuteWithDecimalQty(
127  string $sku,
128  int $stockId,
129  float $requestedQty,
130  bool $expectedResult
131  ): void {
132  $result = $this->isProductSalableForRequestedQty->execute($sku, $stockId, $requestedQty);
133  $this->assertEquals($expectedResult, $result->isSalable());
134  }
135 
139  public function executeWithDecimalQtyDataProvider(): array
140  {
141  return [
142  ['SKU-1', 10, 2.5, true],
143  ['SKU-1', 10, 2, true],
144  ['SKU-2', 30, 2.5, false],
145  ['SKU-2', 30, 2, true]
146  ];
147  }
148 
171  string $sku,
172  int $stockId,
173  int $requestedQty,
174  bool $expectedResult
175  ): void {
176  $result = $this->isProductSalableForRequestedQty->execute($sku, $stockId, $requestedQty);
177  $this->assertEquals($expectedResult, $result->isSalable());
178  }
179 
184  {
185  return [
186  ['SKU-1', 10, 1, false],
187  ['SKU-1', 10, 7, true],
188  ['SKU-1', 10, 8, true],
189  ['SKU-2', 10, 1, false],
190  ['SKU-2', 10, 7, false],
191  ['SKU-3', 10, 1, false],
192  ['SKU-3', 10, 7, false],
193  ['SKU-1', 20, 1, false],
194  ['SKU-1', 20, 7, false],
195  ['SKU-2', 20, 1, false],
196  ['SKU-2', 20, 7, false],
197  ['SKU-3', 20, 1, false],
198  ['SKU-3', 20, 7, false],
199  ['SKU-1', 30, 1, false],
200  ['SKU-1', 30, 7, true],
201  ['SKU-1', 30, 8, true],
202  ['SKU-2', 30, 1, false],
203  ['SKU-2', 30, 7, false],
204  ['SKU-3', 30, 1, false],
205  ['SKU-3', 30, 7, false],
206  ];
207  }
208 
229  public function testExecuteWithMinSaleQty(
230  string $sku,
231  int $stockId,
232  int $requestedQty,
233  bool $expectedResult
234  ): void {
236  $stockItemConfiguration = $this->getStockItemConfiguration->execute($sku, $stockId);
237  $stockItemConfiguration->setUseConfigMinSaleQty(false);
238  $stockItemConfiguration->setMinSaleQty(7);
239  $this->saveStockItemConfiguration->execute($sku, $stockId, $stockItemConfiguration);
240 
241  $result = $this->isProductSalableForRequestedQty->execute($sku, $stockId, $requestedQty);
242  $this->assertEquals($expectedResult, $result->isSalable());
243  }
244 
248  public function executeWithMinSaleQtyDataProvider(): array
249  {
250  return [
251  ['SKU-1', 10, 1, false],
252  ['SKU-1', 10, 7, true],
253  ['SKU-1', 10, 8, true],
254  ['SKU-3', 10, 1, false],
255  ['SKU-3', 10, 7, false],
256  ['SKU-2', 20, 1, false],
257  ['SKU-2', 20, 7, false],
258  ['SKU-1', 30, 1, false],
259  ['SKU-1', 30, 7, true],
260  ['SKU-1', 30, 8, true],
261  ['SKU-2', 30, 1, false],
262  ['SKU-2', 30, 7, false],
263  ['SKU-3', 30, 1, false],
264  ['SKU-3', 30, 7, false],
265  ];
266  }
267 
290  string $sku,
291  int $stockId,
292  int $requestedQty,
293  bool $expectedResult
294  ): void {
295  $result = $this->isProductSalableForRequestedQty->execute($sku, $stockId, $requestedQty);
296  $this->assertEquals($expectedResult, $result->isSalable());
297  }
298 
303  {
304  return [
305  ['SKU-1', 10, 1, true],
306  ['SKU-1', 10, 6, true],
307  ['SKU-1', 10, 7, false],
308  ['SKU-2', 10, 1, false],
309  ['SKU-2', 10, 7, false],
310  ['SKU-3', 10, 1, false],
311  ['SKU-3', 10, 7, false],
312  ['SKU-1', 20, 1, false],
313  ['SKU-1', 20, 7, false],
314  ['SKU-2', 20, 1, true],
315  ['SKU-2', 20, 6, false],
316  ['SKU-2', 20, 7, false],
317  ['SKU-3', 20, 1, false],
318  ['SKU-3', 20, 7, false],
319  ['SKU-1', 30, 1, true],
320  ['SKU-1', 30, 6, true],
321  ['SKU-1', 30, 7, false],
322  ['SKU-2', 30, 1, true],
323  ['SKU-2', 30, 6, false],
324  ['SKU-2', 30, 7, false],
325  ['SKU-3', 30, 1, false],
326  ['SKU-3', 30, 7, false],
327  ];
328  }
329 
351  public function testExecuteWithMaxSaleQty(
352  string $sku,
353  int $stockId,
354  int $requestedQty,
355  bool $expectedResult
356  ): void {
358  $stockItemConfiguration = $this->getStockItemConfiguration->execute($sku, $stockId);
359  $stockItemConfiguration->setUseConfigMaxSaleQty(false);
360  $stockItemConfiguration->setMaxSaleQty(6);
361  $this->saveStockItemConfiguration->execute($sku, $stockId, $stockItemConfiguration);
362 
363  $result = $this->isProductSalableForRequestedQty->execute($sku, $stockId, $requestedQty);
364  $this->assertEquals($expectedResult, $result->isSalable());
365  }
366 
370  public function executeWithMaxSaleQtyDataProvider(): array
371  {
372  return [
373  ['SKU-1', 10, 1, true],
374  ['SKU-1', 10, 6, true],
375  ['SKU-1', 10, 7, false],
376  ['SKU-3', 10, 1, false],
377  ['SKU-3', 10, 7, false],
378  ['SKU-2', 20, 1, true],
379  ['SKU-2', 20, 6, false],
380  ['SKU-2', 20, 7, false],
381  ['SKU-1', 30, 1, true],
382  ['SKU-1', 30, 6, true],
383  ['SKU-1', 30, 7, false],
384  ['SKU-2', 30, 1, true],
385  ['SKU-2', 30, 6, false],
386  ['SKU-2', 30, 7, false],
387  ['SKU-3', 30, 1, false],
388  ['SKU-3', 30, 7, false],
389  ];
390  }
391 
415  string $sku,
416  int $stockId,
417  int $requestedQty,
418  bool $expectedResult
419  ): void {
420  $result = $this->isProductSalableForRequestedQty->execute($sku, $stockId, $requestedQty);
421  $this->assertEquals($expectedResult, $result->isSalable());
422  }
423 
428  {
429  return [
430  ['SKU-1', 10, 1, false],
431  ['SKU-1', 10, 3, true],
432  ['SKU-1', 10, 6, true],
433  ['SKU-1', 10, 9, false],
434  ['SKU-2', 10, 1, false],
435  ['SKU-2', 10, 3, false],
436  ['SKU-3', 10, 1, false],
437  ['SKU-3', 10, 3, false],
438  ['SKU-1', 20, 1, false],
439  ['SKU-1', 20, 3, false],
440  ['SKU-2', 20, 1, false],
441  ['SKU-2', 20, 3, true],
442  ['SKU-2', 20, 6, false],
443  ['SKU-3', 20, 1, false],
444  ['SKU-3', 20, 3, false],
445  ['SKU-1', 30, 1, false],
446  ['SKU-1', 30, 3, true],
447  ['SKU-1', 30, 6, true],
448  ['SKU-1', 30, 9, false],
449  ['SKU-2', 30, 1, false],
450  ['SKU-2', 30, 3, true],
451  ['SKU-2', 30, 6, false],
452  ['SKU-3', 30, 1, false],
453  ['SKU-3', 30, 3, false],
454  ];
455  }
456 
478  public function testExecuteWithQtyIncrements(
479  string $sku,
480  int $stockId,
481  int $requestedQty,
482  bool $expectedResult
483  ): void {
485  $stockItemConfiguration = $this->getStockItemConfiguration->execute($sku, $stockId);
486  $stockItemConfiguration->setUseConfigEnableQtyInc(false);
487  $stockItemConfiguration->setEnableQtyIncrements(true);
488  $stockItemConfiguration->setUseConfigQtyIncrements(false);
489  $stockItemConfiguration->setQtyIncrements(3);
490  $this->saveStockItemConfiguration->execute($sku, $stockId, $stockItemConfiguration);
491 
492  $result = $this->isProductSalableForRequestedQty->execute($sku, $stockId, $requestedQty);
493  $this->assertEquals($expectedResult, $result->isSalable());
494  }
495 
499  public function executeWithQtyIncrementsDataProvider(): array
500  {
501  return [
502  ['SKU-1', 10, 1, false],
503  ['SKU-1', 10, 3, true],
504  ['SKU-1', 10, 6, true],
505  ['SKU-1', 10, 9, false],
506  ['SKU-3', 10, 1, false],
507  ['SKU-3', 10, 3, false],
508  ['SKU-2', 20, 1, false],
509  ['SKU-2', 20, 3, true],
510  ['SKU-2', 20, 6, false],
511  ['SKU-1', 30, 1, false],
512  ['SKU-1', 30, 3, true],
513  ['SKU-1', 30, 6, true],
514  ['SKU-1', 30, 9, false],
515  ['SKU-2', 30, 1, false],
516  ['SKU-2', 30, 3, true],
517  ['SKU-2', 30, 6, false],
518  ['SKU-3', 30, 1, false],
519  ['SKU-3', 30, 3, false],
520  ];
521  }
522 }
testExecuteWithDecimalQty(string $sku, int $stockId, float $requestedQty, bool $expectedResult)
testExecuteWithUseConfigMinSaleQty(string $sku, int $stockId, int $requestedQty, bool $expectedResult)
testExecuteWithUseConfigQtyIncrements(string $sku, int $stockId, int $requestedQty, bool $expectedResult)
testExecuteWithMissingConfiguration(string $sku, int $stockId, int $requestedQty, bool $expectedResult)
testExecuteWithUseConfigMaxSaleQty(string $sku, int $stockId, int $requestedQty, bool $expectedResult)