class HtmxUtilitiesTest

Test all Htmx public utility methods.

Attributes

#[CoversClass(Htmx::class)] #[Group('Htmx')]

Hierarchy

Expanded class hierarchy of HtmxUtilitiesTest

File

core/tests/Drupal/Tests/Core/Htmx/HtmxUtilitiesTest.php, line 17

Namespace

Drupal\Tests\Core\Htmx
View source
class HtmxUtilitiesTest extends UnitTestCase {
  
  /**
   * Class under test.
   */
  protected Htmx $htmx;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    // Set up test values.
    $attributes = new Attribute([
      'apple' => 'orange',
      'banana' => 'grape',
    ]);
    $headers = new HeaderBag([
      'pear' => 'kiwi',
      'mango' => 'peach',
    ]);
    $this->htmx = new Htmx($attributes, $headers);
  }
  
  /**
   * Test ::hasHeader.
   */
  public function testHasHeader() : void {
    $this->assertTrue($this->htmx
      ->hasHeader('pear'));
    $this->assertTrue($this->htmx
      ->hasHeader('mango'));
    $this->assertFalse($this->htmx
      ->hasHeader('kiwi'));
  }
  
  /**
   * Test ::hasAttribute.
   */
  public function testHasAttribute() : void {
    $this->assertTrue($this->htmx
      ->hasAttribute('apple'));
    $this->assertTrue($this->htmx
      ->hasAttribute('banana'));
    $this->assertFalse($this->htmx
      ->hasAttribute('orange'));
  }
  
  /**
   * Test ::removeHeader.
   */
  public function testRemoveHeader() : void {
    $this->htmx
      ->removeHeader('pear');
    $this->assertFalse($this->htmx
      ->hasHeader('pear'));
  }
  
  /**
   * Test ::removeAttribute.
   */
  public function testRemoveAttribute() : void {
    $this->htmx
      ->removeAttribute('apple');
    $this->assertFalse($this->htmx
      ->hasAttribute('apple'));
  }
  
  /**
   * Test ::getAttributes.
   */
  public function testGetAttributes() : void {
    $attributes = $this->htmx
      ->getAttributes();
    $this->assertEquals(2, count($attributes->storage()));
  }
  
  /**
   * Test ::getHeaders().
   */
  public function testGetHeaders() : void {
    $headers = $this->htmx
      ->getHeaders();
    $this->assertEquals(2, $headers->count());
  }
  
  /**
   * Test ::applyTo with defaults.
   */
  public function testApplyTo() : void {
    $render = [
      '#attributes' => [
        'plum' => 'strawberry',
      ],
      '#attached' => [
        'http_header' => [
          [
            'melon',
            'watermelon',
            TRUE,
          ],
        ],
      ],
    ];
    $this->htmx
      ->applyTo($render);
    $this->assertTrue(isset($render['#attributes']));
    $this->assertTrue(isset($render['#attached']['http_header']));
    $this->assertTrue(isset($render['#attached']['library']));
    // We added 2 attributes and 2 headers.
    $this->assertEquals(3, count($render['#attributes']));
    $this->assertEquals(3, count($render['#attached']['http_header']));
    $this->assertEquals([
      'core/drupal.htmx',
    ], $render['#attached']['library']);
  }
  
  /**
   * Test ::applyTo with attribute key..
   */
  public function testApplyToWithKey() : void {
    $render = [];
    $this->htmx
      ->applyTo($render, '#wrapper_attributes');
    $this->assertTrue(isset($render['#wrapper_attributes']));
    $this->assertEquals(2, count($render['#wrapper_attributes']));
  }
  
  /**
   * Test ::createFromRenderArray.
   */
  public function testCreateFromRenderArray() : void {
    $source = [
      '#attributes' => [
        'data-hx-apple' => 'orange',
        'banana' => 'grape',
      ],
      '#attached' => [
        'http_header' => [
          [
            'pear',
            'kiwi',
            TRUE,
          ],
          [
            'hx-mango',
            'peach',
            TRUE,
          ],
        ],
      ],
      '#cache' => [
        'tags' => [
          'node:3',
          'node:12',
        ],
      ],
    ];
    $htmx = Htmx::createFromRenderArray($source);
    $this->assertTrue($htmx->hasAttribute('data-hx-apple'));
    $this->assertFalse($htmx->hasAttribute('banana'));
    $this->assertTrue($htmx->hasHeader('hx-mango'));
    $this->assertFalse($htmx->hasHeader('pear'));
    $render = [];
    $htmx->applyTo($render);
    $this->assertEquals(1, count($render['#attributes']));
    $this->assertEquals(1, count($render['#attached']['http_header']));
    $this->assertEquals([
      'node:3',
      'node:12',
    ], $render['#cache']['tags']);
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
HtmxUtilitiesTest::$htmx protected property Class under test.
HtmxUtilitiesTest::setUp protected function Overrides UnitTestCase::setUp
HtmxUtilitiesTest::testApplyTo public function Test ::applyTo with defaults.
HtmxUtilitiesTest::testApplyToWithKey public function Test ::applyTo with attribute key..
HtmxUtilitiesTest::testCreateFromRenderArray public function Test ::createFromRenderArray.
HtmxUtilitiesTest::testGetAttributes public function Test ::getAttributes.
HtmxUtilitiesTest::testGetHeaders public function Test ::getHeaders().
HtmxUtilitiesTest::testHasAttribute public function Test ::hasAttribute.
HtmxUtilitiesTest::testHasHeader public function Test ::hasHeader.
HtmxUtilitiesTest::testRemoveAttribute public function Test ::removeAttribute.
HtmxUtilitiesTest::testRemoveHeader public function Test ::removeHeader.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.
UnitTestCase::setupMockIterator protected function Set up a traversable class mock to return specific items when iterated.

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