Skip to content

Commit f8fbb55

Browse files
author
Barny Shergold
authored
Issue 667 : Extend environment data to be read from local file (magento#722)
1 parent e4dfd23 commit f8fbb55

File tree

2 files changed

+84
-6
lines changed

2 files changed

+84
-6
lines changed

src/Config/EnvironmentData.php

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99

1010
use Magento\MagentoCloud\Config\System\Variables;
1111
use Magento\MagentoCloud\PlatformVariable\DecoderInterface;
12+
use Magento\MagentoCloud\Filesystem\FileList;
13+
use Magento\MagentoCloud\Filesystem\Driver\File;
14+
use Magento\MagentoCloud\Filesystem\FileSystemException;
15+
use Symfony\Component\Yaml\Yaml;
1216

1317
/**
1418
* Returns cloud environment data.
@@ -30,16 +34,34 @@ class EnvironmentData implements EnvironmentDataInterface
3034
*/
3135
private $data = [];
3236

37+
/**
38+
* @var FileList
39+
*/
40+
private $fileList;
41+
42+
/**
43+
* @var File
44+
*/
45+
private $file;
46+
3347
/**
3448
* Environment constructor.
3549
*
3650
* @param Variables $systemConfig
3751
* @param DecoderInterface $decoder
52+
* @param FileList $fileList
53+
* @param File $file
3854
*/
39-
public function __construct(Variables $systemConfig, DecoderInterface $decoder)
40-
{
55+
public function __construct(
56+
Variables $systemConfig,
57+
DecoderInterface $decoder,
58+
FileList $fileList,
59+
File $file
60+
) {
4161
$this->systemConfig = $systemConfig;
4262
$this->decoder = $decoder;
63+
$this->fileList = $fileList;
64+
$this->file = $file;
4365
}
4466

4567
/**
@@ -114,13 +136,25 @@ public function getApplication(): array
114136
return $this->data['application'];
115137
}
116138

117-
return $this->data['application'] = $this->getEnvVar(SystemConfigInterface::VAR_ENV_APPLICATION, []);
139+
$application = $this->getEnvVar(SystemConfigInterface::VAR_ENV_APPLICATION, []);
140+
141+
if (!$application) {
142+
try {
143+
$application = Yaml::parse(
144+
$this->file->fileGetContents($this->fileList->getAppConfig())
145+
);
146+
} catch (FileSystemException $e) {
147+
// Do nothing as $application needs to be empty
148+
}
149+
}
150+
151+
return $this->data['application'] = $application;
118152
}
119153

120154
/**
121155
* Returns name of environment branch
122-
*
123156
* @return string
157+
*
124158
*/
125159
public function getBranchName(): string
126160
{

src/Test/Unit/Config/EnvironmentDataTest.php

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
use Magento\MagentoCloud\Config\System\Variables;
1414
use Magento\MagentoCloud\Config\SystemConfigInterface;
1515
use Magento\MagentoCloud\PlatformVariable\DecoderInterface;
16+
use Magento\MagentoCloud\Filesystem\FileList;
17+
use Magento\MagentoCloud\Filesystem\Driver\File;
18+
use Magento\MagentoCloud\Filesystem\FileSystemException;
1619
use phpmock\phpunit\PHPMock;
1720
use PHPStan\Testing\TestCase;
1821
use PHPUnit\Framework\MockObject\MockObject;
@@ -39,6 +42,16 @@ class EnvironmentDataTest extends TestCase
3942
*/
4043
private $decoderMock;
4144

45+
/**
46+
* @var FileList|MockObject
47+
*/
48+
private $fileListMock;
49+
50+
/**
51+
* @var File|MockObject
52+
*/
53+
private $fileMock;
54+
4255
/**
4356
* @inheritDoc
4457
*/
@@ -65,8 +78,15 @@ protected function setUp(): void
6578
);
6679

6780
$this->decoderMock = $this->getMockForAbstractClass(DecoderInterface::class);
68-
69-
$this->environmentData = new EnvironmentData($this->variable, $this->decoderMock);
81+
$this->fileListMock = $this->createMock(FileList::class);
82+
$this->fileMock = $this->createMock(File::class);
83+
84+
$this->environmentData = new EnvironmentData(
85+
$this->variable,
86+
$this->decoderMock,
87+
$this->fileListMock,
88+
$this->fileMock
89+
);
7090
}
7191

7292
public function testGetEnv(): void
@@ -127,4 +147,28 @@ public function testGetBranchName(): void
127147

128148
$this->assertEquals('production', $this->environmentData->getBranchName());
129149
}
150+
151+
// Following tests to see if .magento.app.yaml can be read (includes file missing)
152+
public function testGetApplicationWithNoSystemVariablesFileExists(): void
153+
{
154+
$_ENV = null;
155+
156+
$this->fileMock->expects($this->once())
157+
->method('fileGetContents')
158+
->willReturn('[]');
159+
160+
$this->assertEquals([], $this->environmentData->getApplication());
161+
}
162+
163+
public function testGetApplicationWithNoSystemVariablesFileNotExists(): void
164+
{
165+
$_ENV = null;
166+
$exception = new FilesystemException('.magento.app.yaml not exist');
167+
168+
$this->fileMock->expects($this->once())
169+
->method('fileGetContents')
170+
->willThrowException($exception);
171+
172+
$this->assertEquals([], $this->environmentData->getApplication());
173+
}
130174
}

0 commit comments

Comments
 (0)