1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PHPUnit\TeamCity; |
4
|
|
|
|
5
|
|
|
class TestListener extends \PHPUnit_Util_Printer implements \PHPUnit_Framework_TestListener |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Teamcity service message names |
9
|
|
|
*/ |
10
|
|
|
const MESSAGE_SUITE_STARTED = 'testSuiteStarted'; |
11
|
|
|
const MESSAGE_SUITE_FINISHED = 'testSuiteFinished'; |
12
|
|
|
const MESSAGE_TEST_STARTED = 'testStarted'; |
13
|
|
|
const MESSAGE_TEST_FAILED = 'testFailed'; |
14
|
|
|
const MESSAGE_TEST_IGNORED = 'testIgnored'; |
15
|
|
|
const MESSAGE_TEST_FINISHED = 'testFinished'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Comparison failure message type |
19
|
|
|
*/ |
20
|
|
|
const MESSAGE_COMPARISON_FAILURE = 'comparisonFailure'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* If true, all the standard output (and standard error) messages |
24
|
|
|
* received between testStarted and testFinished messages will be considered test output |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $captureStandardOutput = 'true'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Create and write service message to out |
32
|
|
|
* |
33
|
|
|
* @param string $type |
34
|
|
|
* @param \PHPUnit_Framework_Test $test |
35
|
|
|
* @param array $params |
36
|
|
|
*/ |
37
|
14 |
|
protected function writeServiceMessage($type, \PHPUnit_Framework_Test $test, array $params = array()) |
38
|
|
|
{ |
39
|
14 |
|
$message = $this->createServiceMessage($type, $test, $params); |
40
|
14 |
|
$this->write($message); |
41
|
14 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Create service message |
45
|
|
|
* |
46
|
|
|
* @param string $type |
47
|
|
|
* @param \PHPUnit_Framework_Test $test |
48
|
|
|
* @param array $params |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
14 |
|
protected function createServiceMessage($type, \PHPUnit_Framework_Test $test, array $params = array()) |
52
|
|
|
{ |
53
|
|
|
$params += array( |
54
|
14 |
|
'name' => $this->getTestName($test), |
55
|
14 |
|
'timestamp' => $this->getTimestamp(), |
56
|
14 |
|
'flowId' => $this->getFlowId($test) |
57
|
14 |
|
); |
58
|
14 |
|
$attributes = array(); |
59
|
14 |
|
foreach ($params as $name => $value) { |
60
|
14 |
|
$attributes[] = sprintf("%s='%s'", $name, $this->escapeValue($value)); |
61
|
14 |
|
} |
62
|
14 |
|
return sprintf('##teamcity[%s %s]%s', $type, implode(' ', $attributes), PHP_EOL); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Create timestamp for service message |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
14 |
|
protected function getTimestamp() |
71
|
|
|
{ |
72
|
14 |
|
list($usec, $sec) = explode(' ', microtime()); |
73
|
14 |
|
$msec = floor($usec * 1000); |
74
|
14 |
|
return date("Y-m-d\\TH:i:s.{$msec}O", $sec); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param \PHPUnit_Framework_Test $test |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
14 |
|
protected function getTestName(\PHPUnit_Framework_Test $test) |
82
|
|
|
{ |
83
|
14 |
|
if ($test instanceof \PHPUnit_Framework_TestCase) { |
84
|
2 |
|
$name = $test->getName(); |
85
|
14 |
|
} elseif ($test instanceof \PHPUnit_Framework_TestSuite) { |
86
|
3 |
|
$name = $test->getName(); |
87
|
13 |
|
} elseif ($test instanceof \PHPUnit_Framework_SelfDescribing) { |
88
|
1 |
|
$name = $test->toString(); |
89
|
1 |
|
} else { |
90
|
9 |
|
$name = get_class($test); |
91
|
|
|
} |
92
|
14 |
|
return $name; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param \PHPUnit_Framework_Test $test |
97
|
|
|
* @return int |
98
|
|
|
*/ |
99
|
14 |
|
protected function getFlowId(\PHPUnit_Framework_Test $test) |
|
|
|
|
100
|
|
|
{ |
101
|
14 |
|
return getmypid(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $string |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
14 |
|
protected function escapeValue($string) |
109
|
|
|
{ |
110
|
14 |
|
$string = trim($string); |
111
|
14 |
|
return strtr( |
112
|
14 |
|
$string, |
113
|
|
|
array( |
114
|
14 |
|
"|" => "||", |
115
|
14 |
|
"'" => "|'", |
116
|
14 |
|
"\n" => "|n", |
117
|
14 |
|
"\r" => "|r", |
118
|
14 |
|
"[" => "|[", |
119
|
|
|
"]" => "|]" |
120
|
14 |
|
) |
121
|
14 |
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* An error occurred. |
126
|
|
|
* |
127
|
|
|
* @param \PHPUnit_Framework_Test $test |
128
|
|
|
* @param \Exception $e |
129
|
|
|
* @param float $time |
130
|
|
|
*/ |
131
|
4 |
|
public function addError(\PHPUnit_Framework_Test $test, \Exception $e, $time) |
132
|
|
|
{ |
133
|
|
|
$params = array( |
134
|
4 |
|
'message' => \PHPUnit_Framework_TestFailure::exceptionToString($e), |
135
|
4 |
|
'details' => \PHPUnit_Util_Filter::getFilteredStacktrace($e), |
136
|
4 |
|
); |
137
|
|
|
|
138
|
4 |
|
if ($e instanceof \PHPUnit_Framework_ExpectationFailedException) { |
139
|
1 |
|
$comparisonFailure = $e->getComparisonFailure(); |
140
|
1 |
|
if (null !== $comparisonFailure) { |
141
|
|
|
$params += array( |
142
|
1 |
|
'type' => self::MESSAGE_COMPARISON_FAILURE, |
143
|
1 |
|
'expected' => $comparisonFailure->getExpectedAsString(), |
144
|
1 |
|
'actual' => $comparisonFailure->getActualAsString() |
145
|
1 |
|
); |
146
|
1 |
|
} |
147
|
1 |
|
} |
148
|
|
|
|
149
|
4 |
|
$this->writeServiceMessage( |
150
|
4 |
|
self::MESSAGE_TEST_FAILED, |
151
|
4 |
|
$test, |
152
|
|
|
$params |
153
|
4 |
|
); |
154
|
4 |
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* A failure occurred. |
158
|
|
|
* |
159
|
|
|
* @param \PHPUnit_Framework_Test|\PHPUnit_Framework_TestCase $test |
160
|
|
|
* @param \PHPUnit_Framework_AssertionFailedError $e |
161
|
|
|
* @param float $time |
162
|
|
|
*/ |
163
|
2 |
|
public function addFailure(\PHPUnit_Framework_Test $test, \PHPUnit_Framework_AssertionFailedError $e, $time) |
164
|
|
|
{ |
165
|
2 |
|
$this->addError($test, $e, $time); |
166
|
2 |
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Incomplete test. |
170
|
|
|
* |
171
|
|
|
* @param \PHPUnit_Framework_Test $test |
172
|
|
|
* @param \Exception $e |
173
|
|
|
* @param float $time |
174
|
|
|
*/ |
175
|
1 |
|
public function addIncompleteTest(\PHPUnit_Framework_Test $test, \Exception $e, $time) |
176
|
|
|
{ |
177
|
1 |
|
$this->addSkippedTest($test, $e, $time); |
178
|
1 |
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Risky test. |
182
|
|
|
* |
183
|
|
|
* @param \PHPUnit_Framework_Test $test |
184
|
|
|
* @param \Exception $e |
185
|
|
|
* @param float $time |
186
|
|
|
* @since Method available since Release 4.0.0 |
187
|
|
|
*/ |
188
|
1 |
|
public function addRiskyTest(\PHPUnit_Framework_Test $test, \Exception $e, $time) |
189
|
|
|
{ |
190
|
1 |
|
$this->addSkippedTest($test, $e, $time); |
191
|
1 |
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Skipped test. |
195
|
|
|
* |
196
|
|
|
* @param \PHPUnit_Framework_Test $test |
197
|
|
|
* @param \Exception $e |
198
|
|
|
* @param float $time |
199
|
|
|
*/ |
200
|
3 |
|
public function addSkippedTest(\PHPUnit_Framework_Test $test, \Exception $e, $time) |
201
|
|
|
{ |
202
|
3 |
|
$this->writeServiceMessage( |
203
|
3 |
|
self::MESSAGE_TEST_IGNORED, |
204
|
3 |
|
$test, |
205
|
|
|
array( |
206
|
3 |
|
'message' => $e->getMessage(), |
207
|
|
|
) |
208
|
3 |
|
); |
209
|
3 |
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* A failure occurred. |
213
|
|
|
* |
214
|
|
|
* @param \PHPUnit_Framework_Test $test |
215
|
|
|
* @param \PHPUnit_Framework_Warning $e |
216
|
|
|
* @param float $time |
217
|
|
|
*/ |
218
|
1 |
|
public function addWarning(\PHPUnit_Framework_Test $test, \PHPUnit_Framework_Warning $e, $time) |
219
|
|
|
{ |
220
|
|
|
// Since PHPUnit 5.1 |
221
|
1 |
|
if ($e instanceof \Exception) { |
222
|
|
|
$this->addError($test, $e, $time); |
223
|
|
|
} else { |
224
|
1 |
|
$this->writeServiceMessage( |
225
|
1 |
|
self::MESSAGE_TEST_FAILED, |
226
|
1 |
|
$test, |
227
|
|
|
array( |
228
|
1 |
|
'message' => $e->getMessage(), |
229
|
|
|
) |
230
|
1 |
|
); |
231
|
|
|
} |
232
|
1 |
|
} |
233
|
|
|
|
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* A test suite started. |
237
|
|
|
* |
238
|
|
|
* @param \PHPUnit_Framework_TestSuite $suite |
239
|
|
|
*/ |
240
|
2 |
|
public function startTestSuite(\PHPUnit_Framework_TestSuite $suite) |
241
|
|
|
{ |
242
|
2 |
|
$this->writeServiceMessage( |
243
|
2 |
|
self::MESSAGE_SUITE_STARTED, |
244
|
|
|
$suite |
245
|
2 |
|
); |
246
|
2 |
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* A test suite ended. |
250
|
|
|
* |
251
|
|
|
* @param \PHPUnit_Framework_TestSuite $suite |
252
|
|
|
*/ |
253
|
2 |
|
public function endTestSuite(\PHPUnit_Framework_TestSuite $suite) |
254
|
|
|
{ |
255
|
2 |
|
$this->writeServiceMessage( |
256
|
2 |
|
self::MESSAGE_SUITE_FINISHED, |
257
|
|
|
$suite |
258
|
2 |
|
); |
259
|
2 |
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* A test started. |
263
|
|
|
* |
264
|
|
|
* @param \PHPUnit_Framework_Test $test |
265
|
|
|
*/ |
266
|
3 |
|
public function startTest(\PHPUnit_Framework_Test $test) |
267
|
|
|
{ |
268
|
3 |
|
$this->writeServiceMessage( |
269
|
3 |
|
self::MESSAGE_TEST_STARTED, |
270
|
3 |
|
$test, |
271
|
|
|
array( |
272
|
3 |
|
'captureStandardOutput' => $this->captureStandardOutput, |
273
|
|
|
) |
274
|
3 |
|
); |
275
|
3 |
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* A test ended. |
279
|
|
|
* |
280
|
|
|
* @param \PHPUnit_Framework_Test $test |
281
|
|
|
* @param float $time seconds |
282
|
|
|
*/ |
283
|
2 |
|
public function endTest(\PHPUnit_Framework_Test $test, $time) |
284
|
|
|
{ |
285
|
2 |
|
$this->writeServiceMessage( |
286
|
2 |
|
self::MESSAGE_TEST_FINISHED, |
287
|
2 |
|
$test, |
288
|
|
|
array( |
289
|
2 |
|
'duration' => floor($time * 1000), |
290
|
|
|
) |
291
|
2 |
|
); |
292
|
2 |
|
} |
293
|
|
|
} |
294
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.