-
Notifications
You must be signed in to change notification settings - Fork 423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce a strict_variables option to the Engine #325
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -180,6 +180,29 @@ public function testAnchoredDotNotationThrowsExceptions() | |
$context->push(array('a' => 1)); | ||
$context->findAnchoredDot('a'); | ||
} | ||
|
||
/** | ||
* @expectedException Mustache_Exception_UnknownVariableException | ||
*/ | ||
public function testUnknownVariableThrowsException() | ||
{ | ||
$context = new Mustache_Context(null, true); | ||
$context->push(array('a' => 1)); | ||
$context->find('b'); | ||
} | ||
|
||
/** | ||
* @expectedException Mustache_Exception_UnknownVariableException | ||
*/ | ||
public function testAnchoredDotNotationUnknownVariableThrowsException() | ||
{ | ||
$context = new Mustache_Context(null, true); | ||
$a = array( | ||
'a' => array('b' => 1), | ||
); | ||
$context->push($a); | ||
$context->find('a.c'); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'd like to see positive and negative tests. we can probably do it with a single method for "throws exception" and a single method for "doesn't throw exception" and a dataprovider for each. for the examples below, assume context like this: [
'a' => ['b' => 1],
'c' => 1,
'd' => 0,
] we need coverage for:
|
||
} | ||
|
||
class Mustache_Test_TestDummy | ||
|
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()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why aren't sections (and inverted sections) strict?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR is a bit old so I have forgotten a bit about it. I will try to deep dive in again next week but this is likely due to some previous comments:
#325 (comment)
#325 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gotcha. i'll poke around a bit and see if it makes sense to me.