You are here

public function AttributeTest::testConstructor in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Template/AttributeTest.php \Drupal\Tests\Core\Template\AttributeTest::testConstructor()
  2. 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 24

Class

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

Namespace

Drupal\Tests\Core\Template

Code

public function testConstructor() {
  $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']);
}