Skip to content

Commit bcde0fa

Browse files
committed
Fix extendConfig default overriding user-defined values in the config.
1 parent d7278cd commit bcde0fa

File tree

2 files changed

+105
-4
lines changed

2 files changed

+105
-4
lines changed

src/ServiceProviders/ExtendsConfigTrait.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ trait ExtendsConfigTrait {
1818
/**
1919
* Extends the WP Emerge config in the container with a new key.
2020
*
21-
* @codeCoverageIgnore
2221
* @param \Pimple\Container $container
2322
* @param string $key
2423
* @param mixed $default
2524
* @return void
2625
*/
27-
protected function extendConfig( $container, $key, $default ) {
28-
$config = Arr::get( $container, WPEMERGE_CONFIG_KEY . '.' . $key, $default );
26+
public function extendConfig( $container, $key, $default ) {
27+
$config = isset( $container[ WPEMERGE_CONFIG_KEY ] ) ? $container[ WPEMERGE_CONFIG_KEY ] : [];
28+
$config = Arr::get( $config, $key, $default );
2929

30-
if ( is_array( $default ) && is_array( $config ) ) {
30+
if ( $config !== $default && is_array( $config ) && is_array( $default ) ) {
3131
$config = array_replace_recursive( $default, $config );
3232
}
3333

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
namespace WPEmergeTests\ServiceProviders;
4+
5+
use Mockery;
6+
use Pimple\Container;
7+
use WPEmerge\ServiceProviders\ExtendsConfigTrait;
8+
use WP_UnitTestCase;
9+
10+
/**
11+
* @coversDefaultClass \WPEmerge\ServiceProviders\ExtendsConfigTrait
12+
*/
13+
class ExtendsConfigTraitTest extends WP_UnitTestCase {
14+
public function setUp() {
15+
parent::setUp();
16+
17+
$this->subject = $this->getMockForTrait( ExtendsConfigTrait::class );
18+
}
19+
20+
public function tearDown() {
21+
parent::tearDown();
22+
23+
unset( $this->subject );
24+
}
25+
26+
/**
27+
* @covers ::extendConfig
28+
*/
29+
public function testExtendConfig_ConfigNotSet_Default() {
30+
$container = new Container( [
31+
WPEMERGE_CONFIG_KEY => [],
32+
] );
33+
$key = 'foo';
34+
$default = 'bar';
35+
$expected = $default;
36+
37+
$this->subject->extendConfig( $container, $key, $default );
38+
39+
$this->assertEquals( $expected, $container[ WPEMERGE_CONFIG_KEY ][ $key ] );
40+
}
41+
42+
/**
43+
* @covers ::extendConfig
44+
*/
45+
public function testExtendConfig_NotArrays_Replace() {
46+
$container = new Container( [
47+
WPEMERGE_CONFIG_KEY => [
48+
'foo' => 'foo',
49+
],
50+
] );
51+
$key = 'foo';
52+
$default = 'bar';
53+
$expected = 'foo';
54+
55+
$this->subject->extendConfig( $container, $key, $default );
56+
57+
$this->assertEquals( $expected, $container[ WPEMERGE_CONFIG_KEY ][ $key ] );
58+
}
59+
60+
/**
61+
* @covers ::extendConfig
62+
*/
63+
public function testExtendConfig_Arrays_RecursiveReplace() {
64+
$container = new Container( [
65+
WPEMERGE_CONFIG_KEY => [
66+
'foo' => [
67+
'foo' => 'foo',
68+
'bar' => 'bar',
69+
'baz' => [
70+
'foo' => 'foo',
71+
]
72+
],
73+
],
74+
] );
75+
$key = 'foo';
76+
$default = [
77+
'bar' => 'foobarbaz',
78+
'baz' => [
79+
'bar' => 'bar',
80+
],
81+
'foobarbaz' => 'foobarbaz',
82+
];
83+
$expected = [
84+
// Value is NOT missing.
85+
'foo' => 'foo',
86+
// Value is NOT replaced by default.
87+
'bar' => 'bar',
88+
'baz' => [
89+
'foo' => 'foo',
90+
// Key from default is added in nested array.
91+
'bar' => 'bar',
92+
],
93+
// Key from default is added.
94+
'foobarbaz' => 'foobarbaz',
95+
];
96+
97+
$this->subject->extendConfig( $container, $key, $default );
98+
99+
$this->assertEquals( $expected, $container[ WPEMERGE_CONFIG_KEY ][ $key ] );
100+
}
101+
}

0 commit comments

Comments
 (0)