flatrr/tests/Config/ConfigTest.php

75 lines
2 KiB
PHP
Raw Normal View History

2018-08-17 21:02:50 +00:00
<?php
/* Flatrr | https://github.com/jobyone/flatrr | MIT License */
2022-12-06 01:34:57 +00:00
2018-08-17 21:02:50 +00:00
declare(strict_types=1);
2022-12-06 01:34:57 +00:00
2018-08-17 22:09:45 +00:00
namespace Flatrr\Config;
2018-08-17 21:02:50 +00:00
use PHPUnit\Framework\TestCase;
2024-03-07 18:42:47 +00:00
use Symfony\Component\Yaml\Yaml;
2018-08-17 21:02:50 +00:00
class ConfigTest extends TestCase
{
public function testVariables()
{
//single level of variable
$c = new Config();
$c['a.b'] = 'a';
$c['c'] = '${a.b}';
$this->assertEquals('a', $c['c']);
//variable referencing another variable
$c['d'] = '${c}';
$this->assertEquals('a', $c['d']);
}
public function testReading()
{
$data = [
'a' => 'b',
'c' => [
'd' => 'e'
]
];
//json
$a = new Config();
2022-12-06 01:34:57 +00:00
$a->readFile(__DIR__ . '/configtest.json');
2018-08-17 21:02:50 +00:00
$this->assertEquals($data, $a->get());
//yaml
$a = new Config();
2022-12-06 01:34:57 +00:00
$a->readFile(__DIR__ . '/configtest.yaml');
2018-08-17 21:02:50 +00:00
$this->assertEquals($data, $a->get());
//nonexistant files
$a = new Config();
$a->readFile(__DIR__ . '/does-not-exist.json');
$a->readFile(__DIR__ . '/does-not-exist.yaml');
$this->assertEquals([], $a->get());
2018-08-17 21:02:50 +00:00
}
public function testSerializing()
{
$data = [
'a' => 'b',
'c' => [
'd' => 'e'
]
];
$c = new Config($data);
//json
$this->assertEquals($data, json_decode($c->json(), true));
//yaml
2024-03-07 18:42:47 +00:00
$this->assertEquals($data, Yaml::parse($c->yaml()));
2018-08-17 21:02:50 +00:00
}
2022-12-06 01:34:57 +00:00
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']);
}
2018-08-17 21:02:50 +00:00
}