You are here

class TocFormatterTest in TOC API 8

Tests TOC API formatter.

@group TocApi

@coversDefaultClass \Drupal\toc_api\TocFormatter

Hierarchy

Expanded class hierarchy of TocFormatterTest

File

tests/src/Unit/TocFormatterTest.php, line 20
Contains \Drupal\Tests\toc_api\Unit\TocFormatterTest.

Namespace

Drupal\Tests\toc_api\Unit
View source
class TocFormatterTest extends UnitTestCase {

  /**
   * The table of contents formatter.
   *
   * @var \Drupal\toc_api\TocFormatter
   */
  protected $formatter;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->formatter = new TocFormatter();
  }

  /**
   * Tests converting string to valid HTML id with TocFormatter::convertStringToId().
   *
   * @param string $string
   *   The string to run through $this->formatter->convertStringToId().
   * @param string $expected
   *   The expected result from calling the function.
   *
   * @see TocFormatter::convertStringToId()
   *
   * @dataProvider providerConvertStringToId
   */
  public function testConvertStringToId($string, $expected) {
    $result = $this->formatter
      ->convertStringToId($string);
    $this
      ->assertEquals($expected, $result);
  }

  /**
   * Data provider for testConvertStringToId().
   *
   * @see testConvertStringToId()
   */
  public function providerConvertStringToId() {
    $tests[] = [
      'One',
      'one',
    ];
    $tests[] = [
      'One   two',
      'one-two',
    ];
    $tests[] = [
      'One ! two',
      'one-two',
    ];
    $tests[] = [
      '--One ! two--',
      'one-two',
    ];
    $tests[] = [
      'Spécial characters',
      'special-characters',
    ];
    return $tests;
  }

  /**
   * Tests converting number to list style type with TocFormatter::convertNumberToListTypeValue().
   *
   * @param int $number
   *   The number to run through TocFormatter::convertNumberToListTypeValue().
   * @param string $type
   *   The type to run through $this->formatter->convertNumberToListTypeValue().
   * @param string $expected
   *   The expected result from calling the function.
   *
   * @see TocFormatter::convertNumberToListTypeValue()
   *
   * @dataProvider providerConvertNumberToListTypeValue
   */
  public function testConvertNumberToListTypeValue($number, $type, $expected) {
    $result = $this->formatter
      ->convertNumberToListTypeValue($number, $type);
    $this
      ->assertEquals($expected, $result);
  }

  /**
   * Data provider for testConvertNumberToListTypeValue().
   *
   * @see testConvertNumberToListTypeValue()
   */
  public function providerConvertNumberToListTypeValue() {
    $tests[] = [
      1,
      NULL,
      1,
    ];
    $tests[] = [
      1,
      'decimal',
      '1',
    ];
    $tests[] = [
      1,
      'random',
      '1',
    ];
    $tests[] = [
      0,
      'random',
      '0',
    ];
    $tests[] = [
      1,
      'lower-alpha',
      'a',
    ];
    $tests[] = [
      1,
      'upper-alpha',
      'A',
    ];
    $tests[] = [
      25,
      'lower-alpha',
      'y',
    ];
    $tests[] = [
      26,
      'lower-alpha',
      'z',
    ];
    $tests[] = [
      27,
      'lower-alpha',
      'a',
    ];
    $tests[] = [
      52,
      'lower-alpha',
      'z',
    ];
    $tests[] = [
      53,
      'lower-alpha',
      'a',
    ];
    $tests[] = [
      0,
      'lower-alpha',
      '0',
    ];
    $tests[] = [
      1,
      'lower-roman',
      'i',
    ];
    $tests[] = [
      1,
      'upper-roman',
      'I',
    ];
    $tests[] = [
      0,
      'lower-roman',
      '0',
    ];
    $tests[] = [
      4,
      'lower-roman',
      'iv',
    ];
    return $tests;
  }

  /**
   * Tests converting header keys to list style type values with TocFormatter::convertHeaderKeysToValues().
   *
   * @param array $keys
   *   The array to run through TocFormatter::convertHeaderKeysToValues().
   * @param array $options
   *   The array to run through TocFormatter::convertHeaderKeysToValues().
   * @param string $expected
   *   The expected result from calling the function.
   *
   * @see TocFormatter::convertHeaderKeysToValues()
   *
   * @dataProvider providerConvertHeaderKeysToValues
   */
  public function testConvertHeaderKeysToValues(array $keys, array $options, $expected) {
    $result = $this->formatter
      ->convertHeaderKeysToValues($keys, $options);
    $this
      ->assertEquals($expected, $result);
  }

  /**
   * Data provider for testConvertHeaderKeysToValues().
   *
   * @see testConvertHeaderKeysToValues()
   */
  public function providerConvertHeaderKeysToValues() {
    $options = [
      'number_path_truncate' => TRUE,
      'headers' => [
        'h1' => [
          'number_type' => 'decimal',
        ],
        'h2' => [
          'number_type' => 'lower-alpha',
        ],
        'h3' => [
          'number_type' => 'lower-roman',
        ],
      ],
    ];
    $tests[] = [
      [
        'h1' => 2,
        'h2' => 2,
        'h3' => 2,
      ],
      $options,
      [
        'h1' => '2',
        'h2' => 'b',
        'h3' => 'ii',
      ],
    ];
    $tests[] = [
      [
        'h1' => 0,
        'h2' => 2,
        'h3' => 2,
      ],
      $options,
      [
        'h2' => 'b',
        'h3' => 'ii',
      ],
    ];
    $tests[] = [
      [
        'h1' => 2,
        'h2' => 2,
        'h3' => 0,
      ],
      $options,
      [
        'h1' => '2',
        'h2' => 'b',
      ],
    ];
    $tests[] = [
      [
        'h1' => 2,
        'h2' => 2,
        'h3' => 0,
        'h4' => 0,
      ],
      $options,
      [
        'h1' => '2',
        'h2' => 'b',
      ],
    ];
    $tests[] = [
      [
        'h1' => 2,
        'h2' => 0,
        'h3' => 2,
      ],
      $options,
      [
        'h1' => '2',
        'h2' => '0',
        'h3' => 'ii',
      ],
    ];
    $tests[] = [
      [
        'h1' => 2,
        'h2' => 2,
        'h3' => 0,
      ],
      [
        'number_path_truncate' => FALSE,
      ] + $options,
      [
        'h1' => '2',
        'h2' => 'b',
        'h3' => '0',
      ],
    ];
    return $tests;
  }

}

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.
TocFormatterTest::$formatter protected property The table of contents formatter.
TocFormatterTest::providerConvertHeaderKeysToValues public function Data provider for testConvertHeaderKeysToValues().
TocFormatterTest::providerConvertNumberToListTypeValue public function Data provider for testConvertNumberToListTypeValue().
TocFormatterTest::providerConvertStringToId public function Data provider for testConvertStringToId().
TocFormatterTest::setUp protected function Overrides UnitTestCase::setUp
TocFormatterTest::testConvertHeaderKeysToValues public function Tests converting header keys to list style type values with TocFormatter::convertHeaderKeysToValues().
TocFormatterTest::testConvertNumberToListTypeValue public function Tests converting number to list style type with TocFormatter::convertNumberToListTypeValue().
TocFormatterTest::testConvertStringToId public function Tests converting string to valid HTML id with TocFormatter::convertStringToId().
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.