Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add a `throws_exceptions` constructor option.

This allows overriding strictness without subclassing. Closes #64.
  • Loading branch information...
commit fab64b14bfbcc2d537cc4bda2a6baea61de2920b 1 parent 6c2b85c
@bobthecow authored
Showing with 49 additions and 2 deletions.
  1. +15 −0 Mustache.php
  2. +34 −2 test/MustacheExceptionTest.php
View
15 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 @@ protected function _setOptions(array $options) {
}
$this->_pragmas = $options['pragmas'];
}
+
+ if (isset($options['throws_exceptions'])) {
+ foreach ($options['throws_exceptions'] as $exception => $value) {
+ $this->_throwsExceptions[$exception] = $value;
+ }
+ }
}
/**
View
36 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 @@ public function testGetPragmaOptionsThrowsExceptionsIfItThinksYouHaveAPragmaButI
$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);
}
-}
+
+ public function getThrownExceptions() {
+ return $this->_throwsExceptions;
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.