Skip to content

Commit 615f48e

Browse files
committed
Making TaskNotFoundException more informative by adding task name to exception object.
1 parent e026435 commit 615f48e

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed

lib/phake/Node.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public function get_task($task_name) {
150150
} else if ($this->parent) {
151151
return $this->parent->get_task($task_name);
152152
} else {
153-
throw new TaskNotFoundException;
153+
throw TaskNotFoundException::create($task_name);
154154
}
155155

156156
} else {

lib/phake/TaskNotFoundException.php

+42-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,45 @@
22

33
namespace phake;
44

5-
class TaskNotFoundException extends \Exception {};
5+
use Exception;
6+
7+
class TaskNotFoundException extends Exception
8+
{
9+
/**
10+
* Task name
11+
*
12+
* @var string
13+
*/
14+
private $taskName;
15+
16+
/**
17+
* Factory method for creating new exceptions
18+
*
19+
* @param string $taskName name of task which is not found
20+
* @param int $code exception code
21+
* @param Exception $previous previous exception used for the exception chaining
22+
*
23+
* @return TaskNotFoundException
24+
*/
25+
public static function create($taskName, $code = 0, Exception $previous = null)
26+
{
27+
$message = sprintf('Task "%s" not found', $taskName);
28+
if (version_compare(PHP_VERSION, '5.4', '<')) {
29+
$exception = new self($message, $code, $previous);
30+
} else {
31+
$exception = new static($message, $code, $previous);
32+
}
33+
$exception->taskName = $taskName;
34+
return $exception;
35+
}
36+
37+
/**
38+
* Return name of not founded task
39+
*
40+
* @return string
41+
*/
42+
public function getTaskName()
43+
{
44+
return $this->taskName;
45+
}
46+
}

tests/TaskNotFoundExceptionTest.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
use phake\TaskNotFoundException;
4+
5+
/**
6+
* Tests of phake\TaskNotFoundException
7+
*
8+
* @covers phake\TaskNotFoundException
9+
*/
10+
class TaskNotFoundExceptionTest extends PHPUnit_Framework_TestCase
11+
{
12+
/**
13+
* Test factory method
14+
*
15+
* @covers phake\TaskNotFoundException::create
16+
*/
17+
public function testCreate()
18+
{
19+
$e = TaskNotFoundException::create('foo');
20+
$this->assertInstanceOf('phake\TaskNotFoundException', $e);
21+
$this->assertEquals('Task "foo" not found', $e->getMessage());
22+
}
23+
24+
/**
25+
* Test getTaskName
26+
*
27+
* @covers phake\TaskNotFoundException::getTaskName
28+
*/
29+
public function testGetTaskName()
30+
{
31+
$e = TaskNotFoundException::create('foo');
32+
$this->assertEquals('foo', $e->getTaskName());
33+
}
34+
}

0 commit comments

Comments
 (0)