You are here

class LengthIndicatorTest in Length Indicator 8

Same name in this branch
  1. 8 tests/src/FunctionalJavascript/LengthIndicatorTest.php \Drupal\Tests\length_indicator\FunctionalJavascript\LengthIndicatorTest
  2. 8 tests/src/Unit/LengthIndicatorTest.php \Drupal\Tests\length_indicator\Unit\LengthIndicatorTest

Tests the code in length_indicator.module.

@group length_indicator

Note we load code, so isolate the test.

@runInSeparateProcess @preserveGlobalState disabled

Hierarchy

Expanded class hierarchy of LengthIndicatorTest

File

tests/src/Unit/LengthIndicatorTest.php, line 18

Namespace

Drupal\Tests\length_indicator\Unit
View source
class LengthIndicatorTest extends UnitTestCase {

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    include_once __DIR__ . '/../../../length_indicator.module';
  }

  /**
   * Tests _length_indicator_get_width_and_pos().
   *
   * @dataProvider providerTestLengthIndicatorGetWidthAndPos
   */
  public function testLengthIndicatorGetWidthAndPos($optimin, $optimax, $tolerance, array $expected) {
    $service_length_indicator = new GetWidthPos();
    $this
      ->assertEquals($expected, $service_length_indicator
      ->getWidthAndPosition($optimin, $optimax, $tolerance));
  }

  /**
   * Data provider for testLengthIndicatorGetWidthAndPos().
   *
   * @return array
   *   An array with of arguments for testLengthIndicatorGetWidthAndPos().
   */
  public function providerTestLengthIndicatorGetWidthAndPos() {
    return [
      [
        10,
        15,
        5,
        [
          [
            'width' => 20.0,
            'pos' => 0,
            'class' => 'length-indicator__indicator--bad',
          ],
          [
            'width' => 20.0,
            'pos' => 5,
            'class' => 'length-indicator__indicator--ok',
          ],
          [
            'width' => 20.0,
            'pos' => 10,
            'class' => 'length-indicator__indicator--good',
          ],
          [
            'width' => 20.0,
            'pos' => 16,
            'class' => 'length-indicator__indicator--ok',
          ],
          [
            'width' => 20.0,
            'pos' => 21,
            'class' => 'length-indicator__indicator--bad',
          ],
        ],
      ],
      'zero_tolerance' => [
        10,
        15,
        0,
        [
          [
            'width' => 40.0,
            'pos' => 0,
            'class' => 'length-indicator__indicator--bad',
          ],
          [
            'width' => 0.0,
            'pos' => 10,
            'class' => 'length-indicator__indicator--ok',
          ],
          [
            'width' => 20.0,
            'pos' => 10,
            'class' => 'length-indicator__indicator--good',
          ],
          [
            'width' => 0.0,
            'pos' => 16,
            'class' => 'length-indicator__indicator--ok',
          ],
          [
            'width' => 40.0,
            'pos' => 16,
            'class' => 'length-indicator__indicator--bad',
          ],
        ],
      ],
      [
        100,
        250,
        75,
        [
          [
            'width' => 7.142857142857142,
            'pos' => 0,
            'class' => 'length-indicator__indicator--bad',
          ],
          [
            'width' => 21.428571428571427,
            'pos' => 25,
            'class' => 'length-indicator__indicator--ok',
          ],
          [
            'width' => 42.85714285714286,
            'pos' => 100,
            'class' => 'length-indicator__indicator--good',
          ],
          [
            'width' => 21.42857142857143,
            'pos' => 251,
            'class' => 'length-indicator__indicator--ok',
          ],
          [
            'width' => 7.142857142857142,
            'pos' => 326,
            'class' => 'length-indicator__indicator--bad',
          ],
        ],
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LengthIndicatorTest::providerTestLengthIndicatorGetWidthAndPos public function Data provider for testLengthIndicatorGetWidthAndPos().
LengthIndicatorTest::setUp public function Overrides UnitTestCase::setUp
LengthIndicatorTest::testLengthIndicatorGetWidthAndPos public function Tests _length_indicator_get_width_and_pos().
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.
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.