Fixed fatal error for unknown macro

This commit is contained in:
Martin Hasoň
2013-05-11 18:50:26 +02:00
parent 9e96837827
commit 09fedb80f8
5 changed files with 66 additions and 1 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
* 1.13.2 (2013-XX-XX)
n/a
* fixed fatal error that should be an exception when macro does not exist in template
* 1.13.1 (2013-06-06)
+6
View File
@@ -455,10 +455,16 @@ abstract class Twig_Template implements Twig_TemplateInterface
* @param array $arguments The arguments of macro
*
* @return string The content of a macro
*
* @throws Twig_Error_Runtime if the macro is not defined
*/
protected function callMacro(Twig_Template $template, $macro, array $arguments)
{
if (!isset($template->macros[$macro]['reflection'])) {
if (!isset($template->macros[$macro])) {
throw new Twig_Error_Runtime(sprintf('Macro "%s" is not defined in the template "%s".', $macro, $template->getTemplateName()));
}
$template->macros[$macro]['reflection'] = new ReflectionMethod($template, $template->macros[$macro]['method']);
}
@@ -0,0 +1,9 @@
--TEST--
Exception for unknown macro
--TEMPLATE--
{% import _self as macros %}
{{ macros.foo() }}
--DATA--
return array()
--EXCEPTION--
Twig_Error_Runtime: Macro "foo" is not defined in the template "index.twig" in "index.twig" at line 3.
@@ -0,0 +1,11 @@
--TEST--
Exception for unknown macro in different template
--TEMPLATE--
{% import foo_template as macros %}
{{ macros.foo() }}
--TEMPLATE(foo.twig)--
foo
--DATA--
return array('foo_template' => 'foo.twig')
--EXCEPTION--
Twig_Error_Runtime: Macro "foo" is not defined in the template "foo.twig" in "index.twig" at line 3.
+39
View File
@@ -361,6 +361,16 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase
return $tests;
}
/**
* @expectedException Twig_Error_Runtime
* @expectedExceptionMessage Macro "foo" is not defined in the template "my/template".
*/
public function testCallUnknownMacro()
{
$template = new Twig_TemplateTest($this->getMock('Twig_Environment'));
$template->callMacro(new Twig_Tests_TemplateWithMacros('my/template'), 'foo', array());
}
}
class Twig_TemplateTest extends Twig_Template
@@ -419,6 +429,35 @@ class Twig_TemplateTest extends Twig_Template
return parent::getAttribute($object, $item, $arguments, $type, $isDefinedTest, $ignoreStrictCheck);
}
}
public function callMacro(Twig_Template $template, $macro, array $arguments)
{
return parent::callMacro($template, $macro, $arguments);
}
}
class Twig_Tests_TemplateWithMacros extends Twig_Template
{
protected $name;
public function __construct($name, array $macros = array())
{
$this->name = $name;
$this->macros = $macros;
}
public function getTemplateName()
{
return $this->name;
}
public function getDate()
{
}
protected function doDisplay(array $context, array $blocks = array())
{
}
}
class Twig_TemplateArrayAccessObject implements ArrayAccess