|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace TddWizard\Fixtures\Core; |
| 5 | + |
| 6 | +use Magento\Framework\App\Config\MutableScopeConfigInterface; |
| 7 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 8 | +use Magento\Store\Model\ScopeInterface; |
| 9 | +use Magento\Store\Model\StoreManagerInterface; |
| 10 | +use Magento\TestFramework\Helper\Bootstrap; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | + |
| 13 | +/** |
| 14 | + * @magentoAppIsolation enabled |
| 15 | + * @magentoDbIsolation enabled |
| 16 | + */ |
| 17 | +class ConfigFixtureTest extends TestCase |
| 18 | +{ |
| 19 | + const STORE_NAME_PATH = 'general/store_information/name'; |
| 20 | + private const SECOND_STORE_ID_FROM_FIXTURE = 'fixture_second_store'; |
| 21 | + private const FIRST_STORE_ID = 'default'; |
| 22 | + /** |
| 23 | + * @var MutableScopeConfigInterface |
| 24 | + */ |
| 25 | + private $scopeConfig; |
| 26 | + /** |
| 27 | + * @var StoreManagerInterface |
| 28 | + */ |
| 29 | + private $storeManager; |
| 30 | + |
| 31 | + protected function setUp(): void |
| 32 | + { |
| 33 | + $objectManager = Bootstrap::getObjectManager(); |
| 34 | + $this->scopeConfig = $objectManager->get(MutableScopeConfigInterface::class); |
| 35 | + $this->storeManager = $objectManager->get(StoreManagerInterface::class); |
| 36 | + } |
| 37 | + |
| 38 | + public function testSetGlobalChangesDefaultScope() |
| 39 | + { |
| 40 | + ConfigFixture::setGlobal(self::STORE_NAME_PATH, 'Ye Olde Wizard Shop'); |
| 41 | + $this->assertConfigValue( |
| 42 | + 'Ye Olde Wizard Shop', |
| 43 | + self::STORE_NAME_PATH, |
| 44 | + ScopeConfigInterface::SCOPE_TYPE_DEFAULT |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @magentoAppArea frontend |
| 50 | + */ |
| 51 | + public function testSetGlobalOverridesAllScopes() |
| 52 | + { |
| 53 | + $this->givenStoreValue(self::STORE_NAME_PATH, 'Store Override'); |
| 54 | + $this->givenWebsiteValue(self::STORE_NAME_PATH, 'Website Override'); |
| 55 | + ConfigFixture::setGlobal(self::STORE_NAME_PATH, 'Global Value'); |
| 56 | + $this->assertConfigValue('Global Value', self::STORE_NAME_PATH, ScopeInterface::SCOPE_STORE); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @magentoAppArea frontend |
| 61 | + * @magentoDataFixture Magento/Store/_files/second_store.php |
| 62 | + */ |
| 63 | + public function testSetForStoreWithCurrentStore() |
| 64 | + { |
| 65 | + $this->storeManager->setCurrentStore(self::SECOND_STORE_ID_FROM_FIXTURE); |
| 66 | + ConfigFixture::setForStore(self::STORE_NAME_PATH, 'Store store'); |
| 67 | + $this->assertConfigValue( |
| 68 | + 'Store store', |
| 69 | + self::STORE_NAME_PATH, |
| 70 | + ScopeInterface::SCOPE_STORE, |
| 71 | + self::SECOND_STORE_ID_FROM_FIXTURE |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @magentoAppArea frontend |
| 77 | + * @magentoDataFixture Magento/Store/_files/second_store.php |
| 78 | + */ |
| 79 | + public function testSetForStoreWithExplicitStore() |
| 80 | + { |
| 81 | + ConfigFixture::setForStore(self::STORE_NAME_PATH, 'Store 1', self::FIRST_STORE_ID); |
| 82 | + ConfigFixture::setForStore(self::STORE_NAME_PATH, 'Store 2', self::SECOND_STORE_ID_FROM_FIXTURE); |
| 83 | + $this->assertConfigValue( |
| 84 | + 'Store 1', |
| 85 | + self::STORE_NAME_PATH, |
| 86 | + ScopeInterface::SCOPE_STORE, |
| 87 | + self::FIRST_STORE_ID |
| 88 | + ); |
| 89 | + $this->assertConfigValue( |
| 90 | + 'Store 2', |
| 91 | + self::STORE_NAME_PATH, |
| 92 | + ScopeInterface::SCOPE_STORE, |
| 93 | + self::SECOND_STORE_ID_FROM_FIXTURE |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + private function givenStoreValue(string $path, string $storeValue): void |
| 98 | + { |
| 99 | + $this->scopeConfig->setValue($path, $storeValue, ScopeInterface::SCOPE_STORE); |
| 100 | + } |
| 101 | + |
| 102 | + private function givenWebsiteValue(string $path, string $websiteValue): void |
| 103 | + { |
| 104 | + $this->scopeConfig->setValue($path, $websiteValue, ScopeInterface::SCOPE_WEBSITE); |
| 105 | + } |
| 106 | + |
| 107 | + private function assertConfigValue($expectedValue, string $path, string $scope, string $scopeCode = null): void |
| 108 | + { |
| 109 | + $this->assertEquals( |
| 110 | + $expectedValue, |
| 111 | + $this->scopeConfig->getValue($path, $scope, $scopeCode) |
| 112 | + ); |
| 113 | + } |
| 114 | +} |
0 commit comments