Compare commits

...

1 commit
main ... v1.4

Author SHA1 Message Date
31db2e67c5 porting tests and a bug fix from v1.5 2022-12-06 11:25:59 -07:00
11 changed files with 230 additions and 34 deletions

6
.gitignore vendored
View file

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

View file

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

View file

@ -28,6 +28,7 @@ trait FlatArrayTrait
return; return;
} }
$out = array_pop($arr); $out = array_pop($arr);
$this->unset($name);
$this->set($name, $arr); $this->set($name, $arr);
return $out; return $out;
} }
@ -52,6 +53,7 @@ trait FlatArrayTrait
return; return;
} }
$out = array_shift($arr); $out = array_shift($arr);
$this->unset($name);
$this->set($name, $arr); $this->set($name, $arr);
return $out; return $out;
} }

View file

@ -1,6 +1,8 @@
<?php <?php
/* Flatrr | https://gitlab.com/byjoby/flatrr | MIT License */ /* Flatrr | https://github.com/jobyone/flatrr | MIT License */
declare(strict_types=1); declare(strict_types=1);
namespace Flatrr\Config; namespace Flatrr\Config;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
@ -30,11 +32,11 @@ class ConfigTest extends TestCase
]; ];
//json //json
$a = new Config(); $a = new Config();
$a->readFile(__DIR__.'/configtest.json'); $a->readFile(__DIR__ . '/configtest.json');
$this->assertEquals($data, $a->get()); $this->assertEquals($data, $a->get());
//yaml //yaml
$a = new Config(); $a = new Config();
$a->readFile(__DIR__.'/configtest.yaml'); $a->readFile(__DIR__ . '/configtest.yaml');
$this->assertEquals($data, $a->get()); $this->assertEquals($data, $a->get());
} }
@ -52,4 +54,16 @@ class ConfigTest extends TestCase
//yaml //yaml
$this->assertEquals($data, Spyc::YAMLLoad($c->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 <?php
/* Flatrr | https://gitlab.com/byjoby/flatrr | MIT License */ /* Flatrr | https://github.com/jobyone/flatrr | MIT License */
declare(strict_types=1); declare(strict_types=1);
namespace Flatrr; namespace Flatrr;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
@ -12,11 +14,27 @@ class FlatArrayPushPopTest extends TestCase
$f = new FlatArray(); $f = new FlatArray();
$f->push(null, 'foo'); $f->push(null, 'foo');
$f->push(null, 'bar'); $f->push(null, 'bar');
$this->assertEquals(['foo','bar'], $f->get()); $this->assertEquals(['foo', 'bar'], $f->get());
$this->assertEquals('bar', $f->pop(null)); $this->assertEquals('bar', $f->pop(null));
$this->assertEquals(['foo'], $f->get()); $this->assertEquals(['foo'], $f->get());
$this->assertEquals('foo', $f->pop(null)); $this->assertEquals('foo', $f->pop(null));
$this->assertEquals([], $f->get()); $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() public function testShiftUnshift()
@ -24,10 +42,25 @@ class FlatArrayPushPopTest extends TestCase
$f = new FlatArray(); $f = new FlatArray();
$f->unshift(null, 'foo'); $f->unshift(null, 'foo');
$f->unshift(null, 'bar'); $f->unshift(null, 'bar');
$this->assertEquals(['bar','foo'], $f->get()); $this->assertEquals(['bar', 'foo'], $f->get());
$this->assertEquals('bar', $f->shift(null)); $this->assertEquals('bar', $f->shift(null));
$this->assertEquals(['foo'], $f->get()); $this->assertEquals(['foo'], $f->get());
$this->assertEquals('foo', $f->shift(null)); $this->assertEquals('foo', $f->shift(null));
$this->assertEquals([], $f->get()); $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 <?php
/* Flatrr | https://gitlab.com/byjoby/flatrr | MIT License */ /* Flatrr | https://github.com/jobyone/flatrr | MIT License */
declare(strict_types=1); declare(strict_types=1);
namespace Flatrr; namespace Flatrr;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
@ -11,7 +13,7 @@ class FlatArrayTest extends TestCase
{ {
$data = [ $data = [
'a' => 'A', 'a' => 'A',
'b' => ['c'=>'C'] 'b' => ['c' => 'C']
]; ];
$a = new FlatArray($data); $a = new FlatArray($data);
//first level //first level
@ -21,8 +23,8 @@ class FlatArrayTest extends TestCase
$this->assertEquals('C', $a['b.c']); $this->assertEquals('C', $a['b.c']);
$this->assertEquals('C', $a->get('b.c')); $this->assertEquals('C', $a->get('b.c'));
//returning array //returning array
$this->assertEquals(['c'=>'C'], $a['b']); $this->assertEquals(['c' => 'C'], $a['b']);
$this->assertEquals(['c'=>'C'], $a->get('b')); $this->assertEquals(['c' => 'C'], $a->get('b'));
//returning entire array by requesting null or empty string //returning entire array by requesting null or empty string
$this->assertEquals($data, $a[null]); $this->assertEquals($data, $a[null]);
$this->assertEquals($data, $a->get()); $this->assertEquals($data, $a->get());
@ -62,7 +64,7 @@ class FlatArrayTest extends TestCase
{ {
$data = [ $data = [
'a' => 'A', 'a' => 'A',
'b' => ['c'=>'C'] 'b' => ['c' => 'C']
]; ];
$a = new FlatArray($data); $a = new FlatArray($data);
//setting on first layer //setting on first layer
@ -91,7 +93,7 @@ class FlatArrayTest extends TestCase
public function testSettingFalseyValues() public function testSettingFalseyValues()
{ {
$a = new FlatArray(['foo'=>['bar'=>'baz']]); $a = new FlatArray(['foo' => ['bar' => 'baz']]);
$a['foo.bar'] = false; $a['foo.bar'] = false;
$this->assertFalse($a['foo.bar']); $this->assertFalse($a['foo.bar']);
$a['foo.bar'] = 0; $a['foo.bar'] = 0;
@ -104,21 +106,21 @@ class FlatArrayTest extends TestCase
public function testMerginFalseyValues() public function testMerginFalseyValues()
{ {
$a = new FlatArray(['foo'=>['bar'=>'baz']]); $a = new FlatArray(['foo' => ['bar' => 'baz']]);
$a->merge(['foo'=>['bar'=>false]], null, true); $a->merge(['foo' => ['bar' => false]], null, true);
$this->assertFalse($a['foo.bar']); $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']); $this->assertSame(0, $a['foo.bar']);
$a->merge(['foo'=>['bar'=>'']], null, true); $a->merge(['foo' => ['bar' => '']], null, true);
$this->assertSame('', $a['foo.bar']); $this->assertSame('', $a['foo.bar']);
$a->merge(['foo'=>['bar'=>[]]], null, true); $a->merge(['foo' => ['bar' => []]], null, true);
$this->assertSame([], $a['foo.bar']); $this->assertSame([], $a['foo.bar']);
} }
public function testCaseSensitivity() public function testCaseSensitivity()
{ {
$h = new FlatArray([ $h = new FlatArray([
'ABC'=>['ABC'=>'ABC'] 'ABC' => ['ABC' => 'ABC']
]); ]);
$this->assertNull($h['abc.abc']); $this->assertNull($h['abc.abc']);
$this->assertNull($h['Abc.aBC']); $this->assertNull($h['Abc.aBC']);
@ -126,7 +128,7 @@ class FlatArrayTest extends TestCase
public function testAccidentalSubstrings() public function testAccidentalSubstrings()
{ {
$h = new FlatArray(['foo'=>'bar']); $h = new FlatArray(['foo' => 'bar']);
$this->assertNull($h['foo.baz']); $this->assertNull($h['foo.baz']);
} }
@ -169,7 +171,7 @@ class FlatArrayTest extends TestCase
//overwrite false with mismatched array-ness //overwrite false with mismatched array-ness
$c = new FlatArray($data); $c = new FlatArray($data);
$c->merge([ $c->merge([
'a' => ['b'=>'c'], 'a' => ['b' => 'c'],
'c' => 'd' 'c' => 'd'
]); ]);
$this->assertEquals('b', $c['a']); $this->assertEquals('b', $c['a']);
@ -177,21 +179,116 @@ class FlatArrayTest extends TestCase
//overwrite true with mismatched array-ness //overwrite true with mismatched array-ness
$c = new FlatArray($data); $c = new FlatArray($data);
$c->merge([ $c->merge([
'a' => ['b'=>'c'], 'a' => ['b' => 'c'],
'c' => 'd' 'c' => 'd'
], null, true); ], null, true);
$this->assertEquals('c', $c['a.b']); $this->assertEquals('c', $c['a.b']);
$this->assertEquals('d', $c['c']); $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() public function testConstructionUnflattening()
{ {
$arr = new FlatArray([ $arr = new FlatArray([
'foo.bar' => 'baz' 'foo.bar' => 'baz'
]); ]);
$this->assertEquals( $this->assertEquals(
['foo'=>['bar'=>'baz']], ['foo' => ['bar' => 'baz']],
$arr->get() $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 <?php
/* Flatrr | https://gitlab.com/byjoby/flatrr | MIT License */ /* Flatrr | https://github.com/jobyone/flatrr | MIT License */
declare(strict_types=1); declare(strict_types=1);
namespace Flatrr; namespace Flatrr;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
@ -11,7 +13,7 @@ class SelfReferencingFlatArrayTest extends TestCase
{ {
$f = new SelfReferencingFlatArray([ $f = new SelfReferencingFlatArray([
'foo' => 'bar', 'foo' => 'bar',
'bar' => ['baz'=>'qux'], 'bar' => ['baz' => 'qux'],
'test' => [ 'test' => [
'foo' => '${foo}', 'foo' => '${foo}',
'bar' => '${bar.baz}', 'bar' => '${bar.baz}',
@ -49,11 +51,13 @@ class SelfReferencingFlatArrayTest extends TestCase
$this->assertEquals('${foo}', $f['nested.escaped.full']); $this->assertEquals('${foo}', $f['nested.escaped.full']);
$this->assertEquals('${foo}', $f['nested.escaped.left']); $this->assertEquals('${foo}', $f['nested.escaped.left']);
$this->assertEquals('${foo}', $f['nested.escaped.right']); $this->assertEquals('${foo}', $f['nested.escaped.right']);
//raw
$this->assertEquals('${foo}', $f->get('test.foo', true));
} }
public function testSettingFalseyValues() public function testSettingFalseyValues()
{ {
$a = new SelfReferencingFlatArray(['foo'=>['bar'=>'baz']]); $a = new SelfReferencingFlatArray(['foo' => ['bar' => 'baz']]);
$a['foo.bar'] = false; $a['foo.bar'] = false;
$this->assertFalse($a['foo.bar']); $this->assertFalse($a['foo.bar']);
$a['foo.bar'] = 0; $a['foo.bar'] = 0;
@ -64,16 +68,53 @@ class SelfReferencingFlatArrayTest extends TestCase
$this->assertSame([], $a['foo.bar']); $this->assertSame([], $a['foo.bar']);
} }
public function testMerginFalseyValues() public function testMergingFalseyValues()
{ {
$a = new SelfReferencingFlatArray(['foo'=>['bar'=>'baz']]); $a = new SelfReferencingFlatArray(['foo' => ['bar' => 'baz']]);
$a->merge(['foo'=>['bar'=>false]], null, true); $a->merge(['foo' => ['bar' => false]], null, true);
$this->assertFalse($a['foo.bar']); $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']); $this->assertSame(0, $a['foo.bar']);
$a->merge(['foo'=>['bar'=>'']], null, true); $a->merge(['foo' => ['bar' => '']], null, true);
$this->assertSame('', $a['foo.bar']); $this->assertSame('', $a['foo.bar']);
$a->merge(['foo'=>['bar'=>[]]], null, true); $a->merge(['foo' => ['bar' => []]], null, true);
$this->assertSame([], $a['foo.bar']); $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'));
}
} }