From 53c18d33e08180f5829b5400e9140f0abb36cc25 Mon Sep 17 00:00:00 2001 From: Joby Elliott Date: Mon, 5 Dec 2022 18:34:57 -0700 Subject: [PATCH] dramatically improved test coverage --- src/Config/Config.php | 52 ++------------------ tests/Config/ConfigTest.php | 18 ++++++- tests/Config/configtestdir/inifile.ini | 2 + tests/Config/configtestdir/jsonfile.json | 3 ++ tests/Config/configtestdir/yamlfile.yaml | 1 + tests/Config/configtestdir/ymlfile.yml | 1 + tests/FlatArrayPushPopTest.php | 36 +++++++++++++- tests/FlatArrayTest.php | 60 ++++++++++++++++++------ tests/SelfReferencingFlatArrayTest.php | 20 ++++---- 9 files changed, 118 insertions(+), 75 deletions(-) create mode 100644 tests/Config/configtestdir/inifile.ini create mode 100644 tests/Config/configtestdir/jsonfile.json create mode 100644 tests/Config/configtestdir/yamlfile.yaml create mode 100644 tests/Config/configtestdir/ymlfile.yml diff --git a/src/Config/Config.php b/src/Config/Config.php index fa268e8..7de0917 100644 --- a/src/Config/Config.php +++ b/src/Config/Config.php @@ -8,9 +8,6 @@ use Spyc; class Config extends SelfReferencingFlatArray implements ConfigInterface { - /** @var bool */ - public $strict = false; - public function readDir(string $dir, string $name = null, bool $overwrite = false): void { $dir = realpath($dir); @@ -24,29 +21,6 @@ class Config extends SelfReferencingFlatArray implements ConfigInterface } } - /** @return null|array */ - protected function parse(string $input, string $format): null|array - { - $fn = 'parse_' . $format; - if (!method_exists($this, $fn)) { - if ($this->strict) { - throw new \Exception("Don't know how to parse the format \"$format\""); - } else { - return null; - } - } - if ($out = $this->$fn($input)) { - return $out; - } - return array(); - } - - /** @return array */ - protected function parse_yaml(string $input): array - { - return Spyc::YAMLLoadString($input); - } - public function json(bool $raw = false): string { return json_encode($this->get(null, $raw), JSON_PRETTY_PRINT); @@ -64,7 +38,7 @@ class Config extends SelfReferencingFlatArray implements ConfigInterface } /** @return array */ - protected function read_json(string $filename): array + protected function read_json(string $filename): null|array { return json_decode(file_get_contents($filename), true); } @@ -83,30 +57,12 @@ class Config extends SelfReferencingFlatArray implements ConfigInterface public function readFile(string $filename, string $name = null, bool $overwrite = false): void { - if (!is_file($filename) || !is_readable($filename)) { - if ($this->strict) { - throw new \Exception("Couldn't read config file \"$filename\""); - } else { - return; - } - } + if (!is_file($filename) || !is_readable($filename)) return; $format = strtolower(preg_replace('/.+\./', '', $filename)); $fn = 'read_' . $format; - if (!method_exists($this, $fn)) { - if ($this->strict) { - throw new \Exception("Don't know how to read the format \"$format\""); - } else { - return; - } - } + if (!method_exists($this, $fn)) return; $data = $this->$fn($filename); - if (!$data) { - if ($this->strict) { - throw new \Exception("Error reading \"" . $filename . "\""); - } else { - return; - } - } + if ($data === null) return; $this->merge($data, $name, $overwrite); } } diff --git a/tests/Config/ConfigTest.php b/tests/Config/ConfigTest.php index 3f6a0ad..2bd0047 100644 --- a/tests/Config/ConfigTest.php +++ b/tests/Config/ConfigTest.php @@ -1,6 +1,8 @@ readFile(__DIR__.'/configtest.json'); + $a->readFile(__DIR__ . '/configtest.json'); $this->assertEquals($data, $a->get()); //yaml $a = new Config(); - $a->readFile(__DIR__.'/configtest.yaml'); + $a->readFile(__DIR__ . '/configtest.yaml'); $this->assertEquals($data, $a->get()); } @@ -52,4 +54,16 @@ class ConfigTest extends TestCase //yaml $this->assertEquals($data, Spyc::YAMLLoad($c->yaml())); } + + public function testReadingDirectory() + { + $config = new Config; + $config->readDir(__DIR__ . '/nonexistantdir'); + $this->assertEquals([], $config->get()); + $config->readDir(__DIR__ . '/configtestdir'); + $this->assertEquals('b', $config['ini_file.a']); + $this->assertEquals('a', $config['yaml_file']); + $this->assertEquals('a', $config['json_file']); + $this->assertEquals('a', $config['yml_file']); + } } diff --git a/tests/Config/configtestdir/inifile.ini b/tests/Config/configtestdir/inifile.ini new file mode 100644 index 0000000..2aca6aa --- /dev/null +++ b/tests/Config/configtestdir/inifile.ini @@ -0,0 +1,2 @@ +[ini_file] +a = b \ No newline at end of file diff --git a/tests/Config/configtestdir/jsonfile.json b/tests/Config/configtestdir/jsonfile.json new file mode 100644 index 0000000..f5a5513 --- /dev/null +++ b/tests/Config/configtestdir/jsonfile.json @@ -0,0 +1,3 @@ +{ + "json_file": "a" +} \ No newline at end of file diff --git a/tests/Config/configtestdir/yamlfile.yaml b/tests/Config/configtestdir/yamlfile.yaml new file mode 100644 index 0000000..089ce12 --- /dev/null +++ b/tests/Config/configtestdir/yamlfile.yaml @@ -0,0 +1 @@ +yaml_file: a \ No newline at end of file diff --git a/tests/Config/configtestdir/ymlfile.yml b/tests/Config/configtestdir/ymlfile.yml new file mode 100644 index 0000000..788a785 --- /dev/null +++ b/tests/Config/configtestdir/ymlfile.yml @@ -0,0 +1 @@ +yml_file: a diff --git a/tests/FlatArrayPushPopTest.php b/tests/FlatArrayPushPopTest.php index 8bc4508..066a5dc 100644 --- a/tests/FlatArrayPushPopTest.php +++ b/tests/FlatArrayPushPopTest.php @@ -1,6 +1,8 @@ push(null, 'foo'); $f->push(null, 'bar'); - $this->assertEquals(['foo','bar'], $f->get()); + $this->assertEquals(['foo', 'bar'], $f->get()); $this->assertEquals('bar', $f->pop(null)); $this->assertEquals(['foo'], $f->get()); $this->assertEquals('foo', $f->pop(null)); $this->assertEquals([], $f->get()); } + public function testPushIndexCreation() + { + // pushing to a nonexistent index creates it as an array + $f = new FlatArray(); + $f->push('a.b', 'c'); + $this->assertEquals(['c'], $f['a.b']); + $this->assertEquals(['a' => ['b' => ['c']]], $f->get()); + // pushing to an existing non-array index does nothing + $f = new FlatArray(['a' => 'b']); + $f->push('a', 'c'); + $this->assertEquals(['a' => 'b'], $f->get()); + // poping off a non-array does nothing + $this->assertNull($f->pop('a')); + } + public function testShiftUnshift() { $f = new FlatArray(); $f->unshift(null, 'foo'); $f->unshift(null, 'bar'); - $this->assertEquals(['bar','foo'], $f->get()); + $this->assertEquals(['bar', 'foo'], $f->get()); $this->assertEquals('bar', $f->shift(null)); $this->assertEquals(['foo'], $f->get()); $this->assertEquals('foo', $f->shift(null)); $this->assertEquals([], $f->get()); } + + public function testUnshiftIndexCreation() + { + // unshifting to a nonexistent index creates it as an array + $f = new FlatArray(); + $f->unshift('a.b', 'c'); + $this->assertEquals(['c'], $f['a.b']); + $this->assertEquals(['a' => ['b' => ['c']]], $f->get()); + // unshifting to an existing non-array index does nothing + $f = new FlatArray(['a' => 'b']); + $f->unshift('a', 'c'); + $this->assertEquals(['a' => 'b'], $f->get()); + // shifting off a non-array does nothing + $this->assertNull($f->shift('a')); + } } diff --git a/tests/FlatArrayTest.php b/tests/FlatArrayTest.php index 22e87ad..abf2189 100644 --- a/tests/FlatArrayTest.php +++ b/tests/FlatArrayTest.php @@ -1,6 +1,8 @@ 'A', - 'b' => ['c'=>'C'] + 'b' => ['c' => 'C'] ]; $a = new FlatArray($data); //first level @@ -21,8 +23,8 @@ class FlatArrayTest extends TestCase $this->assertEquals('C', $a['b.c']); $this->assertEquals('C', $a->get('b.c')); //returning array - $this->assertEquals(['c'=>'C'], $a['b']); - $this->assertEquals(['c'=>'C'], $a->get('b')); + $this->assertEquals(['c' => 'C'], $a['b']); + $this->assertEquals(['c' => 'C'], $a->get('b')); //returning entire array by requesting null or empty string $this->assertEquals($data, $a[null]); $this->assertEquals($data, $a->get()); @@ -62,7 +64,7 @@ class FlatArrayTest extends TestCase { $data = [ 'a' => 'A', - 'b' => ['c'=>'C'] + 'b' => ['c' => 'C'] ]; $a = new FlatArray($data); //setting on first layer @@ -91,7 +93,7 @@ class FlatArrayTest extends TestCase public function testSettingFalseyValues() { - $a = new FlatArray(['foo'=>['bar'=>'baz']]); + $a = new FlatArray(['foo' => ['bar' => 'baz']]); $a['foo.bar'] = false; $this->assertFalse($a['foo.bar']); $a['foo.bar'] = 0; @@ -104,21 +106,21 @@ class FlatArrayTest extends TestCase public function testMerginFalseyValues() { - $a = new FlatArray(['foo'=>['bar'=>'baz']]); - $a->merge(['foo'=>['bar'=>false]], null, true); + $a = new FlatArray(['foo' => ['bar' => 'baz']]); + $a->merge(['foo' => ['bar' => false]], null, true); $this->assertFalse($a['foo.bar']); - $a->merge(['foo'=>['bar'=>0]], null, true); + $a->merge(['foo' => ['bar' => 0]], null, true); $this->assertSame(0, $a['foo.bar']); - $a->merge(['foo'=>['bar'=>'']], null, true); + $a->merge(['foo' => ['bar' => '']], null, true); $this->assertSame('', $a['foo.bar']); - $a->merge(['foo'=>['bar'=>[]]], null, true); + $a->merge(['foo' => ['bar' => []]], null, true); $this->assertSame([], $a['foo.bar']); } public function testCaseSensitivity() { $h = new FlatArray([ - 'ABC'=>['ABC'=>'ABC'] + 'ABC' => ['ABC' => 'ABC'] ]); $this->assertNull($h['abc.abc']); $this->assertNull($h['Abc.aBC']); @@ -126,7 +128,7 @@ class FlatArrayTest extends TestCase public function testAccidentalSubstrings() { - $h = new FlatArray(['foo'=>'bar']); + $h = new FlatArray(['foo' => 'bar']); $this->assertNull($h['foo.baz']); } @@ -169,7 +171,7 @@ class FlatArrayTest extends TestCase //overwrite false with mismatched array-ness $c = new FlatArray($data); $c->merge([ - 'a' => ['b'=>'c'], + 'a' => ['b' => 'c'], 'c' => 'd' ]); $this->assertEquals('b', $c['a']); @@ -177,7 +179,7 @@ class FlatArrayTest extends TestCase //overwrite true with mismatched array-ness $c = new FlatArray($data); $c->merge([ - 'a' => ['b'=>'c'], + 'a' => ['b' => 'c'], 'c' => 'd' ], null, true); $this->assertEquals('c', $c['a.b']); @@ -190,8 +192,36 @@ class FlatArrayTest extends TestCase 'foo.bar' => 'baz' ]); $this->assertEquals( - ['foo'=>['bar'=>'baz']], + ['foo' => ['bar' => 'baz']], $arr->get() ); } + + public function testUnset() + { + $arr = new FlatArray([ + 'a' => [ + 'b' => 'c', + 'd' => 'e' + ] + ]); + unset($arr['a.b']); + $this->assertEquals([ + 'a' => [ + 'd' => 'e' + ] + ], $arr->get()); + } + + public function testForeach() + { + $reference = [ + 'b' => 'c', + 'd' => 'e' + ]; + $arr = new FlatArray($reference); + foreach ($arr as $key => $value) { + $this->assertEquals($reference[$key], $value); + } + } } diff --git a/tests/SelfReferencingFlatArrayTest.php b/tests/SelfReferencingFlatArrayTest.php index 037c1e5..4686d3a 100644 --- a/tests/SelfReferencingFlatArrayTest.php +++ b/tests/SelfReferencingFlatArrayTest.php @@ -1,6 +1,8 @@ 'bar', - 'bar' => ['baz'=>'qux'], + 'bar' => ['baz' => 'qux'], 'test' => [ 'foo' => '${foo}', 'bar' => '${bar.baz}', @@ -49,11 +51,13 @@ class SelfReferencingFlatArrayTest extends TestCase $this->assertEquals('${foo}', $f['nested.escaped.full']); $this->assertEquals('${foo}', $f['nested.escaped.left']); $this->assertEquals('${foo}', $f['nested.escaped.right']); + //raw + $this->assertEquals('${foo}', $f->get('test.foo', true)); } public function testSettingFalseyValues() { - $a = new SelfReferencingFlatArray(['foo'=>['bar'=>'baz']]); + $a = new SelfReferencingFlatArray(['foo' => ['bar' => 'baz']]); $a['foo.bar'] = false; $this->assertFalse($a['foo.bar']); $a['foo.bar'] = 0; @@ -64,16 +68,16 @@ class SelfReferencingFlatArrayTest extends TestCase $this->assertSame([], $a['foo.bar']); } - public function testMerginFalseyValues() + public function testMergingFalseyValues() { - $a = new SelfReferencingFlatArray(['foo'=>['bar'=>'baz']]); - $a->merge(['foo'=>['bar'=>false]], null, true); + $a = new SelfReferencingFlatArray(['foo' => ['bar' => 'baz']]); + $a->merge(['foo' => ['bar' => false]], null, true); $this->assertFalse($a['foo.bar']); - $a->merge(['foo'=>['bar'=>0]], null, true); + $a->merge(['foo' => ['bar' => 0]], null, true); $this->assertSame(0, $a['foo.bar']); - $a->merge(['foo'=>['bar'=>'']], null, true); + $a->merge(['foo' => ['bar' => '']], null, true); $this->assertSame('', $a['foo.bar']); - $a->merge(['foo'=>['bar'=>[]]], null, true); + $a->merge(['foo' => ['bar' => []]], null, true); $this->assertSame([], $a['foo.bar']); } }