From c6fdfcd885024201d2e8b89dd8f2df6adf5120f5 Mon Sep 17 00:00:00 2001 From: Joby Elliott Date: Mon, 5 Dec 2022 20:21:43 -0700 Subject: [PATCH] full test coverage and consistent behavior --- src/FlatArrayTrait.php | 10 ++-- tests/FlatArrayPushPopTest.php | 1 + tests/FlatArrayTest.php | 67 ++++++++++++++++++++++++++ tests/SelfReferencingFlatArrayTest.php | 37 ++++++++++++++ 4 files changed, 110 insertions(+), 5 deletions(-) diff --git a/src/FlatArrayTrait.php b/src/FlatArrayTrait.php index b4891a0..7634685 100644 --- a/src/FlatArrayTrait.php +++ b/src/FlatArrayTrait.php @@ -6,9 +6,9 @@ namespace Flatrr; trait FlatArrayTrait { /** @var array */ - private $_arrayData = []; + protected $_arrayData = []; /** @var array */ - private $_flattenCache = []; + protected $_flattenCache = []; public function push(null|string $name, mixed $value): static { @@ -31,6 +31,7 @@ trait FlatArrayTrait return null; } $out = array_pop($arr); + $this->unset($name); $this->set($name, $arr); return $out; } @@ -56,6 +57,7 @@ trait FlatArrayTrait return null; } $out = array_shift($arr); + $this->unset($name); $this->set($name, $arr); return $out; } @@ -204,9 +206,7 @@ trait FlatArrayTrait $parent[$key] = array_replace_recursive($parent[$key], $value); } else { //set the hard way - if (!is_array($parent)) { - $parent = []; - } + if (!is_array($parent)) $parent = []; $parent[$key] = $value; } return null; diff --git a/tests/FlatArrayPushPopTest.php b/tests/FlatArrayPushPopTest.php index 066a5dc..1afd689 100644 --- a/tests/FlatArrayPushPopTest.php +++ b/tests/FlatArrayPushPopTest.php @@ -19,6 +19,7 @@ class FlatArrayPushPopTest extends TestCase $this->assertEquals(['foo'], $f->get()); $this->assertEquals('foo', $f->pop(null)); $this->assertEquals([], $f->get()); + $this->assertNull($f->pop(null)); } public function testPushIndexCreation() diff --git a/tests/FlatArrayTest.php b/tests/FlatArrayTest.php index abf2189..7268e9c 100644 --- a/tests/FlatArrayTest.php +++ b/tests/FlatArrayTest.php @@ -186,6 +186,70 @@ class FlatArrayTest extends TestCase $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([ @@ -211,6 +275,9 @@ class FlatArrayTest extends TestCase 'd' => 'e' ] ], $arr->get()); + // unset root + $arr->unset(null); + $this->assertEquals([], $arr->get()); } public function testForeach() diff --git a/tests/SelfReferencingFlatArrayTest.php b/tests/SelfReferencingFlatArrayTest.php index 4686d3a..da99489 100644 --- a/tests/SelfReferencingFlatArrayTest.php +++ b/tests/SelfReferencingFlatArrayTest.php @@ -80,4 +80,41 @@ class SelfReferencingFlatArrayTest extends TestCase $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')); + } }