fix typo & utf8 problem

This commit is contained in:
DavidBadura 2015-04-03 20:27:45 +00:00
parent 014c986103
commit 44b0da1398
4 changed files with 134 additions and 21 deletions

View File

@ -373,7 +373,7 @@ class Task
/**
* @return bool
*/
public function isReccuring()
public function isRecurring()
{
return $this->status == self::STATUS_RECURRING;
}

View File

@ -5,6 +5,10 @@ namespace DavidBadura\Taskwarrior;
use DavidBadura\Taskwarrior\Serializer\Handler\CarbonHandler;
use DavidBadura\Taskwarrior\Serializer\Handler\RecurringHandler;
use JMS\Serializer\Handler\HandlerRegistryInterface;
use JMS\Serializer\JsonSerializationVisitor;
use JMS\Serializer\Naming\CamelCaseNamingStrategy;
use JMS\Serializer\Naming\SerializedNameAnnotationStrategy;
use JMS\Serializer\SerializationContext;
use JMS\Serializer\Serializer;
use JMS\Serializer\SerializerBuilder;
@ -41,10 +45,15 @@ class TaskManager
/**
* @param Task $task
* @throws TaskwarriorException
*/
public function save(Task $task)
{
$this->validate($task);
$errors = $this->validate($task);
if ($errors) {
throw new TaskwarriorException(implode(', ', $errors));
}
if (!$task->getUuid()) {
$this->add($task);
@ -152,7 +161,7 @@ class TaskManager
return;
}
if ($task->isPending() || $task->isWaiting() || $task->isReccuring()) {
if ($task->isPending() || $task->isWaiting() || $task->isRecurring()) {
return;
}
@ -163,6 +172,29 @@ class TaskManager
$this->refresh($task);
}
/**
* @param Task $task
* @return array
*/
public function validate(Task $task)
{
$errors = [];
if ($task->isRecurring() && !$task->getDue()) {
$errors[] = 'You cannot remove the due date from a recurring task.';
}
if ($task->isRecurring() && !$task->getRecurring()) {
$errors[] = 'You cannot remove the recurrence from a recurring task.';
}
if ($task->getRecurring() && !$task->getDue()) {
$errors[] = "A recurring task must also have a 'due' date.";
}
return $errors;
}
/**
* @param Task $task
*/
@ -226,17 +258,6 @@ class TaskManager
);
}
/**
* @param Task $task
* @throws TaskwarriorException
*/
private function validate(Task $task)
{
if ($task->isReccuring() && !$task->getRecurring()) {
throw new TaskwarriorException('You cannot remove the recurrence from a recurring task.');
}
}
/**
* @param Task $old
* @param Task $new
@ -316,12 +337,20 @@ class TaskManager
*/
private function getSerializer()
{
$propertyNamingStrategy = new SerializedNameAnnotationStrategy(new CamelCaseNamingStrategy());
$visitor = new JsonSerializationVisitor($propertyNamingStrategy);
$visitor->setOptions(JSON_UNESCAPED_UNICODE);
return SerializerBuilder::create()
->setPropertyNamingStrategy($propertyNamingStrategy)
->configureHandlers(function (HandlerRegistryInterface $registry) {
$registry->registerSubscribingHandler(new CarbonHandler());
$registry->registerSubscribingHandler(new RecurringHandler());
})
->addDefaultHandlers()
->setSerializationVisitor('json', $visitor)
->addDefaultDeserializationVisitors()
->build();
}

View File

@ -82,7 +82,7 @@ class Taskwarrior
{
$result = $this->command('_project', $filter);
return array_filter(explode("\n", $result), 'strlen');
return $this->parseResult($result);
}
/**
@ -94,11 +94,11 @@ class Taskwarrior
{
$result = $this->command('_tags', $filter);
return array_filter(explode("\n", $result), 'strlen');
return $this->parseResult($result);
}
/**
* @param string $json
* @param string $json
* @return string
* @throws TaskwarriorException
*/
@ -158,12 +158,21 @@ class Taskwarrior
$builder->add($param);
}
/*
* Local hack to allow utf8 chars
*/
$oldLocal = setlocale(LC_CTYPE, 0);
setlocale(LC_CTYPE, 'UTF8', 'en_US.UTF-8');
$process = $builder->getProcess();
setlocale(LC_CTYPE, $oldLocal);
$process->run();
if (!$process->isSuccessful()) {
throw new TaskwarriorException(
$process->getErrorOutput(),
$this->extractErrorMessage($process->getErrorOutput()),
$process->getExitCode(),
$process->getCommandLine()
);
@ -253,6 +262,34 @@ class Taskwarrior
return $builder;
}
/**
* @param string $string
* @return string
*/
private function extractErrorMessage($string)
{
$message = '';
foreach (explode("\n", $string) as $line) {
if (strpos($line, 'Using alternate') === 0 || strpos($line, 'Configuration override') === 0) {
continue;
}
$message .= $line . "\n";
}
return trim($message);
}
/**
* @param string $string
* @return array
*/
private function parseResult($string)
{
return array_filter(explode("\n", $string), 'strlen');
}
/**
* @param string $string
* @return string|null

View File

@ -486,6 +486,22 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase
$this->assertCount(1, $this->taskManager->filter('+b'));
}
public function testTagUnicode()
{
$task1 = new Task();
$task1->setDescription('foo1');
$task1->addTag('später');
$this->taskManager->save($task1);
$this->taskManager->clear();
$task1 = $this->taskManager->find($task1->getUuid());
$this->assertEquals(array('später'), $task1->getTags());
$this->assertEquals(array('next', 'nocal', 'nocolor', 'nonag', 'später'), $this->taskwarrior->tags());
$this->assertCount(1,$this->taskManager->filter('+später'));
}
public function testWait()
{
$task1 = new Task();
@ -521,7 +537,7 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase
$this->assertCount(2, $this->taskManager->filterAll());
$this->assertTrue($task1->isReccuring());
$this->assertTrue($task1->isRecurring());
$result = $this->taskManager->filter();
$this->assertCount(1, $result);
@ -529,9 +545,10 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase
$this->assertTrue($result[0]->isPending());
}
public function testRecurringException()
public function testRecurringRemoveRecurringException()
{
$this->setExpectedException('DavidBadura\Taskwarrior\TaskwarriorException');
$this->setExpectedException('DavidBadura\Taskwarrior\TaskwarriorException',
'You cannot remove the recurrence from a recurring task.');
$task1 = new Task();
$task1->setDescription('foo1');
@ -545,6 +562,36 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase
$this->taskManager->save($task1);
}
public function testRecurringRemoveDueException()
{
$this->setExpectedException('DavidBadura\Taskwarrior\TaskwarriorException',
'You cannot remove the due date from a recurring task.');
$task1 = new Task();
$task1->setDescription('foo1');
$task1->setDue('tomorrow');
$task1->setRecurring('daily');
$this->taskManager->save($task1);
$task1->setDue(null);
$this->taskManager->save($task1);
}
public function testRecurringWithoutDue()
{
$this->setExpectedException('DavidBadura\Taskwarrior\TaskwarriorException',
"A recurring task must also have a 'due' date.");
$task1 = new Task();
$task1->setDescription('foo1');
$task1->setRecurring('daily');
$this->taskManager->save($task1);
}
public function testRecurringNull()
{
$task1 = new Task();