enhanced error reporting when the template file is an instance of SplFileInfo

This commit is contained in:
Fabien Potencier
2012-04-08 14:12:00 +02:00
parent fe3c17f51f
commit 1b47b40104
3 changed files with 23 additions and 1 deletions
+1
View File
@@ -1,5 +1,6 @@
* 1.7.0 (2012-XX-XX)
* enhanced error reporting when the template file is an instance of SplFileInfo
* added Twig_Environment::mergeGlobals()
* fixed a regression when a template only extends another one without defining any blocks
* added compilation checks to avoid misuses of the sandbox tag
+6 -1
View File
@@ -140,7 +140,12 @@ class Twig_Error extends Exception
}
if (null !== $this->filename) {
$this->message .= sprintf(' in %s', is_string($this->filename) ? '"'.$this->filename.'"' : json_encode($this->filename));
if (is_string($this->filename) || (is_object($this->filename) && method_exists($this->filename, '__toString'))) {
$filename = sprintf('"%s"', $this->filename);
} else {
$filename = json_encode($this->filename);
}
$this->message .= sprintf(' in %s', $filename);
}
if ($this->lineno >= 0) {
+16
View File
@@ -11,6 +11,22 @@
class Twig_Tests_ErrorTest extends PHPUnit_Framework_TestCase
{
public function testErrorWithObjectFilename()
{
$error = new Twig_Error('foo');
$error->setTemplateFile(new SplFileInfo(__FILE__));
$this->assertContains('test/Twig/Tests/ErrorTest.php', $error->getMessage());
}
public function testErrorWithArrayFilename()
{
$error = new Twig_Error('foo');
$error->setTemplateFile(array('foo' => 'bar'));
$this->assertEquals('foo in {"foo":"bar"}', $error->getMessage());
}
public function testTwigExceptionAddsFileAndLineWhenMissing()
{
$loader = new Twig_Loader_Array(array('index' => "\n\n{{ foo.bar }}"));