18 $template = $this->getMockForAbstractClass(
'Twig_Template', array(),
'',
false);
19 $template->displayBlock(
'foo', array(), array(
'foo' => array(
new stdClass(),
'foo')));
34 'empty_array' => array(),
35 'array' => array(
'foo' =>
'foo'),
38 'object' =>
new stdClass(),
43 $this->fail(
'Accessing an invalid attribute should throw an exception.');
45 $this->assertSame(sprintf(
$message,
'index'), $e->getMessage());
52 array(
'{{ string["a"] }}',
'Impossible to access a key ("a") on a string variable ("foo") in "%s" at line 1.'),
53 array(
'{{ null["a"] }}',
'Impossible to access a key ("a") on a null variable in "%s" at line 1.'),
54 array(
'{{ empty_array["a"] }}',
'Key "a" does not exist as the array is empty in "%s" at line 1.'),
55 array(
'{{ array["a"] }}',
'Key "a" for array with keys "foo" does not exist in "%s" at line 1.'),
56 array(
'{{ array_access["a"] }}',
'Key "a" in object with ArrayAccess of class "Twig_TemplateArrayAccessObject" does not exist in "%s" at line 1.'),
57 array(
'{{ string.a }}',
'Impossible to access an attribute ("a") on a string variable ("foo") in "%s" at line 1.'),
58 array(
'{{ string.a() }}',
'Impossible to invoke a method ("a") on a string variable ("foo") in "%s" at line 1.'),
59 array(
'{{ null.a }}',
'Impossible to access an attribute ("a") on a null variable in "%s" at line 1.'),
60 array(
'{{ null.a() }}',
'Impossible to invoke a method ("a") on a null variable in "%s" at line 1.'),
61 array(
'{{ array.a() }}',
'Impossible to invoke a method ("a") on an array in "%s" at line 1.'),
62 array(
'{{ empty_array.a }}',
'Key "a" does not exist as the array is empty in "%s" at line 1.'),
63 array(
'{{ array.a }}',
'Key "a" for array with keys "foo" does not exist in "%s" at line 1.'),
64 array(
'{{ attribute(array, -10) }}',
'Key "-10" for array with keys "foo" does not exist in "%s" at line 1.'),
65 array(
'{{ array_access.a }}',
'Neither the property "a" nor one of the methods "a()", "geta()"/"isa()" or "__call()" exist and have public access in class "Twig_TemplateArrayAccessObject" in "%s" at line 1.'),
66 array(
'{% from _self import foo %}{% macro foo(obj) %}{{ obj.missing_method() }}{% endmacro %}{{ foo(array_access) }}',
'Neither the property "missing_method" nor one of the methods "missing_method()", "getmissing_method()"/"ismissing_method()" or "__call()" exist and have public access in class "Twig_TemplateArrayAccessObject" in "%s" at line 1.'),
67 array(
'{{ magic_exception.test }}',
'An exception has been thrown during the rendering of a template ("Hey! Don\'t try to isset me!") in "%s" at line 1.'),
68 array(
'{{ object["a"] }}',
'Impossible to access a key "a" on an object of class "stdClass" that does not implement ArrayAccess interface in "%s" at line 1.'),
77 $twig =
new Twig_Environment($this->getMockBuilder(
'Twig_LoaderInterface')->getMock());
83 $template->getAttribute($object, $item, array(),
'any');
88 $this->addToAssertionCount(1);
94 $this->addToAssertionCount(1);
97 $this->assertContains(
'is not allowed', $e->getMessage());
117 $twig =
new Twig_Environment($this->getMockBuilder(
'Twig_TemplateTestLoaderInterface')->getMock());
123 $this->assertInstanceof(
'Twig_Markup',
$template->getAttribute($template1,
'string'));
124 $this->assertEquals(
'some_string',
$template->getAttribute($template1,
'string'));
126 $this->assertInstanceof(
'Twig_Markup',
$template->getAttribute($template1,
'true'));
127 $this->assertEquals(
'1',
$template->getAttribute($template1,
'true'));
129 $this->assertInstanceof(
'Twig_Markup',
$template->getAttribute($template1,
'zero'));
130 $this->assertEquals(
'0',
$template->getAttribute($template1,
'zero'));
132 $this->assertNotInstanceof(
'Twig_Markup',
$template->getAttribute($template1,
'empty'));
133 $this->assertSame(
'',
$template->getAttribute($template1,
'empty'));
165 $twig =
new Twig_Environment($this->getMockBuilder(
'Twig_TemplateTestLoaderInterface')->getMock());
171 $this->assertInstanceof(
'Twig_Markup',
$template->getAttribute($template1,
'string'));
172 $this->assertEquals(
'some_string',
$template->getAttribute($template1,
'string'));
174 $this->assertInstanceof(
'Twig_Markup',
$template->getAttribute($template1,
'true'));
175 $this->assertEquals(
'1',
$template->getAttribute($template1,
'true'));
177 $this->assertInstanceof(
'Twig_Markup',
$template->getAttribute($template1,
'zero'));
178 $this->assertEquals(
'0',
$template->getAttribute($template1,
'zero'));
180 $this->assertNotInstanceof(
'Twig_Markup',
$template->getAttribute($template1,
'empty'));
181 $this->assertSame(
'',
$template->getAttribute($template1,
'empty'));
183 $blocks = array(
'name' => array($template1,
'block_name'));
192 $template->getAttribute($template1,
'renderBlock', array(
'name', array(), $blocks));
193 $template->getAttribute($template1,
'displayBlock', array(
'name', array(), $blocks));
194 $template->getAttribute($template1,
'hasBlock', array(
'name', array()));
195 $template->getAttribute($template1,
'render', array(array()));
196 $template->getAttribute($template1,
'display', array(array()));
211 $twig =
new Twig_Environment($this->getMockBuilder(
'Twig_TemplateTestLoaderInterface')->getMock());
214 $template->renderBlock(
'unknown', array());
215 $template->displayBlock(
'unknown', array());
222 $array = array(
'Zero',
'One', -1 =>
'MinusOne',
'' =>
'EmptyString',
'1.5' =>
'FloatButString',
'01' =>
'IntegerButStringWithLeadingZeros');
224 $this->assertSame(
'Zero', $array[
false]);
225 $this->assertSame(
'One', $array[
true]);
226 $this->assertSame(
'One', $array[1.5]);
227 $this->assertSame(
'One', $array[
'1']);
228 $this->assertSame(
'MinusOne', $array[-1.5]);
229 $this->assertSame(
'FloatButString', $array[
'1.5']);
230 $this->assertSame(
'IntegerButStringWithLeadingZeros', $array[
'01']);
231 $this->assertSame(
'EmptyString', $array[
null]);
233 $this->assertSame(
'Zero',
$template->getAttribute($array,
false),
'false is treated as 0 when accessing an array (equals PHP behavior)');
234 $this->assertSame(
'One',
$template->getAttribute($array,
true),
'true is treated as 1 when accessing an array (equals PHP behavior)');
235 $this->assertSame(
'One',
$template->getAttribute($array, 1.5),
'float is casted to int when accessing an array (equals PHP behavior)');
236 $this->assertSame(
'One',
$template->getAttribute($array,
'1'),
'"1" is treated as integer 1 when accessing an array (equals PHP behavior)');
237 $this->assertSame(
'MinusOne',
$template->getAttribute($array, -1.5),
'negative float is casted to int when accessing an array (equals PHP behavior)');
238 $this->assertSame(
'FloatButString',
$template->getAttribute($array,
'1.5'),
'"1.5" is treated as-is when accessing an array (equals PHP behavior)');
239 $this->assertSame(
'IntegerButStringWithLeadingZeros',
$template->getAttribute($array,
'01'),
'"01" is treated as-is when accessing an array (equals PHP behavior)');
240 $this->assertSame(
'EmptyString',
$template->getAttribute($array,
null),
'null is treated as "" when accessing an array (equals PHP behavior)');
250 $this->assertEquals($value,
$template->getAttribute($object, $item, $arguments,
$type));
261 $this->assertEquals($value,
$template->getAttribute($object, $item, $arguments,
$type));
263 if (method_exists($this,
'expectException')) {
264 $this->expectException(
'Twig_Error_Runtime');
265 if (
null !== $exceptionMessage) {
266 $this->expectExceptionMessage($exceptionMessage);
269 $this->setExpectedException(
'Twig_Error_Runtime', $exceptionMessage);
271 $this->assertEquals($value,
$template->getAttribute($object, $item, $arguments,
$type));
282 $this->assertEquals($defined,
$template->getAttribute($object, $item, $arguments,
$type,
true));
292 $this->assertEquals($defined,
$template->getAttribute($object, $item, $arguments,
$type,
true));
301 $this->assertNull(
$template->getAttribute($object,
'foo'));
307 'defined' =>
'defined',
318 $stdObject = (object) $array;
333 array(
true,
'defined',
'defined'),
334 array(
false,
null,
'undefined'),
335 array(
false,
null,
'protected'),
336 array(
true, 0,
'zero'),
339 array(
true,
null,
'null'),
340 array(
true,
true,
'bar'),
341 array(
true,
'baz',
'baz'),
342 array(
true,
'09',
'09'),
343 array(
true,
'+4',
'+4'),
345 $testObjects = array(
347 array($array, $arrayType),
348 array($objectArray, $arrayType),
349 array($stdObject, $anyType),
350 array($magicPropertyObject, $anyType),
351 array($methodObject, $methodType),
352 array($methodObject, $anyType),
353 array($propertyObject, $anyType),
354 array($propertyObject1, $anyType),
355 array($propertyObject2, $anyType),
359 foreach ($testObjects as $testObject) {
360 foreach ($basicTests as
$test) {
366 if (
'+4' ===
$test[2] && $methodObject === $testObject[0]) {
376 array(
true,
null, $propertyObject3,
'foo', array(), $anyType),
381 array(
true,
'defined', $methodObject,
'defined', array(), $methodType),
382 array(
true,
'defined', $methodObject,
'DEFINED', array(), $methodType),
383 array(
true,
'defined', $methodObject,
'getDefined', array(), $methodType),
384 array(
true,
'defined', $methodObject,
'GETDEFINED', array(), $methodType),
385 array(
true,
'static', $methodObject,
'static', array(), $methodType),
386 array(
true,
'static', $methodObject,
'getStatic', array(), $methodType),
388 array(
true,
'__call_undefined', $magicMethodObject,
'undefined', array(), $methodType),
389 array(
true,
'__call_UNDEFINED', $magicMethodObject,
'UNDEFINED', array(), $methodType),
394 if ($anyType !==
$test[5]) {
404 array(
true,
'a', $methodAndPropObject,
'a', array(), $anyType),
405 array(
true,
'a', $methodAndPropObject,
'a', array(), $methodType),
406 array(
false,
null, $methodAndPropObject,
'a', array(), $arrayType),
408 array(
true,
'b_prop', $methodAndPropObject,
'b', array(), $anyType),
409 array(
true,
'b', $methodAndPropObject,
'B', array(), $anyType),
410 array(
true,
'b', $methodAndPropObject,
'b', array(), $methodType),
411 array(
true,
'b', $methodAndPropObject,
'B', array(), $methodType),
412 array(
false,
null, $methodAndPropObject,
'b', array(), $arrayType),
414 array(
false,
null, $methodAndPropObject,
'c', array(), $anyType),
415 array(
false,
null, $methodAndPropObject,
'c', array(), $methodType),
416 array(
false,
null, $methodAndPropObject,
'c', array(), $arrayType),
421 array(
false,
null, 42,
'a', array(), $anyType,
'Impossible to access an attribute ("a") on a integer variable ("42") in "index.twig".'),
422 array(
false,
null,
'string',
'a', array(), $anyType,
'Impossible to access an attribute ("a") on a string variable ("string") in "index.twig".'),
423 array(
false,
null, array(),
'a', array(), $anyType,
'Key "a" does not exist as the array is empty in "index.twig".'),
437 $this->assertNull(
$template->getAttribute($getIsObject,
'get'));
439 $this->assertNull(
$template->getAttribute($getIsObject, 0));
449 parent::__construct(
$env);
450 self::$cache = array();
466 return 'some_string';
495 if (function_exists(
'twig_template_get_attributes')) {
496 return twig_template_get_attributes($this, $object, $item, $arguments,
$type, $isDefinedTest, $ignoreStrictCheck);
498 return parent::getAttribute($object, $item, $arguments,
$type, $isDefinedTest, $ignoreStrictCheck);
512 'defined' =>
'defined',
524 return array_key_exists(
$name, $this->attributes);
529 return array_key_exists(
$name, $this->attributes) ? $this->attributes[
$name] :
null;
559 return array_key_exists(
$name, $this->attributes);
564 return array_key_exists(
$name, $this->attributes) ? $this->attributes[
$name] :
null;
572 throw new Exception(
'Hey! Don\'t try to isset me!');
591 return new ArrayIterator(array(
'foo',
'bar'));
598 'defined' =>
'defined',
607 return array_key_exists($offset, $this->
data);
630 $this->foo = @$notExist;
667 return 'should never be returned';
688 public function get()
699 private $a =
'a_prop';
706 public $b =
'b_prop';
713 private $c =
'c_prop';
723 public function __call($method, $arguments)
725 return '__call_'.$method;
731 public function __call($method, $arguments)
733 throw new BadMethodCallException(sprintf(
'Unknown method "%s".', $method));
742 $node->setAttribute(
'disable_c_ext',
true);
An exception for terminatinating execution or to throw for unit testing.
getPriority()
Returns the priority for this visitor.
enterNode(Twig_NodeInterface $node, Twig_Environment $env)
Called before child nodes are visited.
leaveNode(Twig_NodeInterface $node, Twig_Environment $env)
Called after child nodes are visited.
Stores the Twig configuration.
Exception thrown when an error occurs at runtime.
Loads a template from an array.
Exception thrown when a security error occurs at runtime.
Represents a security policy which need to be enforced when sandbox mode is enabled.
__call($method, $arguments)
__call($method, $arguments)
offsetSet($offset, $value)
block_name($context, array $blocks=array())
getAttribute($object, $item, array $arguments=array(), $type=Twig_Template::ANY_CALL, $isDefinedTest=false, $ignoreStrictCheck=false)
Returns the attribute value for a given array/object.
getTemplateName()
Returns the template name.
__construct(Twig_Environment $env, $name='index.twig')
doDisplay(array $context, array $blocks=array())
Auto-generated method to display the template with the given context.
getDebugInfo()
Returns debug information about the template.
doGetParent(array $context)
Default base class for compiled templates.
testGetAttribute($defined, $value, $object, $item, $arguments, $type)
@dataProvider getGetAttributeTests
testGetAttributeWithTemplateAsObject()
@group legacy
testGetAttributeWithSandbox($object, $item, $allowed)
@dataProvider getGetAttributeWithSandbox
testGetAttributeOnArrayWithConfusableKey()
testGetAttributeWithTemplateAsObjectForDeprecations()
@group legacy @expectedDeprecation Calling "string" on template "index1.twig" from template "index....
testDisplayBlocksAcceptTemplateOnlyAsBlocks()
@expectedException LogicException
testGetAttributeDefinedStrict($defined, $value, $object, $item, $arguments, $type)
@dataProvider getGetAttributeTests
testGetAttributeDefined($defined, $value, $object, $item, $arguments, $type)
@dataProvider getGetAttributeTests
testGetAttributeCallExceptions()
testGetAttributeStrict($defined, $value, $object, $item, $arguments, $type, $exceptionMessage=null)
@dataProvider getGetAttributeTests
testRenderBlockWithUndefinedBlock()
@group legacy @expectedDeprecation Silent display of undefined block "unknown" in template "index....
testGetAttributeExceptions($template, $message)
@dataProvider getAttributeExceptions
getGetAttributeWithSandbox()
testGetIsMethods()
@expectedException Twig_Error_Runtime
Interface all loaders must implement.
Represents a node in the AST.
Twig_NodeVisitorInterface is the interface the all node visitor classes must implement.
Adds a getSourceContext() method for loaders.
catch(Exception $e) $message