Add exception chaining

This commit is contained in:
Chris Wilkinson
2016-02-23 07:49:22 +00:00
parent 865de4114a
commit c858bb1f1a
8 changed files with 82 additions and 13 deletions
+9 -4
View File
@@ -17,13 +17,18 @@ class Mustache_Exception_SyntaxException extends LogicException implements Musta
protected $token; protected $token;
/** /**
* @param string $msg * @param string $msg
* @param array $token * @param array $token
* @param Exception $previous
*/ */
public function __construct($msg, array $token) public function __construct($msg, array $token, Exception $previous = null)
{ {
$this->token = $token; $this->token = $token;
parent::__construct($msg); if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
parent::__construct($msg, 0, $previous);
} else {
parent::__construct($msg);
}
} }
/** /**
@@ -17,12 +17,18 @@ class Mustache_Exception_UnknownFilterException extends UnexpectedValueException
protected $filterName; protected $filterName;
/** /**
* @param string $filterName * @param string $filterName
* @param Exception $previous
*/ */
public function __construct($filterName) public function __construct($filterName, Exception $previous = null)
{ {
$this->filterName = $filterName; $this->filterName = $filterName;
parent::__construct(sprintf('Unknown filter: %s', $filterName)); $message = sprintf('Unknown filter: %s', $filterName);
if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
parent::__construct($message, 0, $previous);
} else {
parent::__construct($message);
}
} }
public function getFilterName() public function getFilterName()
@@ -17,12 +17,18 @@ class Mustache_Exception_UnknownHelperException extends InvalidArgumentException
protected $helperName; protected $helperName;
/** /**
* @param string $helperName * @param string $helperName
* @param Exception $previous
*/ */
public function __construct($helperName) public function __construct($helperName, Exception $previous = null)
{ {
$this->helperName = $helperName; $this->helperName = $helperName;
parent::__construct(sprintf('Unknown helper: %s', $helperName)); $message = sprintf('Unknown helper: %s', $helperName);
if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
parent::__construct($message, 0, $previous);
} else {
parent::__construct($message);
}
} }
public function getHelperName() public function getHelperName()
@@ -17,12 +17,18 @@ class Mustache_Exception_UnknownTemplateException extends InvalidArgumentExcepti
protected $templateName; protected $templateName;
/** /**
* @param string $templateName * @param string $templateName
* @param Exception $previous
*/ */
public function __construct($templateName) public function __construct($templateName, Exception $previous = null)
{ {
$this->templateName = $templateName; $this->templateName = $templateName;
parent::__construct(sprintf('Unknown template: %s', $templateName)); $message = sprintf('Unknown template: %s', $templateName);
if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
parent::__construct($message, 0, $previous);
} else {
parent::__construct($message);
}
} }
public function getTemplateName() public function getTemplateName()
@@ -24,4 +24,16 @@ class Mustache_Test_Exception_SyntaxExceptionTest extends PHPUnit_Framework_Test
$e = new Mustache_Exception_SyntaxException('ignore this', $token); $e = new Mustache_Exception_SyntaxException('ignore this', $token);
$this->assertEquals($token, $e->getToken()); $this->assertEquals($token, $e->getToken());
} }
public function testPrevious()
{
if (version_compare(PHP_VERSION, '5.3.0', '<')) {
$this->markTestSkipped('Exception chaining requires at least PHP 5.3');
}
$previous = new Exception();
$e = new Mustache_Exception_SyntaxException('foo', array(), $previous);
$this->assertSame($previous, $e->getPrevious());
}
} }
@@ -29,4 +29,16 @@ class Mustache_Test_Exception_UnknownFilterExceptionTest extends PHPUnit_Framewo
$e = new Mustache_Exception_UnknownFilterException('eggs'); $e = new Mustache_Exception_UnknownFilterException('eggs');
$this->assertEquals('eggs', $e->getFilterName()); $this->assertEquals('eggs', $e->getFilterName());
} }
public function testPrevious()
{
if (version_compare(PHP_VERSION, '5.3.0', '<')) {
$this->markTestSkipped('Exception chaining requires at least PHP 5.3');
}
$previous = new Exception();
$e = new Mustache_Exception_UnknownFilterException('foo', $previous);
$this->assertSame($previous, $e->getPrevious());
}
} }
@@ -29,4 +29,15 @@ class Mustache_Test_Exception_UnknownHelperExceptionTest extends PHPUnit_Framewo
$e = new Mustache_Exception_UnknownHelperException('gamma'); $e = new Mustache_Exception_UnknownHelperException('gamma');
$this->assertEquals('gamma', $e->getHelperName()); $this->assertEquals('gamma', $e->getHelperName());
} }
public function testPrevious()
{
if (version_compare(PHP_VERSION, '5.3.0', '<')) {
$this->markTestSkipped('Exception chaining requires at least PHP 5.3');
}
$previous = new Exception();
$e = new Mustache_Exception_UnknownHelperException('foo', $previous);
$this->assertSame($previous, $e->getPrevious());
}
} }
@@ -29,4 +29,15 @@ class Mustache_Test_Exception_UnknownTemplateExceptionTest extends PHPUnit_Frame
$e = new Mustache_Exception_UnknownTemplateException('yoshi'); $e = new Mustache_Exception_UnknownTemplateException('yoshi');
$this->assertEquals('yoshi', $e->getTemplateName()); $this->assertEquals('yoshi', $e->getTemplateName());
} }
public function testPrevious()
{
if (version_compare(PHP_VERSION, '5.3.0', '<')) {
$this->markTestSkipped('Exception chaining requires at least PHP 5.3');
}
$previous = new Exception();
$e = new Mustache_Exception_UnknownTemplateException('foo', $previous);
$this->assertSame($previous, $e->getPrevious());
}
} }