full test coverage and consistent behavior

This commit is contained in:
Joby 2022-12-05 20:21:43 -07:00
parent 53c18d33e0
commit c6fdfcd885
4 changed files with 110 additions and 5 deletions

View file

@ -6,9 +6,9 @@ namespace Flatrr;
trait FlatArrayTrait
{
/** @var array<string|mixed> */
private $_arrayData = [];
protected $_arrayData = [];
/** @var array<string|mixed> */
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;

View file

@ -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()

View file

@ -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()

View file

@ -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'));
}
}