From dad551a72a1e5598666ff8d874dac951ed6e14b3 Mon Sep 17 00:00:00 2001 From: DavidBadura Date: Thu, 5 Feb 2015 22:00:30 +0000 Subject: [PATCH] add done & delete --- src/Task.php | 68 +++++++++++++++++++++++++++++++++++++++ src/Taskwarrior.php | 30 +++++++++++++---- tests/TaskwarriorTest.php | 59 +++++++++++++++++++++++++++++++++ 3 files changed, 151 insertions(+), 6 deletions(-) diff --git a/src/Task.php b/src/Task.php index a52079b..2a29549 100644 --- a/src/Task.php +++ b/src/Task.php @@ -9,6 +9,11 @@ use JMS\Serializer\Annotation as JMS; */ class Task { + const STATUS_PENDING = 'pending'; + const STATUS_COMPLETED = 'completed'; + const STATUS_DELETED = 'deleted'; + const STATUS_WAITING = 'waiting'; + /** * @var string * @@ -23,6 +28,21 @@ class Task */ private $description; + /** + * @var string + * + * @JMS\Type(name="string") + */ + private $status; + + /** + * + */ + public function __construct() + { + $this->status = self::STATUS_PENDING; + } + /** * @return string */ @@ -54,4 +74,52 @@ class Task { $this->description = $description; } + + /** + * @return string + */ + public function getStatus() + { + return $this->status; + } + + /** + * @param string $status + */ + public function setStatus($status) + { + $this->status = $status; + } + + /** + * @return bool + */ + public function isPending() + { + return $this->status == self::STATUS_PENDING; + } + + /** + * @return bool + */ + public function isCompleted() + { + return $this->status == self::STATUS_COMPLETED; + } + + /** + * @return bool + */ + public function isWaiting() + { + return $this->status == self::STATUS_WAITING; + } + + /** + * @return bool + */ + public function isDeleted() + { + return $this->status == self::STATUS_DELETED; + } } \ No newline at end of file diff --git a/src/Taskwarrior.php b/src/Taskwarrior.php index e81e5ae..cc6322d 100644 --- a/src/Taskwarrior.php +++ b/src/Taskwarrior.php @@ -3,9 +3,8 @@ namespace DavidBadura\Taskwarrior; use JMS\Serializer\SerializerBuilder; -use Symfony\Component\Process\ProcessBuilder; use Symfony\Component\Filesystem\Filesystem; - +use Symfony\Component\Process\ProcessBuilder; /** * @author David Badura @@ -25,7 +24,7 @@ class Taskwarrior /** * @param string $taskrc * @param string $taskData - * @param array $rcOptions + * @param array $rcOptions */ public function __construct($taskrc = '~/.taskrc', $taskData = '~/.task', $rcOptions = []) { @@ -98,12 +97,26 @@ class Taskwarrior return $result; } + /** + * @param string $filter + * @return Task[] + */ + public function filterPending($filter = '') + { + return $this->filter($filter . ' status:pending'); + } + /** * @param Task $task */ public function delete(Task $task) { - // todo + if (!$task->getUuid()) { + return; + } + + $this->command('delete', $task->getUuid()); + $task->setStatus(Task::STATUS_DELETED); } /** @@ -111,7 +124,12 @@ class Taskwarrior */ public function done(Task $task) { - // todo + if (!$task->getUuid()) { + return; + } + + $this->command('done', $task->getUuid()); + $task->setStatus(Task::STATUS_COMPLETED); } /** @@ -167,7 +185,7 @@ class Taskwarrior /** * @param string $command * @param string $filter - * @param array $options + * @param array $options * @return string * @throws TaskwarriorException */ diff --git a/tests/TaskwarriorTest.php b/tests/TaskwarriorTest.php index 8d8625d..aff7826 100644 --- a/tests/TaskwarriorTest.php +++ b/tests/TaskwarriorTest.php @@ -116,6 +116,65 @@ class TaskwarriorTest extends \PHPUnit_Framework_TestCase $this->assertCount(2, $this->taskwarrior->filter()); } + public function testPending() + { + $task1 = new Task(); + $task1->setDescription('foo1'); + + $this->taskwarrior->save($task1); + + $this->assertEquals(Task::STATUS_PENDING, $task1->getStatus()); + $this->assertTrue($task1->isPending()); + + $this->taskwarrior->clear(); + $result = $this->taskwarrior->find($task1->getUuid()); + + $this->assertEquals(Task::STATUS_PENDING, $result->getStatus()); + $this->assertTrue($result->isPending()); + + $this->assertCount(1, $this->taskwarrior->filterPending()); + } + + public function testDelete() + { + $task1 = new Task(); + $task1->setDescription('foo1'); + + $this->assertCount(0, $this->taskwarrior->filter()); + + $this->taskwarrior->save($task1); + $this->assertCount(1, $this->taskwarrior->filter()); + $this->assertCount(1, $this->taskwarrior->filterPending()); + $this->assertFalse($task1->isDeleted()); + $this->assertEquals(Task::STATUS_PENDING, $task1->getStatus()); + + $this->taskwarrior->delete($task1); + $this->assertCount(1, $this->taskwarrior->filter()); + $this->assertCount(0, $this->taskwarrior->filterPending()); + $this->assertTrue($task1->isDeleted()); + $this->assertEquals(Task::STATUS_DELETED, $task1->getStatus()); + } + + public function testCompleted() + { + $task1 = new Task(); + $task1->setDescription('foo1'); + + $this->assertCount(0, $this->taskwarrior->filter()); + + $this->taskwarrior->save($task1); + $this->assertCount(1, $this->taskwarrior->filter()); + $this->assertCount(1, $this->taskwarrior->filterPending()); + $this->assertFalse($task1->isCompleted()); + $this->assertEquals(Task::STATUS_PENDING, $task1->getStatus()); + + $this->taskwarrior->done($task1); + $this->assertCount(1, $this->taskwarrior->filter()); + $this->assertCount(0, $this->taskwarrior->filterPending()); + $this->assertTrue($task1->isCompleted()); + $this->assertEquals(Task::STATUS_COMPLETED, $task1->getStatus()); + } + public function testModifyDescription() { $task1 = new Task();