18 $template = $this->getMockForAbstractClass(
'Twig_Template',
array(),
'',
false);
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());
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'));
211 $twig =
new Twig_Environment($this->getMockBuilder(
'Twig_TemplateTestLoaderInterface')->getMock());
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]) {
370 $tests[] =
array($test[0], $test[1], $testObject[0], $test[2],
array(), $testObject[1]);
375 $tests = array_merge($tests,
array(
376 array(
true, null, $propertyObject3,
'foo',
array(), $anyType),
380 $tests = array_merge($tests,
array(
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),
393 foreach ($tests as
$test) {
394 if ($anyType !== $test[5]) {
403 $tests = array_merge($tests,
array(
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),
420 $tests = array_merge($tests,
array(
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);
509 protected $protected =
'protected';
512 'defined' =>
'defined',
524 return array_key_exists(
$name, $this->attributes);
529 return array_key_exists(
$name, $this->attributes) ? $this->attributes[
$name] : null;
543 public $defined =
'defined';
555 protected $protected =
'protected';
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!');
578 public $defined =
'defined';
584 protected $protected =
'protected';
598 'defined' =>
'defined',
607 return array_key_exists($offset, $this->
data);
612 return $this->offsetExists($offset) ? $this->
data[$offset] :
'n/a';
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)
742 $node->setAttribute(
'disable_c_ext',
true);
Represents a node in the AST.
testGetAttributeDefined($defined, $value, $object, $item, $arguments, $type)
getGetAttributeTests
Exception thrown when a security error occurs at runtime.
getAttribute($object, $item, array $arguments=array(), $type=Twig_Template::ANY_CALL, $isDefinedTest=false, $ignoreStrictCheck=false)
__call($method, $arguments)
getGetAttributeWithSandbox()
block_name($context, array $blocks=array())
Exception thrown when an error occurs at runtime.
testGetAttributeWithTemplateAsObject()
legacy
testGetAttributeStrict($defined, $value, $object, $item, $arguments, $type, $exceptionMessage=null)
getGetAttributeTests
doDisplay(array $context, array $blocks=array())
testGetAttributeWithSandbox($object, $item, $allowed)
getGetAttributeWithSandbox
testRenderBlockWithUndefinedBlock()
legacy Silent display of undefined block "unknown" in template "index.twig" is deprecated since vers...
__construct(Twig_Environment $env, $name='index.twig')
catch(Exception $e) $message
Twig_NodeVisitorInterface is the interface the all node visitor classes must implement.
leaveNode(Twig_NodeInterface $node, Twig_Environment $env)
Called after child nodes are visited.
testGetAttributeExceptions($template, $message)
getAttributeExceptions
doGetParent(array $context)
__call($method, $arguments)
Adds a getSourceContext() method for loaders.
enterNode(Twig_NodeInterface $node, Twig_Environment $env)
Called before child nodes are visited.
testGetAttributeCallExceptions()
Create styles array
The data for the language used.
Default base class for compiled templates.
getPriority()
Returns the priority for this visitor.
testGetAttributeDefinedStrict($defined, $value, $object, $item, $arguments, $type)
getGetAttributeTests
Create new PHPExcel object
obj_idprivate
Represents a security policy which need to be enforced when sandbox mode is enabled.
testGetAttributeOnArrayWithConfusableKey()
Stores the Twig configuration.
offsetSet($offset, $value)
testGetAttribute($defined, $value, $object, $item, $arguments, $type)
getGetAttributeTests
Interface all loaders must implement.
Loads a template from an array.
testDisplayBlocksAcceptTemplateOnlyAsBlocks()
LogicException
testGetIsMethods()
Twig_Error_Runtime
testGetAttributeWithTemplateAsObjectForDeprecations()
legacy Calling "string" on template "index1.twig" from template "index.twig" is deprecated since ver...