Skip to content

Commit 153b2f8

Browse files
committed
feat: Added DeploymentConfig
chore: covered scope config with more tests
1 parent 566f52b commit 153b2f8

File tree

5 files changed

+178
-1
lines changed

5 files changed

+178
-1
lines changed

composer.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
"phpunit/phpunit": "^11.5",
1616
"brianium/paratest": "^7.7"
1717
},
18+
"suggest": {
19+
"ecomdev/testcontainers-magento-data": "Database Containers pre-populated with various data",
20+
"ext-pdo": "For datbase connections with ResourceConnection implemenation"
21+
},
1822
"license": [
1923
"MIT"
2024
],
@@ -49,5 +53,6 @@
4953
"allow-plugins": {
5054
"magento/composer-dependency-version-audit-plugin": false
5155
}
52-
}
56+
},
57+
"$schema": "https://getcomposer.org/schema.json"
5358
}

src/ArrayConfig.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public function withValue(string $scope, string $path, mixed $value): self
3232

3333
public function getValueByPath(string $scope, string $path): mixed
3434
{
35+
if ($path === '') {
36+
return $this->data[$scope] ?? [];
37+
}
38+
3539
$parts = explode('/', $path);
3640
$current = $this->data[$scope] ?? [];
3741
foreach ($parts as $part) {

src/DeploymentConfig.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace EcomDev\Magento2TestEssentials;
4+
5+
use Magento\Framework\App\DeploymentConfig as MagentoDeploymentConfig;
6+
use PDO;
7+
8+
final class DeploymentConfig extends MagentoDeploymentConfig
9+
{
10+
private function __construct(
11+
private readonly ArrayConfig $data
12+
) {
13+
$reader = new class ($data) extends MagentoDeploymentConfig\Reader {
14+
public function __construct(private readonly ArrayConfig $data)
15+
{
16+
}
17+
18+
public function load($fileKey = null)
19+
{
20+
return $this->data->getValueByPath('file', '');
21+
}
22+
};
23+
24+
parent::__construct($reader);
25+
}
26+
27+
public static function new(): self
28+
{
29+
return new self(ArrayConfig::new());
30+
}
31+
32+
public function withDatabaseConnection(
33+
string $host,
34+
string $username,
35+
string $password,
36+
string $dbname,
37+
string $tablePrefix = ''
38+
): self {
39+
return $this->withSetting('db/table_prefix', $tablePrefix)
40+
->withSetting('db/connection/default/host', $host)
41+
->withSetting('db/connection/default/username', $username)
42+
->withSetting('db/connection/default/password', $password)
43+
->withSetting('db/connection/default/dbname', $dbname)
44+
->withSetting('db/connection/default/active', 1)
45+
->withSetting('db/connection/default/engine', 'innodb')
46+
->withSetting('db/connection/default/initStatements', ['SET NAMES utf8'])
47+
->withSetting('db/connection/default/driver_options', [
48+
PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false
49+
]);
50+
}
51+
52+
public function withSetting(string $path, mixed $value): self
53+
{
54+
$data = $this->data->withValue('file', $path, $value);
55+
return new self($data);
56+
}
57+
}

tests/DeploymentConfigTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace EcomDev\Magento2TestEssentials;
4+
5+
use PHPUnit\Framework\Attributes\Test;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class DeploymentConfigTest extends TestCase
9+
{
10+
#[Test]
11+
public function availableWhenInstallDateSet()
12+
{
13+
$this->assertTrue(
14+
DeploymentConfig::new()
15+
->withSetting('install/date', (new \DateTime())->format('Y-m-d H:i:s'))
16+
->isAvailable()
17+
);
18+
}
19+
20+
#[Test]
21+
public function dbIsNotAvailableWhenNoDbConnection()
22+
{
23+
$this->assertFalse(
24+
DeploymentConfig::new()->isDbAvailable()
25+
);
26+
}
27+
28+
#[Test]
29+
public function reportsDbAvailableWhenSettingsProvided()
30+
{
31+
$this->assertTrue(
32+
DeploymentConfig::new()
33+
->withDatabaseConnection('db', 'magento', 'magento', 'magento')
34+
->isDbAvailable()
35+
);
36+
}
37+
38+
#[Test]
39+
public function generatesReadyToUseDbConnectionSettings()
40+
{
41+
$this->assertEquals(
42+
[
43+
'table_prefix' => 'magento_',
44+
'connection' => [
45+
'default' => [
46+
'host' => 'db',
47+
'username' => 'magento',
48+
'password' => 'magento_pwd',
49+
'dbname' => 'magento2',
50+
'engine' => 'innodb',
51+
'initStatements' => [
52+
'SET NAMES utf8',
53+
],
54+
'active' => 1,
55+
'driver_options' => [
56+
\PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false
57+
],
58+
]
59+
],
60+
],
61+
DeploymentConfig::new()
62+
->withDatabaseConnection('db', 'magento', 'magento_pwd', 'magento2', 'magento_')
63+
->getConfigData('db')
64+
);
65+
}
66+
}

tests/ScopeConfigTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
use EcomDev\Magento2TestEssentials\Store\Store;
1313
use EcomDev\Magento2TestEssentials\Store\StoreManager;
1414
use EcomDev\Magento2TestEssentials\Store\Website;
15+
use Magento\Framework\App\Config\ScopeConfigInterface;
1516
use Magento\Store\Model\ScopeInterface;
17+
use PHPUnit\Framework\Attributes\DataProvider;
1618
use PHPUnit\Framework\TestCase;
1719
use PHPUnit\Framework\Attributes\Test;
1820

@@ -148,4 +150,47 @@ public function castsOnlyTruthyValuesInFlagCheck()
148150
]
149151
);
150152
}
153+
154+
#[Test]
155+
#[DataProvider('combinations')]
156+
public function checkVariousPathCombinations(string $path, string $scope, string $scopeCode, mixed $expectedValue)
157+
{
158+
$config = ScopeConfig::new()
159+
->withStoreManager(
160+
StoreManager::new()
161+
->withWebsite(
162+
Website::new(1, 'base')
163+
)
164+
->withStore(
165+
Store::new(1, 'default')
166+
->withWebsite(1, 1)
167+
)
168+
->withStore(
169+
Store::new(2, 'english')
170+
->withWebsite(1, 1)
171+
)
172+
)
173+
->withDefaultValue('some/other', 'default_one_value')
174+
->withWebsiteValue('base', 'some/other/path_two', 'base_website_value')
175+
->withStoreValue('default', 'some/other/path_three', 'default_store_value')
176+
->withStoreValue('english', 'some/other', null);
177+
178+
$this->assertEquals(
179+
$expectedValue,
180+
$config->getValue($path, $scope, $scopeCode)
181+
);
182+
}
183+
184+
public static function combinations()
185+
{
186+
return [
187+
['some/other/path_three', ScopeInterface::SCOPE_STORE, 'default', 'default_store_value'],
188+
['some/other', ScopeInterface::SCOPE_STORES, 'english', [
189+
'path_two' => 'base_website_value'
190+
]],
191+
['some/other/path_two', ScopeInterface::SCOPE_WEBSITE, 'base', 'base_website_value'],
192+
['some/other/path_two', ScopeInterface::SCOPE_WEBSITES, 'default', null],
193+
['some/other', ScopeConfigInterface::SCOPE_TYPE_DEFAULT, '', 'default_one_value'],
194+
];
195+
}
151196
}

0 commit comments

Comments
 (0)