You are here

class WebformArrayHelperTest in Webform 8.5

Same name and namespace in other branches
  1. 6.x tests/src/Unit/Utility/WebformArrayHelperTest.php \Drupal\Tests\webform\Unit\Utility\WebformArrayHelperTest

Tests webform array utility.

@group webform

@coversDefaultClass \Drupal\webform\Utility\WebformArrayHelper

Hierarchy

Expanded class hierarchy of WebformArrayHelperTest

File

tests/src/Unit/Utility/WebformArrayHelperTest.php, line 15

Namespace

Drupal\Tests\webform\Unit\Utility
View source
class WebformArrayHelperTest extends UnitTestCase {

  /**
   * Tests converting arrays to readable string with WebformArrayHelper::toString().
   *
   * @param array $array
   *   The array to run through WebformArrayHelper::toString().
   * @param string $conjunction
   *   The $conjunction to run through WebformArrayHelper::toString().
   * @param string $expected
   *   The expected result from calling the function.
   *
   * @see WebformArrayHelper::toString()
   *
   * @dataProvider providerToString
   */
  public function testToString(array $array, $conjunction, $expected) {
    $result = WebformArrayHelper::toString($array, $conjunction);
    $this
      ->assertEquals($expected, $result);
  }

  /**
   * Data provider for testToString().
   *
   * @see testToString()
   */
  public function providerToString() {
    $tests[] = [
      [
        'Jack',
        'Jill',
      ],
      'and',
      'Jack and Jill',
    ];
    $tests[] = [
      [
        'Jack',
        'Jill',
      ],
      'or',
      'Jack or Jill',
    ];
    $tests[] = [
      [
        'Jack',
        'Jill',
        'Bill',
      ],
      'and',
      'Jack, Jill, and Bill',
    ];
    $tests[] = [
      [
        '',
      ],
      'and',
      '',
      'WebformArrayHelper::toString with no one',
    ];
    $tests[] = [
      [
        'Jack',
      ],
      'and',
      'Jack',
    ];
    return $tests;
  }

  /**
   * Tests determining type of array with WebformArrayHelper::IsAssociative().
   *
   * @param array $array
   *   The array to run through WebformArrayHelper::IsAssociative().
   * @param string $expected
   *   The expected result from calling the function.
   *
   * @see WebformArrayHelper::IsAssociative()
   *
   * @dataProvider providerIsAssociative
   */
  public function testIsAssociative(array $array, $expected) {
    $result = WebformArrayHelper::IsAssociative($array);
    $this
      ->assertEquals($expected, $result);
  }

  /**
   * Data provider for testIsAssociative().
   *
   * @see testIsAssociative()
   */
  public function providerIsAssociative() {
    $tests[] = [
      [
        'Jack',
      ],
      FALSE,
    ];
    $tests[] = [
      [
        0 => 'Jack',
        1 => 'Jill',
      ],
      FALSE,
    ];
    $tests[] = [
      [
        0 => 'Jack',
        2 => 'Jill',
      ],
      TRUE,
    ];
    $tests[] = [
      [
        'name' => 'Jack',
      ],
      TRUE,
    ];
    $tests[] = [
      [
        'Jack',
        'name' => 'Jill',
      ],
      TRUE,
    ];
    $tests[] = [
      [
        'name' => 'Jack',
      ],
      TRUE,
    ];
    $tests[] = [
      [
        'name' => 'Jack',
        'Jill',
      ],
      TRUE,
    ];
    return $tests;
  }

  /**
   * Tests determining type of array with WebformArrayHelper::InArray().
   *
   * @param array $needles
   *   The searched values.
   * @param array $haystack
   *   The array.
   * @param string $expected
   *   The expected result from calling the function.
   *
   * @see WebformArrayHelper::InArray()
   *
   * @dataProvider providerInArray
   */
  public function testInArray(array $needles, array $haystack, $expected) {
    $result = WebformArrayHelper::InArray($needles, $haystack);
    $this
      ->assertEquals($expected, $result);
  }

  /**
   * Data provider for testInArray().
   *
   * @see testInArray()
   */
  public function providerInArray() {
    $tests[] = [
      [],
      [
        'A',
        'B',
        'C',
      ],
      FALSE,
    ];
    $tests[] = [
      [
        'A',
      ],
      [
        'A',
        'B',
        'C',
      ],
      TRUE,
    ];
    $tests[] = [
      [
        'A',
        'B',
      ],
      [
        'A',
        'B',
        'C',
      ],
      TRUE,
    ];
    $tests[] = [
      [
        'D',
      ],
      [
        'A',
        'B',
        'C',
      ],
      FALSE,
    ];
    $tests[] = [
      [
        1,
      ],
      [
        1,
        2,
        3,
      ],
      TRUE,
    ];
    $tests[] = [
      [
        4,
      ],
      [
        1,
        2,
        3,
      ],
      FALSE,
    ];
    return $tests;
  }

  /**
   * Tests navigating an associative array's keys.
   *
   * @see WebformArrayHelper::getFirstKey()
   * @see WebformArrayHelper::getLastKey()
   * @see WebformArrayHelper::getPreviousKey()
   * @see WebformArrayHelper::getNextKey()
   */
  public function testGetKey() {
    $array = [
      'one' => 'One',
      'two' => 'Two',
      'three' => 'Three',
      'four' => 'Four',
      'five' => 'Five',
    ];
    $this
      ->assertEquals(WebformArrayHelper::getFirstKey($array), 'one');
    $this
      ->assertEquals(WebformArrayHelper::getFirstKey([]), NULL);
    $this
      ->assertEquals(WebformArrayHelper::getLastKey($array), 'five');
    $this
      ->assertEquals(WebformArrayHelper::getLastKey([]), NULL);
    $this
      ->assertEquals(WebformArrayHelper::getNextKey($array, 'one'), 'two');
    $this
      ->assertEquals(WebformArrayHelper::getNextKey($array, 'five'), NULL);
    $this
      ->assertEquals(WebformArrayHelper::getNextKey($array, 'six'), NULL);
    $this
      ->assertEquals(WebformArrayHelper::getPreviousKey($array, 'five'), 'four');
    $this
      ->assertEquals(WebformArrayHelper::getPreviousKey($array, 'one'), NULL);
    $this
      ->assertEquals(WebformArrayHelper::getNextKey($array, 'six'), NULL);
  }

  /**
   * Tests prefix an associative array.
   *
   * @see WebformArrayHelper::addPrefix()
   * @see WebformArrayHelper::removePrefix()
   */
  public function testPrefixing() {
    $this
      ->assertEquals(WebformArrayHelper::addPrefix([
      'test' => 'test',
    ]), [
      '#test' => 'test',
    ]);
    $this
      ->assertEquals(WebformArrayHelper::addPrefix([
      'test' => 'test',
    ], '@'), [
      '@test' => 'test',
    ]);
    $this
      ->assertEquals(WebformArrayHelper::removePrefix([
      '#test' => 'test',
    ]), [
      'test' => 'test',
    ]);
    $this
      ->assertEquals(WebformArrayHelper::removePrefix([
      '@test' => 'test',
    ], '@'), [
      'test' => 'test',
    ]);
  }

}

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
WebformArrayHelperTest::providerInArray public function Data provider for testInArray().
WebformArrayHelperTest::providerIsAssociative public function Data provider for testIsAssociative().
WebformArrayHelperTest::providerToString public function Data provider for testToString().
WebformArrayHelperTest::testGetKey public function Tests navigating an associative array's keys.
WebformArrayHelperTest::testInArray public function Tests determining type of array with WebformArrayHelper::InArray().
WebformArrayHelperTest::testIsAssociative public function Tests determining type of array with WebformArrayHelper::IsAssociative().
WebformArrayHelperTest::testPrefixing public function Tests prefix an associative array.
WebformArrayHelperTest::testToString public function Tests converting arrays to readable string with WebformArrayHelper::toString().