Skip to content

Commit 7f6863f

Browse files
author
Bohdan Korablov
committed
MAGECLOUD-2729: Reading environment-specific .magento.env.yaml doesn't work on build phase
1 parent edb6678 commit 7f6863f

File tree

6 files changed

+14
-256
lines changed

6 files changed

+14
-256
lines changed

src/Config/Environment.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -262,14 +262,4 @@ public function isMasterBranch(): bool
262262
return isset($_ENV['MAGENTO_CLOUD_ENVIRONMENT'])
263263
&& preg_match(self::GIT_MASTER_BRANCH_RE, $_ENV['MAGENTO_CLOUD_ENVIRONMENT']);
264264
}
265-
266-
/**
267-
* Returns branch name of the current environment.
268-
*
269-
* @return string
270-
*/
271-
public function getBranchName(): string
272-
{
273-
return $_ENV['MAGENTO_CLOUD_BRANCH'] ?? '';
274-
}
275265
}

src/Config/Environment/Reader.php

Lines changed: 5 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
*/
66
namespace Magento\MagentoCloud\Config\Environment;
77

8-
use Magento\MagentoCloud\Config\Environment;
98
use Magento\MagentoCloud\Filesystem\ConfigFileList;
109
use Magento\MagentoCloud\Filesystem\FileSystemException;
1110
use Magento\MagentoCloud\Filesystem\Reader\ReaderInterface;
1211
use Magento\MagentoCloud\Filesystem\Driver\File;
13-
use Magento\MagentoCloud\Filesystem\SystemList;
1412
use Symfony\Component\Yaml\Yaml;
1513
use Symfony\Component\Yaml\Exception\ParseException;
1614

@@ -19,21 +17,6 @@
1917
*/
2018
class Reader implements ReaderInterface
2119
{
22-
/**
23-
* @string
24-
*/
25-
const DIR_ENV_CONFIG = '.magento.env';
26-
27-
/**
28-
* @var SystemList
29-
*/
30-
private $systemList;
31-
32-
/**
33-
* @var Environment
34-
*/
35-
private $environment;
36-
3720
/**
3821
* @var ConfigFileList
3922
*/
@@ -52,19 +35,11 @@ class Reader implements ReaderInterface
5235
private $config;
5336

5437
/**
55-
* @param SystemList $systemList
56-
* @param Environment $environment
5738
* @param ConfigFileList $configFileList
5839
* @param File $file
5940
*/
60-
public function __construct(
61-
SystemList $systemList,
62-
Environment $environment,
63-
ConfigFileList $configFileList,
64-
File $file
65-
) {
66-
$this->systemList = $systemList;
67-
$this->environment = $environment;
41+
public function __construct(ConfigFileList $configFileList, File $file)
42+
{
6843
$this->configFileList = $configFileList;
6944
$this->file = $file;
7045
}
@@ -77,73 +52,12 @@ public function __construct(
7752
public function read(): array
7853
{
7954
if ($this->config === null) {
80-
$mainConfigPath = $this->configFileList->getEnvConfig();
55+
$path = $this->configFileList->getEnvConfig();
8156

82-
$branchConfigPath = sprintf(
83-
'%s/%s/%s.yaml',
84-
$this->systemList->getMagentoRoot(),
85-
self::DIR_ENV_CONFIG,
86-
$this->environment->getBranchName()
87-
);
88-
89-
$this->config = $this->mergeConfigs(
90-
$this->parseConfig($mainConfigPath),
91-
$this->parseConfig($branchConfigPath)
92-
);
57+
$this->config = !$this->file->isExists($path) ?
58+
[] : (array)Yaml::parse($this->file->fileGetContents($path));
9359
}
9460

9561
return $this->config;
9662
}
97-
98-
/**
99-
* Returns parsed yaml config from file.
100-
* Returns an empty array if file doesn't exists
101-
*
102-
* @param string $path
103-
* @return array
104-
* @throws FileSystemException
105-
*/
106-
private function parseConfig(string $path): array
107-
{
108-
return !$this->file->isExists($path) ?
109-
[] : (array)Yaml::parse($this->file->fileGetContents($path));
110-
}
111-
112-
/**
113-
* Merges configuration from $branchConfig into $mainConfig by each stage or by each logger configuration.
114-
*
115-
* Separately merges each stage and logger configuration to avoid cases when the variable has an array value
116-
* and it should be replaced instead of recursive merging.
117-
*
118-
* @param array $mainConfig
119-
* @param array $branchConfig
120-
* @return array
121-
*/
122-
private function mergeConfigs(array $mainConfig, array $branchConfig): array
123-
{
124-
$newConfig = $mainConfig;
125-
126-
foreach ($branchConfig as $sectionName => $sectionConfig) {
127-
if (!is_array($sectionConfig)) {
128-
continue;
129-
}
130-
131-
foreach ($sectionConfig as $stageName => $stageConfig) {
132-
if (!is_array($stageConfig)) {
133-
continue;
134-
}
135-
136-
if (isset($newConfig[$sectionName][$stageName])) {
137-
$newConfig[$sectionName][$stageName] = array_merge(
138-
$newConfig[$sectionName][$stageName],
139-
$stageConfig
140-
);
141-
} else {
142-
$newConfig[$sectionName][$stageName] = $stageConfig;
143-
}
144-
}
145-
}
146-
147-
return $newConfig;
148-
}
14963
}

src/Test/Unit/Config/Environment/ReaderTest.php

Lines changed: 9 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55
*/
66
namespace Magento\MagentoCloud\Test\Unit\Config\Environment;
77

8-
use Magento\MagentoCloud\Config\Environment;
98
use Magento\MagentoCloud\Config\Environment\Reader;
109
use Magento\MagentoCloud\Filesystem\ConfigFileList;
1110
use Magento\MagentoCloud\Filesystem\Driver\File;
12-
use Magento\MagentoCloud\Filesystem\SystemList;
1311
use phpmock\phpunit\PHPMock;
1412
use PHPUnit\Framework\MockObject\MockObject;
1513
use PHPUnit\Framework\TestCase;
@@ -21,16 +19,6 @@ class ReaderTest extends TestCase
2119
{
2220
use PHPMock;
2321

24-
/**
25-
* @var SystemList|MockObject
26-
*/
27-
private $systemListMock;
28-
29-
/**
30-
* @var Environment|MockObject
31-
*/
32-
private $environmentMock;
33-
3422
/**
3523
* @var ConfigFileList|MockObject
3624
*/
@@ -57,14 +45,10 @@ protected function setUp()
5745
self::defineFunctionMock('Magento\MagentoCloud\Filesystem\Driver', 'file_get_contents');
5846
self::defineFunctionMock('Magento\MagentoCloud\Filesystem\Driver', 'file_exists');
5947

60-
$this->systemListMock = $this->createMock(SystemList::class);
61-
$this->environmentMock = $this->createMock(Environment::class);
6248
$this->configFileListMock = $this->createMock(ConfigFileList::class);
6349
$this->fileMock = $this->createPartialMock(File::class, []);
6450

6551
$this->reader = new Reader(
66-
$this->systemListMock,
67-
$this->environmentMock,
6852
$this->configFileListMock,
6953
$this->fileMock
7054
);
@@ -77,98 +61,18 @@ public function testRead()
7761
$this->configFileListMock->expects($this->once())
7862
->method('getEnvConfig')
7963
->willReturn($baseDir . '/.magento.env.yaml');
80-
$this->systemListMock->expects($this->once())
81-
->method('getMagentoRoot')
82-
->willReturn($baseDir);
83-
$this->environmentMock->expects($this->once())
84-
->method('getBranchName')
85-
->willReturn('test-branch');
8664

8765
$this->reader->read();
8866
$this->assertEquals(
8967
[
9068
'stage' => [
91-
'global' => ['SCD_ON_DEMAND' => false, 'UPDATE_URLS' => false],
92-
'deploy' => ['DATABASE_CONFIGURATION' => ['host' => 'localhost'], 'SCD_THREADS' => 3],
93-
'build' => ['SCD_THREADS' => 2],
94-
],
95-
'log' => [
96-
'gelf' => [
97-
'min_level' => 'info',
98-
'use_default_formatter' => true,
99-
'additional' => ['project' => 'project'],
100-
],
101-
'syslog' => ['ident' => 'ident-branch', 'facility' => 7],
102-
],
103-
],
104-
$this->reader->read()
105-
);
106-
}
107-
108-
public function testReadBranchConfigNotExists()
109-
{
110-
$baseDir = __DIR__ . '/_file/';
111-
112-
$this->configFileListMock->expects($this->once())
113-
->method('getEnvConfig')
114-
->willReturn($baseDir . '/.magento.env.yaml');
115-
$this->systemListMock->expects($this->once())
116-
->method('getMagentoRoot')
117-
->willReturn($baseDir);
118-
$this->environmentMock->expects($this->once())
119-
->method('getBranchName')
120-
->willReturn('not-exist');
121-
122-
$this->assertEquals(
123-
[
124-
'stage' => [
125-
'global' => ['SCD_ON_DEMAND' => true, 'UPDATE_URLS' => false],
126-
'deploy' => [
127-
'DATABASE_CONFIGURATION' => [
128-
'host' => '127.0.0.1',
129-
'port' => '3306',
130-
'schema' => 'test_schema',
131-
],
132-
'SCD_THREADS' => 5,
69+
'global' => [
70+
'SCD_ON_DEMAND' => true,
71+
'UPDATE_URLS' => false
13372
],
134-
],
135-
'log' => [
136-
'gelf' => [
137-
'min_level' => 'info',
138-
'use_default_formatter' => true,
139-
'additional' => ['project' => 'project', 'app_id' => 'app'],
140-
],
141-
],
142-
],
143-
$this->reader->read()
144-
);
145-
}
146-
147-
public function testReadBranchConfigWithEmptySectionAndStage()
148-
{
149-
$baseDir = __DIR__ . '/_file/';
150-
151-
$this->configFileListMock->expects($this->once())
152-
->method('getEnvConfig')
153-
->willReturn($baseDir . '/.magento.env.yaml');
154-
$this->systemListMock->expects($this->once())
155-
->method('getMagentoRoot')
156-
->willReturn($baseDir);
157-
$this->environmentMock->expects($this->once())
158-
->method('getBranchName')
159-
->willReturn('test-branch-emty');
160-
161-
$this->assertEquals(
162-
[
163-
'stage' => [
164-
'global' => ['SCD_ON_DEMAND' => true, 'UPDATE_URLS' => false],
16573
'deploy' => [
166-
'DATABASE_CONFIGURATION' => [
167-
'host' => '127.0.0.1',
168-
'port' => '3306',
169-
'schema' => 'test_schema',
170-
],
171-
'SCD_THREADS' => 5,
74+
'DATABASE_CONFIGURATION' => ['host' => '127.0.0.1', 'port' => 3306, 'schema' => 'test_schema'],
75+
'SCD_THREADS' => 5
17276
],
17377
],
17478
'log' => [
@@ -190,29 +94,16 @@ public function testReadMainConfigWithEmptySectionAndStage()
19094
$this->configFileListMock->expects($this->once())
19195
->method('getEnvConfig')
19296
->willReturn($baseDir . '/.magento-with-empty-sections.env.yaml');
193-
$this->systemListMock->expects($this->once())
194-
->method('getMagentoRoot')
195-
->willReturn($baseDir);
196-
$this->environmentMock->expects($this->once())
197-
->method('getBranchName')
198-
->willReturn('test-branch');
19997

20098
$this->reader->read();
20199
$this->assertEquals(
202100
[
203101
'stage' => [
204-
'global' => ['SCD_ON_DEMAND' => false],
205-
'deploy' => ['DATABASE_CONFIGURATION' => ['host' => 'localhost'], 'SCD_THREADS' => 3],
206-
'build' => ['SCD_THREADS' => 2],
207-
],
208-
'log' => [
209-
'gelf' => [
210-
'min_level' => 'info',
211-
'use_default_formatter' => true,
212-
'additional' => ['project' => 'project'],
213-
],
214-
'syslog' => ['ident' => 'ident-branch', 'facility' => 7],
102+
'global' => ['SCD_ON_DEMAND' => true],
103+
'deploy' => null,
104+
'build' => null,
215105
],
106+
'log' => null,
216107
],
217108
$this->reader->read()
218109
);

src/Test/Unit/Config/Environment/_file/.magento.env/test-branch-emty.yaml

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/Test/Unit/Config/Environment/_file/.magento.env/test-branch.yaml

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/Test/Unit/Config/EnvironmentTest.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,4 @@ public function isMasterBranchDataProvider(): array
152152
[true, 'production-lad13m'],
153153
];
154154
}
155-
156-
public function testGetBranchName()
157-
{
158-
$_ENV['MAGENTO_CLOUD_BRANCH'] = 'test-branch';
159-
160-
$this->assertEquals('test-branch', $this->environment->getBranchName());
161-
}
162-
163-
public function testGetBranchNameEmpty()
164-
{
165-
$this->assertEquals('', $this->environment->getBranchName());
166-
}
167155
}

0 commit comments

Comments
 (0)