125 lines
2.9 KiB
PHP
125 lines
2.9 KiB
PHP
<?php
|
|
namespace nulib\app\config {
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use nulib\app\config\impl\result;
|
|
use nulib\app\config\impl\config1;
|
|
use nulib\app\config\impl\config2;
|
|
|
|
class ConfigManagerTest extends TestCase {
|
|
function testConfigurators() {
|
|
$config = new ConfigManager();
|
|
|
|
result::reset();
|
|
$config->addConfigurator(config1::class);
|
|
$config->configure();
|
|
self::assertSame([
|
|
"config1::static configure1",
|
|
], impl\result::$configured);
|
|
|
|
result::reset();
|
|
$config->addConfigurator(config1::class);
|
|
$config->configure();
|
|
$config->configure();
|
|
$config->configure();
|
|
self::assertSame([
|
|
"config1::static configure1",
|
|
], impl\result::$configured);
|
|
|
|
result::reset();
|
|
$config->addConfigurator(new config1());
|
|
$config->configure();
|
|
self::assertSame([
|
|
"config1::static configure1",
|
|
"config1::configure2",
|
|
], impl\result::$configured);
|
|
|
|
result::reset();
|
|
$config->addConfigurator(new config1());
|
|
$config->configure(["include" => "2"]);
|
|
self::assertSame([
|
|
"config1::configure2",
|
|
], impl\result::$configured);
|
|
$config->configure(["include" => "1"]);
|
|
self::assertSame([
|
|
"config1::configure2",
|
|
"config1::static configure1",
|
|
], impl\result::$configured);
|
|
|
|
result::reset();
|
|
$config->addConfigurator([
|
|
config1::class,
|
|
new config2(),
|
|
]);
|
|
$config->configure();
|
|
self::assertSame([
|
|
"config1::static configure1",
|
|
"config2::static configure1",
|
|
"config2::configure2",
|
|
], impl\result::$configured);
|
|
}
|
|
|
|
function testConfig() {
|
|
$config = new ConfigManager();
|
|
|
|
$config->addConfig([
|
|
"app" => [
|
|
"var" => "array",
|
|
]
|
|
]);
|
|
self::assertSame("array", $config->getValue("app.var"));
|
|
|
|
$config->addConfig(new ArrayConfig([
|
|
"app" => [
|
|
"var" => "instance",
|
|
]
|
|
]));
|
|
self::assertSame("instance", $config->getValue("app.var"));
|
|
|
|
$config->addConfig(config1::class);
|
|
self::assertSame("class1", $config->getValue("app.var"));
|
|
|
|
$config->addConfig(config2::class);
|
|
self::assertSame("class2", $config->getValue("app.var"));
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace nulib\app\config\impl {
|
|
class result {
|
|
static array $configured = [];
|
|
|
|
static function reset() {
|
|
self::$configured = [];
|
|
}
|
|
}
|
|
|
|
class config1 {
|
|
const APP = [
|
|
"var" => "class1",
|
|
];
|
|
|
|
static function configure1() {
|
|
result::$configured[] = "config1::static configure1";
|
|
}
|
|
|
|
function configure2() {
|
|
result::$configured[] = "config1::configure2";
|
|
}
|
|
}
|
|
|
|
class config2 {
|
|
const APP = [
|
|
"var" => "class2",
|
|
];
|
|
|
|
static function configure1() {
|
|
result::$configured[] = "config2::static configure1";
|
|
}
|
|
|
|
function configure2() {
|
|
result::$configured[] = "config2::configure2";
|
|
}
|
|
}
|
|
}
|