90% of 1088OPs |
92% of 198Lines |
78% of 117Branches |
59% of 97Paths |
# | |
---|---|
1 |
<?php |
2 |
|
3 |
namespace mageekguy\atoum; |
4 |
|
5 |
use |
6 |
mageekguy\atoum, |
7 |
mageekguy\atoum\asserter, |
8 |
mageekguy\atoum\exceptions |
9 |
; |
10 |
|
11 |
class score |
12 |
{ |
13 |
protected $passNumber = 0; |
14 |
protected $failAssertions = array(); |
15 |
protected $exceptions = array(); |
16 |
protected $runtimeExceptions = array(); |
17 |
protected $errors = array(); |
18 |
protected $outputs = array(); |
19 |
protected $durations = array(); |
20 |
protected $memoryUsages = array(); |
21 |
protected $voidMethods = array(); |
22 |
protected $uncompletedMethods = array(); |
23 |
protected $skippedMethods = array(); |
24 |
protected $coverage = null; |
25 |
|
26 |
private static $failId = 0; |
27 |
|
28 |
public function __construct(score\coverage $coverage = null)100% |
29 |
{ |
30 |
$this->setCoverage($coverage); |
31 |
} |
32 |
|
33 |
public function setCoverage(score\coverage $coverage = null)100% |
34 |
{ |
35 |
$this->coverage = $coverage ?: new score\coverage(); |
36 |
|
37 |
return $this; |
38 |
} |
39 |
|
40 |
public function getCoverage()100% |
41 |
{ |
42 |
return $this->coverage; |
43 |
} |
44 |
|
45 |
public function reset()100% |
46 |
{ |
47 |
$this->passNumber = 0; |
48 |
$this->failAssertions = array(); |
49 |
$this->exceptions = array(); |
50 |
$this->runtimeExceptions = array(); |
51 |
$this->errors = array(); |
52 |
$this->outputs = array(); |
53 |
$this->durations = array(); |
54 |
$this->memoryUsages = array(); |
55 |
$this->uncompletedMethods = array(); |
56 |
$this->coverage->reset(); |
57 |
|
58 |
return $this; |
59 |
} |
60 |
|
61 |
public function getAssertionNumber()100% |
62 |
{ |
63 |
return ($this->passNumber + sizeof($this->failAssertions)); |
64 |
} |
65 |
|
66 |
public function getPassNumber()100% |
67 |
{ |
68 |
return $this->passNumber; |
69 |
} |
70 |
|
71 |
public function getRuntimeExceptions()100% |
72 |
{ |
73 |
return $this->runtimeExceptions; |
74 |
} |
75 |
|
76 |
public function getVoidMethods()0% |
77 |
{ |
78 |
return $this->voidMethods; |
79 |
} |
80 |
|
81 |
public function getLastVoidMethod()100% |
82 |
{ |
83 |
return end($this->voidMethods) ?: null; |
84 |
} |
85 |
|
86 |
public function getVoidMethodNumber()0% |
87 |
{ |
88 |
return sizeof($this->voidMethods); |
89 |
} |
90 |
|
91 |
public function getUncompletedMethods()100% |
92 |
{ |
93 |
return $this->uncompletedMethods; |
94 |
} |
95 |
|
96 |
public function getUncompletedMethodNumber()100% |
97 |
{ |
98 |
return sizeof($this->uncompletedMethods); |
99 |
} |
100 |
|
101 |
public function getLastUncompleteMethod()100% |
102 |
{ |
103 |
return end($this->uncompletedMethods) ?: null; |
104 |
} |
105 |
|
106 |
public function getSkippedMethods()100% |
107 |
{ |
108 |
return $this->skippedMethods; |
109 |
} |
110 |
|
111 |
public function getLastSkippedMethod()100% |
112 |
{ |
113 |
return end($this->skippedMethods) ?: null; |
114 |
} |
115 |
|
116 |
public function getSkippedMethodNumber()100% |
117 |
{ |
118 |
return sizeof($this->skippedMethods); |
119 |
} |
120 |
|
121 |
public function getOutputs()100% |
122 |
{ |
123 |
return array_values($this->outputs); |
124 |
} |
125 |
|
126 |
public function getOutputNumber()100% |
127 |
{ |
128 |
return sizeof($this->outputs); |
129 |
} |
130 |
|
131 |
public function getTotalDuration()0% |
132 |
{ |
133 |
$total = 0.0; |
134 |
|
135 |
foreach ($this->durations as $duration) |
136 |
{ |
137 |
$total += $duration['value']; |
138 |
} |
139 |
|
140 |
return $total; |
141 |
} |
142 |
|
143 |
public function getDurations()100% |
144 |
{ |
145 |
return array_values($this->durations); |
146 |
} |
147 |
|
148 |
public function getDurationNumber()100% |
149 |
{ |
150 |
return sizeof($this->durations); |
151 |
} |
152 |
|
153 |
public function getTotalMemoryUsage()0% |
154 |
{ |
155 |
$total = 0.0; |
156 |
|
157 |
foreach ($this->memoryUsages as $memoryUsage) |
158 |
{ |
159 |
$total += $memoryUsage['value']; |
160 |
} |
161 |
|
162 |
return $total; |
163 |
} |
164 |
|
165 |
public function getMemoryUsages()100% |
166 |
{ |
167 |
return array_values($this->memoryUsages); |
168 |
} |
169 |
|
170 |
public function getMemoryUsageNumber()100% |
171 |
{ |
172 |
return sizeof($this->memoryUsages); |
173 |
} |
174 |
|
175 |
public function getFailAssertions()100% |
176 |
{ |
177 |
return self::sort(self::cleanAssertions($this->failAssertions)); |
178 |
} |
179 |
|
180 |
public function getLastFailAssertion()100% |
181 |
{ |
182 |
$lastFailAssertion = end($this->failAssertions) ?: null; |
183 |
|
184 |
if ($lastFailAssertion !== null) |
185 |
{ |
186 |
$lastFailAssertion = self::cleanAssertion($lastFailAssertion); |
187 |
} |
188 |
|
189 |
return $lastFailAssertion; |
190 |
} |
191 |
|
192 |
public function getFailNumber()100% |
193 |
{ |
194 |
return sizeof($this->getFailAssertions()); |
195 |
} |
196 |
|
197 |
public function getErrors()100% |
198 |
{ |
199 |
return self::sort($this->errors); |
200 |
} |
201 |
|
202 |
public function getErrorNumber()100% |
203 |
{ |
204 |
return sizeof($this->errors); |
205 |
} |
206 |
|
207 |
public function getExceptions()100% |
208 |
{ |
209 |
return self::sort($this->exceptions); |
210 |
} |
211 |
|
212 |
public function getExceptionNumber()100% |
213 |
{ |
214 |
return sizeof($this->exceptions); |
215 |
} |
216 |
|
217 |
public function getRuntimeExceptionNumber()100% |
218 |
{ |
219 |
return sizeof($this->runtimeExceptions); |
220 |
} |
221 |
|
222 |
public function getMethodsWithFail()100% |
223 |
{ |
224 |
return self::getMethods($this->getFailAssertions()); |
225 |
} |
226 |
|
227 |
public function getMethodsWithError()100% |
228 |
{ |
229 |
return self::getMethods($this->getErrors()); |
230 |
} |
231 |
|
232 |
public function getMethodsWithException()100% |
233 |
{ |
234 |
return self::getMethods($this->getExceptions()); |
235 |
} |
236 |
|
237 |
public function getMethodsNotCompleted()0% |
238 |
{ |
239 |
return self::getMethods($this->getUncompletedMethods()); |
240 |
} |
241 |
|
242 |
public function addPass()100% |
243 |
{ |
244 |
$this->passNumber++; |
245 |
|
246 |
return $this; |
247 |
} |
248 |
|
249 |
public function getLastErroredMethod()100% |
250 |
{ |
251 |
return end($this->errors) ?: null; |
252 |
} |
253 |
|
254 |
public function getLastException()100% |
255 |
{ |
256 |
return end($this->exceptions) ?: null; |
257 |
} |
258 |
|
259 |
public function getLastRuntimeException()100% |
260 |
{ |
261 |
return end($this->runtimeExceptions) ?: null; |
262 |
} |
263 |
|
264 |
public function addFail($file, $class, $method, $line, $asserter, $reason, $case = null, $dataSetKey = null, $dataSetProvider = null)100% |
265 |
{ |
266 |
$this->failAssertions[] = array( |
267 |
'id' => ++self::$failId, |
268 |
'case' => $case, |
269 |
'dataSetKey' => $dataSetKey, |
270 |
'dataSetProvider' => $dataSetProvider, |
271 |
'class' => $class, |
272 |
'method' => $method, |
273 |
'file' => $file, |
274 |
'line' => $line, |
275 |
'asserter' => $asserter, |
276 |
'fail' => $reason |
277 |
); |
278 |
|
279 |
return self::$failId; |
280 |
} |
281 |
|
282 |
public function addException($file, $class, $method, $line, \exception $exception, $case = null, $dataSetKey = null, $dataSetProvider = null)100% |
283 |
{ |
284 |
$this->exceptions[] = array( |
285 |
'case' => $case, |
286 |
'dataSetKey' => $dataSetKey, |
287 |
'dataSetProvider' => $dataSetProvider, |
288 |
'class' => $class, |
289 |
'method' => $method, |
290 |
'file' => $file, |
291 |
'line' => $line, |
292 |
'value' => (string) $exception |
293 |
); |
294 |
|
295 |
return $this; |
296 |
} |
297 |
|
298 |
public function addRuntimeException($file, $class, $method, exceptions\runtime $exception)100% |
299 |
{ |
300 |
$this->runtimeExceptions[] = $exception; |
301 |
|
302 |
return $this; |
303 |
} |
304 |
|
305 |
public function addError($file, $class, $method, $line, $type, $message, $errorFile = null, $errorLine = null, $case = null, $dataSetKey = null, $dataSetProvider = null)100% |
306 |
{ |
307 |
$this->errors[] = array( |
308 |
'case' => $case, |
309 |
'dataSetKey' => $dataSetKey, |
310 |
'dataSetProvider' => $dataSetProvider, |
311 |
'class' => $class, |
312 |
'method' => $method, |
313 |
'file' => $file, |
314 |
'line' => $line, |
315 |
'type' => $type, |
316 |
'message' => trim($message), |
317 |
'errorFile' => $errorFile, |
318 |
'errorLine' => $errorLine |
319 |
); |
320 |
|
321 |
return $this; |
322 |
} |
323 |
|
324 |
public function addOutput($file, $class, $method, $output)100% |
325 |
{ |
326 |
if ($output != '') |
327 |
{ |
328 |
$this->outputs[] = array( |
329 |
'class' => $class, |
330 |
'method' => $method, |
331 |
'value' => $output |
332 |
); |
333 |
} |
334 |
|
335 |
return $this; |
336 |
} |
337 |
|
338 |
public function addDuration($file, $class, $method, $duration)100% |
339 |
{ |
340 |
if ($duration > 0) |
341 |
{ |
342 |
$this->durations[] = array( |
343 |
'class' => $class, |
344 |
'method' => $method, |
345 |
'value' => $duration, |
346 |
'path' => $file |
347 |
); |
348 |
} |
349 |
|
350 |
return $this; |
351 |
} |
352 |
|
353 |
public function addMemoryUsage($file, $class, $method, $memoryUsage)100% |
354 |
{ |
355 |
if ($memoryUsage > 0) |
356 |
{ |
357 |
$this->memoryUsages[] = array( |
358 |
'class' => $class, |
359 |
'method' => $method, |
360 |
'value' => $memoryUsage |
361 |
); |
362 |
} |
363 |
|
364 |
return $this; |
365 |
} |
366 |
|
367 |
public function addVoidMethod($file, $class, $method)100% |
368 |
{ |
369 |
$this->voidMethods[] = array( |
370 |
'file' => $file, |
371 |
'class' => $class, |
372 |
'method' => $method |
373 |
); |
374 |
|
375 |
return $this; |
376 |
} |
377 |
|
378 |
public function addUncompletedMethod($file, $class, $method, $exitCode, $output)100% |
379 |
{ |
380 |
$this->uncompletedMethods[] = array( |
381 |
'file' => $file, |
382 |
'class' => $class, |
383 |
'method' => $method, |
384 |
'exitCode' => $exitCode, |
385 |
'output' => $output |
386 |
); |
387 |
|
388 |
return $this; |
389 |
} |
390 |
|
391 |
public function addSkippedMethod($file, $class, $method, $line, $message)100% |
392 |
{ |
393 |
$this->skippedMethods[] = array( |
394 |
'file' => $file, |
395 |
'class' => $class, |
396 |
'method' => $method, |
397 |
'line' => $line, |
398 |
'message' => $message |
399 |
); |
400 |
|
401 |
return $this; |
402 |
} |
403 |
|
404 |
public function merge(score $score)100% |
405 |
{ |
406 |
$this->passNumber += $score->getPassNumber(); |
407 |
$this->failAssertions = array_merge($this->failAssertions, $score->failAssertions); |
408 |
$this->exceptions = array_merge($this->exceptions, $score->exceptions); |
409 |
$this->runtimeExceptions = array_merge($this->runtimeExceptions, $score->runtimeExceptions); |
410 |
$this->errors = array_merge($this->errors, $score->errors); |
411 |
$this->outputs = array_merge($this->outputs, $score->outputs); |
412 |
$this->durations = array_merge($this->durations, $score->durations); |
413 |
$this->memoryUsages = array_merge($this->memoryUsages, $score->memoryUsages); |
414 |
$this->voidMethods = array_merge($this->voidMethods, $score->voidMethods); |
415 |
$this->uncompletedMethods = array_merge($this->uncompletedMethods, $score->uncompletedMethods); |
416 |
$this->skippedMethods = array_merge($this->skippedMethods, $score->skippedMethods); |
417 |
$this->coverage->merge($score->coverage); |
418 |
|
419 |
return $this; |
420 |
} |
421 |
|
422 |
public function errorExists($message = null, $type = null, $messageIsPattern = false)100% |
423 |
{ |
424 |
$messageIsNull = $message === null; |
425 |
$typeIsNull = $type === null; |
426 |
|
427 |
foreach ($this->errors as $key => $error) |
428 |
{ |
429 |
$messageMatch = $messageIsNull === true ? true : ($messageIsPattern == false ? $message == $error['message'] : preg_match($message, $error['message']) == 1); |
430 |
$typeMatch = $typeIsNull === true ? true : $error['type'] == $type; |
431 |
|
432 |
if ($messageMatch === true && $typeMatch === true) |
433 |
{ |
434 |
return $key; |
435 |
} |
436 |
} |
437 |
|
438 |
return null; |
439 |
} |
440 |
|
441 |
public function deleteError($key)100% |
442 |
{ |
443 |
if (isset($this->errors[$key]) === false) |
444 |
{ |
445 |
throw new exceptions\logic\invalidArgument('Error key \'' . $key . '\' does not exist'); |
446 |
} |
447 |
|
448 |
unset($this->errors[$key]); |
449 |
|
450 |
return $this; |
451 |
} |
452 |
|
453 |
public function failExists(asserter\exception $exception)0% |
454 |
{ |
455 |
$id = $exception->getCode(); |
456 |
|
457 |
return (sizeof(array_filter($this->failAssertions, function($assertion) use ($id) { return ($assertion['id'] === $id); })) > 0); |
458 |
} |
459 |
|
460 |
private static function getMethods(array $array)100% |
461 |
{ |
462 |
$methods = array(); |
463 |
|
464 |
foreach ($array as $value) |
465 |
{ |
466 |
if (isset($methods[$value['class']]) === false || in_array($value['method'], $methods[$value['class']]) === false) |
467 |
{ |
468 |
$methods[$value['class']][] = $value['method']; |
469 |
} |
470 |
} |
471 |
|
472 |
return $methods; |
473 |
} |
474 |
|
475 |
private static function cleanAssertions(array $assertions)100% |
476 |
{ |
477 |
return array_map(array(__CLASS__, 'cleanAssertion'), array_values($assertions)); |
478 |
} |
479 |
|
480 |
private static function cleanAssertion(array $assertion)100% |
481 |
{ |
482 |
unset($assertion['id']); |
483 |
|
484 |
return $assertion; |
485 |
} |
486 |
|
487 |
private static function sort(array $array)100% |
488 |
{ |
489 |
usort($array, function($a, $b) { |
490 |
if ($a['file'] !== $b['file']) |
491 |
{ |
492 |
return strcmp($a['file'], $b['file']); |
493 |
} |
494 |
else if ($a['line'] === $b['line']) |
495 |
{ |
496 |
return 0; |
497 |
} |
498 |
else |
499 |
{ |
500 |
return ($a['line'] < $b['line'] ? -1 : 1); |
501 |
} |
502 |
} |
503 |
); |
504 |
|
505 |
return $array; |
506 |
} |
507 |
} |