2017-03-24 16:18:02 -04:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
|
|
|
* Ion
|
|
|
|
*
|
|
|
|
* Building blocks for web development
|
|
|
|
*
|
2018-01-17 12:45:58 -05:00
|
|
|
* PHP version 7.1
|
2017-03-24 16:18:02 -04:00
|
|
|
*
|
|
|
|
* @package Ion
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2018-01-17 12:45:58 -05:00
|
|
|
* @copyright 2015 - 2018 Timothy J. Warren
|
2017-03-24 16:18:02 -04:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2018-10-05 13:04:59 -04:00
|
|
|
* @version 2.4.1
|
2017-03-24 16:18:02 -04:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/ion
|
|
|
|
*/
|
2016-08-04 14:55:37 -04:00
|
|
|
|
2016-08-29 11:34:25 -04:00
|
|
|
namespace Aviat\Ion\Tests;
|
|
|
|
|
2016-08-04 14:55:37 -04:00
|
|
|
use Aviat\Ion\Config;
|
|
|
|
|
2016-10-19 13:24:08 -04:00
|
|
|
class ConfigTest extends Ion_TestCase {
|
2016-08-04 14:55:37 -04:00
|
|
|
|
2019-12-05 15:38:34 -05:00
|
|
|
protected $config;
|
|
|
|
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
2016-08-04 14:55:37 -04:00
|
|
|
$this->config = new Config([
|
|
|
|
'foo' => 'bar',
|
|
|
|
'asset_path' => '/assets',
|
2018-10-05 11:32:12 -04:00
|
|
|
'bar' => 'baz',
|
|
|
|
'a' => [
|
|
|
|
'b' => [
|
|
|
|
'c' => TRUE,
|
|
|
|
],
|
|
|
|
],
|
2016-08-04 14:55:37 -04:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2019-12-05 15:38:34 -05:00
|
|
|
public function testConfigHas(): void
|
2018-10-05 11:32:12 -04:00
|
|
|
{
|
|
|
|
$this->assertTrue($this->config->has('foo'));
|
|
|
|
$this->assertTrue($this->config->has(['a', 'b', 'c']));
|
|
|
|
|
|
|
|
$this->assertFalse($this->config->has('baz'));
|
|
|
|
$this->assertFalse($this->config->has(['c', 'b', 'a']));
|
|
|
|
}
|
|
|
|
|
2019-12-05 15:38:34 -05:00
|
|
|
public function testConfigGet(): void
|
2016-08-04 14:55:37 -04:00
|
|
|
{
|
|
|
|
$this->assertEquals('bar', $this->config->get('foo'));
|
|
|
|
$this->assertEquals('baz', $this->config->get('bar'));
|
|
|
|
$this->assertNull($this->config->get('baz'));
|
|
|
|
$this->assertNull($this->config->get(['apple', 'sauce', 'is']));
|
|
|
|
}
|
|
|
|
|
2019-12-05 15:38:34 -05:00
|
|
|
public function testConfigSet(): void
|
2016-08-04 14:55:37 -04:00
|
|
|
{
|
|
|
|
$ret = $this->config->set('foo', 'foobar');
|
2019-12-05 15:38:34 -05:00
|
|
|
$this->assertInstanceOf(Config::class, $ret);
|
2016-08-04 14:55:37 -04:00
|
|
|
$this->assertEquals('foobar', $this->config->get('foo'));
|
|
|
|
|
|
|
|
$this->config->set(['apple', 'sauce', 'is'], 'great');
|
|
|
|
$apple = $this->config->get('apple');
|
2019-12-05 15:38:34 -05:00
|
|
|
$this->assertEquals('great', $apple['sauce']['is'], 'Config value not set correctly');
|
2016-08-29 11:34:25 -04:00
|
|
|
|
2016-08-04 14:55:37 -04:00
|
|
|
$this->assertEquals('great', $this->config->get(['apple', 'sauce', 'is']), "Array argument get for config failed.");
|
|
|
|
}
|
|
|
|
|
2019-12-05 15:38:34 -05:00
|
|
|
public function testConfigBadSet(): void
|
2016-08-04 14:55:37 -04:00
|
|
|
{
|
2016-08-29 11:34:25 -04:00
|
|
|
$this->expectException('InvalidArgumentException');
|
2016-08-04 14:55:37 -04:00
|
|
|
$this->config->set(NULL, FALSE);
|
|
|
|
}
|
|
|
|
|
2019-12-05 15:38:34 -05:00
|
|
|
public function dataConfigDelete(): array
|
2016-08-04 14:55:37 -04:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'top level delete' => [
|
|
|
|
'key' => 'apple',
|
|
|
|
'assertKeys' => [
|
|
|
|
[
|
|
|
|
'path' => ['apple', 'sauce', 'is'],
|
|
|
|
'expected' => NULL
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'path' => ['apple', 'sauce'],
|
|
|
|
'expected' => NULL
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'path' => 'apple',
|
|
|
|
'expected' => NULL
|
|
|
|
]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'mid level delete' => [
|
|
|
|
'key' => ['apple', 'sauce'],
|
|
|
|
'assertKeys' => [
|
|
|
|
[
|
|
|
|
'path' => ['apple', 'sauce', 'is'],
|
|
|
|
'expected' => NULL
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'path' => ['apple', 'sauce'],
|
|
|
|
'expected' => NULL
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'path' => 'apple',
|
|
|
|
'expected' => [
|
|
|
|
'sauce' => NULL
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'deep delete' => [
|
|
|
|
'key' => ['apple', 'sauce', 'is'],
|
|
|
|
'assertKeys' => [
|
|
|
|
[
|
|
|
|
'path' => ['apple', 'sauce', 'is'],
|
|
|
|
'expected' => NULL
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'path' => ['apple', 'sauce'],
|
|
|
|
'expected' => [
|
|
|
|
'is' => NULL
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataConfigDelete
|
|
|
|
*/
|
2019-12-05 15:38:34 -05:00
|
|
|
public function testConfigDelete($key, array $assertKeys): void
|
2016-08-04 14:55:37 -04:00
|
|
|
{
|
|
|
|
$config = new Config([]);
|
|
|
|
$config->set(['apple', 'sauce', 'is'], 'great');
|
|
|
|
$config->delete($key);
|
|
|
|
|
|
|
|
foreach($assertKeys as $pair)
|
|
|
|
{
|
|
|
|
$this->assertEquals($pair['expected'], $config->get($pair['path']));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 15:38:34 -05:00
|
|
|
public function testGetNonExistentConfigItem(): void
|
2016-08-04 14:55:37 -04:00
|
|
|
{
|
|
|
|
$this->assertNull($this->config->get('foobar'));
|
|
|
|
}
|
|
|
|
}
|