add project attr

This commit is contained in:
DavidBadura 2015-02-06 13:08:13 +01:00
parent 11c9f610a8
commit fa478272b6
5 changed files with 151 additions and 12 deletions

View File

@ -6,9 +6,13 @@
$tm = \DavidBadura\Taskwarrior\TaskManager::create(); $tm = \DavidBadura\Taskwarrior\TaskManager::create();
$task = new \DavidBadura\Taskwarrior\Task(); $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); $tm->save($task);
$tasks = $tm->filter('+home'); $tasks = $tm->filter('project:hobby');
``` ```

View File

@ -28,6 +28,13 @@ class Task
*/ */
private $description; private $description;
/**
* @var string
*
* @JMS\Type(name="string")
*/
private $project;
/** /**
* @var \DateTime * @var \DateTime
* *
@ -90,6 +97,22 @@ class Task
$this->description = $description; $this->description = $description;
} }
/**
* @return string
*/
public function getProject()
{
return $this->project;
}
/**
* @param string $project
*/
public function setProject($project)
{
$this->project = $project;
}
/** /**
* @return \DateTime * @return \DateTime
*/ */

View File

@ -128,6 +128,14 @@ class TaskManager
$this->update($task); $this->update($task);
} }
/**
* @return array
*/
public function projects()
{
return $this->taskwarrior->projects();
}
/** /**
* *
*/ */
@ -171,17 +179,15 @@ class TaskManager
*/ */
private function edit(Task $task) 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); $this->update($task);
} }

View File

@ -50,6 +50,40 @@ class Taskwarrior
$this->command('done', $uuid); $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 * @param $json
* @return string * @return string
@ -64,6 +98,8 @@ class Taskwarrior
$output = $this->command('import', $file); $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)) { 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(); throw new TaskwarriorException();
} }
@ -123,6 +159,15 @@ class Taskwarrior
return $process->getOutput(); return $process->getOutput();
} }
/**
* @return string
* @throws TaskwarriorException
*/
public function version()
{
return $this->command('_version');
}
/** /**
* @return ProcessBuilder * @return ProcessBuilder
*/ */

View File

@ -26,6 +26,7 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase
{ {
$this->taskwarrior = new Taskwarrior(__DIR__ . '/.taskrc', __DIR__ . '/.task'); $this->taskwarrior = new Taskwarrior(__DIR__ . '/.taskrc', __DIR__ . '/.task');
$this->taskManager = new TaskManager($this->taskwarrior); $this->taskManager = new TaskManager($this->taskwarrior);
$this->taskwarrior->version(); // to initialise
} }
public function tearDown() public function tearDown()
@ -56,6 +57,15 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($task, $result); $this->assertEquals($task, $result);
} }
public function testSaveTaskWithoutDescription()
{
$this->setExpectedException('DavidBadura\Taskwarrior\TaskwarriorException');
$task = new Task();
$this->taskManager->save($task);
}
public function testFindFromCache() public function testFindFromCache()
{ {
$task = new Task(); $task = new Task();
@ -290,6 +300,57 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array($task2, $task1, $task3), $tasks); $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 * @param string $string
* @return \DateTime * @return \DateTime