5 * Copyright (c) 2002-2011, Sebastian Bergmann <sebastian@phpunit.de>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * * Neither the name of Sebastian Bergmann nor the names of his
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
38 * @subpackage Util_Log
39 * @author Sebastian Bergmann <sebastian@phpunit.de>
40 * @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
41 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
42 * @link http://www.phpunit.de/
43 * @since File available since Release 2.3.0
47 * A TestListener that generates a logfile of the test execution in XML markup.
49 * The XML markup used is the same as the one that is used by the JUnit Ant task.
52 * @subpackage Util_Log
53 * @author Sebastian Bergmann <sebastian@phpunit.de>
54 * @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
55 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
56 * @version Release: 3.5.14
57 * @link http://www.phpunit.de/
58 * @since Class available since Release 2.1.0
60 class PHPUnit_Util_Log_JUnit extends PHPUnit_Util_Printer implements PHPUnit_Framework_TestListener
75 protected $logIncompleteSkipped = FALSE;
80 protected $writeDocument = TRUE;
85 protected $testSuites = array();
90 protected $testSuiteTests = array(0);
95 protected $testSuiteAssertions = array(0);
100 protected $testSuiteErrors = array(0);
105 protected $testSuiteFailures = array(0);
110 protected $testSuiteTimes = array(0);
115 protected $testSuiteLevel = 0;
120 protected $currentTestCase = NULL;
125 protected $attachCurrentTestCase = TRUE;
131 * @param boolean $logIncompleteSkipped
133 public function __construct($out = NULL, $logIncompleteSkipped = FALSE)
135 $this->document = new DOMDocument('1.0', 'UTF-8');
136 $this->document->formatOutput = TRUE;
138 $this->root = $this->document->createElement('testsuites');
139 $this->document->appendChild($this->root);
141 parent::__construct($out);
143 $this->logIncompleteSkipped = $logIncompleteSkipped;
147 * Flush buffer and close output.
150 public function flush()
152 if ($this->writeDocument === TRUE) {
153 $this->write($this->getXML());
162 * @param PHPUnit_Framework_Test $test
163 * @param Exception $e
166 public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
168 if ($this->currentTestCase !== NULL) {
169 if ($test instanceof PHPUnit_Framework_SelfDescribing) {
170 $buffer = $test->toString() . "\n";
175 $buffer .= PHPUnit_Framework_TestFailure::exceptionToString($e) .
177 PHPUnit_Util_Filter::getFilteredStacktrace($e, FALSE);
179 $error = $this->document->createElement(
180 'error', PHPUnit_Util_XML::prepareString($buffer)
183 $error->setAttribute('type', get_class($e));
185 $this->currentTestCase->appendChild($error);
187 $this->testSuiteErrors[$this->testSuiteLevel]++;
192 * A failure occurred.
194 * @param PHPUnit_Framework_Test $test
195 * @param PHPUnit_Framework_AssertionFailedError $e
198 public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
200 if ($this->currentTestCase !== NULL) {
201 if (!$test instanceof PHPUnit_Framework_Warning) {
202 if ($test instanceof PHPUnit_Framework_SelfDescribing) {
203 $buffer = $test->toString() . "\n";
208 $buffer .= PHPUnit_Framework_TestFailure::exceptionToString($e).
210 PHPUnit_Util_Filter::getFilteredStacktrace(
214 $failure = $this->document->createElement(
215 'failure', PHPUnit_Util_XML::prepareString($buffer)
218 $failure->setAttribute('type', get_class($e));
220 $this->currentTestCase->appendChild($failure);
222 $this->testSuiteFailures[$this->testSuiteLevel]++;
230 * @param PHPUnit_Framework_Test $test
231 * @param Exception $e
234 public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
236 if ($this->logIncompleteSkipped && $this->currentTestCase !== NULL) {
237 $error = $this->document->createElement(
239 PHPUnit_Util_XML::prepareString(
240 "Incomplete Test\n" .
241 PHPUnit_Util_Filter::getFilteredStacktrace($e, FALSE)
245 $error->setAttribute('type', get_class($e));
247 $this->currentTestCase->appendChild($error);
249 $this->testSuiteErrors[$this->testSuiteLevel]++;
251 $this->attachCurrentTestCase = FALSE;
258 * @param PHPUnit_Framework_Test $test
259 * @param Exception $e
261 * @since Method available since Release 3.0.0
263 public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
265 if ($this->logIncompleteSkipped && $this->currentTestCase !== NULL) {
266 $error = $this->document->createElement(
268 PHPUnit_Util_XML::prepareString(
270 PHPUnit_Util_Filter::getFilteredStacktrace($e, FALSE)
274 $error->setAttribute('type', get_class($e));
276 $this->currentTestCase->appendChild($error);
278 $this->testSuiteErrors[$this->testSuiteLevel]++;
280 $this->attachCurrentTestCase = FALSE;
285 * A testsuite started.
287 * @param PHPUnit_Framework_TestSuite $suite
288 * @since Method available since Release 2.2.0
290 public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
292 $testSuite = $this->document->createElement('testsuite');
293 $testSuite->setAttribute('name', $suite->getName());
295 if (class_exists($suite->getName(), FALSE)) {
297 $class = new ReflectionClass($suite->getName());
299 $testSuite->setAttribute('file', $class->getFileName());
301 $packageInformation = PHPUnit_Util_Class::getPackageInformation(
302 $suite->getName(), $class->getDocComment()
305 if (!empty($packageInformation['namespace'])) {
306 $testSuite->setAttribute(
307 'namespace', $packageInformation['namespace']
311 if (!empty($packageInformation['fullPackage'])) {
312 $testSuite->setAttribute(
313 'fullPackage', $packageInformation['fullPackage']
317 if (!empty($packageInformation['category'])) {
318 $testSuite->setAttribute(
319 'category', $packageInformation['category']
323 if (!empty($packageInformation['package'])) {
324 $testSuite->setAttribute(
325 'package', $packageInformation['package']
329 if (!empty($packageInformation['subpackage'])) {
330 $testSuite->setAttribute(
331 'subpackage', $packageInformation['subpackage']
336 catch (ReflectionException $e) {
340 if ($this->testSuiteLevel > 0) {
341 $this->testSuites[$this->testSuiteLevel]->appendChild($testSuite);
343 $this->root->appendChild($testSuite);
346 $this->testSuiteLevel++;
347 $this->testSuites[$this->testSuiteLevel] = $testSuite;
348 $this->testSuiteTests[$this->testSuiteLevel] = 0;
349 $this->testSuiteAssertions[$this->testSuiteLevel] = 0;
350 $this->testSuiteErrors[$this->testSuiteLevel] = 0;
351 $this->testSuiteFailures[$this->testSuiteLevel] = 0;
352 $this->testSuiteTimes[$this->testSuiteLevel] = 0;
358 * @param PHPUnit_Framework_TestSuite $suite
359 * @since Method available since Release 2.2.0
361 public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
363 $this->testSuites[$this->testSuiteLevel]->setAttribute(
364 'tests', $this->testSuiteTests[$this->testSuiteLevel]
367 $this->testSuites[$this->testSuiteLevel]->setAttribute(
368 'assertions', $this->testSuiteAssertions[$this->testSuiteLevel]
371 $this->testSuites[$this->testSuiteLevel]->setAttribute(
372 'failures', $this->testSuiteFailures[$this->testSuiteLevel]
375 $this->testSuites[$this->testSuiteLevel]->setAttribute(
376 'errors', $this->testSuiteErrors[$this->testSuiteLevel]
379 $this->testSuites[$this->testSuiteLevel]->setAttribute(
380 'time', sprintf('%F', $this->testSuiteTimes[$this->testSuiteLevel])
383 if ($this->testSuiteLevel > 1) {
384 $this->testSuiteTests[$this->testSuiteLevel - 1] += $this->testSuiteTests[$this->testSuiteLevel];
385 $this->testSuiteAssertions[$this->testSuiteLevel - 1] += $this->testSuiteAssertions[$this->testSuiteLevel];
386 $this->testSuiteErrors[$this->testSuiteLevel - 1] += $this->testSuiteErrors[$this->testSuiteLevel];
387 $this->testSuiteFailures[$this->testSuiteLevel - 1] += $this->testSuiteFailures[$this->testSuiteLevel];
388 $this->testSuiteTimes[$this->testSuiteLevel - 1] += $this->testSuiteTimes[$this->testSuiteLevel];
391 $this->testSuiteLevel--;
397 * @param PHPUnit_Framework_Test $test
399 public function startTest(PHPUnit_Framework_Test $test)
401 if (!$test instanceof PHPUnit_Framework_Warning) {
402 $testCase = $this->document->createElement('testcase');
403 $testCase->setAttribute('name', $test->getName());
405 if ($test instanceof PHPUnit_Framework_TestCase) {
406 $class = new ReflectionClass($test);
407 $methodName = $test->getName();
409 if ($class->hasMethod($methodName)) {
410 $method = $class->getMethod($test->getName());
412 $testCase->setAttribute('class', $class->getName());
413 $testCase->setAttribute('file', $class->getFileName());
414 $testCase->setAttribute('line', $method->getStartLine());
418 $this->currentTestCase = $testCase;
425 * @param PHPUnit_Framework_Test $test
428 public function endTest(PHPUnit_Framework_Test $test, $time)
430 if (!$test instanceof PHPUnit_Framework_Warning) {
431 if ($this->attachCurrentTestCase) {
432 if ($test instanceof PHPUnit_Framework_TestCase) {
433 $numAssertions = $test->getNumAssertions();
434 $this->testSuiteAssertions[$this->testSuiteLevel] += $numAssertions;
436 $this->currentTestCase->setAttribute(
437 'assertions', $numAssertions
441 $this->currentTestCase->setAttribute(
442 'time', sprintf('%F', $time)
445 $this->testSuites[$this->testSuiteLevel]->appendChild(
446 $this->currentTestCase
449 $this->testSuiteTests[$this->testSuiteLevel]++;
450 $this->testSuiteTimes[$this->testSuiteLevel] += $time;
454 $this->attachCurrentTestCase = TRUE;
455 $this->currentTestCase = NULL;
459 * Returns the XML as a string.
462 * @since Method available since Release 2.2.0
464 public function getXML()
466 return $this->document->saveXML();
470 * Enables or disables the writing of the document
473 * This is a "hack" needed for the integration of
474 * PHPUnit with Phing.
477 * @since Method available since Release 2.2.0
479 public function setWriteDocument($flag)
481 if (is_bool($flag)) {
482 $this->writeDocument = $flag;