Files
Twig/tests/Fixtures/expressions/array.test
2025-03-21 09:42:17 +01:00

165 lines
2.9 KiB
Plaintext

--TEST--
Twig supports array notation
--TEMPLATE--
{# empty array #}
{{ []|join(',') }}
{{ [1, 2]|join(',') }}
{{ ['foo', "bar"]|join(',') }}
{{ {0: 1, 'foo': 'bar'}|join(',') }}
{{ {0: 1, 'foo': 'bar'}|keys|join(',') }}
{{ {0: 1, foo: 'bar'}|join(',') }}
{{ {0: 1, foo: 'bar'}|keys|join(',') }}
{# nested arrays #}
{% set a = [1, 2, [1, 2], {'foo': {'foo': 'bar'}}] %}
{{ a[2]|join(',') }}
{{ a[3]["foo"]|join(',') }}
{# works even if [] is used inside the array #}
{{ [foo[bar]]|join(',') }}
{# elements can be any expression #}
{{ ['foo'|upper, bar|upper, bar == foo]|join(',') }}
{# arrays can have a trailing , like in PHP #}
{{
[
1,
2,
]|join(',')
}}
{# keys can be any expression #}
{% set a = 1 %}
{% set b = "foo" %}
{% set markup_instance %}fooe{% endset %}
{% set ary = { (a): 'a', (b): 'b', 'c': 'c', (a ~ b): 'd', (markup_instance): 'e' } %}
{{ ary|keys|join(',') }}
{{ ary|join(',') }}
{# ArrayAccess #}
{{ array_access['a'] }}
{# ObjectStorage #}
{{ object_storage[object] }}
{{ object_storage[object_storage]|default('bar') }}
{# array that does not exist #}
{{ does_not_exist[0]|default('ok') }}
{{ does_not_exist[0].does_not_exist_either|default('ok') }}
{{ does_not_exist[0]['does_not_exist_either']|default('ok') }}
{# indexes are kept #}
{% set trad = {194:'ABC',141:'DEF',100:'GHI',170:'JKL',110:'MNO',111:'PQR'} %}
{% set trad2 = {'194':'ABC','141':'DEF','100':'GHI','170':'JKL','110':'MNO','111':'PQR'} %}
{{ trad == trad2 ? 'OK' : 'KO' }}
{% set trad = {11: 'ABC', 2: 'DEF', 4: 'GHI', 3: 'JKL'} %}
{% set trad2 = {'11': 'ABC', '2': 'DEF', '4': 'GHI', '3': 'JKL'} %}
{{ trad == trad2 ? 'OK' : 'KO' }}
{# indexes are kept #}
{{ { 1: "first", 0: "second" } == { '1': "first", '0': "second" } ? 'OK' : 'KO' }}
{{ { 1: "first", 0: "second" } == indices_1 ? 'OK' : 'KO' }}
{{ { 1: "first", 'foo': "second", 2: "third" } == { '1': "first", 'foo': "second", '2': "third" } ? 'OK' : 'KO' }}
{{ { 1: "first", 'foo': "second", 2: "third" } == indices_2 ? 'OK' : 'KO' }}
--DATA--
$objectStorage = new SplObjectStorage();
$object = new stdClass();
$objectStorage[$object] = 'foo';
return [
'bar' => 'bar',
'foo' => ['bar' => 'bar'],
'array_access' => new \ArrayObject(['a' => 'b']),
'object_storage' => $objectStorage,
'object' => $object,
'indices_1' => [ 1 => 'first', 0 => 'second' ],
'indices_2' => [ 1 => 'first', 'foo' => 'second', 2 => 'third' ],
]
--EXPECT--
1,2
foo,bar
1,bar
0,foo
1,bar
0,foo
1,2
bar
bar
FOO,BAR,
1,2
1,foo,c,1foo,fooe
a,b,c,d,e
b
foo
bar
ok
ok
ok
OK
OK
OK
OK
OK
OK
--DATA--
return [
'bar' => 'bar',
'foo' => ['bar' => 'bar'],
'array_access' => new \ArrayObject(['a' => 'b']),
'object' => new stdClass(),
'indices_1' => [ 1 => 'first', 0 => 'second' ],
'indices_2' => [ 1 => 'first', 'foo' => 'second', 2 => 'third' ],
]
--CONFIG--
return ['strict_variables' => false]
--EXPECT--
1,2
foo,bar
1,bar
0,foo
1,bar
0,foo
1,2
bar
bar
FOO,BAR,
1,2
1,foo,c,1foo,fooe
a,b,c,d,e
b
bar
ok
ok
ok
OK
OK
OK
OK
OK
OK