From: Fabien Potencier Date: Wed, 2 Oct 2013 19:50:59 +0000 (+0200) Subject: Revert "merged branch hason/macros (PR #1139)" X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=74586e9f080a1d475acc5d44c6e76dae66b642b0;p=web%2Fkonrad%2Ftwig.git Revert "merged branch hason/macros (PR #1139)" This reverts commit 03926034c452bbf7f3bcc14dd167acf39bb33818, reversing changes made to 730aa25bedeee922c361640f6e4c01e4e6bac998. Conflicts: CHANGELOG doc/tags/macro.rst doc/templates.rst lib/Twig/ExpressionParser.php --- diff --git a/CHANGELOG b/CHANGELOG index 120f43a..dd02e50 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -9,9 +9,6 @@ * fixed the filesystem loader cache when a template name exists in several namespaces * fixed template_from_string when the template includes or extends other ones * fixed a crash of the C extension on an edge case - * in the template can be directly called macros defined in the same template - * added support for named arguments for macros - * fixed fatal error that should be an exception when macro does not exist in template * 1.13.2 (2013-08-03) diff --git a/doc/tags/macro.rst b/doc/tags/macro.rst index 800957e..11c115a 100644 --- a/doc/tags/macro.rst +++ b/doc/tags/macro.rst @@ -1,9 +1,6 @@ ``macro`` ========= -.. versionadded:: 1.14 - Support for default argument values was added in Twig 1.14. - Macros are comparable with functions in regular programming languages. They are useful to put often used HTML idioms into reusable elements to not repeat yourself. @@ -18,20 +15,10 @@ Here is a small example of a macro that renders a form element: Macros differs from native PHP functions in a few ways: -* Arguments of a macro are always optional; - -* Default argument values may be defined by using the ``default`` filter in the - macro body. - -.. tip:: - - A macro may define default values for scalar arguments as follows: - - .. code-block:: jinja +* Default argument values are defined by using the ``default`` filter in the + macro body; - {% macro input(name, value = "", type = "text", size = 20) %} - - {% endmacro %} +* Arguments of a macro are always optional. But as with PHP functions, macros don't have access to the current template variables. @@ -59,7 +46,6 @@ The macro can then be called at will:

{{ forms.input('username') }}

{{ forms.input('password', null, 'password') }}

-

{{ forms.input(name='username', size=40) }}

If macros are defined and used in the same template, you can use the special ``_self`` variable to import them: diff --git a/doc/templates.rst b/doc/templates.rst index 1894928..a191072 100644 --- a/doc/templates.rst +++ b/doc/templates.rst @@ -191,19 +191,12 @@ progression of integers: Go to the :doc:`functions` page to learn more about the built-in functions. -.. _named_arguments: - Named Arguments --------------- .. versionadded:: 1.12 Support for named arguments was added in Twig 1.12. -.. versionadded:: 1.14 - Support for named arguments for macros was added in Twig 1.14. - -Arguments for filters, functions and macros can also be passed as *named arguments*: - .. code-block:: jinja {% for i in range(low=1, high=10, step=2) %} @@ -503,10 +496,6 @@ Macros .. versionadded:: 1.12 Support for default argument values was added in Twig 1.12. -.. versionadded:: 1.14 - Support for macro call with named arguments was added in Twig 1.14. - Support for directly call macros defined in the same template was added in Twig 1.14. - Macros are comparable with functions in regular programming languages. They are useful to reuse often used HTML fragments to not repeat yourself. @@ -528,21 +517,6 @@ Macros can be defined in any template, and need to be "imported" via the

{{ forms.input('username') }}

-Macros defined in the same template can be directly called: - -.. code-block:: jinja - - {% macro submit(name) %} - - {% endmacro %} - -

{{ submit('Send') }}

- -.. note:: - - If the macro name matches the name of a function, it need to be "imported" - via the :doc:`import`. - Alternatively, you can import individual macro names from a template into the current namespace via the :doc:`from` tag and optionally alias them: @@ -566,14 +540,6 @@ macro call: {% endmacro %} -Arguments for macro can also be passed as :ref:`named arguments`: - -.. code-block:: jinja - - {% import "forms.html" as forms %} - -

{{ forms.input(name='username', size=40) }}

- .. _twig-expressions: Expressions diff --git a/lib/Twig/ExpressionParser.php b/lib/Twig/ExpressionParser.php index 8f85745..73be91d 100644 --- a/lib/Twig/ExpressionParser.php +++ b/lib/Twig/ExpressionParser.php @@ -318,21 +318,21 @@ class Twig_ExpressionParser return new Twig_Node_Expression_GetAttr($args->getNode(0), $args->getNode(1), count($args) > 2 ? $args->getNode(2) : new Twig_Node_Expression_Array(array(), $line), Twig_Template::ANY_CALL, $line); default: - $args = $this->parseArguments(true); - if (null !== $alias = $this->parser->getImportedSymbol('macro', $name)) { - return new Twig_Node_Expression_MacroCall($alias['node'], $alias['name'], $this->createArrayFromArguments($args), $line); - } - - try { - $class = $this->getFunctionNodeClass($name, $line); - } catch (Twig_Error_Syntax $e) { - if (!$this->parser->hasMacro($name)) { - throw $e; + if (null !== $alias = $this->parser->getImportedSymbol('function', $name)) { + $arguments = new Twig_Node_Expression_Array(array(), $line); + foreach ($this->parseArguments() as $n) { + $arguments->addElement($n); } - return new Twig_Node_Expression_MacroCall(new Twig_Node_Expression_Name('_self', $line), $name, $this->createArrayFromArguments($args), $line); + $node = new Twig_Node_Expression_MethodCall($alias['node'], $alias['name'], $arguments, $line); + $node->setAttribute('safe', true); + + return $node; } + $args = $this->parseArguments(true); + $class = $this->getFunctionNodeClass($name, $line); + return new $class($name, $args, $line); } } @@ -354,6 +354,13 @@ class Twig_ExpressionParser ($token->getType() == Twig_Token::OPERATOR_TYPE && preg_match(Twig_Lexer::REGEX_NAME, $token->getValue())) ) { $arg = new Twig_Node_Expression_Constant($token->getValue(), $lineno); + + if ($stream->test(Twig_Token::PUNCTUATION_TYPE, '(')) { + $type = Twig_TemplateInterface::METHOD_CALL; + foreach ($this->parseArguments() as $n) { + $arguments->addElement($n); + } + } } else { throw new Twig_Error_Syntax('Expected name or number', $lineno, $this->parser->getFilename()); } @@ -363,14 +370,10 @@ class Twig_ExpressionParser throw new Twig_Error_Syntax(sprintf('Dynamic macro names are not supported (called on "%s")', $node->getAttribute('name')), $token->getLine(), $this->parser->getFilename()); } - $arguments = $this->createArrayFromArguments($this->parseArguments(true)); - - return new Twig_Node_Expression_MacroCall($node, $arg->getAttribute('value'), $arguments, $lineno); - } + $node = new Twig_Node_Expression_MethodCall($node, 'get'.$arg->getAttribute('value'), $arguments, $lineno); + $node->setAttribute('safe', true); - if ($stream->test(Twig_Token::PUNCTUATION_TYPE, '(')) { - $type = Twig_Template::METHOD_CALL; - $arguments = $this->createArrayFromArguments($this->parseArguments()); + return $node; } } else { $type = Twig_Template::ARRAY_CALL; @@ -449,8 +452,6 @@ class Twig_ExpressionParser * * @param Boolean $namedArguments Whether to allow named arguments or not * @param Boolean $definition Whether we are parsing arguments for a function definition - * - * @return Twig_Node */ public function parseArguments($namedArguments = false, $definition = false) { @@ -489,15 +490,18 @@ class Twig_ExpressionParser } } - if ($definition && null === $name) { - $name = $value->getAttribute('name'); - $value = new Twig_Node_Expression_Constant(null, $this->parser->getCurrentToken()->getLine()); - } - - if (null === $name) { - $args[] = $value; - } else { + if ($definition) { + if (null === $name) { + $name = $value->getAttribute('name'); + $value = new Twig_Node_Expression_Constant(null, $this->parser->getCurrentToken()->getLine()); + } $args[$name] = $value; + } else { + if (null === $name) { + $args[] = $value; + } else { + $args[$name] = $value; + } } } $stream->expect(Twig_Token::PUNCTUATION_TYPE, ')', 'A list of arguments must be closed by a parenthesis'); @@ -593,15 +597,4 @@ class Twig_ExpressionParser return true; } - - private function createArrayFromArguments(Twig_Node $arguments, $line = null) - { - $line = null === $line ? $arguments->getLine() : $line; - $array = new Twig_Node_Expression_Array(array(), $line); - foreach ($arguments as $key => $value) { - $array->addElement($value, new Twig_Node_Expression_Constant($key, $value->getLine())); - } - - return $array; - } } diff --git a/lib/Twig/Node/Expression/MacroCall.php b/lib/Twig/Node/Expression/MacroCall.php deleted file mode 100644 index 3e6b8c1..0000000 --- a/lib/Twig/Node/Expression/MacroCall.php +++ /dev/null @@ -1,60 +0,0 @@ - - */ -class Twig_Node_Expression_MacroCall extends Twig_Node_Expression -{ - public function __construct(Twig_Node_Expression $template, $name, Twig_Node_Expression_Array $arguments, $lineno) - { - parent::__construct(array('template' => $template, 'arguments' => $arguments), array('name' => $name), $lineno); - } - - public function compile(Twig_Compiler $compiler) - { - $namedNames = array(); - $namedCount = 0; - $positionalCount = 0; - foreach ($this->getNode('arguments')->getKeyValuePairs() as $pair) { - $name = $pair['key']->getAttribute('value'); - if (!is_int($name)) { - $namedCount++; - $namedNames[$name] = 1; - } elseif ($namedCount > 0) { - throw new Twig_Error_Syntax(sprintf('Positional arguments cannot be used after named arguments for macro "%s".', $this->getAttribute('name')), $this->lineno); - } else { - $positionalCount++; - } - } - - $compiler - ->raw('$this->callMacro(') - ->subcompile($this->getNode('template')) - ->raw(', ')->repr($this->getAttribute('name')) - ->raw(', ')->subcompile($this->getNode('arguments')) - ; - - if ($namedCount > 0) { - $compiler - ->raw(', ')->repr($namedNames) - ->raw(', ')->repr($namedCount) - ->raw(', ')->repr($positionalCount) - ; - } - - $compiler - ->raw(')') - ; - } -} diff --git a/lib/Twig/Node/Macro.php b/lib/Twig/Node/Macro.php index 43c75e5..8991061 100644 --- a/lib/Twig/Node/Macro.php +++ b/lib/Twig/Node/Macro.php @@ -18,7 +18,7 @@ class Twig_Node_Macro extends Twig_Node { public function __construct($name, Twig_NodeInterface $body, Twig_NodeInterface $arguments, $lineno, $tag = null) { - parent::__construct(array('body' => $body, 'arguments' => $arguments), array('name' => $name, 'method' => 'get'.ucfirst($name)), $lineno, $tag); + parent::__construct(array('body' => $body, 'arguments' => $arguments), array('name' => $name), $lineno, $tag); } /** @@ -30,7 +30,7 @@ class Twig_Node_Macro extends Twig_Node { $compiler ->addDebugInfo($this) - ->write(sprintf("public function %s(", $this->getAttribute('method'))) + ->write(sprintf("public function get%s(", $this->getAttribute('name'))) ; $count = count($this->getNode('arguments')); diff --git a/lib/Twig/Node/Module.php b/lib/Twig/Node/Module.php index 551458a..585048b 100644 --- a/lib/Twig/Node/Module.php +++ b/lib/Twig/Node/Module.php @@ -235,41 +235,9 @@ class Twig_Node_Module extends Twig_Node $compiler ->outdent() - ->write(");\n\n") - ; - - // macro information - $compiler - ->write("\$this->macros = array(\n") - ->indent() - ; - - foreach ($this->getNode('macros') as $name => $node) { - $compiler - ->addIndentation()->repr($name)->raw(" => array(\n") - ->indent() - ->write("'method' => ")->repr($node->getAttribute('method'))->raw(",\n") - ->write("'default_argument_values' => array(\n") - ->indent() - ; - foreach ($node->getNode('arguments') as $argument => $value) { - $compiler->addIndentation()->repr($argument)->raw (' => ')->subcompile($value)->raw(",\n"); - } - $compiler - ->outdent() - ->write(")\n") - ->outdent() - ->write("),\n") - ; - } - $compiler - ->outdent() ->write(");\n") - ; - - $compiler ->outdent() - ->write("}\n\n") + ->write("}\n\n"); ; } diff --git a/lib/Twig/NodeVisitor/SafeAnalysis.php b/lib/Twig/NodeVisitor/SafeAnalysis.php index 214e8b6..a5d06de 100644 --- a/lib/Twig/NodeVisitor/SafeAnalysis.php +++ b/lib/Twig/NodeVisitor/SafeAnalysis.php @@ -97,8 +97,6 @@ class Twig_NodeVisitor_SafeAnalysis implements Twig_NodeVisitorInterface } else { $this->setSafe($node, array()); } - } elseif ($node instanceof Twig_Node_Expression_MacroCall) { - $this->setSafe($node, array('all')); } elseif ($node instanceof Twig_Node_Expression_GetAttr && $node->getNode('node') instanceof Twig_Node_Expression_Name) { $name = $node->getNode('node')->getAttribute('name'); // attributes on template instances are safe diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index a27b801..ba1d921 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -24,7 +24,6 @@ abstract class Twig_Template implements Twig_TemplateInterface protected $env; protected $blocks; protected $traits; - protected $macros; /** * Constructor. @@ -36,7 +35,6 @@ abstract class Twig_Template implements Twig_TemplateInterface $this->env = $env; $this->blocks = array(); $this->traits = array(); - $this->macros = array(); } /** @@ -448,65 +446,6 @@ abstract class Twig_Template implements Twig_TemplateInterface } /** - * Calls macro in a template. - * - * @param Twig_Template $template The template - * @param string $macro The name of macro - * @param array $arguments The arguments of macro - * @param array $namedNames An array of names of arguments as keys - * @param integer $namedCount The count of named arguments - * @param integer $positionalCount The count of positional arguments - * - * @return string The content of a macro - * - * @throws Twig_Error_Runtime if the macro is not defined - * @throws Twig_Error_Runtime if the argument is defined twice - * @throws Twig_Error_Runtime if the argument is unknown - */ - protected function callMacro(Twig_Template $template, $macro, array $arguments, array $namedNames = array(), $namedCount = 0, $positionalCount = -1) - { - 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']); - } - - if ($namedCount < 1) { - return $template->macros[$macro]['reflection']->invokeArgs($template, $arguments); - } - - $i = 0; - $args = array(); - foreach ($template->macros[$macro]['default_argument_values'] as $name => $value) { - if (isset($namedNames[$name])) { - if ($i < $positionalCount) { - throw new Twig_Error_Runtime(sprintf('Argument "%s" is defined twice for macro "%s" defined in the template "%s".', $name, $macro, $template->getTemplateName())); - } - - $args[] = $arguments[$name]; - if (--$namedCount < 1) { - break; - } - } elseif ($i < $positionalCount) { - $args[] = $arguments[$i]; - } else { - $args[] = $value; - } - - $i++; - } - - if ($namedCount > 0) { - $parameters = array_keys(array_diff_key($namedNames, $template->macros[$macro]['default_argument_values'])); - throw new Twig_Error_Runtime(sprintf('Unknown argument%s "%s" for macro "%s" defined in the template "%s".', count($parameters) > 1 ? 's' : '' , implode('", "', $parameters), $macro, $template->getTemplateName())); - } - - return $template->macros[$macro]['reflection']->invokeArgs($template, $args); - } - - /** * This method is only useful when testing Twig. Do not use it. */ public static function clearCache() diff --git a/lib/Twig/TokenParser/From.php b/lib/Twig/TokenParser/From.php index ff6e575..a54054d 100644 --- a/lib/Twig/TokenParser/From.php +++ b/lib/Twig/TokenParser/From.php @@ -56,7 +56,7 @@ class Twig_TokenParser_From extends Twig_TokenParser $node = new Twig_Node_Import($macro, new Twig_Node_Expression_AssignName($this->parser->getVarName(), $token->getLine()), $token->getLine(), $this->getTag()); foreach ($targets as $name => $alias) { - $this->parser->addImportedSymbol('macro', $alias, $name, $node->getNode('var')); + $this->parser->addImportedSymbol('function', $alias, 'get'.$name, $node->getNode('var')); } return $node; diff --git a/test/Twig/Tests/ExpressionParserTest.php b/test/Twig/Tests/ExpressionParserTest.php index 7848077..8ec6537 100644 --- a/test/Twig/Tests/ExpressionParserTest.php +++ b/test/Twig/Tests/ExpressionParserTest.php @@ -227,6 +227,17 @@ class Twig_Tests_ExpressionParserTest extends PHPUnit_Framework_TestCase } /** + * @expectedException Twig_Error_Syntax + */ + public function testMacroCallDoesNotSupportNamedArguments() + { + $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false)); + $parser = new Twig_Parser($env); + + $parser->parse($env->tokenize('{% from _self import foo %}{% macro foo() %}{% endmacro %}{{ foo(name="Foo") }}', 'index')); + } + + /** * @expectedException Twig_Error_Syntax * @expectedExceptionMessage An argument must be a name. Unexpected token "string" of value "a" ("name" expected) in "index" at line 1 */ diff --git a/test/Twig/Tests/Fixtures/exceptions/unknown_macro.test b/test/Twig/Tests/Fixtures/exceptions/unknown_macro.test deleted file mode 100644 index 41217b0..0000000 --- a/test/Twig/Tests/Fixtures/exceptions/unknown_macro.test +++ /dev/null @@ -1,9 +0,0 @@ ---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 deleted file mode 100644 index 437fd72..0000000 --- a/test/Twig/Tests/Fixtures/exceptions/unknown_macro_different_template.test +++ /dev/null @@ -1,11 +0,0 @@ ---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/Fixtures/macros/auto_importing_in_same_file.test b/test/Twig/Tests/Fixtures/macros/auto_importing_in_same_file.test deleted file mode 100644 index 4ebb930..0000000 --- a/test/Twig/Tests/Fixtures/macros/auto_importing_in_same_file.test +++ /dev/null @@ -1,20 +0,0 @@ ---TEST-- -Auto importing macros in the same file ---TEMPLATE-- -{% macro test(a) %} -{{ a }} -{%- endmacro %} - -{% macro foo(a) %} -foo: {{ test(a) }} -{%- endmacro %} - -{{ test('foo') }} -{{ foo('foo') }} -{{ test(a = 'bar') }} ---DATA-- -return array(); ---EXPECT-- -foo -foo: foo -bar diff --git a/test/Twig/Tests/Fixtures/macros/named_arguments.test b/test/Twig/Tests/Fixtures/macros/named_arguments.test deleted file mode 100644 index a9d04d5..0000000 --- a/test/Twig/Tests/Fixtures/macros/named_arguments.test +++ /dev/null @@ -1,31 +0,0 @@ ---TEST-- -macro with named arguments ---TEMPLATE-- -{% import _self as test %} -{% from _self import test %} - -{% macro test(a, b = 'bar') -%} -{{ a }}{{ b }} -{%- endmacro %} - -{{ test(b = 'bar', a = 'foo') }} -{{ test(b = '2', a = 'foo') }} -{{ test('bar', b = 'foo') }} -{{ test.test(b = 1) }} -{{ test.test(b = 'foo') }} -{{ test.test(2, b = 'foo') }} -{{ test.test(3, b = 'foobar') }} -{{ test.test(a = 1) }} -{{ test.test(a = 2) }} ---DATA-- -return array(); ---EXPECT-- -foobar -foo2 -barfoo -1 -foo -2foo -3foobar -1bar -2bar diff --git a/test/Twig/Tests/Node/Expression/MacroCallTest.php b/test/Twig/Tests/Node/Expression/MacroCallTest.php deleted file mode 100644 index 2687e6e..0000000 --- a/test/Twig/Tests/Node/Expression/MacroCallTest.php +++ /dev/null @@ -1,24 +0,0 @@ -getMock('Twig_Node'), new Twig_Node_Expression_Constant(0, -1), $this->getMock('Twig_Node')), -1); - $node = new Twig_Node_Expression_MacroCall($this->getMock('Twig_Node_Expression'), 'foo', $arguments, -1); - $node->compile($this->getMock('Twig_Compiler', null, array(), '', false)); - } -} diff --git a/test/Twig/Tests/Node/MacroTest.php b/test/Twig/Tests/Node/MacroTest.php index 304e3e2..4d2f641 100644 --- a/test/Twig/Tests/Node/MacroTest.php +++ b/test/Twig/Tests/Node/MacroTest.php @@ -46,7 +46,7 @@ class Twig_Tests_Node_MacroTest extends Twig_Test_NodeTestCase return array( array($node, <<env->mergeGlobals(array( "foo" => \$_foo, diff --git a/test/Twig/Tests/Node/ModuleTest.php b/test/Twig/Tests/Node/ModuleTest.php index 23c66c6..b8996ed 100644 --- a/test/Twig/Tests/Node/ModuleTest.php +++ b/test/Twig/Tests/Node/ModuleTest.php @@ -75,9 +75,6 @@ class __TwigTemplate_a2bfbf7dd6ab85666684fe9297f69363a3fc2046d90f22a317d380c1863 \$this->blocks = array( ); - - \$this->macros = array( - ); } protected function doDisplay(array \$context, array \$blocks = array()) @@ -93,7 +90,7 @@ class __TwigTemplate_a2bfbf7dd6ab85666684fe9297f69363a3fc2046d90f22a317d380c1863 public function getDebugInfo() { - return array ( 22 => 1,); + return array ( 19 => 1,); } } EOF @@ -119,9 +116,6 @@ class __TwigTemplate_a2bfbf7dd6ab85666684fe9297f69363a3fc2046d90f22a317d380c1863 \$this->blocks = array( ); - - \$this->macros = array( - ); } protected function doGetParent(array \$context) @@ -148,7 +142,7 @@ class __TwigTemplate_a2bfbf7dd6ab85666684fe9297f69363a3fc2046d90f22a317d380c1863 public function getDebugInfo() { - return array ( 27 => 1,); + return array ( 24 => 1,); } } EOF diff --git a/test/Twig/Tests/Node/SandboxedModuleTest.php b/test/Twig/Tests/Node/SandboxedModuleTest.php index 1d4b3f6..421f210 100644 --- a/test/Twig/Tests/Node/SandboxedModuleTest.php +++ b/test/Twig/Tests/Node/SandboxedModuleTest.php @@ -73,9 +73,6 @@ class __TwigTemplate_a2bfbf7dd6ab85666684fe9297f69363a3fc2046d90f22a317d380c1863 \$this->blocks = array( ); - - \$this->macros = array( - ); } protected function doDisplay(array \$context, array \$blocks = array()) @@ -101,7 +98,7 @@ class __TwigTemplate_a2bfbf7dd6ab85666684fe9297f69363a3fc2046d90f22a317d380c1863 public function getDebugInfo() { - return array ( 23 => 1,); + return array ( 20 => 1,); } } EOF @@ -131,9 +128,6 @@ class __TwigTemplate_a2bfbf7dd6ab85666684fe9297f69363a3fc2046d90f22a317d380c1863 \$this->blocks = array( ); - - \$this->macros = array( - ); } protected function doGetParent(array \$context) diff --git a/test/Twig/Tests/TemplateTest.php b/test/Twig/Tests/TemplateTest.php index 678cba4..cc3b2a2 100644 --- a/test/Twig/Tests/TemplateTest.php +++ b/test/Twig/Tests/TemplateTest.php @@ -363,55 +363,6 @@ 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()); - } - - /** - * @expectedException Twig_Error_Runtime - * @expectedExceptionMessage Argument "format" is defined twice for macro "date" defined in the template "my/template". - */ - public function testCallMacroWhenArgumentIsDefinedTwice() - { - $template = new Twig_TemplateTest($this->getMock('Twig_Environment')); - $template->callMacro(new Twig_Tests_TemplateWithMacros('my/template', array('date' => array( - 'method' => 'getDate', - 'default_argument_values' => array('format' => null, 'template' => null) - ))), 'date', array('d', 'format' => 'H'), array('format' => 1), 1, 1); - } - - /** - * @expectedException Twig_Error_Runtime - * @expectedExceptionMessage Unknown argument "unknown" for macro "date" defined in the template "my/template". - */ - public function testCallMacroWithWrongNamedArgumentName() - { - $template = new Twig_TemplateTest($this->getMock('Twig_Environment')); - $template->callMacro(new Twig_Tests_TemplateWithMacros('my/template', array('date' => array( - 'method' => 'getDate', - 'default_argument_values' => array('foo' => 1, 'bar' => 2) - ))), 'date', array('foo' => 2), array('foo' => 1, 'unknown' => 1), 2, 0); - } - - /** - * @expectedException Twig_Error_Runtime - * @expectedExceptionMessage Unknown arguments "unknown1", "unknown2" for macro "date" defined in the template "my/template". - */ - public function testCallMacroWithWrongNamedArgumentNames() - { - $template = new Twig_TemplateTest($this->getMock('Twig_Environment')); - $template->callMacro(new Twig_Tests_TemplateWithMacros('my/template', array('date' => array( - 'method' => 'getDate', - 'default_argument_values' => array() - ))), 'date', array(), array('unknown1' => 1, 'unknown2' => 2), 2, 0); - } } class Twig_TemplateTest extends Twig_Template @@ -470,35 +421,6 @@ class Twig_TemplateTest extends Twig_Template return parent::getAttribute($object, $item, $arguments, $type, $isDefinedTest, $ignoreStrictCheck); } } - - public function callMacro(Twig_Template $template, $macro, array $arguments, array $namedNames = array(), $namedCount = 0, $positionalCount = -1) - { - return parent::callMacro($template, $macro, $arguments, $namedNames, $namedCount, $positionalCount); - } -} - -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