|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\TestFramework\Annotation; |
| 8 | + |
| 9 | +use Magento\Catalog\Model\Indexer\Product\Price\ModeSwitcher; |
| 10 | +use Magento\Catalog\Model\Indexer\Product\Price\Processor; |
| 11 | +use Magento\Framework\App\Cache\TypeListInterface; |
| 12 | +use Magento\Framework\App\Config\ConfigResource\ConfigInterface; |
| 13 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 14 | +use Magento\Framework\ObjectManagerInterface; |
| 15 | +use Magento\TestFramework\App\Config; |
| 16 | +use Magento\TestFramework\Helper\Bootstrap; |
| 17 | +use Magento\Catalog\Model\Indexer\Product\Price\DimensionModeConfiguration; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | + |
| 20 | +/** |
| 21 | + * Implementation of the @magentoIndexerDimensionMode DocBlock annotation |
| 22 | + */ |
| 23 | +class IndexerDimensionMode |
| 24 | +{ |
| 25 | + /** @var TypeListInterface */ |
| 26 | + private $cacheTypeList; |
| 27 | + |
| 28 | + /** @var ScopeConfigInterface */ |
| 29 | + private $configReader; |
| 30 | + |
| 31 | + /** @var ModeSwitcher */ |
| 32 | + private $modeSwitcher; |
| 33 | + |
| 34 | + /** @var ConfigInterface */ |
| 35 | + private $configWriter; |
| 36 | + |
| 37 | + /** @var ObjectManagerInterface */ |
| 38 | + private $objectManager; |
| 39 | + |
| 40 | + /** @var \Magento\TestFramework\Db\Mysql */ |
| 41 | + private $db; |
| 42 | + |
| 43 | + /** @var bool */ |
| 44 | + private $isDimensionMode = false; |
| 45 | + |
| 46 | + private function restoreDb() |
| 47 | + { |
| 48 | + $this->db = Bootstrap::getInstance()->getBootstrap()->getApplication()->getDbInstance(); |
| 49 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 50 | + $this->db->restoreFromDbDump(); |
| 51 | + $this->cacheTypeList = $this->objectManager->get(TypeListInterface::class); |
| 52 | + $this->cacheTypeList->cleanType('config'); |
| 53 | + $this->objectManager->get(Config::class)->clean(); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @param string $mode |
| 58 | + */ |
| 59 | + private function setDimensionMode($mode, $test) |
| 60 | + { |
| 61 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 62 | + $this->modeSwitcher = $this->objectManager->get(ModeSwitcher::class); |
| 63 | + $this->configWriter = $this->objectManager->get(ConfigInterface::class); |
| 64 | + $this->configReader = $this->objectManager->get(ScopeConfigInterface::class); |
| 65 | + $this->cacheTypeList = $this->objectManager->get(TypeListInterface::class); |
| 66 | + |
| 67 | + $this->configReader->clean(); |
| 68 | + $previousMode = $this->configReader->getValue(ModeSwitcher::XML_PATH_PRICE_DIMENSIONS_MODE) ?: |
| 69 | + DimensionModeConfiguration::DIMENSION_NONE; |
| 70 | + |
| 71 | + if ($previousMode !== $mode) { |
| 72 | + //Create new tables and move data |
| 73 | + $this->modeSwitcher->createTables($mode); |
| 74 | + $this->modeSwitcher->moveData($mode, $previousMode); |
| 75 | + |
| 76 | + //Change config options |
| 77 | + $this->configWriter->saveConfig(ModeSwitcher::XML_PATH_PRICE_DIMENSIONS_MODE, $mode); |
| 78 | + $this->cacheTypeList->cleanType('config'); |
| 79 | + $this->objectManager->get(Config::class)->clean(); |
| 80 | + |
| 81 | + //Delete old tables |
| 82 | + $this->modeSwitcher->dropTables($previousMode); |
| 83 | + } else { |
| 84 | + $this->fail('Dimensions mode for indexer has not been changed', $test); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Handler for 'startTest' event |
| 90 | + * |
| 91 | + * @param TestCase $test |
| 92 | + * @return void |
| 93 | + */ |
| 94 | + public function startTest(TestCase $test) |
| 95 | + { |
| 96 | + $source = $test->getAnnotations(); |
| 97 | + |
| 98 | + if (isset($source['method']['magentoIndexerDimensionMode'])) { |
| 99 | + $annotations = $source['method']['magentoIndexerDimensionMode']; |
| 100 | + } elseif (isset($source['class']['magentoIndexerDimensionMode'])) { |
| 101 | + $annotations = $source['class']['magentoIndexerDimensionMode']; |
| 102 | + } else { |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + $dbIsolation = $source['method']['magentoDbIsolation'] |
| 107 | + ?? $source['class']['magentoDbIsolation'] |
| 108 | + ?? ['disabled']; |
| 109 | + |
| 110 | + if ($dbIsolation[0] != 'disabled') { |
| 111 | + $this->fail("Invalid @magentoDbIsolation declaration: $dbIsolation[0]", $test); |
| 112 | + } |
| 113 | + |
| 114 | + list($indexerType, $indexerMode) = explode(' ', $annotations[0]); |
| 115 | + |
| 116 | + if ($indexerType == Processor::INDEXER_ID) { |
| 117 | + $this->isDimensionMode = true; |
| 118 | + $this->setDimensionMode($indexerMode, $test); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Handler for 'endTest' event |
| 124 | + * |
| 125 | + * @return void |
| 126 | + */ |
| 127 | + public function endTest() |
| 128 | + { |
| 129 | + if ($this->isDimensionMode) { |
| 130 | + $this->restoreDb(); |
| 131 | + $this->isDimensionMode = false; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Fails the test with specified error message |
| 137 | + * |
| 138 | + * @param string $message |
| 139 | + * @param TestCase $test |
| 140 | + * @throws \Exception |
| 141 | + */ |
| 142 | + private function fail($message, TestCase $test) |
| 143 | + { |
| 144 | + $test->fail("{$message} in the test '{$test->toString()}'"); |
| 145 | + } |
| 146 | +} |
0 commit comments