Skip to content

Commit

Permalink
Merge pull request zfcampus#25 from webimpress/hotfix/21
Browse files Browse the repository at this point in the history
Hotfix zfcampus#21 - patchKey merge configuration instead of override
  • Loading branch information
weierophinney committed Nov 1, 2017
2 parents 09f5af9 + dd3a3f1 commit 8e14178
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 18 deletions.
20 changes: 2 additions & 18 deletions src/ConfigResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,25 +110,9 @@ public function patch($data, $tree = false)
*/
public function patchKey($key, $value)
{
// Get local config file
$config = [];
if (file_exists($this->fileName)) {
$config = include $this->fileName;
if (! is_array($config)) {
$config = [];
}
}
$config = $this->replaceKey($key, $value, $config);
$this->patch([$key => $value]);

// Write to configuration file
$this->writer->toFile($this->fileName, $config);
$this->invalidateCache($this->fileName);

// Reseed configuration
$this->config = $config;

// Return written values
return $config;
return $this->config;
}

/**
Expand Down
128 changes: 128 additions & 0 deletions test/ConfigResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PHPUnit\Framework\TestCase;
use stdClass;
use Zend\Config\Writer\PhpArray;
use Zend\Stdlib\ArrayUtils;
use ZF\Configuration\ConfigResource;

class ConfigResourceTest extends TestCase
Expand Down Expand Up @@ -86,6 +87,8 @@ public function testPatchListUpdatesFileWithMergedConfig()
],
'baz' => 'not what you think',
];
$writer = new PhpArray();
$writer->toFile($this->file, $config);
$configResource = new ConfigResource($config, $this->file, $this->writer);

$patch = [
Expand All @@ -97,8 +100,10 @@ public function testPatchListUpdatesFileWithMergedConfig()
$this->assertEquals($patch, $response);

$expected = [
'foo' => 'bar',
'bar' => [
'baz' => 'UPDATED',
'bat' => 'bogus',
],
'baz' => 'what you think',
];
Expand Down Expand Up @@ -402,4 +407,127 @@ public function testDeleteNonexistentKeyShouldDoNothing()
$test = include $this->file;
$this->assertEquals($expected, $test);
}

public function patchKey()
{
return [
'scalar-top-level' => [
'top',
'updated',
['top' => 'updated']
],

'overwrite-hash' => [
'sub',
'updated',
['sub' => 'updated'],
],

'nested-scalar' => [
'sub.level',
'updated',
[
'sub' => [
'level' => 'updated',
],
],
],
'nested-list' => [
'sub.list',
['three', 'four'],
[
'sub' => [
'list' => ['three', 'four'],
],
],
],
'nested-hash' => [
'sub.hash.two',
'updated',
[
'sub' => [
'hash' => [
'two' => 'updated',
],
],
],
],
'overwrite-nested-null' => [
'sub.null',
'updated',
[
'sub' => [
'null' => 'updated',
],
],
],
'overwrite-nested-object' => [
'sub.object',
'updated',
[
'sub' => [
'object' => 'updated',
],
],
],
'merge-nested' => [
'sub.hash',
[
'two' => 'two-updated',
'three' => 'three-updated',
],
[
'sub' => [
'hash' => [
'one' => 1,
'two' => 'two-updated',
'three' => 'three-updated',
],
],
],
],
'add-new' => [
'sub',
['new' => 'added'],
[
'sub' => [
'new' => 'added',
],
],
],
];
}

/**
* @dataProvider patchKey
*
* @param string $key
* @param mixed $value
* @param mixed $expected
*/
public function testPatchKey($key, $value, $expected)
{
$config = [
'top' => 'level',
'sub' => [
'level' => 2,
'list' => [
'one',
'two',
],
'hash' => [
'one' => 1,
'two' => 2,
],
'null' => null,
'object' => stdClass::class,
],
];
$writer = new PhpArray();
$writer->toFile($this->file, $config);

$updated = $this->configResource->patchKey($key, $value);
$expected = ArrayUtils::merge($config, $expected);
$this->assertEquals($expected, $updated);
}
}

0 comments on commit 8e14178

Please sign in to comment.