You are here

public function AttributeTest::testConstructor in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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 27
Contains \Drupal\Tests\Core\Template\AttributeTest.

Class

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

Namespace

Drupal\Tests\Core\Template

Code

public function testConstructor() {
  $attribute = new Attribute(array(
    'class' => array(
      'example-class',
    ),
  ));
  $this
    ->assertTrue(isset($attribute['class']));
  $this
    ->assertEquals(new AttributeArray('class', array(
    '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(array(
    'class' => 'example-class',
  ));
  $this
    ->assertTrue(isset($attribute['class']));
  $this
    ->assertEquals(new AttributeArray('class', array(
    '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(array(
    'class' => $safe_string
      ->reveal(),
  ));
  $this
    ->assertTrue(isset($attribute['class']));
  $this
    ->assertEquals(new AttributeArray('class', array(
    'example-class',
  )), $attribute['class']);
}