function AttributeTest::testConstructor

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

Tests the constructor of the attribute class.

File

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

Class

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

Namespace

Drupal\Tests\Core\Template

Code

public function testConstructor() : void {
    $attribute = new Attribute([
        'class' => [
            'example-class',
        ],
    ]);
    $this->assertTrue(isset($attribute['class']));
    $this->assertEquals(new AttributeArray('class', [
        'example-class',
    ]), $attribute['class']);
    // Test adding boolean attributes through the constructor.
    $attribute = new Attribute([
        'selected' => TRUE,
        'checked' => FALSE,
    ]);
    $this->assertTrue($attribute['selected']->value());
    $this->assertFalse($attribute['checked']->value());
    // Test that non-array values with name "class" are cast to array.
    $attribute = new Attribute([
        'class' => 'example-class',
    ]);
    $this->assertTrue(isset($attribute['class']));
    $this->assertEquals(new AttributeArray('class', [
        'example-class',
    ]), $attribute['class']);
    // Test that safe string objects work correctly.
    $safe_string = $this->prophesize(MarkupInterface::class);
    $safe_string->__toString()
        ->willReturn('example-class');
    $attribute = new Attribute([
        'class' => $safe_string->reveal(),
    ]);
    $this->assertTrue(isset($attribute['class']));
    $this->assertEquals(new AttributeArray('class', [
        'example-class',
    ]), $attribute['class']);
}

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