From 615f48e399789735258390fc84cd63769844c4a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D1=80=D0=B0?= =?UTF-8?q?=D1=81=D0=B8=D0=BB=D1=8C=D0=BD=D0=B8=D0=BA=D0=BE=D0=B2?= Date: Mon, 6 Oct 2014 12:21:05 +0400 Subject: [PATCH 1/2] Making TaskNotFoundException more informative by adding task name to exception object. --- lib/phake/Node.php | 2 +- lib/phake/TaskNotFoundException.php | 43 ++++++++++++++++++++++++++++- tests/TaskNotFoundExceptionTest.php | 34 +++++++++++++++++++++++ 3 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 tests/TaskNotFoundExceptionTest.php diff --git a/lib/phake/Node.php b/lib/phake/Node.php index 5a0acd3..d9206b9 100644 --- a/lib/phake/Node.php +++ b/lib/phake/Node.php @@ -150,7 +150,7 @@ public function get_task($task_name) { } else if ($this->parent) { return $this->parent->get_task($task_name); } else { - throw new TaskNotFoundException; + throw TaskNotFoundException::create($task_name); } } else { diff --git a/lib/phake/TaskNotFoundException.php b/lib/phake/TaskNotFoundException.php index 3b5f463..1959338 100644 --- a/lib/phake/TaskNotFoundException.php +++ b/lib/phake/TaskNotFoundException.php @@ -2,4 +2,45 @@ namespace phake; -class TaskNotFoundException extends \Exception {}; +use Exception; + +class TaskNotFoundException extends Exception +{ + /** + * Task name + * + * @var string + */ + private $taskName; + + /** + * Factory method for creating new exceptions + * + * @param string $taskName name of task which is not found + * @param int $code exception code + * @param Exception $previous previous exception used for the exception chaining + * + * @return TaskNotFoundException + */ + public static function create($taskName, $code = 0, Exception $previous = null) + { + $message = sprintf('Task "%s" not found', $taskName); + if (version_compare(PHP_VERSION, '5.4', '<')) { + $exception = new self($message, $code, $previous); + } else { + $exception = new static($message, $code, $previous); + } + $exception->taskName = $taskName; + return $exception; + } + + /** + * Return name of not founded task + * + * @return string + */ + public function getTaskName() + { + return $this->taskName; + } +} diff --git a/tests/TaskNotFoundExceptionTest.php b/tests/TaskNotFoundExceptionTest.php new file mode 100644 index 0000000..9d7fe74 --- /dev/null +++ b/tests/TaskNotFoundExceptionTest.php @@ -0,0 +1,34 @@ +assertInstanceOf('phake\TaskNotFoundException', $e); + $this->assertEquals('Task "foo" not found', $e->getMessage()); + } + + /** + * Test getTaskName + * + * @covers phake\TaskNotFoundException::getTaskName + */ + public function testGetTaskName() + { + $e = TaskNotFoundException::create('foo'); + $this->assertEquals('foo', $e->getTaskName()); + } +} From 4b540e5b76ecc11294f3115eb8d3b7eb3303c005 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D1=80=D0=B0?= =?UTF-8?q?=D1=81=D0=B8=D0=BB=D1=8C=D0=BD=D0=B8=D0=BA=D0=BE=D0=B2?= Date: Mon, 6 Oct 2014 12:23:07 +0400 Subject: [PATCH 2/2] Set default value to taskName property. --- lib/phake/TaskNotFoundException.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/phake/TaskNotFoundException.php b/lib/phake/TaskNotFoundException.php index 1959338..eef4b1e 100644 --- a/lib/phake/TaskNotFoundException.php +++ b/lib/phake/TaskNotFoundException.php @@ -11,7 +11,7 @@ class TaskNotFoundException extends Exception * * @var string */ - private $taskName; + private $taskName = ''; /** * Factory method for creating new exceptions