You are here

public function Twig_Tests_TemplateTest::testGetAttributeOnArrayWithConfusableKey in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/twig/twig/test/Twig/Tests/TemplateTest.php \Twig_Tests_TemplateTest::testGetAttributeOnArrayWithConfusableKey()

@dataProvider getTestsDependingOnExtensionAvailability

File

vendor/twig/twig/test/Twig/Tests/TemplateTest.php, line 180

Class

Twig_Tests_TemplateTest

Code

public function testGetAttributeOnArrayWithConfusableKey($useExt = false) {
  $template = new Twig_TemplateTest(new Twig_Environment($this
    ->getMock('Twig_LoaderInterface')), $useExt);
  $array = array(
    'Zero',
    'One',
    -1 => 'MinusOne',
    '' => 'EmptyString',
    '1.5' => 'FloatButString',
    '01' => 'IntegerButStringWithLeadingZeros',
  );
  $this
    ->assertSame('Zero', $array[false]);
  $this
    ->assertSame('One', $array[true]);
  $this
    ->assertSame('One', $array[1.5]);
  $this
    ->assertSame('One', $array['1']);
  $this
    ->assertSame('MinusOne', $array[-1.5]);
  $this
    ->assertSame('FloatButString', $array['1.5']);
  $this
    ->assertSame('IntegerButStringWithLeadingZeros', $array['01']);
  $this
    ->assertSame('EmptyString', $array[null]);
  $this
    ->assertSame('Zero', $template
    ->getAttribute($array, false), 'false is treated as 0 when accessing an array (equals PHP behavior)');
  $this
    ->assertSame('One', $template
    ->getAttribute($array, true), 'true is treated as 1 when accessing an array (equals PHP behavior)');
  $this
    ->assertSame('One', $template
    ->getAttribute($array, 1.5), 'float is casted to int when accessing an array (equals PHP behavior)');
  $this
    ->assertSame('One', $template
    ->getAttribute($array, '1'), '"1" is treated as integer 1 when accessing an array (equals PHP behavior)');
  $this
    ->assertSame('MinusOne', $template
    ->getAttribute($array, -1.5), 'negative float is casted to int when accessing an array (equals PHP behavior)');
  $this
    ->assertSame('FloatButString', $template
    ->getAttribute($array, '1.5'), '"1.5" is treated as-is when accessing an array (equals PHP behavior)');
  $this
    ->assertSame('IntegerButStringWithLeadingZeros', $template
    ->getAttribute($array, '01'), '"01" is treated as-is when accessing an array (equals PHP behavior)');
  $this
    ->assertSame('EmptyString', $template
    ->getAttribute($array, null), 'null is treated as "" when accessing an array (equals PHP behavior)');
}