* @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']);
}
--- /dev/null
+--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.
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
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