dramatically improved test coverage

This commit is contained in:
Joby 2022-12-05 18:34:57 -07:00
parent b3117d36e4
commit 53c18d33e0
9 changed files with 118 additions and 75 deletions

View file

@ -8,9 +8,6 @@ use Spyc;
class Config extends SelfReferencingFlatArray implements ConfigInterface
{
/** @var bool */
public $strict = false;
public function readDir(string $dir, string $name = null, bool $overwrite = false): void
{
$dir = realpath($dir);
@ -24,29 +21,6 @@ class Config extends SelfReferencingFlatArray implements ConfigInterface
}
}
/** @return null|array<mixed|mixed> */
protected function parse(string $input, string $format): null|array
{
$fn = 'parse_' . $format;
if (!method_exists($this, $fn)) {
if ($this->strict) {
throw new \Exception("Don't know how to parse the format \"$format\"");
} else {
return null;
}
}
if ($out = $this->$fn($input)) {
return $out;
}
return array();
}
/** @return array<mixed|mixed> */
protected function parse_yaml(string $input): array
{
return Spyc::YAMLLoadString($input);
}
public function json(bool $raw = false): string
{
return json_encode($this->get(null, $raw), JSON_PRETTY_PRINT);
@ -64,7 +38,7 @@ class Config extends SelfReferencingFlatArray implements ConfigInterface
}
/** @return array<mixed|mixed> */
protected function read_json(string $filename): array
protected function read_json(string $filename): null|array
{
return json_decode(file_get_contents($filename), true);
}
@ -83,30 +57,12 @@ class Config extends SelfReferencingFlatArray implements ConfigInterface
public function readFile(string $filename, string $name = null, bool $overwrite = false): void
{
if (!is_file($filename) || !is_readable($filename)) {
if ($this->strict) {
throw new \Exception("Couldn't read config file \"$filename\"");
} else {
return;
}
}
if (!is_file($filename) || !is_readable($filename)) return;
$format = strtolower(preg_replace('/.+\./', '', $filename));
$fn = 'read_' . $format;
if (!method_exists($this, $fn)) {
if ($this->strict) {
throw new \Exception("Don't know how to read the format \"$format\"");
} else {
return;
}
}
if (!method_exists($this, $fn)) return;
$data = $this->$fn($filename);
if (!$data) {
if ($this->strict) {
throw new \Exception("Error reading \"" . $filename . "\"");
} else {
return;
}
}
if ($data === null) return;
$this->merge($data, $name, $overwrite);
}
}

View file

@ -1,6 +1,8 @@
<?php
/* Flatrr | https://github.com/jobyone/flatrr | MIT License */
declare(strict_types=1);
namespace Flatrr\Config;
use PHPUnit\Framework\TestCase;
@ -30,11 +32,11 @@ class ConfigTest extends TestCase
];
//json
$a = new Config();
$a->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']);
}
}

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://github.com/jobyone/flatrr | MIT License */
declare(strict_types=1);
namespace Flatrr;
use PHPUnit\Framework\TestCase;
@ -12,22 +14,52 @@ class FlatArrayPushPopTest extends TestCase
$f = new FlatArray();
$f->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());
}
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()
{
$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'));
}
}

View file

@ -1,6 +1,8 @@
<?php
/* Flatrr | https://github.com/jobyone/flatrr | MIT License */
declare(strict_types=1);
namespace Flatrr;
use PHPUnit\Framework\TestCase;
@ -11,7 +13,7 @@ class FlatArrayTest extends TestCase
{
$data = [
'a' => '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,7 +179,7 @@ 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']);
@ -190,8 +192,36 @@ class FlatArrayTest extends TestCase
'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());
}
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://github.com/jobyone/flatrr | MIT License */
declare(strict_types=1);
namespace Flatrr;
use PHPUnit\Framework\TestCase;
@ -11,7 +13,7 @@ class SelfReferencingFlatArrayTest extends TestCase
{
$f = new SelfReferencingFlatArray([
'foo' => '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,16 @@ 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']);
}
}