2024-10-04 22:28:27 +00:00
|
|
|
<?php
|
|
|
|
// FsConfigTest.php
|
|
|
|
// Created: 2023-10-20
|
2024-12-02 01:30:36 +00:00
|
|
|
// Updated: 2024-12-02
|
2024-10-04 22:28:27 +00:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
2024-12-02 01:30:36 +00:00
|
|
|
use Index\Config\{GetValueInfoTrait,GetValuesTrait,ImmutableConfigTrait,ScopedConfigValueInfo};
|
2024-10-04 22:28:27 +00:00
|
|
|
use Index\Config\Fs\{FsConfig,FsConfigValueInfo};
|
|
|
|
|
|
|
|
#[CoversClass(FsConfig::class)]
|
|
|
|
#[CoversClass(FsConfigValueInfo::class)]
|
|
|
|
#[CoversClass(ImmutableConfigTrait::class)]
|
|
|
|
#[CoversClass(GetValueInfoTrait::class)]
|
|
|
|
#[CoversClass(GetValuesTrait::class)]
|
|
|
|
final class FsConfigTest extends TestCase {
|
|
|
|
public function testImmutableRemove(): void {
|
|
|
|
$this->expectException(\RuntimeException::class);
|
|
|
|
FsConfig::fromString('test value')->removeValues('test');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testImmutableSetString(): void {
|
|
|
|
$this->expectException(\RuntimeException::class);
|
|
|
|
FsConfig::fromString('test value')->setString('test', 'the');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testImmutableSetInteger(): void {
|
|
|
|
$this->expectException(\RuntimeException::class);
|
|
|
|
FsConfig::fromString('test 1234')->setInteger('test', 5678);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testImmutableSetFloat(): void {
|
|
|
|
$this->expectException(\RuntimeException::class);
|
|
|
|
FsConfig::fromString('test 56.78')->setFloat('test', 12.34);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testImmutableSetBoolean(): void {
|
|
|
|
$this->expectException(\RuntimeException::class);
|
|
|
|
FsConfig::fromString('test true')->setBoolean('test', false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testImmutableSetArray(): void {
|
|
|
|
$this->expectException(\RuntimeException::class);
|
|
|
|
FsConfig::fromString('test words words words')->setArray('test', ['meow', 'meow', 'meow']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testImmutableSetValues(): void {
|
|
|
|
$this->expectException(\RuntimeException::class);
|
|
|
|
FsConfig::fromString('')->setValues([
|
|
|
|
'stringval' => 'the',
|
|
|
|
'intval' => 1234,
|
|
|
|
'floatval' => 56.78,
|
|
|
|
'boolval' => true,
|
|
|
|
'arrval' => ['meow'],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testScoping(): void {
|
|
|
|
$config = FsConfig::fromLines([
|
|
|
|
'test Inaccessible',
|
|
|
|
'scoped:test Accessible',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals('Inaccessible', $config->getString('test'));
|
|
|
|
$this->assertEquals('Accessible', $config->getString('scoped:test'));
|
|
|
|
|
|
|
|
$scoped = $config->scopeTo('scoped');
|
|
|
|
$this->assertEquals('Accessible', $scoped->getString('test'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHasValues(): void {
|
|
|
|
$config = FsConfig::fromLines([
|
|
|
|
'test 123',
|
|
|
|
'scoped:test true',
|
|
|
|
'scoped:meow meow',
|
|
|
|
]);
|
|
|
|
|
|
|
|
// hasValues should always return true when the list is empty
|
|
|
|
$this->assertTrue($config->hasValues([]));
|
|
|
|
$this->assertFalse($config->hasValues('meow'));
|
|
|
|
$this->assertTrue($config->hasValues('test'));
|
|
|
|
$this->assertFalse($config->hasValues(['test', 'meow']));
|
|
|
|
$this->assertTrue($config->hasValues(['scoped:test', 'scoped:meow']));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetAllValueInfos(): void {
|
2024-10-04 22:59:37 +00:00
|
|
|
$config = FsConfig::fromFile(__DIR__ . '/FsConfigTest.cfg');
|
2024-10-04 22:28:27 +00:00
|
|
|
|
|
|
|
$all = $config->getAllValueInfos();
|
|
|
|
$expected = [
|
|
|
|
'chat:port',
|
|
|
|
'chat:msgMaxLength',
|
|
|
|
'chat:floodKickLength',
|
|
|
|
'chat:channels',
|
|
|
|
'chat:channels:lounge:name',
|
|
|
|
'chat:channels:lounge:autoJoin',
|
|
|
|
'chat:channels:prog:name',
|
|
|
|
'chat:channels:games:name',
|
|
|
|
'chat:channels:splat:name',
|
|
|
|
'chat:channels:passwd:name',
|
|
|
|
'chat:channels:passwd:password',
|
|
|
|
'chat:channels:staff:name',
|
|
|
|
'chat:channels:staff:minRank',
|
|
|
|
'msz:secret',
|
|
|
|
'msz:url',
|
|
|
|
'mariadb:host',
|
|
|
|
'mariadb:user',
|
|
|
|
'mariadb:pass',
|
|
|
|
'mariadb:db',
|
|
|
|
];
|
|
|
|
$values = [];
|
|
|
|
|
|
|
|
foreach($all as $info)
|
|
|
|
$values[] = $info->getName();
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $values);
|
|
|
|
|
|
|
|
$subset = $config->getAllValueInfos(3, 6);
|
|
|
|
$expected = [
|
|
|
|
'chat:channels:prog:name',
|
|
|
|
'chat:channels:games:name',
|
|
|
|
'chat:channels:splat:name',
|
|
|
|
];
|
|
|
|
$values = [];
|
|
|
|
|
|
|
|
foreach($subset as $info)
|
|
|
|
$values[] = $info->getName();
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $values);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetValues(): void {
|
2024-10-04 22:59:37 +00:00
|
|
|
$config = FsConfig::fromFile(__DIR__ . '/FsConfigTest.cfg');
|
2024-10-04 22:28:27 +00:00
|
|
|
|
|
|
|
$this->assertNull($config->getValueInfo('doesnotexist'));
|
|
|
|
|
|
|
|
$scoped = $config->scopeTo('chat')->scopeTo('channels', 'passwd');
|
|
|
|
|
|
|
|
$expected = ['chat:channels:passwd:name' => 'Password', 'chat:channels:passwd:password' => 'meow'];
|
|
|
|
$values = [];
|
|
|
|
$valueInfos = $scoped->getValueInfos(['name', 'password', 'minRank']);
|
2024-12-02 01:30:36 +00:00
|
|
|
foreach($valueInfos as $valueInfo) {
|
|
|
|
$this->assertInstanceOf(ScopedConfigValueInfo::class, $valueInfo);
|
2024-10-04 22:28:27 +00:00
|
|
|
$values[$valueInfo->getRealName()] = $valueInfo->getValue();
|
2024-12-02 01:30:36 +00:00
|
|
|
}
|
2024-10-04 22:28:27 +00:00
|
|
|
|
|
|
|
$this->assertEquals($expected, $values);
|
|
|
|
|
|
|
|
$scoped = $config->scopeTo('chat', 'channels', 'lounge');
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
'name' => 'Lounge',
|
|
|
|
'auto_join' => true,
|
|
|
|
'minRank' => 0,
|
|
|
|
];
|
|
|
|
$values = $scoped->getValues([
|
|
|
|
'name',
|
|
|
|
['autoJoin:b', false, 'auto_join'],
|
|
|
|
'minRank:i',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $values);
|
|
|
|
|
|
|
|
$this->assertEquals('', $config->getString('msz:url2'));
|
|
|
|
$this->assertEquals('test', $config->getString('msz:url2', 'test'));
|
|
|
|
$this->assertEquals('https://flashii.net/_sockchat', $config->getString('msz:url'));
|
|
|
|
|
|
|
|
$this->assertEquals(0, $config->getInteger('chat:connMaxCount'));
|
|
|
|
$this->assertEquals(10, $config->getInteger('chat:connMaxCount', 10));
|
|
|
|
$this->assertEquals(30, $config->getInteger('chat:floodKickLength'));
|
|
|
|
$this->assertEquals('30', $config->getString('chat:floodKickLength'));
|
|
|
|
|
|
|
|
$this->assertEquals(0, $config->getFloat('boat'));
|
|
|
|
$this->assertEquals(0.1, $config->getFloat('boat', 0.1));
|
|
|
|
$this->assertEquals(192.168, $config->getFloat('mariadb:host'));
|
|
|
|
$this->assertEquals('192.168.0.123', $config->getString('mariadb:host'));
|
|
|
|
|
|
|
|
$this->assertEquals(false, $config->getBoolean('nonexist'));
|
|
|
|
$this->assertEquals(true, $config->getBoolean('nonexist', true));
|
|
|
|
$this->assertEquals(true, $config->getBoolean('chat:channels:lounge:autoJoin'));
|
|
|
|
$this->assertEquals('true', $config->getString('chat:channels:lounge:autoJoin'));
|
|
|
|
$this->assertEquals(true, $config->getBoolean('mariadb:db'));
|
|
|
|
|
|
|
|
$this->assertEmpty($config->getArray('nonexist'));
|
|
|
|
$this->assertEquals(['de', 'het', 'een'], $config->getArray('nonexist', ['de', 'het', 'een']));
|
|
|
|
$this->assertEquals(['lounge', 'prog', 'games', 'splat', 'passwd', 'staff'], $config->getArray('chat:channels'));
|
|
|
|
$this->assertEquals('lounge prog games splat passwd staff', $config->getString('chat:channels'));
|
|
|
|
$this->assertEquals(['fake', 'secret', 'meow'], $config->getArray('msz:secret'));
|
|
|
|
$this->assertEquals('fake secret meow', $config->getString('msz:secret'));
|
|
|
|
}
|
|
|
|
}
|