Skip to content

Commit e3fbde4

Browse files
committed
feat: Change DeploymentConfig to be able adding multiple database connection configs
1 parent 1b1c592 commit e3fbde4

File tree

2 files changed

+76
-31
lines changed

2 files changed

+76
-31
lines changed

src/DeploymentConfig.php

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,34 @@ public function withDatabaseConnection(
3434
string $username,
3535
string $password,
3636
string $dbname,
37-
string $tablePrefix = ''
37+
string $connectionName = 'default'
3838
): 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-
]);
39+
return $this
40+
->withSetting($this->databaseSettingPath($connectionName, 'host'), $host)
41+
->withSetting($this->databaseSettingPath($connectionName, 'username'), $username)
42+
->withSetting($this->databaseSettingPath($connectionName, 'password'), $password)
43+
->withSetting($this->databaseSettingPath($connectionName, 'dbname'), $dbname)
44+
->withSetting($this->databaseSettingPath($connectionName, 'active'), 1)
45+
->withSetting($this->databaseSettingPath($connectionName, 'engine'), 'innodb')
46+
->withSetting($this->databaseSettingPath($connectionName, 'initStatements'), ['SET NAMES utf8'])
47+
->withSetting($this->databaseSettingPath($connectionName, 'driver_options'), [
48+
PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false
49+
]);
5050
}
5151

5252
public function withSetting(string $path, mixed $value): self
5353
{
5454
$data = $this->data->withValue('file', $path, $value);
5555
return new self($data);
5656
}
57+
58+
public function withTablePrefix(string $prefix): self
59+
{
60+
return $this->withSetting('db/table_prefix', $prefix);
61+
}
62+
63+
private function databaseSettingPath(string $connectionName, string $path)
64+
{
65+
return sprintf('db/connection/%s/%s', $connectionName, $path);
66+
}
5767
}

tests/DeploymentConfigTest.php

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,62 @@ public function generatesReadyToUseDbConnectionSettings()
4040
{
4141
$this->assertEquals(
4242
[
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-
],
43+
'default' => [
44+
'host' => 'db',
45+
'username' => 'magento',
46+
'password' => 'magento_pwd',
47+
'dbname' => 'magento2',
48+
'engine' => 'innodb',
49+
'initStatements' => [
50+
'SET NAMES utf8',
51+
],
52+
'active' => 1,
53+
'driver_options' => [
54+
\PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false
55+
],
56+
]
6057
],
6158
DeploymentConfig::new()
62-
->withDatabaseConnection('db', 'magento', 'magento_pwd', 'magento2', 'magento_')
63-
->getConfigData('db')
59+
->withDatabaseConnection('db', 'magento', 'magento_pwd', 'magento2')
60+
->get('db/connection')
61+
);
62+
}
63+
64+
65+
#[Test]
66+
public function allowsToSpecifyCustomConnection()
67+
{
68+
$this->assertEquals(
69+
[
70+
'indexer' => [
71+
'host' => 'db',
72+
'username' => 'magento',
73+
'password' => 'magento_pwd',
74+
'dbname' => 'magento2_indexer',
75+
'engine' => 'innodb',
76+
'initStatements' => [
77+
'SET NAMES utf8',
78+
],
79+
'active' => 1,
80+
'driver_options' => [
81+
\PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false
82+
],
83+
]
84+
],
85+
DeploymentConfig::new()
86+
->withDatabaseConnection('db', 'magento', 'magento_pwd', 'magento2_indexer', 'indexer')
87+
->get('db/connection')
88+
);
89+
}
90+
91+
#[Test]
92+
public function allowsSpecifyingTablePrefix()
93+
{
94+
$this->assertEquals(
95+
'magento_2_',
96+
DeploymentConfig::new()
97+
->withTablePrefix('magento_2_')
98+
->get('db/table_prefix'),
6499
);
65100
}
66101
}

0 commit comments

Comments
 (0)