-
Notifications
You must be signed in to change notification settings - Fork 421
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce a strict_variables option to the Engine
When the strict_variables option is enabled, any encountered unknown variables throw an UnknownVariableException. This option is useful in a development/CI environnement to catch mistakes in templates early instead of silently returning an empty string. This kind of behavior in case of a variable "miss" is accepted by the mustache manual [1]. [1] https://mustache.github.io/mustache.5.html#Variables
- Loading branch information
Showing
9 changed files
with
240 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Mustache.php. | ||
* | ||
* (c) 2017 Enalean | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
class Mustache_Exception_UnknownVariableException extends UnexpectedValueException implements Mustache_Exception | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $variableName; | ||
|
||
/** | ||
* @param string $variableName | ||
* @param Exception $previous | ||
*/ | ||
public function __construct($variableName, Exception $previous = null) | ||
{ | ||
$this->variableName = $variableName; | ||
$message = sprintf('Unknown variable: %s', $variableName); | ||
if (version_compare(PHP_VERSION, '5.3.0', '>=')) { | ||
parent::__construct($message, 0, $previous); | ||
} else { | ||
parent::__construct($message); // @codeCoverageIgnore | ||
} | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getVariableName() | ||
{ | ||
return $this->variableName; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
test/Mustache/Test/Exception/UnknownVariableExceptionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Mustache.php. | ||
* | ||
* (c) 2017 Enalean | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
class Mustache_Test_Exception_UnknownVariableExceptionTest extends PHPUnit_Framework_TestCase | ||
{ | ||
|
||
public function testInstance() | ||
{ | ||
$e = new Mustache_Exception_UnknownVariableException('alpha'); | ||
$this->assertTrue($e instanceof UnexpectedValueException); | ||
$this->assertTrue($e instanceof Mustache_Exception); | ||
} | ||
|
||
public function testMessage() | ||
{ | ||
$e = new Mustache_Exception_UnknownVariableException('beta'); | ||
$this->assertEquals('Unknown variable: beta', $e->getMessage()); | ||
} | ||
|
||
public function testGetHelperName() | ||
{ | ||
$e = new Mustache_Exception_UnknownVariableException('gamma'); | ||
$this->assertEquals('gamma', $e->getVariableName()); | ||
} | ||
|
||
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_UnknownVariableException('foo', $previous); | ||
$this->assertSame($previous, $e->getPrevious()); | ||
} | ||
} |
Oops, something went wrong.