Skip to content

Instantly share code, notes, and snippets.

@t-kuni
Created November 9, 2018 05:59
Show Gist options
  • Save t-kuni/662e24864bde4e9cb49c6974e2f26f91 to your computer and use it in GitHub Desktop.
Save t-kuni/662e24864bde4e9cb49c6974e2f26f91 to your computer and use it in GitHub Desktop.
public function buildTestCases($variations) {
$iterate = function($variations, $depth = 0, $indexies = []) use (&$iterate) {
$keys = array_keys($variations);
if ($depth >= count($keys)) {
yield $indexies;
} else {
$key = $keys[$depth];
for ($i = 0; $i < count($variations[$key]); $i++) {
$val = $variations[$key][$i];
$iterator = $iterate($variations, $depth + 1, array_merge($indexies, [$key => $val]));
foreach ($iterator as $variation) {
yield $variation;
}
}
}
};
$testCases = [];
$no = 1;
foreach ($iterate($variations) as $variation) {
$title = $no++ . ".";
$keys = array_keys($variation);
$title = array_reduce($keys, function($title, $key) use ($variation) {
$val = $variation[$key];
return "{$title} {$key}:{$val}";
}, $title);
$testCases = array_merge($testCases, [
$title => [$variation]
]);
}
return $testCases;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment