IndexerDimensionMode.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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\TestFramework\Annotation;
  8. use Magento\Catalog\Model\Indexer\Product\Price\ModeSwitcher;
  9. use Magento\Catalog\Model\Indexer\Product\Price\ModeSwitcherConfiguration;
  10. use Magento\Catalog\Model\Indexer\Product\Price\Processor;
  11. use Magento\Framework\App\Cache\TypeListInterface;
  12. use Magento\Framework\App\Config\ScopeConfigInterface;
  13. use Magento\Framework\ObjectManagerInterface;
  14. use Magento\TestFramework\App\Config;
  15. use Magento\TestFramework\Helper\Bootstrap;
  16. use Magento\Catalog\Model\Indexer\Product\Price\DimensionModeConfiguration;
  17. use PHPUnit\Framework\TestCase;
  18. /**
  19. * Implementation of the @magentoIndexerDimensionMode DocBlock annotation
  20. */
  21. class IndexerDimensionMode
  22. {
  23. /** @var TypeListInterface */
  24. private $cacheTypeList;
  25. /** @var ScopeConfigInterface */
  26. private $configReader;
  27. /** @var ModeSwitcher */
  28. private $modeSwitcher;
  29. /** @var ObjectManagerInterface */
  30. private $objectManager;
  31. /** @var \Magento\TestFramework\Db\Mysql */
  32. private $db;
  33. /** @var bool */
  34. private $isDimensionMode = false;
  35. /**
  36. * Restore db
  37. */
  38. private function restoreDb()
  39. {
  40. $this->db = Bootstrap::getInstance()->getBootstrap()->getApplication()->getDbInstance();
  41. $this->objectManager = Bootstrap::getObjectManager();
  42. $this->db->restoreFromDbDump();
  43. $this->cacheTypeList = $this->objectManager->get(TypeListInterface::class);
  44. $this->cacheTypeList->cleanType('config');
  45. $this->objectManager->get(Config::class)->clean();
  46. }
  47. /**
  48. * @param string $mode
  49. * @param TestCase $test
  50. * @throws \Exception
  51. */
  52. private function setDimensionMode(string $mode, TestCase $test)
  53. {
  54. $this->objectManager = Bootstrap::getObjectManager();
  55. $this->modeSwitcher = $this->objectManager->get(ModeSwitcher::class);
  56. $this->configReader = $this->objectManager->get(ScopeConfigInterface::class);
  57. $this->cacheTypeList = $this->objectManager->get(TypeListInterface::class);
  58. $this->configReader->clean();
  59. $previousMode = $this->configReader->getValue(ModeSwitcherConfiguration::XML_PATH_PRICE_DIMENSIONS_MODE) ?:
  60. DimensionModeConfiguration::DIMENSION_NONE;
  61. if ($previousMode !== $mode) {
  62. //Create new tables and move data
  63. $this->modeSwitcher->switchMode($mode, $previousMode);
  64. $this->objectManager->get(Config::class)->clean();
  65. } else {
  66. $this->fail('Dimensions mode for indexer has not been changed', $test);
  67. }
  68. }
  69. /**
  70. * Handler for 'startTest' event
  71. *
  72. * @param TestCase $test
  73. * @return void
  74. * @throws \Exception
  75. */
  76. public function startTest(TestCase $test)
  77. {
  78. $source = $test->getAnnotations();
  79. if (isset($source['method']['magentoIndexerDimensionMode'])) {
  80. $annotations = $source['method']['magentoIndexerDimensionMode'];
  81. } elseif (isset($source['class']['magentoIndexerDimensionMode'])) {
  82. $annotations = $source['class']['magentoIndexerDimensionMode'];
  83. } else {
  84. return;
  85. }
  86. $dbIsolation = $source['method']['magentoDbIsolation']
  87. ?? $source['class']['magentoDbIsolation']
  88. ?? ['disabled'];
  89. if ($dbIsolation[0] != 'disabled') {
  90. $this->fail("Invalid @magentoDbIsolation declaration: $dbIsolation[0]", $test);
  91. }
  92. list($indexerType, $indexerMode) = explode(' ', $annotations[0]);
  93. if ($indexerType == Processor::INDEXER_ID) {
  94. $this->isDimensionMode = true;
  95. $this->setDimensionMode($indexerMode, $test);
  96. }
  97. }
  98. /**
  99. * Handler for 'endTest' event
  100. *
  101. * @return void
  102. */
  103. public function endTest()
  104. {
  105. if ($this->isDimensionMode) {
  106. $this->restoreDb();
  107. $this->isDimensionMode = false;
  108. }
  109. }
  110. /**
  111. * Fails the test with specified error message
  112. *
  113. * @param string $message
  114. * @param TestCase $test
  115. * @throws \Exception
  116. */
  117. private function fail($message, TestCase $test)
  118. {
  119. $test->fail("{$message} in the test '{$test->toString()}'");
  120. }
  121. }