初始化仓库

This commit is contained in:
Yao
2025-08-14 16:44:56 +08:00
commit 45b8c90ad8
5157 changed files with 664203 additions and 0 deletions

View File

@ -0,0 +1,223 @@
<?php
namespace clagiordano\weblibs\configmanager\tests;
use clagiordano\weblibs\configmanager\IConfigurable;
use PHPUnit\Framework\TestCase;
/**
* Class AbstractConfigManagerTest
* @package clagiordano\weblibs\configmanager\tests
*/
abstract class AbstractConfigManagerTest extends TestCase
{
/** @var string $configFile */
protected $configFile = null;
/** @var IConfigurable $config */
protected $config = null;
public static function setUpBeforeClass()
{
parent::setUpBeforeClass();
/**
* Remove temp dir
*/
shell_exec("rm -rf " . __DIR__ . '/../testsdata/temp');
/**
* Create temp dir
*/
$status = mkdir(__DIR__ . '/../testsdata/temp/');
self::assertTrue($status);
}
public static function tearDownAfterClass()
{
parent::tearDownAfterClass();
/**
* Remove temp dir
*/
shell_exec("rm -rf " . __DIR__ . '/../testsdata/temp');
}
public function testBasicUsage()
{
$this->assertNotNull(
$this->config->getValue('app')
);
}
public function testFastUsage()
{
$this->assertNotNull(
$this->config->getValue('app')
);
}
public function testFastInvalidKey()
{
$this->assertNull(
$this->config->getValue('invalidKey')
);
}
public function testFastInvalidKeyWithDefault()
{
$this->assertEquals(
$this->config->getValue('invalidKey', 'defaultValue'),
'defaultValue'
);
}
public function testFastNestedConfig()
{
$this->assertNotNull(
$this->config->getValue('other.multi.deep.nested')
);
}
public function testCheckExistConfig()
{
$this->assertTrue(
$this->config->existValue('other.multi.deep.nested')
);
}
public function testCheckNotExistConfig()
{
$this->assertFalse(
$this->config->existValue('invalid.config.path')
);
}
public function testSetValue()
{
$this->config->setValue('other.multi.deep.nested', __FUNCTION__);
$this->assertEquals(
$this->config->getValue('other.multi.deep.nested'),
__FUNCTION__
);
}
public function testFailedSaveConfig()
{
$this->setExpectedException('Exception');
$this->config->saveConfigFile('/invalid/path');
}
public function testSuccessSaveConfigOnTempAndReload()
{
$this->config->setValue('other.multi.deep.nested', "SUPERNESTED");
$this->config->saveConfigFile("/tmp/testconfig.sample", true);
$this->assertEquals(
$this->config->getValue('other.multi.deep.nested'),
"SUPERNESTED"
);
}
public function testOverwriteSameConfigFile()
{
$this->config->saveConfigFile();
}
public function testFailWriteConfig()
{
$this->setExpectedException('\RuntimeException');
$this->config->saveConfigFile('/invalid/path/test.sample');
}
/**
* @test
* @group permissions
*/
public function canRaiseExceptionOnUnreadableFile()
{
/**
* Create new temp file
*/
$testFile = tempnam(__DIR__ . '/../testsdata/temp/', 'phpunit_');
self::assertFileExists($testFile);
/**
* Make tempfile unreadable by everyone, but still writeable
*/
$status = chmod($testFile, 0200);
self::assertTrue($status);
/**
* Check permissions it must be 0200 ( --w------- )
*/
$filePerms = (fileperms($testFile) & 0777);
self::assertSame(0200, $filePerms);
/**
* Try to read that file, an exception must be thrown
*/
self::setExpectedException('\RuntimeException');
$this->config->loadConfig($testFile);
/**
* Remove temp file
*/
$status = chmod($testFile, 0744);
self::assertTrue($status);
$filePerms = (fileperms($testFile) & 0777);
self::assertSame(0755, $filePerms);
$status = unlink($testFile);
self::assertTrue($status);
self::assertFileNotExists($testFile);
}
/**
* @return array
*/
public function configDataProvider()
{
return [
[
__DIR__ . '/../testsdata/sample_config_data.converted.yml',
'\clagiordano\weblibs\configmanager\YamlConfigManager',
],
[
__DIR__ . '/../testsdata/sample_config_data.converted.json',
'\clagiordano\weblibs\configmanager\JsonConfigManager',
],
[
__DIR__ . '/../testsdata/sample_config_data.converted.php',
'\clagiordano\weblibs\configmanager\ArrayConfigManager',
],
];
}
/**
* @test
* @dataProvider configDataProvider
* @param mixed $targetConfig
* @param mixed $targetInstance
*/
public function canConvertOneFormatToAnother($targetConfig, $targetInstance)
{
if (file_exists($targetConfig)) {
/**
* Drop target file if already existing
*/
unlink($targetConfig);
}
self::assertFileNotExists($targetConfig);
$target = new $targetInstance($targetConfig);
self::assertInstanceOf($targetInstance, $target);
$converted = $this->config->convert($target);
self::assertInstanceOf($targetInstance, $converted);
self::assertFileNotExists($targetConfig);
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace clagiordano\weblibs\configmanager\tests;
use clagiordano\weblibs\configmanager\ArrayConfigManager;
/**
* Class ArrayConfigManagerTest
* @package clagiordano\weblibs\configmanager\tests
*/
class ArrayConfigManagerTest extends AbstractConfigManagerTest
{
protected $configFile = 'testsdata/sample_config_data.php';
public function setUp()
{
parent::setUp();
$this->config = new ArrayConfigManager("TestConfigData.php");
$this->assertInstanceOf('clagiordano\weblibs\configmanager\ArrayConfigManager', $this->config);
$this->assertFileExists($this->configFile);
$this->config->loadConfig($this->configFile);
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace clagiordano\weblibs\configmanager\tests;
use clagiordano\weblibs\configmanager\ConfigManager;
/**
* Class ConfigManagerTest
* @package clagiordano\weblibs\configmanager\tests
*/
class ConfigManagerTest extends AbstractConfigManagerTest
{
protected $configFile = 'testsdata/sample_config_data.php';
public function setUp()
{
parent::setUp();
$this->config = new ConfigManager("TestConfigData.php");
$this->assertInstanceOf('clagiordano\weblibs\configmanager\ConfigManager', $this->config);
$this->assertFileExists($this->configFile);
$this->config->loadConfig($this->configFile);
}
}

View File

@ -0,0 +1,148 @@
<?php
namespace clagiordano\weblibs\configmanager\tests;
use clagiordano\weblibs\configmanager\ArrayConfigManager;
use clagiordano\weblibs\configmanager\FileConverter;
use clagiordano\weblibs\configmanager\YamlConfigManager;
use PHPUnit\Framework\TestCase;
/**
* Class FileConverterTest
* @package clagiordano\weblibs\configmanager\tests
*/
class FileConverterTest extends TestCase
{
/**
* @return array
*/
public function configDataProvider()
{
return [
[
__DIR__ . '/../testsdata/sample_config_data.php',
'\clagiordano\weblibs\configmanager\ArrayConfigManager',
__DIR__ . '/../testsdata/sample_config_data.php.converted.yml',
'\clagiordano\weblibs\configmanager\YamlConfigManager',
],
[
__DIR__ . '/../testsdata/sample_config_data.php',
'\clagiordano\weblibs\configmanager\ArrayConfigManager',
__DIR__ . '/../testsdata/sample_config_data.php.converted.json',
'\clagiordano\weblibs\configmanager\JsonConfigManager',
],
[
__DIR__ . '/../testsdata/sample_config_data.yml',
'\clagiordano\weblibs\configmanager\YamlConfigManager',
__DIR__ . '/../testsdata/sample_config_data.yml.converted.json',
'\clagiordano\weblibs\configmanager\JsonConfigManager',
],
[
__DIR__ . '/../testsdata/sample_config_data.yml',
'\clagiordano\weblibs\configmanager\YamlConfigManager',
__DIR__ . '/../testsdata/sample_config_data.yml.converted.php',
'\clagiordano\weblibs\configmanager\ArrayConfigManager',
],
[
__DIR__ . '/../testsdata/sample_config_data.json',
'\clagiordano\weblibs\configmanager\JsonConfigManager',
__DIR__ . '/../testsdata/sample_config_data.json.converted.yml',
'\clagiordano\weblibs\configmanager\YamlConfigManager',
],
[
__DIR__ . '/../testsdata/sample_config_data.json',
'\clagiordano\weblibs\configmanager\JsonConfigManager',
__DIR__ . '/../testsdata/sample_config_data.json.converted.php',
'\clagiordano\weblibs\configmanager\ArrayConfigManager',
],
];
}
/**
* @test
* @dataProvider configDataProvider
* @param mixed $sourceConfig
* @param mixed $sourceInstance
* @param mixed $targetConfig
* @param mixed $targetInstance
*/
public function canConvertOneFormatToAnother($sourceConfig, $sourceInstance, $targetConfig, $targetInstance)
{
if (file_exists($targetConfig)) {
/**
* Drop target file if already existing
*/
unlink($targetConfig);
}
$source = new $sourceInstance($sourceConfig);
self::assertInstanceOf($sourceInstance, $source);
$target = new $targetInstance($targetConfig);
self::assertInstanceOf($targetInstance, $target);
$converted = FileConverter::convert($source, $target);
self::assertInstanceOf($targetInstance, $converted);
$converted = FileConverter::convertAndSave($source, $target);
self::assertInstanceOf($targetInstance, $converted);
self::assertFileExists($targetConfig);
}
/**
* @test
*/
public function canSuccessConversionOnInvalidSource()
{
$source = new ArrayConfigManager();
$target = new YamlConfigManager(__DIR__ . '/../testsdata/sample_config_data.empty.converted.yml');
$converted = FileConverter::convert($source, $target);
self::assertInstanceOf('\clagiordano\weblibs\configmanager\YamlConfigManager', $converted);
self::assertSame($target, $converted);
}
/**
* @test
*/
public function canSuccessConversionAndSaveOnInvalidSource()
{
$source = new ArrayConfigManager();
$target = new YamlConfigManager(__DIR__ . '/../testsdata/sample_config_data.empty.converted.yml');
$converted = FileConverter::convertAndSave($source, $target);
self::assertInstanceOf('\clagiordano\weblibs\configmanager\YamlConfigManager', $converted);
self::assertSame($target, $converted);
}
/**
* @test
*/
public function cannotFailConversionOnInvalidTarget()
{
$source = new ArrayConfigManager(__DIR__ . '/../testsdata/sample_config_data.php');
$target = new YamlConfigManager();
$converted = FileConverter::convert($source, $target);
self::assertInstanceOf('\clagiordano\weblibs\configmanager\YamlConfigManager', $converted);
}
/**
* @test
*/
public function canFailConversionAndSaveOnInvalidTarget()
{
self::setExpectedException('\RuntimeException');
$source = new ArrayConfigManager(__DIR__ . '/../testsdata/sample_config_data.php');
$target = new YamlConfigManager();
$converted = FileConverter::convertAndSave($source, $target);
self::assertInstanceOf('\clagiordano\weblibs\configmanager\YamlConfigManager', $converted);
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace clagiordano\weblibs\configmanager\tests;
use clagiordano\weblibs\configmanager\JsonConfigManager;
/**
* Class JsonConfigManagerTest
* @package clagiordano\weblibs\configmanager\tests
*/
class JsonConfigManagerTest extends AbstractConfigManagerTest
{
protected $configFile = 'testsdata/sample_config_data.json';
public function setUp()
{
parent::setUp();
$this->config = new JsonConfigManager("TestConfigData.json");
$this->assertInstanceOf('clagiordano\weblibs\configmanager\JsonConfigManager', $this->config);
$this->assertFileExists($this->configFile);
$this->config->loadConfig($this->configFile);
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace clagiordano\weblibs\configmanager\tests;
use clagiordano\weblibs\configmanager\YamlConfigManager;
/**
* Class YamlConfigManagerTest
* @package clagiordano\weblibs\configmanager\tests
*/
class YamlConfigManagerTest extends AbstractConfigManagerTest
{
protected $configFile = 'testsdata/sample_config_data.yml';
public function setUp()
{
parent::setUp();
$this->config = new YamlConfigManager("TestConfigData.yml");
$this->assertInstanceOf('clagiordano\weblibs\configmanager\YamlConfigManager', $this->config);
$this->assertFileExists($this->configFile);
$this->config->loadConfig($this->configFile);
}
}