Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
df7ab092cc | ||
|
8cafda3f26 | ||
|
b9d27d9aa2 | ||
|
728fce1b78 | ||
|
c302c4653a | ||
|
c2e181aa75 | ||
|
00d0ea624f | ||
|
2f87752f6e | ||
|
346e5c239b | ||
|
9d78b4a8eb | ||
|
84ecfb5ad1 | ||
|
40b66fb38b |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
vendor/
|
||||
main.uml
|
||||
composer.lock
|
||||
.phpunit.result.cache
|
||||
|
@@ -5,8 +5,11 @@
|
||||
composer require aerex/baikal-storage-plugin
|
||||
```
|
||||
|
||||
## Configuration
|
||||
Copy sample configuration to your baikal installation. Make sure that the folder is writable
|
||||
|
||||
## Usage
|
||||
- Add the plugin to `Core/Frameworks/Baikal/Core/Server.php`
|
||||
```
|
||||
$this->server->addPlugin(new \Aerex\BaikalStorage\Plugin())
|
||||
$this->server->addPlugin(new \Aerex\BaikalStorage\Plugin(<path-of-config-file>))
|
||||
```
|
||||
|
@@ -17,18 +17,19 @@
|
||||
],
|
||||
"require": {
|
||||
"php": ">=5.5",
|
||||
"sabre/dav" : "~4.0.2",
|
||||
"sabre/dav" : "~4.1.0",
|
||||
"sabre/vobject": "^4.0",
|
||||
"easycorp/easy-log-handler": "^1.0",
|
||||
"nesbot/carbon": "^2.0.0",
|
||||
"laminas/laminas-validator": "^2.13",
|
||||
"laminas/laminas-stdlib": "^3.2",
|
||||
"psr/container": "^1.0",
|
||||
"symfony/config": "3.4",
|
||||
"symfony/process": "^3.4"
|
||||
"symfony/process": "^3.4",
|
||||
"monolog/monolog": "^2.0",
|
||||
"symfony/yaml": "~3.0|~4.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^7.4"
|
||||
"phpunit/phpunit": "^8.5.3"
|
||||
},
|
||||
"authors": [
|
||||
{
|
||||
|
7
config.yaml
Executable file
7
config.yaml
Executable file
@@ -0,0 +1,7 @@
|
||||
logger:
|
||||
file: /home/aerex/baikal-storage-plugin.log
|
||||
level: 'DEBUG'
|
||||
taskwarrior:
|
||||
taskdata: /home/aerex/.task
|
||||
taskrc: /home/aerex/.taskrc
|
||||
project_tag_suffix: project_
|
@@ -6,38 +6,51 @@ 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 = [];
|
||||
private $configDir;
|
||||
private $configFile;
|
||||
|
||||
public function __construct($configDir = null) {
|
||||
if (!isset($configDir)) {
|
||||
$this->configDir = $this->getHomeDir() . '~/.config/baikal';
|
||||
} else {
|
||||
$this->configDir = $configDir;
|
||||
}
|
||||
public function __construct($configFile) {
|
||||
$this->configFile = $configFile;
|
||||
$this->processor = new Processor();
|
||||
}
|
||||
|
||||
private function getHomeDir() {
|
||||
if (stristr(PHP_OS, 'WIN')) {
|
||||
return rtrim($_SERVER['HOMEDRIVE'] . $_SERVER['HOMEPATH'], '\\/');
|
||||
} else {
|
||||
return rtrim($_SERVER['HOME'], '/');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function add($config) {
|
||||
$this->configs[] = $config;
|
||||
}
|
||||
|
||||
public function getConfigTreeBuilder() {
|
||||
$treeBuilder = new TreeBuilder('configs');
|
||||
$rootNode = $treeBuilder->getRootNode();
|
||||
$ref = $rootNode->children();
|
||||
$treeBuilder = new TreeBuilder();
|
||||
$rootNode = $treeBuilder->root('configs');
|
||||
$ref = $rootNode->children()
|
||||
->arrayNode('general')
|
||||
->children()
|
||||
->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());
|
||||
}
|
||||
@@ -46,11 +59,7 @@ class ConfigBuilder implements ConfigurationInterface {
|
||||
}
|
||||
|
||||
public function readContent() {
|
||||
if (!is_dir($this->configDir)) {
|
||||
mkdir($this->configDir, 0755, true);
|
||||
}
|
||||
$contents = sprintf('%s/storage.yml', $this->configDir);
|
||||
return file_get_contents($contents);
|
||||
return file_get_contents($this->configFile);
|
||||
}
|
||||
|
||||
public function loadYaml() {
|
||||
|
@@ -2,16 +2,24 @@
|
||||
|
||||
namespace Aerex\BaikalStorage\Configs;
|
||||
|
||||
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
|
||||
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
|
||||
|
||||
class TaskwarriorConfig {
|
||||
public function get() {
|
||||
$treeBuilder = new TreeBuilder('taskwarrior');
|
||||
$node = $treeBuilder->getRootNode();
|
||||
$node->children()
|
||||
->scalarNode('data_dir')
|
||||
$node = new ArrayNodeDefinition('taskwarrior');
|
||||
$node->canBeEnabled()
|
||||
->children()
|
||||
->scalarNode('taskdata')
|
||||
->defaultValue('~/.task')
|
||||
->end()
|
||||
->scalarNode('taskrc')
|
||||
->defaultValue('~/.taskrc')
|
||||
->end()
|
||||
->scalarNode('project_tag_suffix')
|
||||
->defaultValue('project_')
|
||||
->end()
|
||||
->end();
|
||||
|
||||
return $node;
|
||||
}
|
||||
}
|
||||
|
@@ -18,20 +18,20 @@ class Console extends AbstractConsole {
|
||||
}
|
||||
}
|
||||
|
||||
public function execute($cmd, $args, $input = null) {
|
||||
public function execute($cmd, $args, $input = null, $envs = []) {
|
||||
$stdin[] = $cmd;
|
||||
$stdin[] = array_merge($stdin, $this->defaultArgs, $args);
|
||||
$stdin = array_merge($stdin, $this->defaultArgs, $args);
|
||||
|
||||
if (isset($input)) {
|
||||
$stdin[] = $this->convertToString($input);
|
||||
$process = new Process($stdin);
|
||||
$input = $this->convertToString($input);
|
||||
}
|
||||
$process = new Process(implode(' ', $stdin), null, $envs, $input);
|
||||
$process->inheritEnvironmentVariables();
|
||||
|
||||
try {
|
||||
$process->mustRun();
|
||||
return $process->getOutput();
|
||||
} catch (ProcessFailedException $error) {
|
||||
echo $error->getMessage();
|
||||
throw $error;
|
||||
}
|
||||
}
|
||||
|
72
lib/Logger.php
Normal file
72
lib/Logger.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace Aerex\BaikalStorage;
|
||||
use Monolog\Logger as Monolog;
|
||||
use Monolog\Handler\StreamHandler;
|
||||
|
||||
class Logger {
|
||||
|
||||
private $configs = ['enabled' => false];
|
||||
|
||||
function __construct($configs, $tag) {
|
||||
if (isset($configs['general']) && isset($configs['general']['logger'])) {
|
||||
$this->configs = $configs['general']['logger'];
|
||||
}
|
||||
if ($this->configs['enabled']) {
|
||||
$this->createLoggerFile();
|
||||
$this->logger = new Monolog($tag);
|
||||
$logLevel = Monolog::getLevels()[$this->configs['level']];
|
||||
$this->logger->pushHandler(new StreamHandler($this->configs['file'], $logLevel));
|
||||
}
|
||||
}
|
||||
|
||||
public function createLoggerFile() {
|
||||
if (!file_exists($this->configs['file'])) {
|
||||
if (!fopen($this->configs['file'], 'w')) {
|
||||
throw new \Exception(sprintf('Could not create logger file %s', $this->configs['file']));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function debug($message) {
|
||||
if ($this->configs['enabled']) {
|
||||
$this->logger->debug($message);
|
||||
}
|
||||
}
|
||||
|
||||
public function info($message) {
|
||||
if ($this->configs['enabled']) {
|
||||
$this->logger->info($message);
|
||||
}
|
||||
}
|
||||
|
||||
public function notice($message) {
|
||||
if ($this->configs['enabled']) {
|
||||
$this->logger->notice($message);
|
||||
}
|
||||
}
|
||||
|
||||
public function error($message) {
|
||||
if ($this->configs['enabled']) {
|
||||
$this->logger->error($message);
|
||||
}
|
||||
}
|
||||
|
||||
public function critical($message) {
|
||||
if ($this->configs['enabled']) {
|
||||
$this->logger->critical($message);
|
||||
}
|
||||
}
|
||||
public function alert($message) {
|
||||
if ($this->configs['enabled']) {
|
||||
$this->logger->alert($message);
|
||||
}
|
||||
}
|
||||
|
||||
public function emergency($message) {
|
||||
if ($this->configs['enabled']) {
|
||||
$this->logger->emergency($message);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Aerex\BaikalStorage;
|
||||
|
||||
use Aerex\BaikalStorage\Logger;
|
||||
use Aerex\BaikalStorage\Storages\Taskwarrior;
|
||||
use Aerex\BaikalStorage\Configs\ConfigBuilder;
|
||||
use Aerex\BaikalStorage\Configs\TaskwarriorConfig;
|
||||
@@ -31,26 +32,23 @@ class Plugin extends ServerPlugin {
|
||||
*/
|
||||
protected $storageManager;
|
||||
|
||||
/**
|
||||
* @var ConfigBuilder
|
||||
*/
|
||||
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* Creates the Taskwarrior plugin
|
||||
* Creates the Storage plugin
|
||||
*
|
||||
* @param CalendarProcessor $TWCalManager
|
||||
*
|
||||
*/
|
||||
function __construct($config = null){
|
||||
if (isset($config)) {
|
||||
$this->config = $config;
|
||||
} else {
|
||||
$this->config = new ConfigBuilder();
|
||||
function __construct($configFile){
|
||||
$configs = $this->buildConfigurations($configFile);
|
||||
$this->storageManager = new StorageManager($configs);
|
||||
$this->initializeStorages($configs);
|
||||
}
|
||||
$this->storageManager = new StorageManager($this->config);
|
||||
$this->addStorages();
|
||||
|
||||
public function buildConfigurations($configFile) {
|
||||
$this->config = new ConfigBuilder($configFile);
|
||||
$this->config->add(new TaskwarriorConfig());
|
||||
return $this->config->loadYaml();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -58,10 +56,9 @@ class Plugin extends ServerPlugin {
|
||||
*
|
||||
*/
|
||||
|
||||
public function addStorages() {
|
||||
$taskwarrior = new Taskwarrior(new Console(['rc.verbose=nothing', 'rc.hooks=off']), new TaskwarriorConfig());
|
||||
public function initializeStorages($configs) {
|
||||
$taskwarrior = new Taskwarrior(new Console(['rc.verbose=nothing', 'rc.hooks=off']), $configs, new Logger($configs, 'Taskwarrior'););
|
||||
$this->storageManager->addStorage(Taskwarrior::NAME, $taskwarrior);
|
||||
$this->storageManager->init();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -3,7 +3,6 @@
|
||||
namespace Aerex\BaikalStorage;
|
||||
|
||||
use Sabre\VObject\Component\VCalendar as Calendar;
|
||||
use Aerex\BaikalStorage\Configs\ConfigBuilder;
|
||||
|
||||
class StorageManager {
|
||||
|
||||
@@ -15,13 +14,12 @@ class StorageManager {
|
||||
|
||||
|
||||
/**
|
||||
* @var Config
|
||||
* @var array()
|
||||
*/
|
||||
private $configBuilder;
|
||||
private $configs;
|
||||
|
||||
public function __construct($configBuilder){
|
||||
$this->configBuilder = $configBuilder;
|
||||
public function __construct($configs){
|
||||
$this->configs = $configs;
|
||||
}
|
||||
|
||||
public function getStorages() {
|
||||
@@ -33,24 +31,18 @@ class StorageManager {
|
||||
}
|
||||
|
||||
public function addStorage($name, $storage) {
|
||||
$this->configBuilder->add($storage->getConfig());
|
||||
$this->storages[$name] = $storage;
|
||||
}
|
||||
|
||||
public function init() {
|
||||
$this->configs = $this->configBuilder->loadYaml();
|
||||
}
|
||||
|
||||
public function import(Calendar $calendar) {
|
||||
if (!isset($this->configs)) {
|
||||
throw new \Exception('StorageManger was not initialize or configs are not defined');
|
||||
}
|
||||
foreach ($this->configs as $key => $value) {
|
||||
foreach ($this->configs['storages'] as $key => $value) {
|
||||
$storage = $this->storages[$key];
|
||||
if (!isset($storage)){
|
||||
throw new \Exception();
|
||||
}
|
||||
$storage->setRawConfigs($this->configs[$key]);
|
||||
$storage->save($calendar);
|
||||
}
|
||||
}
|
||||
|
@@ -8,5 +8,4 @@ interface IStorage {
|
||||
public function save(Calendar $c);
|
||||
public function refresh();
|
||||
public function getConfig();
|
||||
public function setRawConfigs($rawConfigs);
|
||||
}
|
||||
|
@@ -4,86 +4,150 @@ namespace Aerex\BaikalStorage\Storages;
|
||||
|
||||
use Sabre\VObject\Component\VCalendar as Calendar;
|
||||
use Carbon\Carbon;
|
||||
use Carbon\CarbonTimeZone;
|
||||
|
||||
class Taskwarrior implements IStorage {
|
||||
|
||||
private const DATA_FILES = ['pending.data', 'completed.data', 'undo.data'];
|
||||
private $rawConfigs;
|
||||
public const NAME = 'taskwarrior';
|
||||
private $tasks = [];
|
||||
public function __construct($console, $config) {
|
||||
private $configs;
|
||||
private $logger;
|
||||
private $tz;
|
||||
|
||||
public function __construct($console, $configs, $logger) {
|
||||
$this->console = $console;
|
||||
$this->config = $config;
|
||||
$this->configs = $configs['storages']['taskwarrior'];
|
||||
$this->logger = $logger;
|
||||
$this->tz = new CarbonTimeZone($configs['general']['timezone']);
|
||||
}
|
||||
|
||||
public function getConfig() {
|
||||
return $this->config;
|
||||
}
|
||||
|
||||
public function setRawConfigs($rawConfigs) {
|
||||
$this->rawConfigs = $rawConfigs;
|
||||
}
|
||||
|
||||
public function refresh() {
|
||||
$dataDir = $this->rawConfigs['data_dir'];
|
||||
$fp = fopen(sprintf('%s/taskwarrior-baikal-storage.lock', $dataDir), 'a');
|
||||
|
||||
if (!$fp || !flock($fp, LOCK_EX | LOCK_NB, $eWouldBlock) || $eWouldBlock) {
|
||||
fputs(STDERR, 'Could not get lock');
|
||||
}
|
||||
|
||||
$mtime = 0;
|
||||
$tasksUpdated = false;
|
||||
foreach (Taskwarrior::DATA_FILES as $dataFile) {
|
||||
$fmtime = filemtime(sprintf('%s/%s', $this->config['data_dir'], $dataFile));
|
||||
if ($fmtime > $mtime) {
|
||||
$mtime = $fmtime;
|
||||
$tasksUpdated = true;
|
||||
$output = $this->console->execute('task', ['sync'], null,
|
||||
['TASKRC' => $this->configs['taskrc'],'TASKDATA' => $this->configs['taskdata']]);
|
||||
$this->tasks = json_decode($this->console->execute('task', ['export'], null,
|
||||
['TASKRC' => $this->configs['taskrc'], 'TASKDATA' => $this->configs['taskdata']]), true);
|
||||
foreach ($this->tasks as $task) {
|
||||
if (isset($task['uid'])) {
|
||||
$this->tasks[$task['uid']] = $task;
|
||||
}
|
||||
}
|
||||
|
||||
if ($tasksUpdated) {
|
||||
$tasks = $this->console->execute('task', ['export']);
|
||||
foreach ($tasks as $task) {
|
||||
$this->tasks[$task['uuid']] = $task;
|
||||
}
|
||||
}
|
||||
fclose($fp);
|
||||
unlink(sprintf('%s/taskwarrior-baikal-storage.lock', $dataDir));
|
||||
$this->logger->info($output);
|
||||
}
|
||||
|
||||
public function vObjectToTask($vtodo) {
|
||||
if ($this->tasks['uid'] == $vtodo->UID) {
|
||||
$task = $this->tasks['uid'];
|
||||
if (isset($this->tasks[(string)$vtodo->UID])) {
|
||||
$task = $this->tasks[(string)$vtodo->UID];
|
||||
} else {
|
||||
$task = [];
|
||||
$task['uid'] = $vtodo->UID;
|
||||
$task['uid'] = (string)$vtodo->UID;
|
||||
}
|
||||
|
||||
|
||||
if (!isset($vtodo->DESCRIPTION) && isset($vtodo->SUMMARY)){
|
||||
$task['description'] = $vtodo->SUMMARY;
|
||||
} else {
|
||||
$task['description'] = $vtodo->DESCRIPTION;
|
||||
if (isset($vtodo->SUMMARY)){
|
||||
$task['description'] = (string)$vtodo->SUMMARY;
|
||||
} else if(isset($vtodo->DESCRIPTION)) {
|
||||
$task['description'] = (string)$vtodo->DESCRIPTION;
|
||||
}
|
||||
|
||||
if (isset($vtodo->DTSTAMP)){
|
||||
$task['entry'] = new Carbon($vtodo->DTSTAMP->getDateTime()->format(\DateTime::W3C));
|
||||
$task['entry'] = new Carbon($vtodo->DTSTAMP->getDateTime()->format(\DateTime::W3C), $this->tz);
|
||||
}
|
||||
|
||||
if (isset($vtodo->DTSTART)) {
|
||||
$task['start'] = new Carbon($vtodo->DTSTART->getDateTime()->format(\DateTime::W3C), $this->tz);
|
||||
}
|
||||
|
||||
if (isset($vtodo->DTEND)){
|
||||
$task['end'] = new Carbon($vtodo->DTEND->getDateTime()->format(\DateTime::W3C), $this->tz);
|
||||
}
|
||||
|
||||
if (isset($vtodo->{'LAST-MODIFIED'})) {
|
||||
$task['modified'] = new Carbon($vtodo->{'LAST-MODIFIED'}->getDateTime()->format(\DateTime::W3C), $this->tz);
|
||||
}
|
||||
|
||||
if (isset($vtodo->PRIORITY)) {
|
||||
$priority = $vtodo->PRIORITY->getJsonValue();
|
||||
if ($priority < 5) {
|
||||
$task['priority'] = 'H';
|
||||
} else if ($priority === 5) {
|
||||
$task['priority'] = 'M';
|
||||
} else if ($priority > 5 && $priority < 10) {
|
||||
$task['priority'] = 'L';
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($vtodo->DUE)){
|
||||
$task['due'] = new Carbon($vtodo->DUE->getDateTime()->format(\DateTime::W3C));
|
||||
$task['due'] = new Carbon($vtodo->DUE->getDateTime());
|
||||
}
|
||||
|
||||
if (isset($vtodo->RRULE)) {
|
||||
$rules = $vtodo->RRULE->getParts();
|
||||
if (isset($rules['FREQ'])) {
|
||||
$task['recu'] = $rules['FREQ'];
|
||||
}
|
||||
if (isset($rules['UNTIL'])) {
|
||||
$task['until'] = $rules['UNTIL'];
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($vtodo->STATUS)) {
|
||||
switch((string)$vtodo->STATUS) {
|
||||
case 'NEEDS-ACTION':
|
||||
$task['status'] = 'pending';
|
||||
break;
|
||||
case 'COMPLETED':
|
||||
$task['status'] = 'completed';
|
||||
if (!isset($task['end'])) {
|
||||
$task['end'] = new Carbon($vtodo->DTSTAMP->getDateTime()->format(\DateTime::W3C), $this->tz);
|
||||
}
|
||||
break;
|
||||
case 'CANCELED':
|
||||
$task['status'] = 'deleted';
|
||||
if (!isset($task['end'])) {
|
||||
$task['end'] = new Carbon($vtodo->DTSTAMP->getDateTime()->format(\DateTime::W3C), $this->tz);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($vtodo->CATEGORIES)) {
|
||||
$task['tags'] = [];
|
||||
foreach ($vtodo->CATEGORIES as $category) {
|
||||
if (isset($this->configs['project_tag_suffix'])) {
|
||||
$projTagSuffixRegExp = sprintf('/^%s/', $this->configs['project_tag_suffix']);
|
||||
if (preg_match($projTagSuffixRegExp, $category)) {
|
||||
$task['project'] = preg_replace($projTagSuffixRegExp, '', $category);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
$task['tags'] = $category;
|
||||
}
|
||||
}
|
||||
|
||||
return $task;
|
||||
}
|
||||
|
||||
public function save(Calendar $c) {
|
||||
try {
|
||||
if (!isset($c->VTODO)){
|
||||
throw new \Exception('Calendar event does not contain VTODO');
|
||||
}
|
||||
$this->logger->info(json_encode($c->jsonSerialize()));
|
||||
$this->refresh();
|
||||
$task = $this->vObjectToTask($c->VTODO);
|
||||
$this->console->execute('task', ['import'], $task);
|
||||
$this->logger->info(json_encode($task));
|
||||
$this->logger->info(
|
||||
sprintf('Executing TASKRC = %s TASKDATA = %s task import %s', $this->configs['taskrc'], $this->configs['taskdata'], json_encode($task))
|
||||
);
|
||||
$output = $this->console->execute('task', ['import'], $task,
|
||||
['TASKRC' => $this->configs['taskrc'],'TASKDATA' => $this->configs['taskdata']]);
|
||||
$this->logger->info($output);
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error($e->getTraceAsString());
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -9,7 +9,7 @@
|
||||
|
||||
|
||||
<testsuite name="Baikal Storage Plug Tests">
|
||||
<directory>./lib/tests</directory>
|
||||
<directory>./lib/tests/</directory>
|
||||
</testsuite>
|
||||
|
||||
<filter>
|
||||
|
49
tests/Configs/ConfigTest.php
Normal file
49
tests/Configs/ConfigTest.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
namespace Aerex\BaikalStorage;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Aerex\BaikalStorage\Configs\ConfigBuilder;
|
||||
use Aerex\BaikalStorage\Configs\TaskwarriorConfig;
|
||||
|
||||
class ConfigTest extends TestCase {
|
||||
|
||||
/**
|
||||
* @var \PHPUnit_Framework_MockObject_MockObject
|
||||
* */
|
||||
|
||||
public $mockConfigBuilder;
|
||||
|
||||
public function testGeneralLoggerConfigs() {
|
||||
$configs = new ConfigBuilder(__DIR__ . '/Fixtures/LoggerConfig.yaml');
|
||||
$contents = $configs->loadYaml();
|
||||
$this->assertEquals(sizeof($contents), 1);
|
||||
$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() {
|
||||
$configs = new ConfigBuilder(__DIR__ . '/Fixtures/TaskwarriorConfig.yaml');
|
||||
$configs->add(new TaskwarriorConfig());
|
||||
$contents = $configs->loadYaml();
|
||||
$this->assertEquals(sizeof($contents), 2);
|
||||
$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_');
|
||||
}
|
||||
}
|
||||
|
3
tests/Configs/Fixtures/LoggerConfig.yaml
Executable file
3
tests/Configs/Fixtures/LoggerConfig.yaml
Executable file
@@ -0,0 +1,3 @@
|
||||
general:
|
||||
logger:
|
||||
file: /home/user/logger.yaml
|
10
tests/Configs/Fixtures/TaskwarriorConfig.yaml
Executable file
10
tests/Configs/Fixtures/TaskwarriorConfig.yaml
Executable file
@@ -0,0 +1,10 @@
|
||||
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_
|
@@ -1,2 +0,0 @@
|
||||
taskwarrior:
|
||||
data_dir: '~/task'
|
@@ -5,9 +5,8 @@ namespace Aerex\BaikalStorage;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Aerex\BaikalStorage\AbstractConsole;
|
||||
use Aerex\BaikalStorage\Configs\ConfigBuilder;
|
||||
use Aerex\BaikalStorage\Configs\TaskwarriorConfig;
|
||||
use Aerex\BaikalStorage\Storages\Taskwarrior;
|
||||
use Aerex\BaikalStorage\Storages\IStorage;
|
||||
use Aerex\BaikalStorage\Logger;
|
||||
use Sabre\VObject\Component\VCalendar as Calendar;
|
||||
|
||||
class StorageManagerTest extends TestCase {
|
||||
@@ -21,50 +20,43 @@ class StorageManagerTest extends TestCase {
|
||||
|
||||
public $mockConfigBuilder;
|
||||
|
||||
function setUp() {
|
||||
protected function setUp(): void {
|
||||
$this->mockConfigBuilder = $this->getMockBuilder(ConfigBuilder::class)
|
||||
->setMethods(['readContent'])
|
||||
->setConstructorArgs([''])
|
||||
->getMock();
|
||||
$this->mockConsole = $this->createMock(AbstractConsole::class);
|
||||
$this->mockStorage = $this->createMock(IStorage::class);
|
||||
$this->mockStorage = $this->createMock(Taskwarrior::class);
|
||||
$this->mockLogger = $this->createMock(Logger::class);
|
||||
$this->configs = [
|
||||
'general' => [
|
||||
'logger' => ['file' => '', 'level'=> 'DEBUG', 'enabled' => true],
|
||||
'timezone' => 'UTC'
|
||||
],
|
||||
'storages' => [
|
||||
'taskwarrior' => ['taskrc' => '', 'taskdata' => '']
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
public function testAddTaskwarriorStorage() {
|
||||
$this->mockConfigBuilder->expects($this->once())
|
||||
->method('readContent')
|
||||
->willReturn(file_get_contents(__DIR__ . '/Fixtures/taskwarrior_config.yml'));
|
||||
$tw = new Taskwarrior($this->mockConsole, new TaskwarriorConfig());
|
||||
$tw = new Taskwarrior($this->mockConsole, $this->configs, $this->mockLogger);
|
||||
$manager = new StorageManager($this->mockConfigBuilder);
|
||||
$manager->addStorage(Taskwarrior::NAME, $tw);
|
||||
$storages = $manager->getStorages();
|
||||
$manager->init();
|
||||
$configs = $manager->getConfigs();
|
||||
$this->assertEquals(sizeof(array_keys($storages)), 1, 'Taskwarrior storage was not added');
|
||||
$this->assertEquals(sizeof(array_keys($configs)), 1, 'Taskwarrior config was not loaded');
|
||||
$this->assertArrayHasKey('taskwarrior', $storages, 'Storages should have taskwarrior');
|
||||
$this->assertArrayHasKey('taskwarrior', $configs, 'Configs should have taskwarrior');
|
||||
}
|
||||
|
||||
public function testTaskwarriorImport() {
|
||||
$cal = new Calendar();
|
||||
$this->mockConfigBuilder->expects($this->once())
|
||||
->method('readContent')
|
||||
->willReturn(file_get_contents(__DIR__ . '/Fixtures/taskwarrior_config.yml'));
|
||||
$this->mockStorage->expects($this->once())
|
||||
->method('save')
|
||||
->with($this->equalTo($cal));
|
||||
$this->mockStorage->expects($this->once())
|
||||
->method('setRawConfigs')
|
||||
->with($this->equalTo(['data_dir' => '~/.task']));
|
||||
$this->mockStorage->expects($this->once())
|
||||
->method('getConfig')
|
||||
->willReturn(new TaskwarriorConfig());
|
||||
|
||||
$manager = new StorageManager($this->mockConfigBuilder);
|
||||
$manager = new StorageManager($this->configs);
|
||||
$manager->addStorage(Taskwarrior::NAME, $this->mockStorage);
|
||||
$manager->init();
|
||||
|
||||
$manager->import($cal);
|
||||
|
||||
}
|
||||
|
60
tests/Storages/TaskwarriorTest.php
Normal file
60
tests/Storages/TaskwarriorTest.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace Aerex\BaikalStorage;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Aerex\BaikalStorage\AbstractConsole;
|
||||
use Aerex\BaikalStorage\Logger;
|
||||
use Sabre\VObject\Component\VCalendar as Calendar;
|
||||
use Aerex\BaikalStorage\Storages\Taskwarrior;
|
||||
|
||||
class TaskwarriorTest extends TestCase {
|
||||
|
||||
/**
|
||||
* @var \PHPUnit_Framework_MockObject_MockObject
|
||||
* */
|
||||
private $mockConsole;
|
||||
|
||||
protected function setUp(): void {
|
||||
$this->mockConsole = $this->createMock(AbstractConsole::class);
|
||||
$this->mockLogger = $this->createMock(Logger::class);
|
||||
}
|
||||
|
||||
public function testVObjectToTask() {
|
||||
$configs = [
|
||||
'general' => [
|
||||
'logger' => ['file' => '', 'level'=> 'DEBUG', 'enabled' => true],
|
||||
'timezone' => 'UTC'
|
||||
],
|
||||
'storages' => [
|
||||
'taskwarrior' => ['taskrc' => '', 'taskdata' => '']
|
||||
]
|
||||
];
|
||||
$this->taskwarrior = new Taskwarrior($this->mockConsole, $configs, $this->mockLogger);
|
||||
$vcalendar = new Calendar([
|
||||
'VTODO' => [
|
||||
'SUMMARY' => 'Finish project',
|
||||
'DTSTAMP' => new \DateTime('2020-07-04 10:00:00'),
|
||||
'DTSTART' => new \DateTime('2020-07-04 12:00:00'),
|
||||
'DTEND' => new \DateTime('2020-07-05 01:00:00'),
|
||||
'DUE' => new \DateTime('2020-07-05 03:00:00'),
|
||||
'LAST_MODIFIED' => new \DateTime('2020-07-04 13:00:00'),
|
||||
'PRIORITY' => 5,
|
||||
'RRULE' => 'FREQ=MONTHLY'
|
||||
]
|
||||
]);
|
||||
|
||||
$task = $this->taskwarrior->vObjectToTask($vcalendar->VTODO);
|
||||
$this->assertArrayHasKey('uid', $task, 'task should have a uid');
|
||||
$this->assertEquals((string)$vcalendar->VTODO->UID, $task['uid']);
|
||||
$this->assertArrayHasKey('description', $task, 'task should have description');
|
||||
$this->assertEquals((string)$vcalendar->VTODO->SUMMARY, $task['description']);
|
||||
$this->assertArrayHasKey('due', $task, 'task should have due');
|
||||
$this->assertEquals($vcalendar->VTODO->DUE->getDateTime(), $task['due']);
|
||||
$this->assertArrayHasKey('entry', $task, 'task should have an entry');
|
||||
$this->assertEquals($vcalendar->VTODO->DTSTAMP->getDateTime(), $task['entry']);
|
||||
$this->assertArrayHasKey('start', $task, 'task should have start');
|
||||
$this->assertEquals($vcalendar->VTODO->DTSTART->getDateTime(), $task['start']);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user