Taskwarrior/tests/TaskManagerTest.php

415 lines
12 KiB
PHP
Raw Normal View History

2015-02-05 13:41:03 -06:00
<?php
namespace DavidBadura\Taskwarrior\Test;
use DavidBadura\Taskwarrior\Task;
2015-02-06 05:38:54 -06:00
use DavidBadura\Taskwarrior\TaskManager;
2015-02-05 13:41:03 -06:00
use DavidBadura\Taskwarrior\Taskwarrior;
use Symfony\Component\Filesystem\Filesystem;
/**
* @author David Badura <badura@simplethings.de>
*/
2015-02-06 05:38:54 -06:00
class TaskManagerTest extends \PHPUnit_Framework_TestCase
2015-02-05 13:41:03 -06:00
{
/**
* @var Taskwarrior
*/
protected $taskwarrior;
2015-02-06 05:38:54 -06:00
/**
* @var TaskManager
*/
protected $taskManager;
2015-02-05 13:41:03 -06:00
public function setUp()
{
$this->taskwarrior = new Taskwarrior(__DIR__ . '/.taskrc', __DIR__ . '/.task');
2015-02-06 05:38:54 -06:00
$this->taskManager = new TaskManager($this->taskwarrior);
2015-02-06 06:08:13 -06:00
$this->taskwarrior->version(); // to initialise
2015-02-05 13:41:03 -06:00
}
public function tearDown()
{
$fs = new Filesystem();
$fs->remove(__DIR__ . '/.taskrc');
$fs->remove(__DIR__ . '/.task');
}
public function testEmpty()
{
2015-02-06 05:38:54 -06:00
$tasks = $this->taskManager->filter();
2015-02-05 13:41:03 -06:00
$this->assertEmpty($tasks);
}
public function testSaveTask()
{
$task = new Task();
$task->setDescription('foo');
2015-02-06 05:38:54 -06:00
$this->taskManager->save($task);
$this->taskManager->clear();
2015-02-05 13:41:03 -06:00
$this->assertNotEmpty($task->getUuid());
2015-02-06 05:38:54 -06:00
$result = $this->taskManager->find($task->getUuid());
2015-02-05 13:41:03 -06:00
$this->assertEquals($task, $result);
}
2015-02-06 06:08:13 -06:00
public function testSaveTaskWithoutDescription()
{
$this->setExpectedException('DavidBadura\Taskwarrior\TaskwarriorException');
$task = new Task();
$this->taskManager->save($task);
}
2015-02-05 13:41:03 -06:00
public function testFindFromCache()
{
$task = new Task();
$task->setDescription('foo');
2015-02-06 05:38:54 -06:00
$this->taskManager->save($task);
2015-02-05 13:41:03 -06:00
2015-02-06 05:38:54 -06:00
$result = $this->taskManager->find($task->getUuid());
2015-02-05 13:41:03 -06:00
$this->assertSame($task, $result);
}
public function testFilterFromCache()
{
$task = new Task();
$task->setDescription('foo');
2015-02-06 05:38:54 -06:00
$this->taskManager->save($task);
2015-02-05 13:41:03 -06:00
2015-02-06 05:38:54 -06:00
$result = $this->taskManager->filterAll($task->getUuid());
2015-02-05 13:41:03 -06:00
$this->assertSame($task, $result[0]);
}
public function testDontFind()
{
2015-02-06 05:38:54 -06:00
$task = $this->taskManager->find('56464asd46s4adas54da6');
2015-02-05 13:41:03 -06:00
$this->assertNull($task);
}
public function testDoubleSave()
{
$task = new Task();
$task->setDescription('foo');
2015-02-06 05:38:54 -06:00
$this->taskManager->save($task);
2015-02-05 13:41:03 -06:00
$this->assertNotEmpty($task->getUuid());
$uuid = $task->getUuid();
2015-02-06 05:38:54 -06:00
$this->taskManager->save($task);
2015-02-05 13:41:03 -06:00
$this->assertEquals($uuid, $task->getUuid());
2015-02-06 05:38:54 -06:00
$this->assertCount(1, $this->taskManager->filter());
2015-02-05 13:41:03 -06:00
2015-02-06 05:38:54 -06:00
$this->taskManager->clear();
2015-02-05 13:41:03 -06:00
2015-02-06 05:38:54 -06:00
$this->taskManager->save($task);
2015-02-05 13:41:03 -06:00
$this->assertEquals($uuid, $task->getUuid());
2015-02-06 05:38:54 -06:00
$this->assertCount(1, $this->taskManager->filter());
2015-02-05 13:41:03 -06:00
}
public function testFilterAll()
{
$task1 = new Task();
$task1->setDescription('foo1');
$task2 = new Task();
$task2->setDescription('foo2');
2015-02-06 05:38:54 -06:00
$this->taskManager->save($task1);
$this->taskManager->save($task2);
2015-02-05 13:41:03 -06:00
2015-02-06 05:38:54 -06:00
$this->assertCount(2, $this->taskManager->filter());
2015-02-05 13:41:03 -06:00
}
2015-02-06 03:05:01 -06:00
public function testMultiFilter()
{
$task1 = new Task();
$task1->setDescription('foo1');
$task2 = new Task();
$task2->setDescription('foo2');
2015-02-06 05:38:54 -06:00
$this->taskManager->save($task1);
$this->taskManager->save($task2);
2015-02-06 03:05:01 -06:00
2015-02-06 05:38:54 -06:00
$this->assertCount(2, $this->taskManager->filter('status:pending')); // todo better test
2015-02-06 03:05:01 -06:00
}
2015-02-05 16:00:30 -06:00
public function testPending()
{
$task1 = new Task();
$task1->setDescription('foo1');
2015-02-06 05:38:54 -06:00
$this->taskManager->save($task1);
2015-02-05 16:00:30 -06:00
$this->assertEquals(Task::STATUS_PENDING, $task1->getStatus());
$this->assertTrue($task1->isPending());
2015-02-06 05:38:54 -06:00
$this->taskManager->clear();
$result = $this->taskManager->find($task1->getUuid());
2015-02-05 16:00:30 -06:00
$this->assertEquals(Task::STATUS_PENDING, $result->getStatus());
$this->assertTrue($result->isPending());
2015-02-06 05:38:54 -06:00
$this->assertCount(1, $this->taskManager->filter());
2015-02-05 16:00:30 -06:00
}
public function testDelete()
{
$task1 = new Task();
$task1->setDescription('foo1');
2015-02-06 05:38:54 -06:00
$this->assertCount(0, $this->taskManager->filter());
2015-02-05 16:00:30 -06:00
2015-02-06 05:38:54 -06:00
$this->taskManager->save($task1);
$this->assertCount(1, $this->taskManager->filterAll());
$this->assertCount(1, $this->taskManager->filter());
2015-02-05 16:00:30 -06:00
$this->assertFalse($task1->isDeleted());
$this->assertEquals(Task::STATUS_PENDING, $task1->getStatus());
2015-02-06 05:38:54 -06:00
$this->taskManager->delete($task1);
$this->assertCount(1, $this->taskManager->filterAll());
$this->assertCount(0, $this->taskManager->filter());
2015-02-05 16:00:30 -06:00
$this->assertTrue($task1->isDeleted());
$this->assertEquals(Task::STATUS_DELETED, $task1->getStatus());
}
public function testCompleted()
{
$task1 = new Task();
$task1->setDescription('foo1');
2015-02-06 05:38:54 -06:00
$this->assertCount(0, $this->taskManager->filter());
2015-02-05 16:00:30 -06:00
2015-02-06 05:38:54 -06:00
$this->taskManager->save($task1);
$this->assertCount(1, $this->taskManager->filterAll());
$this->assertCount(1, $this->taskManager->filter());
2015-02-05 16:00:30 -06:00
$this->assertFalse($task1->isCompleted());
$this->assertEquals(Task::STATUS_PENDING, $task1->getStatus());
2015-02-06 05:38:54 -06:00
$this->taskManager->done($task1);
$this->assertCount(1, $this->taskManager->filterAll());
$this->assertCount(0, $this->taskManager->filter());
2015-02-05 16:00:30 -06:00
$this->assertTrue($task1->isCompleted());
$this->assertEquals(Task::STATUS_COMPLETED, $task1->getStatus());
}
2015-02-05 13:41:03 -06:00
public function testModifyDescription()
{
$task1 = new Task();
$task1->setDescription('foo1');
2015-02-06 05:38:54 -06:00
$this->taskManager->save($task1);
2015-02-05 13:41:03 -06:00
$this->assertEquals('foo1', $task1->getDescription());
$task1->setDescription('bar1');
2015-02-06 05:38:54 -06:00
$this->taskManager->save($task1);
2015-02-05 13:41:03 -06:00
2015-02-06 05:38:54 -06:00
$this->taskManager->clear();
2015-02-05 13:41:03 -06:00
2015-02-06 05:38:54 -06:00
$result = $this->taskManager->find($task1->getUuid());
2015-02-05 13:41:03 -06:00
$this->assertEquals('bar1', $result->getDescription());
}
2015-02-05 17:09:29 -06:00
public function testDue()
{
$date = $this->createDateTime('1989-01-08 11:12:13');
$task1 = new Task();
$task1->setDescription('foo1');
$task1->setDue($date);
2015-02-06 05:38:54 -06:00
$this->taskManager->save($task1);
$this->taskManager->clear();
2015-02-05 17:09:29 -06:00
2015-02-06 05:38:54 -06:00
$task2 = $this->taskManager->find($task1->getUuid());
2015-02-05 17:09:29 -06:00
$this->assertEquals($date, $task2->getDue());
$newDate = $this->createDateTime('2002-02-20 11:12:13');
$task2->setDue($newDate);
2015-02-06 05:38:54 -06:00
$this->taskManager->save($task2);
$this->taskManager->clear();
2015-02-05 17:09:29 -06:00
2015-02-06 05:38:54 -06:00
$task3 = $this->taskManager->find($task1->getUuid());
2015-02-05 17:09:29 -06:00
$this->assertEquals($newDate, $task3->getDue());
2015-02-05 17:34:57 -06:00
$task2->setDue(null);
2015-02-06 05:38:54 -06:00
$this->taskManager->save($task2);
$this->taskManager->clear();
2015-02-05 17:34:57 -06:00
2015-02-06 05:38:54 -06:00
$task3 = $this->taskManager->find($task1->getUuid());
2015-02-05 17:34:57 -06:00
$this->assertNull($task3->getDue());
2015-02-05 17:09:29 -06:00
}
public function testUrgency()
{
$task1 = new Task();
$task1->setDescription('foo1');
2015-02-05 17:34:57 -06:00
$task1->setDue($this->createDateTime('1989-01-08 11:12:13'));
2015-02-05 17:09:29 -06:00
2015-02-06 05:38:54 -06:00
$this->taskManager->save($task1);
2015-02-05 17:09:29 -06:00
$this->assertEquals(12, $task1->getUrgency());
2015-02-05 17:34:57 -06:00
$task1->setDue(null);
2015-02-06 05:38:54 -06:00
$this->taskManager->save($task1);
2015-02-05 17:34:57 -06:00
$this->assertEquals(0, $task1->getUrgency());
}
public function testUrgencySort()
{
$task1 = new Task();
$task1->setDescription('foo1');
$task2 = new Task();
$task2->setDescription('foo2');
$task2->setDue($this->createDateTime('1989-01-08 11:12:13'));
$task3 = new Task();
$task3->setDescription('foo3');
2015-02-06 05:38:54 -06:00
$this->taskManager->save($task1);
$this->taskManager->save($task2);
$this->taskManager->save($task3);
2015-02-05 17:34:57 -06:00
$this->assertEquals(0, $task1->getUrgency());
$this->assertEquals(12, $task2->getUrgency());
$this->assertEquals(0, $task3->getUrgency());
2015-02-06 05:38:54 -06:00
$tasks = $this->taskManager->filter();
2015-02-05 17:34:57 -06:00
$this->assertEquals(array($task2, $task1, $task3), $tasks);
2015-02-05 17:09:29 -06:00
}
2015-02-06 06:08:13 -06:00
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());
}
2015-02-06 11:05:29 -06:00
public function testPriority()
{
$task1 = new Task();
$task1->setDescription('foo1');
$task1->setPriority(Task::PRIORITY_MEDIUM);
$this->taskManager->save($task1);
$this->taskManager->clear();
$task1 = $this->taskManager->find($task1->getUuid());
$this->assertEquals(Task::PRIORITY_MEDIUM, $task1->getPriority());
$task1->setPriority(Task::PRIORITY_HIGH);
$this->taskManager->save($task1);
$this->taskManager->clear();
$task1 = $this->taskManager->find($task1->getUuid());
$this->assertEquals(Task::PRIORITY_HIGH, $task1->getPriority());
}
2015-02-06 11:31:13 -06:00
public function testTag()
{
$task1 = new Task();
$task1->setDescription('foo1');
$this->taskManager->save($task1);
$this->taskManager->clear();
$task1 = $this->taskManager->find($task1->getUuid());
$this->assertEmpty($task1->getTags());
$task1->setTags(array('a', 'b', 'c'));
$this->taskManager->save($task1);
$this->taskManager->clear();
$task1 = $this->taskManager->find($task1->getUuid());
$this->assertEquals(array('a', 'b', 'c'), $task1->getTags());
$task1->addTag('d');
$task1->removeTag('a');
$this->taskManager->save($task1);
$this->taskManager->clear();
$task1 = $this->taskManager->find($task1->getUuid());
$this->assertEquals(array('b', 'c', 'd'), $task1->getTags());
$this->assertCount(0, $this->taskManager->filter('+a'));
$this->assertCount(1, $this->taskManager->filter('+b'));
}
2015-02-05 17:09:29 -06:00
/**
* @param string $string
* @return \DateTime
*/
private function createDateTime($string = 'now')
{
return new \DateTime($string, new \DateTimeZone('UTC'));
}
2015-02-05 13:41:03 -06:00
}