Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Make TaskNotFoundException more informative by adding task name to exception object #55

Merged
merged 2 commits into from
Oct 7, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/phake/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
43 changes: 42 additions & 1 deletion lib/phake/TaskNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
34 changes: 34 additions & 0 deletions tests/TaskNotFoundExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use phake\TaskNotFoundException;

/**
* Tests of phake\TaskNotFoundException
*
* @covers phake\TaskNotFoundException
*/
class TaskNotFoundExceptionTest extends PHPUnit_Framework_TestCase
{
/**
* Test factory method
*
* @covers phake\TaskNotFoundException::create
*/
public function testCreate()
{
$e = TaskNotFoundException::create('foo');
$this->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());
}
}