diff --git a/Mustache.php b/Mustache.php index bdc6237..52736de 100644 --- a/Mustache.php +++ b/Mustache.php @@ -94,6 +94,15 @@ class Mustache { * 'pragmas' => array( * Mustache::PRAGMA_UNESCAPED => true * ), + * + * // an array of thrown exceptions to enable/disable + * 'throws_exceptions' => array( + * MustacheException::UNKNOWN_VARIABLE => false, + * MustacheException::UNCLOSED_SECTION => true, + * MustacheException::UNEXPECTED_CLOSE_SECTION => true, + * MustacheException::UNKNOWN_PARTIAL => false, + * MustacheException::UNKNOWN_PRAGMA => true, + * ), * ); * * @access public @@ -139,6 +148,12 @@ class Mustache { } $this->_pragmas = $options['pragmas']; } + + if (isset($options['throws_exceptions'])) { + foreach ($options['throws_exceptions'] as $exception => $value) { + $this->_throwsExceptions[$exception] = $value; + } + } } /** diff --git a/test/MustacheExceptionTest.php b/test/MustacheExceptionTest.php index 98bddce..2a40223 100644 --- a/test/MustacheExceptionTest.php +++ b/test/MustacheExceptionTest.php @@ -8,7 +8,7 @@ class MustacheExceptionTest extends PHPUnit_Framework_TestCase { protected $pickyMustache; protected $slackerMustache; - + public function setUp() { $this->pickyMustache = new PickyMustache(); $this->slackerMustache = new SlackerMustache(); @@ -91,6 +91,34 @@ class MustacheExceptionTest extends PHPUnit_Framework_TestCase { $mustache = new TestableMustache(); $mustache->testableGetPragmaOptions('PRAGMATIC'); } + + public function testOverrideThrownExceptionsViaConstructorOptions() { + $exceptions = array( + MustacheException::UNKNOWN_VARIABLE, + MustacheException::UNCLOSED_SECTION, + MustacheException::UNEXPECTED_CLOSE_SECTION, + MustacheException::UNKNOWN_PARTIAL, + MustacheException::UNKNOWN_PRAGMA, + ); + + $one = new TestableMustache(null, null, null, array( + 'throws_exceptions' => array_fill_keys($exceptions, true) + )); + + $thrownExceptions = $one->getThrownExceptions(); + foreach ($exceptions as $exception) { + $this->assertTrue($thrownExceptions[$exception]); + } + + $two = new TestableMustache(null, null, null, array( + 'throws_exceptions' => array_fill_keys($exceptions, false) + )); + + $thrownExceptions = $two->getThrownExceptions(); + foreach ($exceptions as $exception) { + $this->assertFalse($thrownExceptions[$exception]); + } + } } class PickyMustache extends Mustache { @@ -117,4 +145,8 @@ class TestableMustache extends Mustache { public function testableGetPragmaOptions($pragma_name) { return $this->_getPragmaOptions($pragma_name); } -} \ No newline at end of file + + public function getThrownExceptions() { + return $this->_throwsExceptions; + } +}