From 31db2e67c57af361c7e234e6302b349ade75379d Mon Sep 17 00:00:00 2001 From: Joby Elliott Date: Tue, 6 Dec 2022 11:25:59 -0700 Subject: [PATCH] porting tests and a bug fix from v1.5 --- .gitignore | 6 +- composer.json | 2 +- src/FlatArrayTrait.php | 2 + tests/Config/ConfigTest.php | 20 +++- 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 | 39 ++++++- tests/FlatArrayTest.php | 129 ++++++++++++++++++++--- tests/SelfReferencingFlatArrayTest.php | 59 +++++++++-- 11 files changed, 230 insertions(+), 34 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/.gitignore b/.gitignore index 3a9875b..82dac7f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ -/vendor/ -composer.lock +/vendor +/composer.lock +/.phpunit.result.cache +/coverage \ No newline at end of file diff --git a/composer.json b/composer.json index 66585af..1d224eb 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ "email": "joby@byjoby.com" }], "require-dev": { - "phpunit/phpunit": "^7.3" + "phpunit/phpunit": "^9.5" }, "autoload": { "psr-4": { diff --git a/src/FlatArrayTrait.php b/src/FlatArrayTrait.php index 3225986..8063d5c 100644 --- a/src/FlatArrayTrait.php +++ b/src/FlatArrayTrait.php @@ -28,6 +28,7 @@ trait FlatArrayTrait return; } $out = array_pop($arr); + $this->unset($name); $this->set($name, $arr); return $out; } @@ -52,6 +53,7 @@ trait FlatArrayTrait return; } $out = array_shift($arr); + $this->unset($name); $this->set($name, $arr); return $out; } diff --git a/tests/Config/ConfigTest.php b/tests/Config/ConfigTest.php index 54edc70..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 5acdaf2..1afd689 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()); + $this->assertNull($f->pop(null)); + } + + 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() @@ -24,10 +42,25 @@ class FlatArrayPushPopTest extends TestCase $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 81b42db..7268e9c 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,21 +179,116 @@ 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']); $this->assertEquals('d', $c['c']); } + public function testMergeViaSet() + { + $arr = new FlatArray([ + 'a' => [ + 'a' => 'b', + 'c' => 'd' + ] + ]); + $arr->set('a', [ + 'e' => 'f', + 'g' => 'h' + ]); + $this->assertEquals( + [ + 'a' => [ + 'a' => 'b', + 'c' => 'd', + 'e' => 'f', + 'g' => 'h' + ] + ], + $arr->get() + ); + } + + public function testNoMergeRootViaSet() + { + $arr = new FlatArray([ + 'a' => 'b', + 'c' => 'd' + ]); + $arr->set(null, [ + 'e' => 'f', + 'g' => 'h' + ]); + $this->assertEquals( + [ + 'e' => 'f', + 'g' => 'h' + ], + $arr->get() + ); + } + + public function testMergeViaSetOverNonArray() + { + $arr = new FlatArray([ + 'a' => 'b' + ]); + $arr->set('a', [ + 'e' => 'f', + 'g' => 'h' + ]); + $this->assertEquals( + [ + 'a' => [ + 'e' => 'f', + 'g' => 'h' + ] + ], + $arr->get() + ); + } + public function testConstructionUnflattening() { $arr = new FlatArray([ '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()); + // unset root + $arr->unset(null); + $this->assertEquals([], $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 7e3b35c..da99489 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,53 @@ 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']); } + + public function testForeach() + { + $reference = [ + 'a' => 'b', + 'b' => '${a}', + 'd' => '${b}' + ]; + $arr = new SelfReferencingFlatArray($reference); + foreach ($arr as $key => $value) { + $this->assertEquals('b', $value); + } + } + + public function testPop() + { + $f = new SelfReferencingFlatArray([ + 'a' => 'b', + 'c' => [ + '${a}' + ] + ]); + $this->assertEquals('b', $f->pop('c')); + $this->assertNull($f->pop('c')); + } + + public function testShift() + { + $f = new SelfReferencingFlatArray([ + 'a' => 'b', + 'c' => [ + '${a}' + ] + ]); + $this->assertEquals('b', $f->shift('c')); + $this->assertNull($f->shift('c')); + } }