5 Commits
1.0.0 ... 1.3.1

Author SHA1 Message Date
4d96cc4e8c fix: change namespaces 2018-11-25 18:41:20 -06:00
98b67855ca refactor: clean up code and remove some dependencies 2018-11-25 17:35:33 -06:00
2d2122e05d refactor: extract ical processor code into an interface type 2018-11-05 00:18:47 -06:00
84e96940ed fix: use 2.0.0 jms serializer 2018-10-28 12:55:35 -05:00
d465f4481e fix: removed parseiCalDateTime
fix: remove jms/serializer from dep
2018-10-28 12:23:27 -05:00
16 changed files with 1942 additions and 668 deletions

View File

@@ -1,7 +1,6 @@
{
"name": "aerex/taskwarrior",
"name": "aerex/taskwarrior-baikal-plugin",
"description": "A Baikal plugin for taskwarrior",
"version": "1.0.0",
"type": "library",
"keywords": [
"task",
@@ -10,12 +9,14 @@
"Baikal",
"sabre"
],
"repositories": [{"type": "vcs", "url": "https://aerex.me/git/Aerex/Taskwarrior"}],
"require": {
"php": ">=5.5",
"sabre/dav": "~3.1.2",
"davidbadura/taskwarrior": "^3.0",
"sabre/vobject": "^4.0",
"jms/serializer": "^1.13",
"easycorp/easy-log-handler": "^1.0"
"easycorp/easy-log-handler": "^1.0",
"zendframework/zend-validator": "^2.10"
},
"require-dev": {
"phpunit/phpunit" : "> 4.8, <=6.0.0"

1259
composer.lock generated

File diff suppressed because it is too large Load Diff

44
src/CalendarProcessor.php Normal file
View File

@@ -0,0 +1,44 @@
<?php
namespace Aerex\TaskwarriorPlugin;
use Aerex\TaskwarriorPlugin\Taskwarrior\TaskwarriorManager;
use Sabre\VObject\Component\VTodo;
/**
* Class CalendarProcessor
*
* @author Aerex
*/
class CalendarProcessor
{
public function __construct(TaskwarriorManager $taskwarriorManager){
$this->taskwarriorManager = $taskwarriorManager;
}
public function importTask(VTodo $Todo){
if($this->taskwarriorManager->exists($Todo->UID)){
$this->taskwarriorManager->updateTask($Todo);
}
try {
$this->taskwarriorManager->addTask($Todo);
} catch(Exception $e){
echo $e->getMessage();
throw $e;
}
}
public function export(){
echo "Not yet implemented";
}
}
?>

View File

@@ -2,8 +2,8 @@
namespace Aerex\TaskwarriorPlugin;
use DavidBadura\Taskwarrior\Taskwarrior;
use Aerex\Taskwarrior\TaskwarriorManager;
use Aerex\TaskwarriorPlugin\Taskwarrior\Taskwarrior;
use Aerex\TaskwarriorPlugin\Taskwarrior\TaskwarriorManager;
class Config {

View File

@@ -9,7 +9,9 @@ use Sabre\HTTP\ResponseInterface;
use Sabre\Xml\ParseException;
use Sabre\DAV\ServerPlugin;
use Sabre\DAV\Server;
use Aerex\TaskwarriorPlugin\iCalEventProcessor;
use Aerex\TaskwarriorPlugin\CalendarProcessor;
use Aerex\TaskwarriorPlugin\Taskwarrior\Taskwarrior;
use Aerex\TaskwarriorPlugin\Taskwarrior\TaskwarriorManager;
use Aerex\TaskwarriorPlugin\Config;
/**
@@ -26,30 +28,24 @@ class Plugin extends ServerPlugin {
protected $server;
/**
* Reference to TaskwarriorConfig object
* @var TaskwarriorConfig
*
*/
protected $twConfig;
/**
* Reference to TaskwarriorCalenderEvent object
* @var TaskwarriorCalendarEvent
* @var TaskwarriorManager
*/
protected $TWCalManager;
/**
* Creates the Taskwarrior plugin
*
* @param TaskwarriorConfig $TWCalManager
* @param CalendarProcessor $TWCalManager
*
*/
function __construct(iCalEventProcessor $TWCalManager = null){
if(!is_null($TWCalManager)){
$this->twCalManager = $TWCalManager;
function __construct(Taskwarrior $taskwarrior = null){
if(!is_null($taskwarrior)){
$this->twCalManager = new CalendarProcessor(new TaskwarriorManager($taskwarrior));
} else {
$this->twCalManager = new iCalEventProcessor();
$this->twCalManager = new CalendarProcessor(new TaskwarriorManager());
}
}
@@ -75,9 +71,10 @@ class Plugin extends ServerPlugin {
$server->on('propPatch', [$this, 'propPatchProtectedPropertyCheck'], 90);
$server->on('propPatch', [$this, 'propPatchNodeUpdate'], 200);
$server->on('propFind', [$this, 'propFind']);
$server->on('propFind', [$this, 'propFindNode'], 120);
$server->on('propFind', [$this, 'propFindLate'], 200);
$server->on('calendarObjectChange', [$this, 'calendarObjectChange']);
//$server->on('propFind', [$this, 'propFind']);
//$server->on('propFind', [$this, 'propFindNode'], 120);
//$server->on('propFind', [$this, 'propFindLate'], 200);
}
@@ -123,9 +120,6 @@ class Plugin extends ServerPlugin {
* */
function calendarObjectChange(RequestInterface $request, ResponseInterface $response, Document $vCal, $calendarPath, &$modified, $isNew) {
$calendarNode = $this->server->tree->getNodeForPath($calendarPath);
$addresses = $this->getAddressesForPrincipal(
$calendarNode->getOwner()
);
if ($isNew) {
try {
$this->processCalendarEventForTaskwarrior($vCal);
@@ -747,48 +741,6 @@ class Plugin extends ServerPlugin {
*/
function propFind(PropFind $propFind, INode $node) {
$propFind->handle('{DAV:}getlastmodified', function() use ($node) {
$lm = $node->getLastModified();
if ($lm) {
return new Xml\Property\GetLastModified($lm);
}
});
if ($node instanceof IFile) {
$propFind->handle('{DAV:}getcontentlength', [$node, 'getSize']);
$propFind->handle('{DAV:}getetag', [$node, 'getETag']);
$propFind->handle('{DAV:}getcontenttype', [$node, 'getContentType']);
}
if ($node instanceof IQuota) {
$quotaInfo = null;
$propFind->handle('{DAV:}quota-used-bytes', function() use (&$quotaInfo, $node) {
$quotaInfo = $node->getQuotaInfo();
return $quotaInfo[0];
});
$propFind->handle('{DAV:}quota-available-bytes', function() use (&$quotaInfo, $node) {
if (!$quotaInfo) {
$quotaInfo = $node->getQuotaInfo();
}
return $quotaInfo[1];
});
}
$propFind->handle('{DAV:}supported-report-set', function() use ($propFind) {
$reports = [];
foreach ($this->server->getPlugins() as $plugin) {
$reports = array_merge($reports, $plugin->getSupportedReportSet($propFind->getPath()));
}
return new Xml\Property\SupportedReportSet($reports);
});
$propFind->handle('{DAV:}resourcetype', function() use ($node) {
return new Xml\Property\ResourceType($this->server->getResourceTypeForNode($node));
});
$propFind->handle('{DAV:}supported-method-set', function() use ($propFind) {
return new Xml\Property\SupportedMethodSet(
$this->server->getAllowedMethods($propFind->getPath())
);
});
}

View File

@@ -1,9 +0,0 @@
<?php
class TaskwarriorProps {
static $DESCRIPTION = 'description';
}
?>

View File

@@ -0,0 +1,10 @@
<?php
namespace Aerex\TaskwarriorPlugin\Taskwarrior\Commands;
use Aerex\TaskwarriorPlugin\Taskwarrior\Task;
interface Strategy {
public function add(Task $task);
}
?>

View File

@@ -0,0 +1,62 @@
<?php
class TodoStrategy implements IStrategy {
public function __construct(TaskwarriorConfig $config){
$this->config = $config;
$this->cmd[] = $this->config->taskBin();
}
public function add(Task $task){
$this->cmd[] = 'add';
if($task->getDescription() != null){
$this->cmd[] = sprintf('"%s"', $task->getDescription());
}
if($task->getCategories() != null){
$categories = implode(' +', $task->getCategories());
$this->cmd[] = $categories;
}
if($task->getDue() != null){
$this->cmd[] = $task->getDue('Y-m-dTH:i:s');
}
if($task->getRecurrence() != null){
$rrule = $task->getRecurrence()->getParts();
$this->cmd[] = sprintf('recur:%s', $rrule['FREQ']);
if(isset($rrule['UNTIL'])){
$this->cmd[] = sprintf('until:%s', $rrule['UNTIL']);
}
}
if($task->getStatus() != null){
$this->cmd[] = sprintf('status:%s', $task->getStatus());
}
return $this->executeCommand($cmd);
}
private function executeCommand($command){
$cmdString = implode(' ', $command);
$process = new Process($cmdString);
$process->run();
if(!$process->isSuccessful()){
throw new TaskwarriorCommandLineException($process);
}
return $process->getOutput();
}
}

264
src/Taskwarrior/Task.php Normal file
View File

@@ -0,0 +1,264 @@
<?php
namespace Aerex\TaskwarriorPlugin\Taskwarrior;
use Zend\Validator\Uuid;
use Carbon\Carbon;
use Sabre\VObject\Component\VTodo;
/**
* Task
* @author Aerex
*/
class Task {
/**
* @var string
*
* @JMS\Type("string")
*/
private $uuid;
/**
* @var string
*
* @JMS\Type("string")
*/
private $description;
/**
* @var string
*
* @JMS\Type("string")
*/
private $priority;
/**
* @var string
*
* @JMS\Type("string")
*/
private $project;
/**
* @var Carbon
*
* @JMS\Type("Carbon")
*/
private $due;
/**
* @var Carbon
*
* @JMS\Type("Carbon")
*/
private $wait;
/**
* @var array
*
* @JMS\Type("array<string>")
*/
private $tags;
/**
* @var float
*
* @JMS\Type("float")
*/
private $urgency;
/**
* @var Carbon
*
* @JMS\Type("Carbon")
*/
private $entry;
/**
* @var Carbon
*
* @JMS\Type("Carbon")
*/
private $start;
/**
* @var string
*
* @JMS\Type("Recurring")
*/
private $recur;
/**
* @var Carbon
*
* @JMS\Type("Carbon")
*/
private $until;
/**
* @var Annotation[]
*
* @JMS\Type("array<Aerex\Taskwarrior\Annotation>")
*/
private $annotations = [];
/**
* @var Carbon
*
* @JMS\Type("Carbon")
*/
private $modified;
/**
* @var Carbon
*
* @JMS\Type("Carbon")
*/
private $end;
/**
* @var string
*
* @JMS\Type("string")
*/
private $status;
/**
* @var Task[]|ArrayCollection
*
* @JMS\Type("Depends")
*/
private $depends;
public function __construct($UUID=null){
$validator = new Uuid();
if(!isset($UUID)){
$this->uuid = uniqid();
} else if(isset($UUID) && !$validator->isValid($UUID)){
throw new Exception(sprintf('%s is not a valid uuid', $UUID));
}
$this->uuid = $UUID;
}
/**
*
* {@inheritDoc}
*
* If description is not available attempt to summary, otherwise throw Exception
*/
public function setDescription(VTodo $component){
if(!isset($component->DESCRIPTION) && isset($component->SUMMARY)){
$this->description = $component->SUMMARY;
} else if(!isset($component->DESCRIPTION) && !isset($component->SUMMARY)){
throw new Exception("Task must have a description or summary");
} else {
$this->description = $component->DESCRIPTION;
}
}
public function getDescription(){
return $this->description;
}
public function setEntryTime(VTodo $document){
if(isset($document->DTSTAMP)){
$this->entry = new Carbon($document->DTSTAMP->getDateTime()->format(\DateTime::W3C));
} else {
throw new Exception('Task must have a entry time');
}
}
public function getEntryTime(){
return $this->entry;
}
public function setStartTime(VTodo $document){
if(isset($document->DTSTART)){
$this->start = new Carbon($document->DTSTART->getDateTime()->format(\DateTime::W3C));
}
}
public function getStartTime(){
return $this->start;
}
public function setModifiedTime(VTodo $document){
if(isset($document->{'LAST-MODIFIED'})){
$this->modified = new Carbon($document->{'LAST-MODIFIED'}->getDateTime()->format(\DateTime::W3C));
}
}
public function getModifiedTime(){
return $this->modified;
}
public function setDue(VTodo $document){
if(isset($document->DUE)){
$this->due = new Carbon($document->DUE->getDateTime()->format(\DateTime::W3C));
}
}
public function getDue(){
return $this->due;
}
public function setStopTime(VTodo $document){
if(isset($document->DTEND)){
$this->end = new Carbon($document->DTEND->getDateTime()->format(\DateTime::W3C));
}
}
public function getStopTime(){
return $this->end;
}
public function setCategories(VTodo $document){
if(isset($document->CATEGORIES)){
$this->tags = explode(',', (string)$document->CATEGORIES);
}
}
public function getCategories(){
return $this->tags;
}
public function setStatus(VTodo $document){
if(isset($document->STATUS)){
switch((string)$document->STATUS){
case 'NEEDS-ACTION':
$this->status = 'pending';
break;
case 'COMPLETED':
$this->status = 'completed';
break;
case 'CANCELED':
$this->status = 'deleted';
break;
}
}
}
public function setRecurrence(VTodo $document){
if(isset($document->RRULE)){
$this->recur = $document->RRULE;
}
}
public function getStatus(){
return $this->status;
}
}

View File

@@ -0,0 +1,68 @@
<?php
namespace Aerex\TaskwarriorPlugin\Taskwarrior;
use Symfony\Component\Process\Process;
use Aerex\TaskwarriorPlugin\Commands\IStrategy;
use Aerex\TaskwarriorPlugin\Config;
class Taskwarrior {
const EXPORT = 'export';
const IMPORT = 'import';
const ADD = 'add';
/**
* @var TaskwarriorConfig
*/
private $config;
/**
* @var string
*/
private $bin;
/**
* @var string
*/
private $taskrc;
/**
* @var string
*/
private $taskData;
/**
* @var array
*/
private $rcOptions;
/**
* @var string
*/
public function __construct(Config $config){
if(!isset($config)){
$this->config = new Configwg();
}
$this->config = $config;
}
public function setStrategy(IStrategy $strategy){
$this->strategy = $strategy;
}
public function createTask($uuid){
$task = new Task($uuid);
return $task;
}
public function add($task){
$this->strategy($task);
}
}
?>

View File

@@ -0,0 +1,162 @@
<?php
namespace Aerex\TaskwarriorPlugin\Taskwarrior;
use Aerex\TaskwarriorPlugin\Taskwarrior\Taskwarrior;
use Aerex\TaskwarriorPlugin\Taskwarrior\Task;
use Sabre\VObject\Component\VTodo;
class TaskwarriorManager {
const DESCRIPTION = 'description';
const CATEGORIES = 'categories';
const TASK_UUID = 'uuid';
const ICAL_UID = 'uid';
private $tasks;
const ENTRY = 'entry';
const START = 'start';
const MODIFIED = 'modified';
const END = 'end';
public function __construct($taskwarrior){
if(!isset($taskwarrior)){
$this->taskwarrior = new Taskwarrior();
} else {
$this->taskwarrior = $taskwarrior;
}
}
public function createTask($UUID){
$task = new Task();
}
public function export(VTodo $document){
}
public function addTask(VTodo $document){
$task = $this->taskwarrior->createTask($document->UID);
$task->setDescription($document);
$task->setEntry($document);
$task->setStartTime($document);
$task->setModifiedTime($document);
$task->setStopTime($document);
$task->setDue($document);
$task->setCategories($document);
$task->setStatus($document);
$task->setRecurrence($document);
return $this->taskwarrior->add($task);
}
public function updateTask(VTodo $document){
$task = $this->taskwarrior->get($document->UID);
$task->setDescription($document);
$task->setEntry($document);
$task->setStartTime($document);
$task->setModifiedTime($document);
$task->setStopTime($document);
$task->setDue($document);
$task->setCategories($document);
$task->setStatus($document);
$task->setRecurrence($document);
$updatedTask = $this->taskwarrior->modify($task);
return $upatedTask;
}
function setEntryTime($entry){
}
function exists($UUID){}
function setEndTime($end){}
function setModifiedTime($modifiedTime){}
function setSummary($description){
if(!isset($description)){
return;
}
$this->taskWarriorJSON[self::DESCRIPTION] = $description;
}
function setStartTime($startTime){}
function setCategories($categories){
if(!isset($categories)){
return;
}
if(!is_array($catergories)){
return;
}
$this->taskWarriorJSON[self::CATEGORIES] = $categories;
}
public function taskExists($taskUuid){
$taskIsInCache = isset($this->cachedTasks[$taskUuid]);
if($taskIsInCache){
return true;
}
$jsonArray = $this->taskwarrior->export($taskUuid);
$taskWithUuidExists = count($jsonArray) > 0;
return $taskWithUuidExists;
}
public function save(){
}
public function build(){
}
/**
* @param Task $task
* @param string $attr
* @param mixed $value
*/
public function setValue(Task $task, $attr, $value)
{
$refClass = new \ReflectionClass(Task::class);
$refProp = $refClass->getProperty($attr);
$refProp->setAccessible(true);
$refProp->setValue($task, $value);
}
function setDescription($description){}
function parseiCalDateTime($iCalDateTime){}
function convertToStringArray($categories){}
}
?>

View File

@@ -1,112 +0,0 @@
<?php
namespace Aerex\TaskwarriorPlugin;
use Aerex\TaskwarriorPlugin\Taskwarrior;
use DavidBadura\Taskwarrior\TaskManager;
use DavidBadura\Taskwarrior\Task;
class TaskwarriorManager extends TaskManager {
const DESCRIPTION = 'description';
const CATEGORIES = 'categories';
const TASK_UUID = 'uuid';
const ICAL_UID = 'uid';
/**
*
* @var \DavidBadura\Taskwarrior\Task
*/
private $tasks;
const ENTRY = 'entry';
const START = 'start';
const MODIFIED = 'modified';
const END = 'end';
public function __construct($taskwarrior){
parent::__construct($taskwarrior);
}
public function createTask($UUID){
$task = new Task();
$this->setValue($task, $ICAL_UID, $UUID);
return $task;
}
public function export(){
}
function setEntryTime($entry){
}
function exists($UUID){}
function setEndTime($end){}
function setModifiedTime($modifiedTime){}
function setSummary($description){
if(!isset($description)){
return;
}
$this->taskWarriorJSON[self::DESCRIPTION] = $description;
}
function setStartTime($startTime){}
function setCategories($categories){
if(!isset($categories)){
return;
}
if(!is_array($catergories)){
return;
}
$this->taskWarriorJSON[self::CATEGORIES] = $categories;
}
public function taskExists($taskUuid){
$taskIsInCache = isset($this->cachedTasks[$taskUuid]);
if($taskIsInCache){
return true;
}
$jsonArray = $this->taskwarrior->export($taskUuid);
$taskWithUuidExists = count($jsonArray) > 0;
return $taskWithUuidExists;
}
public function save(){
}
public function build(){
}
/**
* @param Task $task
* @param string $attr
* @param mixed $value
*/
public function setValue(Task $task, $attr, $value)
{
$refClass = new \ReflectionClass(Task::class);
$refProp = $refClass->getProperty($attr);
$refProp->setAccessible(true);
$refProp->setValue($task, $value);
}
function setDescription($description){}
function parseiCalDateTime($iCalDateTime){}
function convertToStringArray($categories){}
function setDueDate($dueDate){}
}
?>

View File

@@ -1,125 +0,0 @@
<?php
namespace Aerex\TaskwarriorPlugin;
use Aerex\TaskwarriorPlugin\Config;
use Sabre\DAV\Exception;
use Sabre\VObject\Component\VEvent;
use Sabre\VObject\Component\VTodo;
class iCalEventProcessor {
/**
* @var Config
*/
private $config;
/**
* @var string
*
*/
private $taskrc;
/**
* @var string
*/
private $taskDataDir;
/**
* @var array
*/
private $cachedTasks = [];
/**
* @var string
*/
private $taskBinFile;
/**
* @var Taskwarrior
*/
private $taskwarrior;
public function __construct(Config $taskConfig = null){
if(!is_null($taskConfig)){
$this->taskConfig = $taskConfig;
} else {
$this->taskConfig = new Config();
}
$this->taskwarrior = $this->taskConfig->getTaskwarriorInstance();
$this->logger = $this->taskConfig->getLogger();
}
public function importTask(VTodo $ToDoComponent = null){
if(!isset($ToDoComponent)){
$this->logger->error("vCal ToDo component is not defined");
throw new Exception("vCal Todo component is not defined");
}
if($this->taskwarrior->exists($ToDoComponent->UID)){
$this->logger->error("Event already exists " . (string)$ToDoComponent->UID);
}
try {
// parse iCalendar event times to DateTime objects
$entry = $this->taskwarrior->parseiCalDateTime($ToDoComponent->DSTAMP);
$start = $this->taskwarrior->parseiCalDateTime($ToDoComponent->DSTART);
$modified = $this->taskwarrior->parseiCalDateTime($ToDoComponent->{'LAST-MODIFIED'});
$end = $this->taskwarrior->parseiCalDateTime($ToDoComponent->DTEND);
$task = $this->taskwarrior->createTask($ToDoComponent->UID);
$tags = $this->taskwarrior->convertToStringArray($ToDoComponent->CATEGORIES);
$task->setTags($tags);
$task->setDue($ToDoComponent->DUE->getDateTime());
$task->setDescription($ToDoComponent->DESCRIPTION);
// override protected taskwarrior properties using iCal event
$this->taskwarrior->setValue($task, $this->taskwarrior::ENTRY, $entry);
$this->taskwarrior->setValue($task, $this->taskwarrior::START, $start);
$this->taskwarrior->setValue($task, $this->taskwarrior::MODIFIED, $modified);
$this->taskwarrior->setValue($task, $this->taskwarrior::END, $end);
$this->taskwarrior->save($task);
} catch(Exception $e){
$this->logger->error($e->message);
throw $e;
}
}
}
?>

View File

@@ -0,0 +1,59 @@
<?php
use Sabre\DAV\Exception\BadRequest;
use Monolog\Logger;
use Aerex\TaskwarriorPlugin\TaskwarriorManager;
use Aerex\TaskwarriorPlugin\Processors\ToDo;
use Sabre\VObject\Component\VCalendar;
use DavidBadura\Taskwarrior\Task;
use DateTime;
class TodoTest extends \PHPUnit\Framework\TestCase {
/**
* @var \PHPUnit_Framework_MockObject_MockObject
* */
private $mockTaskwarriorManager;
function setup(){
$this->mockTaskwarriorManager = $this->createMock(TaskwarriorManager::class);
}
function testImportAndAddTask(){
$uuid = '9f353281-1051-4c45-92db-462f5d353c76';
$mockVCalendar = new VCalendar();
$mockVTodo = $mockVCalendar->add('VTODO', ['UID' => $uuid]);
$this->mockTaskwarriorManager->expects($this->once())->method('exists')->with($this->equalTo($uuid))
->willReturn(false);
$this->mockTaskwarriorManager->expects($this->once())->method('addTask')->with($this->equalTo($mockVTodo));
$todo = new ToDo($this->mockTaskwarriorManager);
$todo->import($mockVTodo);
}
function testImportAndUpdateTask(){
$uuid = '9f353281-1051-4c45-92db-462f5d353c76';
$mockVCalendar = new VCalendar();
$mockVTodo = $mockVCalendar->add('VTODO', ['UID' => $uuid]);
$this->mockTaskwarriorManager->expects($this->once())->method('exists')
->willReturn(true);
$this->mockTaskwarriorManager->expects($this->once())->method('updateTask');
$todo = new ToDo($this->mockTaskwarriorManager);
$todo->import($mockVTodo);
}
}
?>

173
tests/TaskTest.php Normal file
View File

@@ -0,0 +1,173 @@
<?php
use Sabre\VObject\Component\VCalendar;
use Sabre\DAV\Exception\BadRequest;
use Aerex\TaskwarriorPlugin\Taskwarrior\Task;
use Aerex\TaskwarriorPlugin\Plugin;
use Carbon\Carbon;
use Aerex\TaskwarriorPlugin\Config;
use Sabre\DAV\Server;
use DateTime;
use DateTimeZone;
class TaskTest extends \PHPUnit\Framework\TestCase {
/**
* @var Plugin
*
*/
private $plugin;
/**
* @var VCalendar
*/
private $cal;
/**
* @var Server
*/
private $server;
/**
* @var TaskwarriorCalendarEvent
*/
private $mockTaskCalEvent;
public function testSetDescriptionUsingDescription(){
$uuid = 'f987aa59-9031-4a7b-9cf3-6bfa4dc44a85';
$expectedDescription = 'This is a description';
$mockVCalendar = new VCalendar();
$mockVTodo = $mockVCalendar->add('VTODO', ['UID' => $uuid]);
$mockVTodo->DESCRIPTION = $expectedDescription;
$todo = new Task();
$todo->setDescription($mockVTodo);
$actualDescription = $todo->getDescription();
$this->assertEquals($expectedDescription, $actualDescription);
}
public function testSetDescriptionUsingSummary(){
$uuid = 'f987aa59-9031-4a7b-9cf3-6bfa4dc44a85';
$expectedDescription = 'This is a description';
$mockVCalendar = new VCalendar();
$mockVTodo = $mockVCalendar->add('VTODO', ['UID' => $uuid]);
$mockVTodo->SUMMARY = $expectedDescription;
$todo = new Task();
$todo->setDescription($mockVTodo);
$actualDescription = $todo->getDescription();
$this->assertEquals($expectedDescription, $actualDescription);
}
/**
* @expectedException Error
*/
public function testFailureSetDescription(){
$uuid = 'f987aa59-9031-4a7b-9cf3-6bfa4dc44a85';
$expectedDescription = 'This is a description';
$mockVCalendar = new VCalendar();
$mockVTodo = $mockVCalendar->add('VTODO', ['UID' => $uuid]);
$todo = new Task();
$todo->setDescription($mockVTodo);
$actualDescription = $todo->getDescription();
}
public function testSetEntryTime(){
$uuid = 'f987aa59-9031-4a7b-9cf3-6bfa4dc44a85';
$expectedEntryTime = new DateTime('2018-11-11');
$mockVCalendar = new VCalendar();
$mockVTodo = $mockVCalendar->add('VTODO', ['UID' => $uuid]);
$expectedCarbonDate = new Carbon($expectedEntryTime->format('Y-m-d'));
$mockVTodo->DTSTAMP = $expectedEntryTime;
$todo = new Task();
$todo->setEntryTime($mockVTodo);
$actualEntryTime = $todo->getEntryTime();
$this->assertEquals($expectedCarbonDate, $actualEntryTime);
}
public function testSetStartTime(){
$uuid = 'f987aa59-9031-4a7b-9cf3-6bfa4dc44a85';
$expectedStartTime = new DateTime('2018-11-11');
$mockVCalendar = new VCalendar();
$mockVTodo = $mockVCalendar->add('VTODO', ['UID' => $uuid]);
$expectedCarbonDate = new Carbon($expectedStartTime->format('Y-m-d'));
$mockVTodo->DTSTART = $expectedStartTime;
$todo = new Task();
$todo->setStartTime($mockVTodo);
$actualStartTime = $todo->getStartTime();
$this->assertEquals($expectedCarbonDate, $actualStartTime);
}
public function testSetModifiedTime(){
$uuid = '182a6301-98e6-44df-97eb-8c7620f25b43';
$expectedModifiedTime = new DateTime('2018-11-11');
$mockVCalendar = new VCalendar();
$mockVTodo = $mockVCalendar->add('VTODO', ['UID' => $uuid]);
$expectedCarbonDate = new Carbon($expectedModifiedTime->format('Y-m-d'));
$mockVTodo->{'LAST-MODIFIED'} = $expectedModifiedTime;
$todo = new Task();
$todo->setModifiedTime($mockVTodo);
$actualModifiedTime = $todo->getModifiedTime();
$this->assertEquals($expectedCarbonDate, $actualModifiedTime);
}
public function testSetDue(){
$uuid = 'dde78b02-97d9-4e11-8603-e0fc14474c7c';
$expectedDueTime = new DateTime('2018-11-11');
$mockVCalendar = new VCalendar();
$mockVTodo = $mockVCalendar->add('VTODO', ['UID' => $uuid]);
$expectedCarbonDate = new Carbon($expectedDueTime->format('Y-m-d'));
$mockVTodo->DUE = $expectedDueTime;
$todo = new Task();
$todo->setDue($mockVTodo);
$actualDue = $todo->getDue();
$this->assertEquals($expectedCarbonDate, $actualDue);
}
public function testSetCategories(){
$uuid = '182a6301-98e6-44df-97eb-8c7620f25b43';
$expectedCategories = array("home", "work");
$mockVCalendar = new VCalendar();
$mockVTodo = $mockVCalendar->add('VTODO', ['UID' => $uuid]);
$mockVTodo->CATEGORIES = $expectedCategories;
$todo = new Task();
$todo->setCategories($mockVTodo);
$actualCategories = $todo->getCategories();
$this->assertEquals($expectedCategories, $actualCategories);
}
}

View File

@@ -1,174 +0,0 @@
<?php
use Sabre\DAV\Exception\BadRequest;
use Monolog\Logger;
use Sabre\VObject\Component\VCalendar;
use Aerex\TaskwarriorPlugin\TaskwarriorManager;
use Aerex\TaskwarriorPlugin\iCalEventProcessor;
use Aerex\TaskwarriorPlugin\Config;
use DavidBadura\Taskwarrior\Task;
use DateTime;
class iCalEventProcessorTest extends \PHPUnit\Framework\TestCase {
/**
* @var \PHPUnit_Framework_MockObject_MockObject
* */
private $mockTaskwarrior;
/***
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $mockTaskwarriorConfig;
/***
* @var \PHPUnit_Framework__MockObject
*/
private $mockLogger;
function setup(){
$this->mockTaskwarrior = $this->createMock(TaskwarriorManager::class);
$this->mockTask = $this->createMock(Task::class);
$this->mockTaskwarriorConfig = $this->createMock(Config::class);
$this->mockLogger = $this->createMock(Logger::class);
}
function testConstructorWithConfig() {
$this->mockTaskwarriorConfig->expects($this->once())
->method('getTaskwarriorInstance')
->will($this->returnValue($this->mockTaskwarrior));
$this->taskCalEvent = new iCalEventProcessor($this->mockTaskwarriorConfig);
}
function testBuildToDoComponent(){
$uuid = '9f353281-1051-4c45-92db-462f5d353c76';
$startTime = new DateTime('2018-07-04');
$endTime = new DateTime('2018-07-06');
$modifiedTime = new DateTime('2018-08-05');
$categories = array('home', 'test');
$dueDate = new DateTime('2018-08-20');
$description = 'This is a simple todo';
$mockVCalendar = new VCalendar();
$mockVTodo = $mockVCalendar->add('VTODO', ['UID' => $uuid]);
$mockVTodo->add('DSTAMP', $startTime);
$mockVTodo->add('DUE', $dueDate);
$mockVTodo->add('LAST-MODIFIED',$modifiedTime);
$mockVTodo->add('DSTART', $startTime);
$mockVTodo->add('DTEND', $endTime);
$mockVTodo->add('DESCRIPTION', $description);
$mockVTodo->add('CATEGORIES', $categories);
$this->mockTaskwarrior->expects($this->exactly(4))
->method('parseiCalDateTime')
->will($this->onConsecutiveCalls($startTime, $startTime, $modifiedTime, $endTime));
$this->mockTaskwarrior->expects($this->once())->method('createTask')->with($this->equalTo($uuid))
->willReturn($this->mockTask);
$this->mockTask->expects($this->once())->method('setDue')->with($this->equalTo($dueDate));
$this->mockTask->expects($this->once())->method('setDescription')->with($this->equalTo($description));
$this->mockTaskwarrior->expects($this->once())->method('convertToStringArray')->with($this->equalTo($mockVTodo->CATEGORIES))
->willReturn($categories);
$this->mockTask->expects($this->once())->method('setTags')
->with($this->equalTo($categories));
$this->mockTaskwarrior->expects($this->exactly(4))
->method('setValue');
$this->mockLogger->expects($this->never())->method('error');
$this->mockTaskwarriorConfig
->expects($this->once())
->method('getTaskwarriorInstance')
->will($this->returnValue($this->mockTaskwarrior));
$this
->mockTaskwarrior
->expects($this->once())
->method('save');
$twCalEvent = new iCalEventProcessor($this->mockTaskwarriorConfig);
$twCalEvent->importTask($mockVTodo);
}
/**
* @expectedException Sabre\DAV\Exception
*
*
*/
function testFailTaskNoComponentDefine(){
$this->mockTaskwarrior->expects($this->never())->method('createTask');
$this->mockTaskwarrior->expects($this->never())->method('setEntryTime');
$this->mockTaskwarrior->expects($this->never())->method('setEndTime');
$this->mockTaskwarrior->expects($this->never())->method('setDueDate');
$this->mockTaskwarrior->expects($this->never())->method('setSummary');
$this->mockTaskwarrior->expects($this->never())->method('setCategories');
$this->mockTaskwarrior->expects($this->never())->method('build');
$this->mockTaskwarrior->expects($this->never())->method('save');
$this->mockTaskwarrior->expects($this->never())->method('exists');
$this->mockTaskwarriorConfig->expects($this->once())->method('getTaskwarriorInstance')
->willReturn($this->mockTaskwarrior);
$this->mockLogger->expects($this->once())->method('error');
$this->mockTaskwarriorConfig->expects($this->once())->method('getLogger')
->willReturn($this->mockLogger);
$twCalEvent = new iCalEventProcessor($this->mockTaskwarriorConfig);
$twCalEvent->importTask();
}
/**
* @expectedException Sabre\DAV\Exception\BadRequest
*
*
*/
/*
function testFailTaskExists(){
$uuid = '9f353281-1051-4c45-92db-462f5d353c76';
$mockVCalendar = new VCalendar();
$mockVTodo = $mockVCalendar->add('VTODO', ['UID' => $uuid]);
$expectedErrorMessage = "already exists";
$this->mockTaskwarrior->expects($this->never())->method('createTask');
$this->mockTaskwarrior->expects($this->never())->method('setEntryTime');
$this->mockTaskwarrior->expects($this->never())->method('setEndTime');
$this->mockTaskwarrior->expects($this->never())->method('setDueDate');
$this->mockTaskwarrior->expects($this->never())->method('setSummary');
$this->mockTaskwarrior->expects($this->never())->method('setCategories');
$this->mockTaskwarrior->expects($this->never())->method('build');
$this->mockTaskwarrior->expects($this->never())->method('save');
$this->mockTaskwarrior->expects($this->once())->method('exists')
->willReturn(true);
$this->mockTaskwarriorConfig->expects($this->once())->method('getTaskwarriorInstance')
->willReturn($this->mockTaskwarrior);
$this->mockLogger->expects($this->once())->method('error');
$this->mockTaskwarriorConfig->expects($this->once())->method('getLogger')
->willReturn($this->mockLogger);
$twCalEvent = new iCalEventProcessor($this->mockTaskwarriorConfig);
$twCalEvent->importTask($mockVTodo);
}
*/
}
?>