function AttributeTest::testIterate

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Template/AttributeTest.php \Drupal\Tests\Core\Template\AttributeTest::testIterate()
  2. 8.9.x core/tests/Drupal/Tests/Core/Template/AttributeTest.php \Drupal\Tests\Core\Template\AttributeTest::testIterate()
  3. 10 core/tests/Drupal/Tests/Core/Template/AttributeTest.php \Drupal\Tests\Core\Template\AttributeTest::testIterate()

Tests iterating on the values of the attribute.

File

core/tests/Drupal/Tests/Core/Template/AttributeTest.php, line 329

Class

AttributeTest
@coversDefaultClass \Drupal\Core\Template\Attribute @group Template

Namespace

Drupal\Tests\Core\Template

Code

public function testIterate() : void {
    $attribute = new Attribute([
        'class' => [
            'example-class',
        ],
        'id' => 'example-id',
    ]);
    $counter = 0;
    foreach ($attribute as $key => $value) {
        if ($counter == 0) {
            $this->assertEquals('class', $key);
            $this->assertEquals(new AttributeArray('class', [
                'example-class',
            ]), $value);
        }
        if ($counter == 1) {
            $this->assertEquals('id', $key);
            $this->assertEquals(new AttributeString('id', 'example-id'), $value);
        }
        $counter++;
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.