Pull merge.
[yaffs-website] / vendor / consolidation / log / tests / src / TestDataPermuter.php
1 <?php
2 namespace Consolidation\TestUtils;
3
4 class TestDataPermuter
5 {
6   /**
7    * Given an array of test data, where each element is
8    * data to pass to a unit test AND each unit test requires
9    * only scalar or object test value, find each array
10    * in each test, and build all of the permutations for all
11    * of the data provided in arrays.
12    */
13   public static function expandProviderDataArrays($tests)
14   {
15     $result = [];
16
17     foreach($tests as $test) {
18       $subsitutionIndex = 1;
19       $permutationData = [];
20       $replacements = [];
21
22       foreach($test as $testValue) {
23         if (is_array($testValue)) {
24           $key = "{SUB$subsitutionIndex}";
25           $replacements[$key] = $testValue;
26           $permutationData[] = $key;
27         }
28         else {
29           $permutationData[] = $testValue;
30         }
31       }
32
33       $permuted = static::expandDataMatrix([$permutationData], $replacements);
34       $result = array_merge($result, $permuted);
35     }
36
37     return $result;
38   }
39
40   /**
41    * Given an array of test data, where each element is
42    * data to pass to a unit test, expand all of the
43    * permutations of $replacements, where each key
44    * holds the placeholder value, and the value holds
45    * an array of replacement values.
46    */
47   public static function expandDataMatrix($tests, $replacements)
48   {
49     foreach($replacements as $substitute => $values) {
50       $tests = static::expandOneValue($tests, $substitute, $values);
51     }
52     return $tests;
53   }
54
55   /**
56    * Given an array of test data, where each element is
57    * data to pass to a unit test, find any element in any
58    * one test item whose value is exactly $substitute.
59    * Make a new test item for every item in $values, using
60    * each as the substitution for $substitute.
61    */
62   public static function expandOneValue($tests, $substitute, $values)
63   {
64     $result = [];
65
66     foreach($tests as $test) {
67       $position = array_search($substitute, $test);
68       if ($position === FALSE) {
69         $result[] = $test;
70       }
71       else {
72         foreach($values as $replacement) {
73           $test[$position] = $replacement;
74           $result[] = $test;
75         }
76       }
77     }
78
79     return $result;
80   }
81 }