class WebformArrayHelperTest in Webform 8.5
Same name and namespace in other branches
- 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
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\webform\Unit\Utility\WebformArrayHelperTest
Expanded class hierarchy of WebformArrayHelperTest
File
- tests/
src/ Unit/ Utility/ WebformArrayHelperTest.php, line 15
Namespace
Drupal\Tests\webform\Unit\UtilityView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
protected | function | 340 | |
WebformArrayHelperTest:: |
public | function | Data provider for testInArray(). | |
WebformArrayHelperTest:: |
public | function | Data provider for testIsAssociative(). | |
WebformArrayHelperTest:: |
public | function | Data provider for testToString(). | |
WebformArrayHelperTest:: |
public | function | Tests navigating an associative array's keys. | |
WebformArrayHelperTest:: |
public | function | Tests determining type of array with WebformArrayHelper::InArray(). | |
WebformArrayHelperTest:: |
public | function | Tests determining type of array with WebformArrayHelper::IsAssociative(). | |
WebformArrayHelperTest:: |
public | function | Tests prefix an associative array. | |
WebformArrayHelperTest:: |
public | function | Tests converting arrays to readable string with WebformArrayHelper::toString(). |