Skip to content

Commit

Permalink
Apply zfcampus#25 again
Browse files Browse the repository at this point in the history
  • Loading branch information
TomHAnderson committed Mar 19, 2018
1 parent 6a8c782 commit 4565d7f
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 24 deletions.
31 changes: 7 additions & 24 deletions src/ConfigResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ class ConfigResource

/**
* @param array $config
* @param string $fileName
* @param ConfigWriter $writer
*/
public function __construct(array $config, $fileName, ConfigWriter $writer)
{
Expand Down Expand Up @@ -110,25 +108,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);

// Write to configuration file
$this->writer->toFile($this->fileName, $config);
$this->invalidateCache($this->fileName);
$this->patch([$key => $value]);

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

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

/**
Expand Down Expand Up @@ -192,8 +174,10 @@ public function replaceKey($keys, $value, array $config)

$key = array_shift($keys);

$haveKeys = (count($keys) > 0) ? true : false;

// If no more keys, overwrite and return
if (! $keys) {
if (! $haveKeys) {
$config[$key] = $value;
return $config;
}
Expand Down Expand Up @@ -280,7 +264,6 @@ public function traverseArray(array $array, $currentKey = '')
* @param array $patchValues
* @param string $key
* @param mixed $value
* @throws Exception\InvalidArgumentException
*/
public function createNestedKeyValuePair(&$patchValues, $key, $value)
{
Expand All @@ -305,7 +288,7 @@ public function createNestedKeyValuePair(&$patchValues, $key, $value)
protected function extractAndSet(array $keys, $value, &$array)
{
$key = array_shift($keys);
if ($keys) {
if (count($keys)) {
if (! isset($array[$key]) || ! is_array($array[$key])) {
$array[$key] = [];
}
Expand All @@ -329,7 +312,7 @@ protected function deleteByKey(&$array, array $keys)
return;
}

if (! $keys) {
if (1 > count($keys)) {
unset($array[$key]);
return;
}
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 4565d7f

Please sign in to comment.