You are here

class YamlFormOptionsHelperTest in YAML Form 8

Tests form options utility.

@group YamlFormUnit

@coversDefaultClass \Drupal\yamlform\Utility\YamlFormOptionsHelper

Hierarchy

Expanded class hierarchy of YamlFormOptionsHelperTest

File

tests/src/Unit/YamlFormOptionsHelperTest.php, line 15

Namespace

Drupal\Tests\yamlform\Unit
View source
class YamlFormOptionsHelperTest extends UnitTestCase {

  /**
   * Tests YamlFormOptionsHelper::hasOption().
   *
   * @param string $value
   *   The value to run through YamlFormOptionsHelper::hasOption().
   * @param array $options
   *   The array to run through YamlFormOptionsHelper::hasOption().
   * @param string $expected
   *   The expected result from calling the function.
   *
   * @see YamlFormOptionsHelperl::hasOption()
   *
   * @dataProvider providerHasOption
   */
  public function testHasOption($value, array $options, $expected) {
    $result = YamlFormOptionsHelper::hasOption($value, $options);
    $this
      ->assertEquals($expected, $result);
  }

  /**
   * Data provider for testHasOption().
   *
   * @see testHasOption()
   */
  public function providerHasOption() {
    $tests[] = [
      'value',
      [
        'value' => 'text',
      ],
      TRUE,
    ];
    $tests[] = [
      'value',
      [],
      FALSE,
    ];
    $tests[] = [
      3,
      [
        1 => 'One',
        2 => 'Two',
        'optgroup' => [
          3 => 'Three',
        ],
      ],
      TRUE,
    ];
    $tests[] = [
      'optgroup',
      [
        1 => 'One',
        2 => 'Two',
        'optgroup' => [
          3 => 'Three',
        ],
      ],
      FALSE,
    ];
    return $tests;
  }

  /**
   * Tests YamlFormOptionsHelper::getOptionsText().
   *
   * @param array $values
   *   The array to run through YamlFormOptionsHelper::getOptionsText().
   * @param array $options
   *   The array to run through YamlFormOptionsHelper::getOptionsText().
   * @param string $expected
   *   The expected result from calling the function.
   *
   * @see YamlFormOptionsHelperl::getOptionsText()
   *
   * @dataProvider providerGetOptionsText
   */
  public function testGetOptionsText(array $values, array $options, $expected) {
    $result = YamlFormOptionsHelper::getOptionsText($values, $options);
    $this
      ->assertEquals($expected, $result);
  }

  /**
   * Data provider for testGetOptionsText().
   *
   * @see testGetOptionsText()
   */
  public function providerGetOptionsText() {
    $tests[] = [
      [
        'value',
      ],
      [
        'value' => 'text',
      ],
      [
        'text',
      ],
    ];
    $tests[] = [
      [
        1,
        3,
      ],
      [
        1 => 'One',
        2 => 'Two',
        'optgroup' => [
          3 => 'Three',
        ],
      ],
      [
        'One',
        'Three',
      ],
    ];
    return $tests;
  }

  /**
   * Tests YamlFormOptionsHelper::range().
   *
   * @param array $element
   *   The array to run through YamlFormOptionsHelper::range().
   * @param string $expected
   *   The expected result from calling the function.
   *
   * @see YamlFormOptionsHelperl::range()
   *
   * @dataProvider providerRange
   */
  public function testRange(array $element, $expected) {
    $element += [
      '#min' => 1,
      '#max' => 100,
      '#step' => 1,
      '#pad_length' => NULL,
      '#pad_str' => 0,
    ];
    $result = YamlFormOptionsHelper::range($element['#min'], $element['#max'], $element['#step'], $element['#pad_length'], $element['#pad_str']);
    $this
      ->assertEquals($expected, $result);
  }

  /**
   * Data provider for testRange().
   *
   * @see testRange()
   */
  public function providerRange() {
    $tests[] = [
      [
        '#min' => 1,
        '#max' => 3,
      ],
      [
        1 => 1,
        2 => 2,
        3 => 3,
      ],
    ];
    $tests[] = [
      [
        '#min' => 0,
        '#max' => 6,
        '#step' => 2,
      ],
      [
        0 => 0,
        2 => 2,
        4 => 4,
        6 => 6,
      ],
    ];
    $tests[] = [
      [
        '#min' => 'A',
        '#max' => 'C',
      ],
      [
        'A' => 'A',
        'B' => 'B',
        'C' => 'C',
      ],
    ];
    $tests[] = [
      [
        '#min' => 'a',
        '#max' => 'c',
      ],
      [
        'a' => 'a',
        'b' => 'b',
        'c' => 'c',
      ],
    ];
    $tests[] = [
      [
        '#min' => 1,
        '#max' => 3,
        '#step' => 1,
        '#pad_length' => 2,
        '#pad_str' => 0,
      ],
      [
        '01' => '01',
        '02' => '02',
        '03' => '03',
      ],
    ];
    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.
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.
UnitTestCase::setUp protected function 340
YamlFormOptionsHelperTest::providerGetOptionsText public function Data provider for testGetOptionsText().
YamlFormOptionsHelperTest::providerHasOption public function Data provider for testHasOption().
YamlFormOptionsHelperTest::providerRange public function Data provider for testRange().
YamlFormOptionsHelperTest::testGetOptionsText public function Tests YamlFormOptionsHelper::getOptionsText().
YamlFormOptionsHelperTest::testHasOption public function Tests YamlFormOptionsHelper::hasOption().
YamlFormOptionsHelperTest::testRange public function Tests YamlFormOptionsHelper::range().