From: Martin HasoĊˆ Date: Sat, 11 May 2013 16:50:26 +0000 (+0200) Subject: Fixed fatal error for unknown macro X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=09fedb80f868b793b630de9883271f7407653afe;p=konrad%2Ftwig.git Fixed fatal error for unknown macro --- diff --git a/CHANGELOG b/CHANGELOG index 3e62974..b2d3978 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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) diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index 288a03e..46b8714 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -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']); } diff --git a/test/Twig/Tests/Fixtures/exceptions/unknown_macro.test b/test/Twig/Tests/Fixtures/exceptions/unknown_macro.test new file mode 100644 index 0000000..41217b0 --- /dev/null +++ b/test/Twig/Tests/Fixtures/exceptions/unknown_macro.test @@ -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. diff --git a/test/Twig/Tests/Fixtures/exceptions/unknown_macro_different_template.test b/test/Twig/Tests/Fixtures/exceptions/unknown_macro_different_template.test new file mode 100644 index 0000000..437fd72 --- /dev/null +++ b/test/Twig/Tests/Fixtures/exceptions/unknown_macro_different_template.test @@ -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. diff --git a/test/Twig/Tests/TemplateTest.php b/test/Twig/Tests/TemplateTest.php index 9ae8147..a022260 100644 --- a/test/Twig/Tests/TemplateTest.php +++ b/test/Twig/Tests/TemplateTest.php @@ -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