Skip to content

Commit 597b769

Browse files
ISSUE-33802: ensure that deployment config will reload its data, if the key was not found
1 parent 9728fe3 commit 597b769

File tree

2 files changed

+60
-37
lines changed

2 files changed

+60
-37
lines changed

lib/internal/Magento/Framework/App/DeploymentConfig.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,13 @@ public function __construct(DeploymentConfig\Reader $reader, $overrideData = [])
7373
public function get($key = null, $defaultValue = null)
7474
{
7575
$this->load();
76-
if ($key === null) {
77-
return $this->flatData;
78-
}
79-
80-
if (array_key_exists($key, $this->flatData) && $this->flatData[$key] === null) {
81-
return '';
76+
$result = $this->getByKey($key);
77+
if ($result === null) {
78+
$this->resetData();
79+
$this->load();
80+
$result = $this->getByKey($key);
8281
}
83-
84-
return $this->flatData[$key] ?? $defaultValue;
82+
return $result ?? $defaultValue;
8583
}
8684

8785
/**
@@ -171,7 +169,7 @@ private function load()
171169
* @return array
172170
* @throws RuntimeException
173171
*/
174-
private function flattenParams(array $params, $path = null, array &$flattenResult = null) : array
172+
private function flattenParams(array $params, $path = null, array &$flattenResult = null): array
175173
{
176174
if (null === $flattenResult) {
177175
$flattenResult = [];
@@ -195,4 +193,21 @@ private function flattenParams(array $params, $path = null, array &$flattenResul
195193

196194
return $flattenResult;
197195
}
196+
197+
/**
198+
* @param string|null $key
199+
* @return array|mixed|string
200+
*/
201+
protected function getByKey($key)
202+
{
203+
if ($key === null) {
204+
return $this->flatData;
205+
}
206+
207+
if (array_key_exists($key, $this->flatData) && $this->flatData[$key] === null) {
208+
return '';
209+
}
210+
211+
return $this->flatData[$key] ?? null;
212+
}
198213
}

lib/internal/Magento/Framework/App/Test/Unit/DeploymentConfigTest.php

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php
2+
23
/**
34
* Copyright © Magento, Inc. All rights reserved.
45
* See COPYING.txt for license details.
56
*/
7+
68
declare(strict_types=1);
79

810
namespace Magento\Framework\App\Test\Unit;
@@ -20,12 +22,12 @@ class DeploymentConfigTest extends TestCase
2022
*/
2123
private static $fixture
2224
= [
23-
'configData1' => 'scalar_value',
24-
'configData2' => [
25+
'configData1' => 'scalar_value',
26+
'configData2' => [
2527
'foo' => 1,
2628
'bar' => ['baz' => 2],
2729
],
28-
'configData3' => null,
30+
'configData3' => null,
2931
'test_override' => 'original',
3032
];
3133

@@ -34,16 +36,16 @@ class DeploymentConfigTest extends TestCase
3436
*/
3537
private static $flattenedFixture
3638
= [
37-
'configData1' => 'scalar_value',
38-
'configData2' => [
39+
'configData1' => 'scalar_value',
40+
'configData2' => [
3941
'foo' => 1,
4042
'bar' => ['baz' => 2],
4143
],
42-
'configData2/foo' => 1,
43-
'configData2/bar' => ['baz' => 2],
44+
'configData2/foo' => 1,
45+
'configData2/bar' => ['baz' => 2],
4446
'configData2/bar/baz' => 2,
45-
'configData3' => null,
46-
'test_override' => 'overridden',
47+
'configData3' => null,
48+
'test_override' => 'overridden',
4749
];
4850

4951
/**
@@ -69,32 +71,30 @@ class DeploymentConfigTest extends TestCase
6971
/**
7072
* @var MockObject
7173
*/
72-
private $reader;
74+
private $readerMock;
7375

7476
public static function setUpBeforeClass(): void
7577
{
76-
self::$fixtureConfig = require __DIR__ . '/_files/config.php';
78+
self::$fixtureConfig = require __DIR__ . '/_files/config.php';
7779
self::$fixtureConfigMerged = require __DIR__ . '/_files/other/local_developer_merged.php';
7880
}
7981

8082
protected function setUp(): void
8183
{
82-
$this->reader = $this->createMock(Reader::class);
83-
$this->_deploymentConfig = new DeploymentConfig(
84-
$this->reader,
84+
$this->readerMock = $this->createMock(Reader::class);
85+
$this->_deploymentConfig = new DeploymentConfig(
86+
$this->readerMock,
8587
['test_override' => 'overridden']
8688
);
8789
$this->_deploymentConfigMerged = new DeploymentConfig(
88-
$this->reader,
90+
$this->readerMock,
8991
require __DIR__ . '/_files/other/local_developer.php'
9092
);
9193
}
9294

9395
public function testGetters(): void
9496
{
95-
$this->reader->expects($this->once())->method('load')->willReturn(self::$fixture);
96-
$this->assertSame(self::$flattenedFixture, $this->_deploymentConfig->get());
97-
// second time to ensure loader will be invoked only once
97+
$this->readerMock->expects($this->any())->method('load')->willReturn(self::$fixture);
9898
$this->assertSame(self::$flattenedFixture, $this->_deploymentConfig->get());
9999
$this->assertSame('scalar_value', $this->_deploymentConfig->getConfigData('configData1'));
100100
$this->assertSame(self::$fixture['configData2'], $this->_deploymentConfig->getConfigData('configData2'));
@@ -107,19 +107,19 @@ public function testGetters(): void
107107

108108
public function testIsAvailable(): void
109109
{
110-
$this->reader->expects($this->once())->method('load')->willReturn(
110+
$this->readerMock->expects($this->once())->method('load')->willReturn(
111111
[
112112
ConfigOptionsListConstants::CONFIG_PATH_INSTALL_DATE => 1,
113113
]
114114
);
115-
$object = new DeploymentConfig($this->reader);
115+
$object = new DeploymentConfig($this->readerMock);
116116
$this->assertTrue($object->isAvailable());
117117
}
118118

119119
public function testNotAvailable(): void
120120
{
121-
$this->reader->expects($this->once())->method('load')->willReturn([]);
122-
$object = new DeploymentConfig($this->reader);
121+
$this->readerMock->expects($this->once())->method('load')->willReturn([]);
122+
$object = new DeploymentConfig($this->readerMock);
123123
$this->assertFalse($object->isAvailable());
124124
}
125125

@@ -128,8 +128,8 @@ public function testNotAvailable(): void
128128
*/
129129
public function testNotAvailableThenAvailable(): void
130130
{
131-
$this->reader->expects($this->once())->method('load')->willReturn(['Test']);
132-
$object = new DeploymentConfig($this->reader);
131+
$this->readerMock->expects($this->once())->method('load')->willReturn(['Test']);
132+
$object = new DeploymentConfig($this->readerMock);
133133
$this->assertFalse($object->isAvailable());
134134
$this->assertFalse($object->isAvailable());
135135
}
@@ -142,8 +142,8 @@ public function testKeyCollision(array $data): void
142142
{
143143
$this->expectException('Exception');
144144
$this->expectExceptionMessage('Key collision');
145-
$this->reader->expects($this->once())->method('load')->willReturn($data);
146-
$object = new DeploymentConfig($this->reader);
145+
$this->readerMock->expects($this->once())->method('load')->willReturn($data);
146+
$object = new DeploymentConfig($this->readerMock);
147147
$object->get();
148148
}
149149

@@ -163,7 +163,7 @@ public function keyCollisionDataProvider(): array
163163

164164
public function testResetData(): void
165165
{
166-
$this->reader->expects($this->exactly(2))->method('load')->willReturn(self::$fixture);
166+
$this->readerMock->expects($this->exactly(2))->method('load')->willReturn(self::$fixture);
167167
$this->assertSame(self::$flattenedFixture, $this->_deploymentConfig->get());
168168
$this->_deploymentConfig->resetData();
169169
// second time to ensure loader will be invoked only once after reset
@@ -173,9 +173,17 @@ public function testResetData(): void
173173

174174
public function testIsDbAvailable(): void
175175
{
176-
$this->reader->expects($this->exactly(2))->method('load')->willReturnOnConsecutiveCalls([], ['db' => []]);
176+
$this->readerMock->expects($this->exactly(2))->method('load')->willReturnOnConsecutiveCalls([], ['db' => []]);
177177
$this->assertFalse($this->_deploymentConfig->isDbAvailable());
178178
$this->_deploymentConfig->resetData();
179179
$this->assertTrue($this->_deploymentConfig->isDbAvailable());
180180
}
181+
182+
public function testReloadDataOnMissingConfig(): void
183+
{
184+
$this->readerMock->expects($this->exactly(2))->method('load')->willReturn(self::$fixture);
185+
$defaultValue = 'some_default_value';
186+
$result = $this->_deploymentConfig->get('missing/key', $defaultValue);
187+
$this->assertEquals($defaultValue, $result);
188+
}
181189
}

0 commit comments

Comments
 (0)