UpdateDefaultSourceItemAtLegacyStockItemSaveTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\InventoryCatalog\Test\Integration;
  8. use Magento\CatalogInventory\Api\StockRegistryInterface;
  9. use Magento\InventoryCatalog\Model\GetDefaultSourceItemBySku;
  10. use PHPUnit\Framework\TestCase;
  11. use Magento\TestFramework\Helper\Bootstrap;
  12. class UpdateDefaultSourceItemAtLegacyStockItemSaveTest extends TestCase
  13. {
  14. /**
  15. * @var StockRegistryInterface
  16. */
  17. private $stockRegistry;
  18. /**
  19. * @var GetDefaultSourceItemBySku
  20. */
  21. private $getDefaultSourceItemBySku;
  22. protected function setUp()
  23. {
  24. parent::setUp();
  25. $this->stockRegistry = Bootstrap::getObjectManager()->create(StockRegistryInterface::class);
  26. $this->getDefaultSourceItemBySku = Bootstrap::getObjectManager()->get(GetDefaultSourceItemBySku::class);
  27. }
  28. /**
  29. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
  30. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
  31. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
  32. * @magentoDataFixture ../../../../app/code/Magento/InventoryCatalog/Test/_files/source_items_on_default_source.php
  33. * @magentoDbIsolation enabled
  34. * @throws \Magento\Framework\Exception\NoSuchEntityException
  35. */
  36. public function testSaveLegacyStockItemAssignedToDefaultSource()
  37. {
  38. $stockItem = $this->stockRegistry->getStockItemBySku('SKU-1');
  39. $stockItem->setQty(10);
  40. $this->stockRegistry->updateStockItemBySku('SKU-1', $stockItem);
  41. $defaultSourceItem = $this->getDefaultSourceItemBySku->execute('SKU-1');
  42. self::assertEquals(
  43. 10,
  44. $defaultSourceItem->getQuantity(),
  45. 'Quantity is not updated in default source when legacy stock is updated and product was'
  46. . 'previously assigned to default source'
  47. );
  48. }
  49. /**
  50. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
  51. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
  52. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
  53. * @magentoDataFixture ../../../../app/code/Magento/InventoryCatalog/Test/_files/source_items_on_default_source.php
  54. * @magentoDbIsolation enabled
  55. * @throws \Magento\Framework\Exception\NoSuchEntityException
  56. */
  57. public function testSaveLegacyStockItemNotAssignedToDefaultSource()
  58. {
  59. $stockItem = $this->stockRegistry->getStockItemBySku('SKU-2');
  60. $stockItem->setQty(10);
  61. $this->stockRegistry->updateStockItemBySku('SKU-2', $stockItem);
  62. $defaultSourceItem = $this->getDefaultSourceItemBySku->execute('SKU-2');
  63. self::assertEquals(
  64. 10,
  65. $defaultSourceItem->getQuantity(),
  66. 'Quantity is not updated in default source when legacy stock is updated'
  67. );
  68. // SKU-3 is out of stock and not assigned to default source
  69. $stockItem = $this->stockRegistry->getStockItemBySku('SKU-3');
  70. $stockItem->setQty(10);
  71. $this->stockRegistry->updateStockItemBySku('SKU-3', $stockItem);
  72. $defaultSourceItem = $this->getDefaultSourceItemBySku->execute('SKU-3');
  73. self::assertEquals(
  74. 10,
  75. $defaultSourceItem->getQuantity(),
  76. 'Quantity is not updated in default source when legacy stock is updated and product was not '
  77. . 'previously assigned to default source'
  78. );
  79. }
  80. /**
  81. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
  82. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
  83. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
  84. * @magentoDbIsolation enabled
  85. * @throws \Magento\Framework\Exception\NoSuchEntityException
  86. */
  87. public function testSaveLegacyStockItemWithoutDefaultSourceAssignment()
  88. {
  89. // SKU-3 is out of stock and not assigned to default source
  90. $stockItem = $this->stockRegistry->getStockItemBySku('SKU-3');
  91. $stockItem->setQty(0);
  92. $stockItem->setIsInStock(false);
  93. $this->stockRegistry->updateStockItemBySku('SKU-3', $stockItem);
  94. $defaultSourceItem = $this->getDefaultSourceItemBySku->execute('SKU-3');
  95. self::assertNull(
  96. $defaultSourceItem,
  97. 'Product is assigned to default source on legacy stock item save even if it should not be'
  98. );
  99. // SKU-5 is out of stock and not assigned to default source
  100. $stockItem = $this->stockRegistry->getStockItemBySku('SKU-5');
  101. $stockItem->setQty(1);
  102. $stockItem->setIsInStock(true);
  103. $this->stockRegistry->updateStockItemBySku('SKU-5', $stockItem);
  104. $defaultSourceItem = $this->getDefaultSourceItemBySku->execute('SKU-5');
  105. self::assertNotNull(
  106. $defaultSourceItem,
  107. 'Product is not assigned to default source on legacy stock item save even if it should be'
  108. );
  109. }
  110. }