1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
if(!class_exists('SapphireTestReporter')) { |
4
|
|
|
return; |
5
|
|
|
} |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Test reporter optimised for CLI (ie, plain-text) output |
9
|
|
|
* |
10
|
|
|
* @package framework |
11
|
|
|
* @subpackage testing |
12
|
|
|
*/ |
13
|
|
|
class CliTestReporter extends SapphireTestReporter { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Display error bar if it exists |
17
|
|
|
*/ |
18
|
|
|
public function writeResults() { |
|
|
|
|
19
|
|
|
$passCount = 0; |
20
|
|
|
$failCount = $this->currentSession['failures']; |
21
|
|
|
$testCount = 0; |
22
|
|
|
$incompleteCount = $this->currentSession['incomplete']; |
23
|
|
|
$errorCount = $this->currentSession['errors']; |
24
|
|
|
|
25
|
|
|
foreach($this->suiteResults['suites'] as $suite) { |
26
|
|
|
foreach($suite['tests'] as $test) { |
27
|
|
|
$testCount++; |
28
|
|
|
switch($test['status']) { |
29
|
|
|
case TEST_INCOMPLETE: { |
|
|
|
|
30
|
|
|
$incompleteCount++; |
31
|
|
|
break; |
32
|
|
|
} |
33
|
|
|
case TEST_SUCCESS: { |
|
|
|
|
34
|
|
|
$passCount++; |
35
|
|
|
break; |
36
|
|
|
} |
37
|
|
|
case TEST_ERROR: { |
|
|
|
|
38
|
|
|
$errorCount++; |
39
|
|
|
break; |
40
|
|
|
} |
41
|
|
|
default: { |
|
|
|
|
42
|
|
|
$failCount++; |
43
|
|
|
break; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
echo "\n\n"; |
50
|
|
|
$breakages = $errorCount + $failCount; |
51
|
|
|
if ($breakages == 0 && $incompleteCount > 0) { |
52
|
|
|
echo SS_Cli::text(" OK, BUT INCOMPLETE TESTS! ", "black", "yellow"); |
53
|
|
|
} elseif ($breakages == 0) { |
54
|
|
|
echo SS_Cli::text(" ALL TESTS PASS ", "black", "green"); |
55
|
|
|
} else { |
56
|
|
|
echo SS_Cli::text(" AT LEAST ONE FAILURE ", "black", "red"); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
echo sprintf("\n\n%d tests run: %s, %s, and %s\n", $testCount, SS_Cli::text("$passCount passes"), |
60
|
|
|
SS_Cli::text("$breakages failures"), SS_Cli::text("$incompleteCount incomplete")); |
61
|
|
|
|
62
|
|
|
echo "Maximum memory usage: " . number_format(memory_get_peak_usage()/(1024*1024), 1) . "M\n\n"; |
63
|
|
|
|
64
|
|
|
// Use sake dev/tests/all --showslow to show slow tests |
65
|
|
|
if((isset($_GET['args']) && is_array($_GET['args']) && in_array('--showslow', $_GET['args'])) |
66
|
|
|
|| isset($_GET['showslow'])) { |
67
|
|
|
|
68
|
|
|
$avgSpeed = round(array_sum($this->testSpeeds) / count($this->testSpeeds), 3); |
69
|
|
|
echo "Slow tests (more than the average $avgSpeed seconds):\n"; |
70
|
|
|
|
71
|
|
|
arsort($this->testSpeeds); |
72
|
|
|
foreach($this->testSpeeds as $k => $v) { |
73
|
|
|
// Ignore below-average speeds |
74
|
|
|
if($v < $avgSpeed) break; |
75
|
|
|
|
76
|
|
|
echo " - $k: " . round($v,3) . "\n"; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
echo "\n"; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function endTest( PHPUnit_Framework_Test $test, $time) { |
83
|
|
|
// Status indicator, a la PHPUnit |
84
|
|
|
switch($this->currentTest['status']) { |
85
|
|
|
case TEST_FAILURE: echo SS_Cli::text("F","red", null, true); break; |
86
|
|
|
case TEST_ERROR: echo SS_Cli::text("E","red", null, true); break; |
87
|
|
|
case TEST_INCOMPLETE: echo SS_Cli::text("I","yellow"); break; |
88
|
|
|
case TEST_SUCCESS: echo SS_Cli::text(".","green"); break; |
89
|
|
|
default: echo SS_Cli::text("?", "yellow"); break; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
static $colCount = 0; |
93
|
|
|
$colCount++; |
94
|
|
|
if($colCount % 80 == 0) echo " - $colCount\n"; |
95
|
|
|
|
96
|
|
|
$this->writeTest($this->currentTest); |
97
|
|
|
parent::endTest($test, $time); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
protected function addStatus($status, $message, $exception, $trace) { |
101
|
|
|
if(!$this->currentTest && !$this->currentSuite) { |
|
|
|
|
102
|
|
|
// Log non-test errors immediately |
103
|
|
|
$statusResult = array( |
104
|
|
|
'status' => $status, |
105
|
|
|
'message' => $message, |
106
|
|
|
'exception' => $exception, |
107
|
|
|
'trace' => $trace |
108
|
|
|
); |
109
|
|
|
$this->writeTest($statusResult); |
110
|
|
|
} |
111
|
|
|
parent::addStatus($status, $message, $exception, $trace); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
protected function writeTest($test) { |
115
|
|
|
if ($test['status'] != TEST_SUCCESS) { |
116
|
|
|
$filteredTrace = array(); |
117
|
|
|
foreach($test['trace'] as $item) { |
118
|
|
|
if(isset($item['file']) |
119
|
|
|
&& strpos($item['file'], 'PHPUnit/Framework') === false |
120
|
|
|
&& !isset($item['class'])) { |
121
|
|
|
$filteredTrace[] = $item; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if(isset($item['class']) && isset($item['function']) && $item['class'] == 'PHPUnit_Framework_TestSuite' |
125
|
|
|
&& $item['function'] == 'run') { |
126
|
|
|
break; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$color = ($test['status'] == 2) ? 'yellow' : 'red'; |
132
|
|
|
echo "\n" . SS_Cli::text($test['name'] . "\n". $test['message'] . "\n", $color, null); |
133
|
|
|
echo SS_Backtrace::get_rendered_backtrace($filteredTrace, true); |
134
|
|
|
echo "--------------------\n"; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.