Bläddra i källkod

Merge pull request #288 from thewilkybarkid/exception-previous

Add exception chaining
Justin Hileman 9 år sedan
förälder
incheckning
4aa31aca56

+ 9 - 4
src/Mustache/Exception/SyntaxException.php

@@ -17,13 +17,18 @@ class Mustache_Exception_SyntaxException extends LogicException implements Musta
     protected $token;
 
     /**
-     * @param string $msg
-     * @param array  $token
+     * @param string    $msg
+     * @param array     $token
+     * @param Exception $previous
      */
-    public function __construct($msg, array $token)
+    public function __construct($msg, array $token, Exception $previous = null)
     {
         $this->token = $token;
-        parent::__construct($msg);
+        if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
+            parent::__construct($msg, 0, $previous);
+        } else {
+            parent::__construct($msg);
+        }
     }
 
     /**

+ 9 - 3
src/Mustache/Exception/UnknownFilterException.php

@@ -17,12 +17,18 @@ class Mustache_Exception_UnknownFilterException extends UnexpectedValueException
     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;
-        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()

+ 9 - 3
src/Mustache/Exception/UnknownHelperException.php

@@ -17,12 +17,18 @@ class Mustache_Exception_UnknownHelperException extends InvalidArgumentException
     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;
-        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()

+ 9 - 3
src/Mustache/Exception/UnknownTemplateException.php

@@ -17,12 +17,18 @@ class Mustache_Exception_UnknownTemplateException extends InvalidArgumentExcepti
     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;
-        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()

+ 12 - 0
test/Mustache/Test/Exception/SyntaxExceptionTest.php

@@ -24,4 +24,16 @@ class Mustache_Test_Exception_SyntaxExceptionTest extends PHPUnit_Framework_Test
         $e = new Mustache_Exception_SyntaxException('ignore this', $token);
         $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());
+    }
 }

+ 12 - 0
test/Mustache/Test/Exception/UnknownFilterExceptionTest.php

@@ -29,4 +29,16 @@ class Mustache_Test_Exception_UnknownFilterExceptionTest extends PHPUnit_Framewo
         $e = new Mustache_Exception_UnknownFilterException('eggs');
         $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());
+    }
 }

+ 11 - 0
test/Mustache/Test/Exception/UnknownHelperExceptionTest.php

@@ -29,4 +29,15 @@ class Mustache_Test_Exception_UnknownHelperExceptionTest extends PHPUnit_Framewo
         $e = new Mustache_Exception_UnknownHelperException('gamma');
         $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());
+    }
 }

+ 11 - 0
test/Mustache/Test/Exception/UnknownTemplateExceptionTest.php

@@ -29,4 +29,15 @@ class Mustache_Test_Exception_UnknownTemplateExceptionTest extends PHPUnit_Frame
         $e = new Mustache_Exception_UnknownTemplateException('yoshi');
         $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());
+    }
 }