function GeneratePermutationsTrait::generatePermutations
Same name in other branches
- 8.9.x core/tests/Drupal/Tests/Traits/Core/GeneratePermutationsTrait.php \Drupal\Tests\Traits\Core\GeneratePermutationsTrait::generatePermutations()
- 10 core/tests/Drupal/Tests/Traits/Core/GeneratePermutationsTrait.php \Drupal\Tests\Traits\Core\GeneratePermutationsTrait::generatePermutations()
- 11.x core/tests/Drupal/Tests/Traits/Core/GeneratePermutationsTrait.php \Drupal\Tests\Traits\Core\GeneratePermutationsTrait::generatePermutations()
Converts a list of possible parameters into a stack of permutations.
Takes a list of parameters containing possible values, and converts all of them into a list of items containing every possible permutation.
Example:
$parameters = [
'one' => [
0,
1,
],
'two' => [
2,
3,
],
];
$permutations = $this->generatePermutations($parameters);
// Result:
$permutations == [
[
'one' => 0,
'two' => 2,
],
[
'one' => 1,
'two' => 2,
],
[
'one' => 0,
'two' => 3,
],
[
'one' => 1,
'two' => 3,
],
];
Parameters
array $parameters: An associative array of parameters, keyed by parameter name, and whose values are arrays of parameter values.
Return value
array[] A list of permutations, which is an array of arrays. Each inner array contains the full list of parameters that have been passed, but with a single value only.
5 calls to GeneratePermutationsTrait::generatePermutations()
- CommentCSSTest::testCommentClasses in core/
modules/ comment/ tests/ src/ Functional/ CommentCSSTest.php - Tests CSS classes on comments.
- CommentFieldAccessTest::testAccessToAdministrativeFields in core/
modules/ comment/ tests/ src/ Kernel/ CommentFieldAccessTest.php - Tests permissions on comment fields.
- CommentLinkBuilderTest::getLinkCombinations in core/
modules/ comment/ tests/ src/ Unit/ CommentLinkBuilderTest.php - Data provider for ::testCommentLinkBuilder.
- NodeRevisionPermissionsTest::testNodeRevisionAccessAnyType in core/
modules/ node/ tests/ src/ Functional/ NodeRevisionPermissionsTest.php - Tests general revision access permissions.
- NodeRevisionPermissionsTest::testNodeRevisionAccessPerType in core/
modules/ node/ tests/ src/ Functional/ NodeRevisionPermissionsTest.php - Tests revision access permissions for a specific content type.
File
-
core/
tests/ Drupal/ Tests/ Traits/ Core/ GeneratePermutationsTrait.php, line 41
Class
- GeneratePermutationsTrait
- Adds ability to convert a list of parameters into a stack of permutations.
Namespace
Drupal\Tests\Traits\CoreCode
public static function generatePermutations(array $parameters) {
$all_permutations = [
[],
];
foreach ($parameters as $parameter => $values) {
$new_permutations = [];
// Iterate over all values of the parameter.
foreach ($values as $value) {
// Iterate over all existing permutations.
foreach ($all_permutations as $permutation) {
// Add the new parameter value to existing permutations.
$new_permutations[] = $permutation + [
$parameter => $value,
];
}
}
// Replace the old permutations with the new permutations.
$all_permutations = $new_permutations;
}
return $all_permutations;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.