InvalidateAfterEnablingOrDisablingSourceTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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\InventoryIndexer\Test\Integration;
  8. use Magento\Framework\Indexer\IndexerInterface;
  9. use Magento\Framework\Indexer\IndexerRegistry;
  10. use Magento\InventoryApi\Api\SourceRepositoryInterface;
  11. use Magento\InventoryIndexer\Indexer\InventoryIndexer;
  12. use Magento\TestFramework\Helper\Bootstrap;
  13. use PHPUnit\Framework\TestCase;
  14. /**
  15. * Tests Indexer invalidation after Source enabled or disabled.
  16. */
  17. class InvalidateAfterEnablingOrDisablingSourceTest extends TestCase
  18. {
  19. /**
  20. * @var SourceRepositoryInterface
  21. */
  22. private $sourceRepository;
  23. /**
  24. * @var IndexerInterface
  25. */
  26. private $indexer;
  27. protected function setUp()
  28. {
  29. $this->sourceRepository = Bootstrap::getObjectManager()->get(SourceRepositoryInterface::class);
  30. /** @var IndexerRegistry $indexerRegistry */
  31. $indexerRegistry = Bootstrap::getObjectManager()->get(IndexerRegistry::class);
  32. $this->indexer = $indexerRegistry->get(InventoryIndexer::INDEXER_ID);
  33. }
  34. /**
  35. * Tests Source enabling and disabling when both Stocks and Source Items are connected to current Source.
  36. *
  37. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
  38. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
  39. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
  40. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
  41. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
  42. * @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
  43. *
  44. * @dataProvider indexerInvalidationDataProvider
  45. * @param string $sourceCode
  46. * @param bool $enable
  47. * @param bool $expectedValid
  48. *
  49. * @magentoDbIsolation disabled
  50. */
  51. public function testIndexerInvalidation(string $sourceCode, bool $enable, bool $expectedValid)
  52. {
  53. $this->setSourceEnabledStatus($sourceCode, $enable);
  54. $this->assertEquals($expectedValid, $this->indexer->isValid());
  55. }
  56. /**
  57. * @return array
  58. */
  59. public function indexerInvalidationDataProvider(): array
  60. {
  61. return [
  62. ['eu-1', true, true],
  63. ['eu-1', false, false],
  64. ['eu-disabled', true, false],
  65. ['eu-disabled', false, true],
  66. ];
  67. }
  68. /**
  69. * Tests Source enabling and disabling when no Stocks or Source Items are connected to Source.
  70. *
  71. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
  72. * @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
  73. *
  74. * @dataProvider sourceDoesNotHaveAllRelationsDataProvider
  75. * @param string $sourceCode
  76. * @param bool $enable
  77. * @param bool $expectedValid
  78. */
  79. public function testIndexerInvalidationIfSourceDoesNotHaveAnyRelations(
  80. string $sourceCode,
  81. bool $enable,
  82. bool $expectedValid
  83. ) {
  84. $this->setSourceEnabledStatus($sourceCode, $enable);
  85. $this->assertEquals($expectedValid, $this->indexer->isValid());
  86. }
  87. /**
  88. * Tests Source enabling and disabling when no Stocks are connected to current Source.
  89. *
  90. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
  91. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
  92. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
  93. * @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
  94. *
  95. * @dataProvider sourceDoesNotHaveAllRelationsDataProvider
  96. * @param string $sourceCode
  97. * @param bool $enable
  98. * @param bool $expectedValid
  99. */
  100. public function testIndexerInvalidationIfSourceDoesNotHaveStockLinks(
  101. string $sourceCode,
  102. bool $enable,
  103. bool $expectedValid
  104. ) {
  105. $this->setSourceEnabledStatus($sourceCode, $enable);
  106. $this->assertEquals($expectedValid, $this->indexer->isValid());
  107. }
  108. /**
  109. * Tests Source enabling and disabling when no Source Items are connected to current Source.
  110. *
  111. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
  112. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
  113. * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
  114. * @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
  115. *
  116. * @dataProvider sourceDoesNotHaveAllRelationsDataProvider
  117. * @param string $sourceCode
  118. * @param bool $enable
  119. * @param bool $expectedValid
  120. *
  121. * @magentoDbIsolation disabled
  122. */
  123. public function testIndexerInvalidationIfSourceDoesNotHaveSourceItems(
  124. string $sourceCode,
  125. bool $enable,
  126. bool $expectedValid
  127. ) {
  128. $this->setSourceEnabledStatus($sourceCode, $enable);
  129. $this->assertEquals($expectedValid, $this->indexer->isValid());
  130. }
  131. /**
  132. * @return array
  133. */
  134. public function sourceDoesNotHaveAllRelationsDataProvider(): array
  135. {
  136. return [
  137. ['eu-1', true, true],
  138. ['eu-1', false, true],
  139. ['eu-disabled', true, true],
  140. ['eu-disabled', false, true],
  141. ];
  142. }
  143. /**
  144. * @param string $sourceCode
  145. * @param bool $enable
  146. * @return void
  147. */
  148. private function setSourceEnabledStatus(string $sourceCode, bool $enable)
  149. {
  150. $source = $this->sourceRepository->get($sourceCode);
  151. $source->setEnabled($enable);
  152. $this->sourceRepository->save($source);
  153. }
  154. }