diff --git a/README.md b/README.md index ed4f0b7..5eebbc7 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,6 @@ $task->setDescription('program this lib'); $task->setProject('hobby'); $task->setDue(new \DateTime('tomorrow')); $task->setPriority(Task::PRIORITY_HIGH); - $task->addTag('next'); $tm->save($task); diff --git a/src/Task.php b/src/Task.php index 8989ed0..b456661 100644 --- a/src/Task.php +++ b/src/Task.php @@ -184,6 +184,10 @@ class Task */ public function addTag($tag) { + if (!$this->tags) { + $this->tags = [$tag]; + } + if (!in_array($tag, $this->tags)) { $this->tags[] = $tag; } @@ -194,6 +198,10 @@ class Task */ public function removeTag($tag) { + if (!$this->tags) { + return; + } + if (false !== $key = array_search($tag, $this->tags)) { unset($this->tags[$key]); } diff --git a/tests/TaskManagerTest.php b/tests/TaskManagerTest.php index 03bac37..0db3f95 100644 --- a/tests/TaskManagerTest.php +++ b/tests/TaskManagerTest.php @@ -140,11 +140,14 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase $task2 = new Task(); $task2->setDescription('foo2'); + $task2->setPriority(Task::PRIORITY_HIGH); + $task2->setProject('home'); + $task2->addTag('now'); $this->taskManager->save($task1); $this->taskManager->save($task2); - $this->assertCount(2, $this->taskManager->filter('status:pending')); // todo better test + $this->assertCount(1, $this->taskManager->filter('project:home prio:H +now')); } public function testPending() @@ -384,6 +387,8 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase $task1 = $this->taskManager->find($task1->getUuid()); $this->assertEmpty($task1->getTags()); + $task1->removeTag('a'); + $task1->addTag('a'); $task1->setTags(array('a', 'b', 'c')); $this->taskManager->save($task1); $this->taskManager->clear();