You are here

class RemoveHtmlCommentsTest in Twig - Remove HTML comments 8

Tests the RemoveHtmlComments class.

@package Drupal\Tests\twig_remove_html_comments\Unit @coversDefaultClass \Drupal\twig_remove_html_comments\TwigExtension\RemoveHtmlComments @group twig_remove_html_comments

Hierarchy

Expanded class hierarchy of RemoveHtmlCommentsTest

File

tests/Unit/RemoveHtmlCommentsTest.php, line 16

Namespace

Drupal\Tests\twig_remove_html_comments\Unit
View source
class RemoveHtmlCommentsTest extends UnitTestCase {

  /**
   * The object to test.
   *
   * @var \Drupal\twig_remove_html_comments\TwigExtension\RemoveHtmlComments
   */
  protected $removeHtmlComments;

  /**
   * Sets up to object to test.
   */
  protected function setUp() : void {
    parent::setUp();
    $this->removeHtmlComments = new RemoveHtmlComments();
  }

  /**
   * @covers ::getName
   */
  public function testGetName() : void {
    $this
      ->assertTrue(!empty($this->removeHtmlComments
      ->getName()));
  }

  /**
   * @covers ::getFilters
   */
  public function testGetFilters() : void {
    $filters = $this->removeHtmlComments
      ->getFilters();
    $this
      ->assertTrue(is_array($filters));
    foreach ($filters as $filter) {
      $this
        ->assertTrue($filter instanceof TwigFilter);
    }
  }

  /**
   * @covers ::removeHtmlCommentsFromString
   */
  public function testRemoveHtmlCommentsFromString() : void {
    $stringWithSingleComment = '<!-- This is a HTML comment --><p>HTML p tag</p><h1>HTML 1 tag</h1>';
    $this
      ->assertEquals('<p>HTML p tag</p><h1>HTML 1 tag</h1>', $this->removeHtmlComments
      ->removeHtmlCommentsFromString($stringWithSingleComment));
    $stringWithMultipleComments = '<!-- This is a HTML comment --><p>HTML p tag</p><h1>HTML 1 tag</h1><!-- This is another HTML comment -->';
    $this
      ->assertEquals('<p>HTML p tag</p><h1>HTML 1 tag</h1>', $this->removeHtmlComments
      ->removeHtmlCommentsFromString($stringWithMultipleComments));
    $stringWithoutComments = '<span>This is a normal string without any HTML comments</span>';
    $this
      ->assertEquals('<span>This is a normal string without any HTML comments</span>', $this->removeHtmlComments
      ->removeHtmlCommentsFromString($stringWithoutComments));
    $stringWithoutHTML = 'This is a string without any HTML';
    $this
      ->assertEquals('This is a string without any HTML', $this->removeHtmlComments
      ->removeHtmlCommentsFromString($stringWithoutHTML));
    $this
      ->assertEquals('', $this->removeHtmlComments
      ->removeHtmlCommentsFromString(NULL));
  }

  /**
   * @covers ::removeHtmlCommentsAsRenderArray
   */
  public function testRemoveHtmlCommentsAsRenderArray() : void {
    $stringWithSingleComment = '<!-- This is a HTML comment --><p>HTML p tag</p><h1>HTML 1 tag</h1>';
    $this
      ->assertEquals([
      '#markup' => '<p>HTML p tag</p><h1>HTML 1 tag</h1>',
    ], $this->removeHtmlComments
      ->removeHtmlCommentsAsRenderArray($stringWithSingleComment));
    $stringWithMultipleComments = '<!-- This is a HTML comment --><p>HTML p tag</p><h1>HTML 1 tag</h1><!-- This is another HTML comment -->';
    $this
      ->assertEquals([
      '#markup' => '<p>HTML p tag</p><h1>HTML 1 tag</h1>',
    ], $this->removeHtmlComments
      ->removeHtmlCommentsAsRenderArray($stringWithMultipleComments));
    $stringWithoutComments = '<span>This is a normal string without any HTML comments</span>';
    $this
      ->assertEquals([
      '#markup' => '<span>This is a normal string without any HTML comments</span>',
    ], $this->removeHtmlComments
      ->removeHtmlCommentsAsRenderArray($stringWithoutComments));
    $simpleStringWithoutHTML = 'This is a string without any HTML';
    $this
      ->assertEquals([
      '#markup' => 'This is a string without any HTML',
    ], $this->removeHtmlComments
      ->removeHtmlCommentsAsRenderArray($simpleStringWithoutHTML));
    $this
      ->assertEquals([
      '#markup' => '',
    ], $this->removeHtmlComments
      ->removeHtmlCommentsAsRenderArray(NULL));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
RemoveHtmlCommentsTest::$removeHtmlComments protected property The object to test.
RemoveHtmlCommentsTest::setUp protected function Sets up to object to test. Overrides UnitTestCase::setUp
RemoveHtmlCommentsTest::testGetFilters public function @covers ::getFilters
RemoveHtmlCommentsTest::testGetName public function @covers ::getName
RemoveHtmlCommentsTest::testRemoveHtmlCommentsAsRenderArray public function @covers ::removeHtmlCommentsAsRenderArray
RemoveHtmlCommentsTest::testRemoveHtmlCommentsFromString public function @covers ::removeHtmlCommentsFromString
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
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::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.