|
16 | 16 | */
|
17 | 17 | class lime_test
|
18 | 18 | {
|
19 |
| - const EPSILON = 0.0000000001; |
| 19 | + public const EPSILON = 0.0000000001; |
20 | 20 |
|
21 | 21 | protected $test_nb = 0;
|
22 | 22 | protected $output = null;
|
23 | 23 | protected $results = array();
|
24 | 24 | protected $options = array();
|
25 | 25 |
|
26 |
| - static protected $all_results = array(); |
| 26 | + protected static $all_results = array(); |
| 27 | + |
| 28 | + private const STATE_PASS = 0; |
| 29 | + private const STATE_FAIL = 1; |
| 30 | + |
| 31 | + private static $instanceCount = 0; |
| 32 | + private static $finalState = self::STATE_PASS; |
27 | 33 |
|
28 | 34 | public function __construct($plan = null, $options = array())
|
29 | 35 | {
|
| 36 | + ++self::$instanceCount; |
| 37 | + |
30 | 38 | // for BC
|
31 | 39 | if (!is_array($options))
|
32 | 40 | {
|
@@ -130,31 +138,86 @@ static public function to_xml($results = null)
|
130 | 138 |
|
131 | 139 | public function __destruct()
|
132 | 140 | {
|
133 |
| - $plan = $this->results['stats']['plan']; |
134 |
| - $passed = count($this->results['stats']['passed']); |
| 141 | + $testSuiteState = $this->determineAndPrintStateOfTestSuite(); |
| 142 | + |
| 143 | + flush(); |
| 144 | + |
| 145 | + $this->keepTheWorstState($testSuiteState); |
| 146 | + |
| 147 | + $this->finalizeLastInstanceDestructorWithProcessExit(); |
| 148 | + } |
| 149 | + |
| 150 | + private function determineAndPrintStateOfTestSuite(): int |
| 151 | + { |
| 152 | + $planState = $this->determineAndPrintStateOfPlan(); |
135 | 153 | $failed = count($this->results['stats']['failed']);
|
| 154 | + |
| 155 | + if ($failed) { |
| 156 | + $passed = count($this->results['stats']['passed']); |
| 157 | + |
| 158 | + $this->output->red_bar(sprintf("# Looks like you failed %d tests of %d.", $failed, $passed + $failed)); |
| 159 | + |
| 160 | + return self::STATE_FAIL; |
| 161 | + } |
| 162 | + |
| 163 | + if (self::STATE_FAIL === $planState) { |
| 164 | + return self::STATE_FAIL; |
| 165 | + } |
| 166 | + |
| 167 | + $this->output->green_bar("# Looks like everything went fine."); |
| 168 | + |
| 169 | + return self::STATE_PASS; |
| 170 | + } |
| 171 | + |
| 172 | + private function determineAndPrintStateOfPlan(): int |
| 173 | + { |
| 174 | + $plan = $this->results['stats']['plan']; |
136 | 175 | $total = $this->results['stats']['total'];
|
137 |
| - is_null($plan) and $plan = $total and $this->output->echoln(sprintf("1..%d", $plan)); |
138 | 176 |
|
139 |
| - if ($total > $plan) |
140 |
| - { |
141 |
| - $this->output->red_bar(sprintf("# Looks like you planned %d tests but ran %d extra.", $plan, $total - $plan)); |
| 177 | + if (null === $plan) { |
| 178 | + $plan = $total; |
| 179 | + |
| 180 | + $this->output->echoln(sprintf("1..%d", $plan)); |
142 | 181 | }
|
143 |
| - elseif ($total < $plan) |
144 |
| - { |
| 182 | + |
| 183 | + if ($total > $plan) { |
| 184 | + $this->output->red_bar(sprintf("# Looks like you planned %d tests but ran %d extra.", $plan, $total - $plan)); |
| 185 | + } elseif ($total < $plan) { |
145 | 186 | $this->output->red_bar(sprintf("# Looks like you planned %d tests but only ran %d.", $plan, $total));
|
146 | 187 | }
|
147 | 188 |
|
148 |
| - if ($failed) |
149 |
| - { |
150 |
| - $this->output->red_bar(sprintf("# Looks like you failed %d tests of %d.", $failed, $passed + $failed)); |
| 189 | + return $total === $plan ? self::STATE_PASS : self::STATE_FAIL; |
| 190 | + } |
| 191 | + |
| 192 | + private function keepTheWorstState(int $state): void |
| 193 | + { |
| 194 | + if ($this->stateIsTheWorst($state)) { |
| 195 | + self::$finalState = $state; |
151 | 196 | }
|
152 |
| - else if ($total == $plan) |
153 |
| - { |
154 |
| - $this->output->green_bar("# Looks like everything went fine."); |
| 197 | + } |
| 198 | + |
| 199 | + private function stateIsTheWorst(int $state): bool |
| 200 | + { |
| 201 | + return self::$finalState < $state; |
| 202 | + } |
| 203 | + |
| 204 | + private function finalizeLastInstanceDestructorWithProcessExit(): void |
| 205 | + { |
| 206 | + --self::$instanceCount; |
| 207 | + |
| 208 | + if (0 === self::$instanceCount) { |
| 209 | + exit($this->determineExitCodeFromState(self::$finalState)); |
155 | 210 | }
|
| 211 | + } |
156 | 212 |
|
157 |
| - flush(); |
| 213 | + private function determineExitCodeFromState(int $state): int |
| 214 | + { |
| 215 | + switch ($state) { |
| 216 | + case self::STATE_PASS: |
| 217 | + return 0; |
| 218 | + default: |
| 219 | + return 1; |
| 220 | + } |
158 | 221 | }
|
159 | 222 |
|
160 | 223 | /**
|
@@ -969,7 +1032,7 @@ function lime_shutdown()
|
969 | 1032 | $delta = 0;
|
970 | 1033 | if ($return > 0)
|
971 | 1034 | {
|
972 |
| - $stats['status'] = $file_stats['errors'] ? 'errors' : 'dubious'; |
| 1035 | + $stats['status'] = $file_stats['failed'] ? 'not ok' : ($file_stats['errors'] ? 'errors' : 'dubious'); |
973 | 1036 | $stats['status_code'] = $return;
|
974 | 1037 | }
|
975 | 1038 | else
|
|
0 commit comments