Skip to content

Commit 3d32fe6

Browse files
authored
Merge pull request #60 from tddwizard/config-fixture
Config fixture
2 parents d24e876 + 6e904e2 commit 3d32fe6

File tree

3 files changed

+175
-0
lines changed

3 files changed

+175
-0
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,17 @@ public function testSomethingWithMultipleProducts()
300300
301301
```
302302

303+
### Config Fixtures
304+
305+
With the config fixture you can set a configuration value globally, i.e. it will ensure that it is not only set in the default scope but also in all store scopes:
306+
```
307+
ConfigFixture::setGlobal('general/store_information/name', 'Ye Olde Wizard Shop');
308+
```
309+
310+
It uses `MutableScopeConfigInterface`, so the configuration is not persisted in the database. Use `@magentoAppIsolation enabled` in your test to make sure that changes are reverted in subsequent tests.
311+
312+
You can also set configuration values explicitly for stores with `ConfigFixture::setForStore()`
313+
303314
## Credits
304315

305316
- [Fabian Schmengler][link-author]

src/Core/ConfigFixture.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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\Api\StoreRepositoryInterface;
9+
use Magento\Store\Model\ScopeInterface;
10+
use Magento\TestFramework\Helper\Bootstrap;
11+
12+
class ConfigFixture
13+
{
14+
15+
/**
16+
* Sets configuration in default scope AND all stores, no matter what was configured previously
17+
*
18+
* @param string $path
19+
* @param mixed $value
20+
*/
21+
public static function setGlobal(string $path, $value): void
22+
{
23+
self::scopeConfig()->setValue($path, $value, ScopeConfigInterface::SCOPE_TYPE_DEFAULT);
24+
foreach (self::storeRepository()->getList() as $store) {
25+
self::scopeConfig()->setValue($path, $value, ScopeInterface::SCOPE_STORE, $store->getCode());
26+
}
27+
}
28+
29+
/**
30+
* Sets configuration in store scope
31+
*
32+
* @param string $path
33+
* @param mixed $value
34+
* @param null $storeCode store code or NULL for current store
35+
*/
36+
public static function setForStore(string $path, $value, $storeCode = null) : void
37+
{
38+
self::scopeConfig()->setValue($path, $value, ScopeInterface::SCOPE_STORE, $storeCode);
39+
}
40+
41+
private static function scopeConfig(): MutableScopeConfigInterface
42+
{
43+
return Bootstrap::getObjectManager()->get(MutableScopeConfigInterface::class);
44+
}
45+
46+
private static function storeRepository(): StoreRepositoryInterface
47+
{
48+
return Bootstrap::getObjectManager()->get(StoreRepositoryInterface::class);
49+
}
50+
}

tests/Core/ConfigFixtureTest.php

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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

Comments
 (0)