porting tests and a bug fix from v1.5

This commit is contained in:
Joby 2022-12-06 11:25:59 -07:00
parent 2c1b190b43
commit 31db2e67c5
11 changed files with 230 additions and 34 deletions

6
.gitignore vendored
View file

@ -1,2 +1,4 @@
/vendor/
composer.lock
/vendor
/composer.lock
/.phpunit.result.cache
/coverage

View file

@ -8,7 +8,7 @@
"email": "joby@byjoby.com"
}],
"require-dev": {
"phpunit/phpunit": "^7.3"
"phpunit/phpunit": "^9.5"
},
"autoload": {
"psr-4": {

View file

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

View file

@ -1,6 +1,8 @@
<?php
/* Flatrr | https://gitlab.com/byjoby/flatrr | MIT License */
/* Flatrr | https://github.com/jobyone/flatrr | MIT License */
declare(strict_types=1);
namespace Flatrr\Config;
use PHPUnit\Framework\TestCase;
@ -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']);
}
}

View file

@ -0,0 +1,2 @@
[ini_file]
a = b

View file

@ -0,0 +1,3 @@
{
"json_file": "a"
}

View file

@ -0,0 +1 @@
yaml_file: a

View file

@ -0,0 +1 @@
yml_file: a

View file

@ -1,6 +1,8 @@
<?php
/* Flatrr | https://gitlab.com/byjoby/flatrr | MIT License */
/* Flatrr | https://github.com/jobyone/flatrr | MIT License */
declare(strict_types=1);
namespace Flatrr;
use PHPUnit\Framework\TestCase;
@ -17,6 +19,22 @@ 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()
{
// 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()
@ -30,4 +48,19 @@ class FlatArrayPushPopTest extends TestCase
$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'));
}
}

View file

@ -1,6 +1,8 @@
<?php
/* Flatrr | https://gitlab.com/byjoby/flatrr | MIT License */
/* Flatrr | https://github.com/jobyone/flatrr | MIT License */
declare(strict_types=1);
namespace Flatrr;
use PHPUnit\Framework\TestCase;
@ -184,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([
@ -194,4 +260,35 @@ class FlatArrayTest extends TestCase
$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);
}
}
}

View file

@ -1,6 +1,8 @@
<?php
/* Flatrr | https://gitlab.com/byjoby/flatrr | MIT License */
/* Flatrr | https://github.com/jobyone/flatrr | MIT License */
declare(strict_types=1);
namespace Flatrr;
use PHPUnit\Framework\TestCase;
@ -49,6 +51,8 @@ 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()
@ -64,7 +68,7 @@ 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);
@ -76,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'));
}
}