From 44b0da13984d43b2dfb03950fc8d9cd44a0297fd Mon Sep 17 00:00:00 2001 From: DavidBadura Date: Fri, 3 Apr 2015 20:27:45 +0000 Subject: [PATCH] fix typo & utf8 problem --- src/Task.php | 2 +- src/TaskManager.php | 55 ++++++++++++++++++++++++++++++--------- src/Taskwarrior.php | 45 +++++++++++++++++++++++++++++--- tests/TaskManagerTest.php | 53 ++++++++++++++++++++++++++++++++++--- 4 files changed, 134 insertions(+), 21 deletions(-) diff --git a/src/Task.php b/src/Task.php index 6277a7b..cd3717a 100644 --- a/src/Task.php +++ b/src/Task.php @@ -373,7 +373,7 @@ class Task /** * @return bool */ - public function isReccuring() + public function isRecurring() { return $this->status == self::STATUS_RECURRING; } diff --git a/src/TaskManager.php b/src/TaskManager.php index c292fd0..e54c5f5 100644 --- a/src/TaskManager.php +++ b/src/TaskManager.php @@ -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(); } diff --git a/src/Taskwarrior.php b/src/Taskwarrior.php index a7c4f6b..0dbf632 100644 --- a/src/Taskwarrior.php +++ b/src/Taskwarrior.php @@ -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 diff --git a/tests/TaskManagerTest.php b/tests/TaskManagerTest.php index 945603a..7208da2 100644 --- a/tests/TaskManagerTest.php +++ b/tests/TaskManagerTest.php @@ -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();