From fa478272b6b74a0bd870c9878fe034215884734a Mon Sep 17 00:00:00 2001 From: DavidBadura Date: Fri, 6 Feb 2015 13:08:13 +0100 Subject: [PATCH] add project attr --- README.md | 8 +++-- src/Task.php | 23 +++++++++++++++ src/TaskManager.php | 26 ++++++++++------- src/Taskwarrior.php | 45 +++++++++++++++++++++++++++++ tests/TaskManagerTest.php | 61 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 151 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 6616941..1d0f0aa 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,13 @@ $tm = \DavidBadura\Taskwarrior\TaskManager::create(); $task = new \DavidBadura\Taskwarrior\Task(); -$task->addTag('home'); +$task->setDesciption('program this lib'); +$task->setProject('hobby'); +$task->setDue(new \DateTime('tomorrow')); + +$task->addTag('next'); // todo :D $tm->save($task); -$tasks = $tm->filter('+home'); +$tasks = $tm->filter('project:hobby'); ``` diff --git a/src/Task.php b/src/Task.php index cac9cc7..e9eb6b6 100644 --- a/src/Task.php +++ b/src/Task.php @@ -28,6 +28,13 @@ class Task */ private $description; + /** + * @var string + * + * @JMS\Type(name="string") + */ + private $project; + /** * @var \DateTime * @@ -90,6 +97,22 @@ class Task $this->description = $description; } + /** + * @return string + */ + public function getProject() + { + return $this->project; + } + + /** + * @param string $project + */ + public function setProject($project) + { + $this->project = $project; + } + /** * @return \DateTime */ diff --git a/src/TaskManager.php b/src/TaskManager.php index e916de3..22c6442 100644 --- a/src/TaskManager.php +++ b/src/TaskManager.php @@ -128,6 +128,14 @@ class TaskManager $this->update($task); } + /** + * @return array + */ + public function projects() + { + return $this->taskwarrior->projects(); + } + /** * */ @@ -171,17 +179,15 @@ class TaskManager */ private function edit(Task $task) { - $options = []; + $this->taskwarrior->modify( + [ + 'description' => $task->getDescription(), + 'project' => $task->getProject(), + 'due' => $task->getDue() ? $task->getDue()->format('Ymd\THis\Z') : null, + ], + $task->getUuid() + ); - if ($task->getDue()) { - $options[] = 'due:' . $task->getDue()->format('Ymd\THis\Z'); - } else { - $options[] = 'due:'; - } - - $options[] = $task->getDescription(); - - $this->taskwarrior->command('modify', $task->getUuid(), $options); $this->update($task); } diff --git a/src/Taskwarrior.php b/src/Taskwarrior.php index e82b568..4152f13 100644 --- a/src/Taskwarrior.php +++ b/src/Taskwarrior.php @@ -50,6 +50,40 @@ class Taskwarrior $this->command('done', $uuid); } + /** + * @param array $params + * @param string|array $filter + */ + public function modify(array $params, $filter = null) + { + $options = []; + + if (array_key_exists('due', $params)) { + $options[] = 'due:' . $params['due']; + } + + if (array_key_exists('project', $params)) { + $options[] = 'project:' . $params['project']; + } + + if (array_key_exists('description', $params)) { + $options[] = $params['description']; + } + + $this->command('modify', $filter, $options); + } + + /** + * @return array + * @throws TaskwarriorException + */ + public function projects() + { + $result = $this->command('_project'); + + return array_filter(explode("\n", $result), 'strlen'); + } + /** * @param $json * @return string @@ -64,6 +98,8 @@ class Taskwarrior $output = $this->command('import', $file); + $fs->remove($file); + if (!preg_match('/([0-9a-f]{8}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{12})/', $output, $matches)) { throw new TaskwarriorException(); } @@ -123,6 +159,15 @@ class Taskwarrior return $process->getOutput(); } + /** + * @return string + * @throws TaskwarriorException + */ + public function version() + { + return $this->command('_version'); + } + /** * @return ProcessBuilder */ diff --git a/tests/TaskManagerTest.php b/tests/TaskManagerTest.php index 698f9db..1581698 100644 --- a/tests/TaskManagerTest.php +++ b/tests/TaskManagerTest.php @@ -26,6 +26,7 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase { $this->taskwarrior = new Taskwarrior(__DIR__ . '/.taskrc', __DIR__ . '/.task'); $this->taskManager = new TaskManager($this->taskwarrior); + $this->taskwarrior->version(); // to initialise } public function tearDown() @@ -56,6 +57,15 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase $this->assertEquals($task, $result); } + public function testSaveTaskWithoutDescription() + { + $this->setExpectedException('DavidBadura\Taskwarrior\TaskwarriorException'); + + $task = new Task(); + + $this->taskManager->save($task); + } + public function testFindFromCache() { $task = new Task(); @@ -290,6 +300,57 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase $this->assertEquals(array($task2, $task1, $task3), $tasks); } + public function testProject() + { + $task1 = new Task(); + $task1->setDescription('foo1'); + $task1->setProject('home'); + + $task2 = new Task(); + $task2->setDescription('foo2'); + $task2->setProject('office'); + + $this->taskManager->save($task1); + $this->taskManager->save($task2); + + $this->taskManager->clear(); + + $task1 = $this->taskManager->find($task1->getUuid()); + $task2 = $this->taskManager->find($task2->getUuid()); + + $this->assertEquals('home', $task1->getProject()); + $this->assertEquals('office', $task2->getProject()); + + $this->assertCount(2, $this->taskManager->filter()); + $this->assertCount(1, $this->taskManager->filter('project:home')); + $this->assertCount(1, $this->taskManager->filter('project:office')); + $this->assertCount(0, $this->taskManager->filter('project:hobby')); + + $task2->setProject('home'); + $this->taskManager->save($task2); + + $this->assertCount(2, $this->taskManager->filter()); + $this->assertCount(2, $this->taskManager->filter('project:home')); + $this->assertCount(0, $this->taskManager->filter('project:office')); + $this->assertCount(0, $this->taskManager->filter('project:hobby')); + } + + public function testProjects() + { + $task1 = new Task(); + $task1->setDescription('foo1'); + $task1->setProject('home'); + + $task2 = new Task(); + $task2->setDescription('foo2'); + $task2->setProject('office'); + + $this->taskManager->save($task1); + $this->taskManager->save($task2); + + $this->assertEquals(array('home', 'office'), $this->taskManager->projects()); + } + /** * @param string $string * @return \DateTime