You are here

class NumberTest in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Component/Utility/NumberTest.php \Drupal\Tests\Component\Utility\NumberTest

Tests number manipulation utilities.

@group Utility

@coversDefaultClass \Drupal\Component\Utility\Number

Hierarchy

  • class \Drupal\Tests\Component\Utility\NumberTest extends \PHPUnit\Framework\TestCase

Expanded class hierarchy of NumberTest

See also

\Drupal\Component\Utility\Number

File

core/tests/Drupal/Tests/Component/Utility/NumberTest.php, line 17

Namespace

Drupal\Tests\Component\Utility
View source
class NumberTest extends TestCase {

  /**
   * Tests Number::validStep() without offset.
   *
   * @dataProvider providerTestValidStep
   * @covers ::validStep
   *
   * @param numeric $value
   *   The value argument for Number::validStep().
   * @param numeric $step
   *   The step argument for Number::validStep().
   * @param bool $expected
   *   Expected return value from Number::validStep().
   */
  public function testValidStep($value, $step, $expected) {
    $return = Number::validStep($value, $step);
    $this
      ->assertEquals($expected, $return);
  }

  /**
   * Tests Number::validStep() with offset.
   *
   * @dataProvider providerTestValidStepOffset
   * @covers ::validStep
   *
   * @param numeric $value
   *   The value argument for Number::validStep().
   * @param numeric $step
   *   The step argument for Number::validStep().
   * @param numeric $offset
   *   The offset argument for Number::validStep().
   * @param bool $expected
   *   Expected return value from Number::validStep().
   */
  public function testValidStepOffset($value, $step, $offset, $expected) {
    $return = Number::validStep($value, $step, $offset);
    $this
      ->assertEquals($expected, $return);
  }

  /**
   * Provides data for self::testNumberStep().
   *
   * @see \Drupal\Tests\Component\Utility\Number::testValidStep
   */
  public static function providerTestValidStep() {
    return [
      // Value and step equal.
      [
        10.3,
        10.3,
        TRUE,
      ],
      // Valid integer steps.
      [
        42,
        21,
        TRUE,
      ],
      [
        42,
        3,
        TRUE,
      ],
      // Valid float steps.
      [
        42,
        10.5,
        TRUE,
      ],
      [
        1,
        1 / 3,
        TRUE,
      ],
      [
        -100,
        100 / 7,
        TRUE,
      ],
      [
        1000,
        -10,
        TRUE,
      ],
      // Valid and very small float steps.
      [
        1000.12345,
        1.0E-10,
        TRUE,
      ],
      [
        3.9999999999999,
        1.0E-13,
        TRUE,
      ],
      // Invalid integer steps.
      [
        100,
        30,
        FALSE,
      ],
      [
        -10,
        4,
        FALSE,
      ],
      // Invalid float steps.
      [
        6,
        5 / 7,
        FALSE,
      ],
      [
        10.3,
        10.25,
        FALSE,
      ],
      // Step mismatches very close to being valid.
      [
        70 + 9.0E-7,
        10 + 9.0E-7,
        FALSE,
      ],
      [
        1936.5,
        3.0E-8,
        FALSE,
      ],
    ];
  }

  /**
   * Data provider for \Drupal\Tests\Component\Utility\NumberTest::testValidStepOffset().
   *
   * @see \Drupal\Tests\Component\Utility\NumberTest::testValidStepOffset()
   */
  public static function providerTestValidStepOffset() {
    return [
      // Try obvious fits.
      [
        11.3,
        10.3,
        1,
        TRUE,
      ],
      [
        100,
        10,
        50,
        TRUE,
      ],
      [
        -100,
        90 / 7,
        -10,
        TRUE,
      ],
      [
        2 / 7 + 5 / 9,
        1 / 7,
        5 / 9,
        TRUE,
      ],
      // Ensure a small offset is still invalid.
      [
        10.3,
        10.3,
        0.0001,
        FALSE,
      ],
      [
        1 / 5,
        1 / 7,
        1 / 11,
        FALSE,
      ],
      // Try negative values and offsets.
      [
        1000,
        10,
        -5,
        FALSE,
      ],
      [
        -10,
        4,
        0,
        FALSE,
      ],
      [
        -10,
        4,
        -4,
        FALSE,
      ],
    ];
  }

  /**
   * Tests the alphadecimal conversion functions.
   *
   * @dataProvider providerTestConversions
   * @covers ::intToAlphadecimal
   * @covers ::alphadecimalToInt
   *
   * @param int $value
   *   The integer value.
   * @param string $expected
   *   The expected alphadecimal value.
   */
  public function testConversions($value, $expected) {
    $this
      ->assertSame(Number::intToAlphadecimal($value), $expected);
    $this
      ->assertSame($value, Number::alphadecimalToInt($expected));
  }

  /**
   * Data provider for testConversions().
   *
   * @see testConversions()
   *
   * @return array
   *   An array containing:
   *     - The integer value.
   *     - The alphadecimal value.
   */
  public function providerTestConversions() {
    return [
      [
        0,
        '00',
      ],
      [
        1,
        '01',
      ],
      [
        10,
        '0a',
      ],
      [
        20,
        '0k',
      ],
      [
        35,
        '0z',
      ],
      [
        36,
        '110',
      ],
      [
        100,
        '12s',
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
NumberTest::providerTestConversions public function Data provider for testConversions().
NumberTest::providerTestValidStep public static function Provides data for self::testNumberStep().
NumberTest::providerTestValidStepOffset public static function Data provider for \Drupal\Tests\Component\Utility\NumberTest::testValidStepOffset().
NumberTest::testConversions public function Tests the alphadecimal conversion functions.
NumberTest::testValidStep public function Tests Number::validStep() without offset.
NumberTest::testValidStepOffset public function Tests Number::validStep() with offset.