|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Store\App\Config\Source; |
| 9 | + |
| 10 | +use Magento\Framework\App\DeploymentConfig\FileReader; |
| 11 | +use Magento\Framework\App\DeploymentConfig\Writer; |
| 12 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 13 | +use Magento\Framework\Config\File\ConfigFilePool; |
| 14 | +use Magento\Framework\Filesystem; |
| 15 | +use Magento\Store\Model\StoreManagerInterface; |
| 16 | +use Magento\TestFramework\Helper\Bootstrap; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | + |
| 19 | +/** |
| 20 | + * Test that initial scopes config are loaded if database is available |
| 21 | + */ |
| 22 | +class InitialConfigSourceTest extends TestCase |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var Filesystem |
| 26 | + */ |
| 27 | + private $filesystem; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var ConfigFilePool |
| 31 | + */ |
| 32 | + private $configFilePool; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var FileReader |
| 36 | + */ |
| 37 | + private $reader; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var Writer |
| 41 | + */ |
| 42 | + private $writer; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var array |
| 46 | + */ |
| 47 | + private $config; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var array |
| 51 | + */ |
| 52 | + private $envConfig; |
| 53 | + |
| 54 | + /** |
| 55 | + * @var StoreManagerInterface |
| 56 | + */ |
| 57 | + private $storeManager; |
| 58 | + |
| 59 | + /** |
| 60 | + * @inheritdoc |
| 61 | + */ |
| 62 | + protected function setUp() |
| 63 | + { |
| 64 | + $objectManager = Bootstrap::getObjectManager(); |
| 65 | + $this->reader = $objectManager->get(FileReader::class); |
| 66 | + $this->writer = $objectManager->get(Writer::class); |
| 67 | + $this->filesystem = $objectManager->get(Filesystem::class); |
| 68 | + $this->configFilePool = $objectManager->get(ConfigFilePool::class); |
| 69 | + $this->storeManager = $objectManager->get(StoreManagerInterface::class); |
| 70 | + $this->config = $this->loadConfig(); |
| 71 | + $this->envConfig = $this->loadEnvConfig(); |
| 72 | + $this->loadDumpConfig(); |
| 73 | + $this->storeManager->reinitStores(); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @inheritdoc |
| 78 | + */ |
| 79 | + public function tearDown() |
| 80 | + { |
| 81 | + $this->clearConfig(ConfigFilePool::APP_CONFIG); |
| 82 | + $this->clearConfig(ConfigFilePool::APP_ENV); |
| 83 | + $this->writer->saveConfig([ConfigFilePool::APP_CONFIG => $this->config]); |
| 84 | + $this->writer->saveConfig([ConfigFilePool::APP_ENV => $this->envConfig]); |
| 85 | + $this->storeManager->reinitStores(); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Test that initial scopes config are loaded if database is available |
| 90 | + * |
| 91 | + * @param array $websites |
| 92 | + * @param string $defaultWebsite |
| 93 | + * @param bool $offline |
| 94 | + * @throws \Magento\Framework\Exception\LocalizedException |
| 95 | + * @dataProvider getDefaultDataProvider |
| 96 | + */ |
| 97 | + public function testGetWebsites(array $websites, string $defaultWebsite, bool $offline = false): void |
| 98 | + { |
| 99 | + if ($offline) { |
| 100 | + // remove application environment config for emulate work without db |
| 101 | + $this->clearConfig(ConfigFilePool::APP_ENV); |
| 102 | + } |
| 103 | + $this->assertEquals($defaultWebsite, $this->storeManager->getWebsite()->getCode()); |
| 104 | + $this->assertEquals($websites, array_keys($this->storeManager->getWebsites(true, true)), '', 0.0, 10, true); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @return array |
| 109 | + */ |
| 110 | + public function getDefaultDataProvider(): array |
| 111 | + { |
| 112 | + return [ |
| 113 | + [ |
| 114 | + [ |
| 115 | + 'admin', |
| 116 | + 'base', |
| 117 | + ], |
| 118 | + 'base', |
| 119 | + false |
| 120 | + ], |
| 121 | + [ |
| 122 | + [ |
| 123 | + 'admin', |
| 124 | + 'main', |
| 125 | + ], |
| 126 | + 'main', |
| 127 | + true |
| 128 | + ] |
| 129 | + ]; |
| 130 | + } |
| 131 | + |
| 132 | + private function clearConfig(string $type): void |
| 133 | + { |
| 134 | + $this->filesystem->getDirectoryWrite(DirectoryList::CONFIG)->writeFile( |
| 135 | + $this->configFilePool->getPath($type), |
| 136 | + "<?php\n return [];\n" |
| 137 | + ); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * @return void |
| 142 | + */ |
| 143 | + private function loadDumpConfig(): void |
| 144 | + { |
| 145 | + $data = array_replace_recursive( |
| 146 | + $this->config, |
| 147 | + $this->getDumpConfig() |
| 148 | + ); |
| 149 | + $this->writer->saveConfig([ConfigFilePool::APP_CONFIG => $data], true); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * @return array |
| 154 | + */ |
| 155 | + private function getDumpConfig(): array |
| 156 | + { |
| 157 | + return require __DIR__ . '/../../../_files/dump_config.php'; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * @return array |
| 162 | + */ |
| 163 | + private function loadConfig(): array |
| 164 | + { |
| 165 | + return $this->reader->load(ConfigFilePool::APP_CONFIG); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * @return array |
| 170 | + */ |
| 171 | + private function loadEnvConfig(): array |
| 172 | + { |
| 173 | + return $this->reader->load(ConfigFilePool::APP_ENV); |
| 174 | + } |
| 175 | +} |
0 commit comments