refactor: Added general config validations and timezone config
This commit is contained in:
parent
00d0ea624f
commit
c2e181aa75
@ -6,6 +6,7 @@ use Symfony\Component\Config\Definition\ConfigurationInterface;
|
||||
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
|
||||
use Symfony\Component\Config\Definition\Processor;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
use Carbon\CarbonTimeZone;
|
||||
|
||||
class ConfigBuilder implements ConfigurationInterface {
|
||||
private $configs = [];
|
||||
@ -24,20 +25,32 @@ class ConfigBuilder implements ConfigurationInterface {
|
||||
$treeBuilder = new TreeBuilder();
|
||||
$rootNode = $treeBuilder->root('configs');
|
||||
$ref = $rootNode->children()
|
||||
->arrayNode('logger')
|
||||
->canBeEnabled()
|
||||
->arrayNode('general')
|
||||
->children()
|
||||
->scalarNode('file')->end()
|
||||
->scalarNode('level')
|
||||
->defaultValue('ERROR')
|
||||
->validate()
|
||||
->IfNotInArray(['DEBUG', 'INFO', 'NOTICE', 'WARNING', 'ERROR', 'CRITICAL', 'ALERT', 'EMERGENCY'])
|
||||
->thenInvalid('Invalid log level %s')
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->end();
|
||||
|
||||
->arrayNode('logger')
|
||||
->canBeEnabled()
|
||||
->children()
|
||||
->scalarNode('file')->end()
|
||||
->scalarNode('level')
|
||||
->defaultValue('ERROR')
|
||||
->validate()
|
||||
->IfNotInArray(['DEBUG', 'INFO', 'NOTICE', 'WARNING', 'ERROR', 'CRITICAL', 'ALERT', 'EMERGENCY'])
|
||||
->thenInvalid('Invalid log level %s')
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->scalarNode('timezone')
|
||||
->defaultValue('UTC')
|
||||
->validate()
|
||||
->IfNotInArray(CarbonTimeZone::listIdentifiers())
|
||||
->thenInvalid('Invalid timezone identifier %s')
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->arrayNode('storages')
|
||||
->children();
|
||||
foreach ($this->configs as $config) {
|
||||
$ref = $ref->append($config->get());
|
||||
}
|
||||
|
@ -25,7 +25,7 @@ class Console extends AbstractConsole {
|
||||
if (isset($input)) {
|
||||
$input = $this->convertToString($input);
|
||||
}
|
||||
$process = new Process(implode(' ', $stdin), $input, $envs);
|
||||
$process = new Process(implode(' ', $stdin), null, $envs, $input);
|
||||
$process->inheritEnvironmentVariables();
|
||||
|
||||
try {
|
||||
|
@ -39,11 +39,13 @@ class StorageManager {
|
||||
throw new \Exception('StorageManger was not initialize or configs are not defined');
|
||||
}
|
||||
foreach ($this->configs as $key => $value) {
|
||||
$storage = $this->storages[$key];
|
||||
if (!isset($storage)){
|
||||
throw new \Exception();
|
||||
if ($key !== 'logger') {
|
||||
$storage = $this->storages[$key];
|
||||
if (!isset($storage)){
|
||||
throw new \Exception();
|
||||
}
|
||||
$storage->save($calendar);
|
||||
}
|
||||
$storage->save($calendar);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -26,9 +26,9 @@ class Taskwarrior implements IStorage {
|
||||
public function refresh() {
|
||||
$output = $this->console->execute('task', ['sync'], null,
|
||||
['TASKRC' => $this->configs['taskrc'],'TASKDATA' => $this->configs['taskdata']]);
|
||||
$tasks = json_decode($this->console->execute('task', ['export'], null,
|
||||
$this->tasks = json_decode($this->console->execute('task', ['export'], null,
|
||||
['TASKRC' => $this->configs['taskrc'], 'TASKDATA' => $this->configs['taskdata']]), true);
|
||||
foreach ($tasks as $task) {
|
||||
foreach ($this->tasks as $task) {
|
||||
if (isset($task['uid'])) {
|
||||
$this->tasks[$task['uid']] = $task;
|
||||
}
|
||||
|
@ -13,18 +13,21 @@ class ConfigTest extends TestCase {
|
||||
|
||||
public $mockConfigBuilder;
|
||||
|
||||
public function testLoggerConfigs() {
|
||||
public function testGeneralLoggerConfigs() {
|
||||
$configs = new ConfigBuilder(__DIR__ . '/Fixtures/LoggerConfig.yaml');
|
||||
$contents = $configs->loadYaml();
|
||||
$this->assertEquals(sizeof($contents), 1);
|
||||
$this->assertArrayHasKey('logger', $contents, 'config missing logger property');
|
||||
$this->assertArrayHasKey('file', $contents['logger'], 'config missing logger.file property');
|
||||
$this->assertEquals($contents['logger']['file'], '/home/user/logger.yaml');
|
||||
$this->assertArrayHasKey('level', $contents['logger'], 'config missing logger.level property');
|
||||
$this->assertEquals($contents['logger']['level'], 'ERROR', 'ERROR is not set as default logger level');
|
||||
$this->assertArrayHasKey('enabled', $contents['logger'], 'config missing logger.enabled property');
|
||||
$this->assertTrue($contents['logger']['enabled']);
|
||||
|
||||
$this->assertArrayHasKey('general', $contents, 'config missing general config');
|
||||
$generalConfigs = $contents['general'];
|
||||
$this->assertArrayHasKey('logger', $generalConfigs, 'general config is missing logger property');
|
||||
$this->assertArrayHasKey('file', $generalConfigs['logger'], 'general logger config missing file property');
|
||||
$this->assertEquals($generalConfigs['logger']['file'], '/home/user/logger.yaml');
|
||||
$this->assertArrayHasKey('level', $generalConfigs['logger'], 'general logger config missing level property');
|
||||
$this->assertEquals($generalConfigs['logger']['level'], 'ERROR', 'ERROR is not set as default logger level');
|
||||
$this->assertArrayHasKey('enabled', $generalConfigs['logger'], 'general config logger enabled property is missing');
|
||||
$this->assertTrue($generalConfigs['logger']['enabled']);
|
||||
$this->assertArrayHasKey('timezone', $generalConfigs, 'general config is missing timezone property');
|
||||
$this->assertEquals($generalConfigs['timezone'], 'UTC', 'UTC is not set as default timezone');
|
||||
}
|
||||
|
||||
public function testTaskwarriorConfig() {
|
||||
@ -32,19 +35,15 @@ class ConfigTest extends TestCase {
|
||||
$configs->add(new TaskwarriorConfig());
|
||||
$contents = $configs->loadYaml();
|
||||
$this->assertEquals(sizeof($contents), 2);
|
||||
$this->assertArrayHasKey('logger', $contents, 'config missing logger property');
|
||||
$this->assertArrayHasKey('file', $contents['logger'], 'config missing logger.file property');
|
||||
$this->assertArrayHasKey('level', $contents['logger'], 'config missing logger.level property');
|
||||
$this->assertArrayHasKey('enabled', $contents['logger'], 'config missing logger.enabled property');
|
||||
$this->assertArrayHasKey('taskwarrior', $contents, 'config missing taskwarrior property');
|
||||
$this->assertArrayHasKey('taskrc', $contents['taskwarrior'], 'config missing taskwarrior.taskrc property');
|
||||
$this->assertEquals($contents['taskwarrior']['taskrc'], '/home/aerex/.taskrc');
|
||||
$this->assertArrayHasKey('taskdata', $contents['taskwarrior'], 'config missing taskwarrior.taskdata property');
|
||||
$this->assertEquals($contents['taskwarrior']['taskdata'], '/home/aerex/.task');
|
||||
$this->assertArrayHasKey('project_tag_suffix', $contents['taskwarrior'], 'config missing taskwarrior.taskdata property');
|
||||
$this->assertEquals($contents['taskwarrior']['project_tag_suffix'], 'project_');
|
||||
$this->assertArrayHasKey('storages', $contents, 'storages config missing');
|
||||
$this->assertArrayHasKey('taskwarrior', $contents['storages'], 'storage config missing taskwarrior property');
|
||||
$taskwarriorConfigs = $contents['storages']['taskwarrior'];
|
||||
$this->assertArrayHasKey('taskrc', $taskwarriorConfigs, 'taskwarrior config is missing taskrc property');
|
||||
$this->assertEquals($taskwarriorConfigs['taskrc'], '/home/aerex/.taskrc');
|
||||
$this->assertArrayHasKey('taskdata', $taskwarriorConfigs, 'taskwarrior config is missing taskdata property');
|
||||
$this->assertEquals($taskwarriorConfigs['taskdata'], '/home/aerex/.task');
|
||||
$this->assertArrayHasKey('project_tag_suffix', $taskwarriorConfigs, 'taskwarrior config is missing project_tag_suffix property');
|
||||
$this->assertEquals($taskwarriorConfigs['project_tag_suffix'], 'project_');
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -1,2 +1,3 @@
|
||||
logger:
|
||||
file: /home/user/logger.yaml
|
||||
general:
|
||||
logger:
|
||||
file: /home/user/logger.yaml
|
||||
|
@ -1,7 +1,10 @@
|
||||
logger:
|
||||
file: /home/aerex/baikal-storage-plugin.log
|
||||
level: DEBUG
|
||||
taskwarrior:
|
||||
taskdata: /home/aerex/.task
|
||||
taskrc: /home/aerex/.taskrc
|
||||
project_tag_suffix: project_
|
||||
general:
|
||||
logger:
|
||||
file: /home/aerex/baikal-storage-plugin.log
|
||||
level: DEBUG
|
||||
timezone: 'America/Denver'
|
||||
storages:
|
||||
taskwarrior:
|
||||
taskdata: /home/aerex/.task
|
||||
taskrc: /home/aerex/.taskrc
|
||||
project_tag_suffix: project_
|
||||
|
Loading…
Reference in New Issue
Block a user