3 * This file is part of PHPUnit.
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
12 * A TestListener that generates a logfile of the
13 * test execution using the Test Anything Protocol (TAP).
15 * @since Class available since Release 3.0.0
17 class PHPUnit_Util_Log_TAP extends PHPUnit_Util_Printer implements PHPUnit_Framework_TestListener
22 protected $testNumber = 0;
27 protected $testSuiteLevel = 0;
32 protected $testSuccessful = true;
39 * @throws PHPUnit_Framework_Exception
41 * @since Method available since Release 3.3.4
43 public function __construct($out = null)
45 parent::__construct($out);
46 $this->write("TAP version 13\n");
52 * @param PHPUnit_Framework_Test $test
56 public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
58 $this->writeNotOk($test, 'Error');
64 * @param PHPUnit_Framework_Test $test
65 * @param PHPUnit_Framework_AssertionFailedError $e
68 public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
70 $this->writeNotOk($test, 'Failure');
74 PHPUnit_Framework_TestFailure::exceptionToString($e)
78 'message' => $message[0],
82 if ($e instanceof PHPUnit_Framework_ExpectationFailedException) {
83 $cf = $e->getComparisonFailure();
86 $diagnostic['data'] = array(
87 'got' => $cf->getActual(),
88 'expected' => $cf->getExpected()
93 $yaml = new Symfony\Component\Yaml\Dumper;
98 $yaml->dump($diagnostic, 2, 2)
106 * @param PHPUnit_Framework_Test $test
107 * @param Exception $e
110 public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
112 $this->writeNotOk($test, '', 'TODO Incomplete Test');
118 * @param PHPUnit_Framework_Test $test
119 * @param Exception $e
122 * @since Method available since Release 4.0.0
124 public function addRiskyTest(PHPUnit_Framework_Test $test, Exception $e, $time)
128 "ok %d - # RISKY%s\n",
130 $e->getMessage() != '' ? ' ' . $e->getMessage() : ''
134 $this->testSuccessful = false;
140 * @param PHPUnit_Framework_Test $test
141 * @param Exception $e
144 * @since Method available since Release 3.0.0
146 public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
150 "ok %d - # SKIP%s\n",
152 $e->getMessage() != '' ? ' ' . $e->getMessage() : ''
156 $this->testSuccessful = false;
160 * A testsuite started.
162 * @param PHPUnit_Framework_TestSuite $suite
164 public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
166 $this->testSuiteLevel++;
172 * @param PHPUnit_Framework_TestSuite $suite
174 public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
176 $this->testSuiteLevel--;
178 if ($this->testSuiteLevel == 0) {
179 $this->write(sprintf("1..%d\n", $this->testNumber));
186 * @param PHPUnit_Framework_Test $test
188 public function startTest(PHPUnit_Framework_Test $test)
191 $this->testSuccessful = true;
197 * @param PHPUnit_Framework_Test $test
200 public function endTest(PHPUnit_Framework_Test $test, $time)
202 if ($this->testSuccessful === true) {
207 PHPUnit_Util_Test::describe($test)
212 $this->writeDiagnostics($test);
216 * @param PHPUnit_Framework_Test $test
217 * @param string $prefix
218 * @param string $directive
220 protected function writeNotOk(PHPUnit_Framework_Test $test, $prefix = '', $directive = '')
224 "not ok %d - %s%s%s\n",
226 $prefix != '' ? $prefix . ': ' : '',
227 PHPUnit_Util_Test::describe($test),
228 $directive != '' ? ' # ' . $directive : ''
232 $this->testSuccessful = false;
236 * @param PHPUnit_Framework_Test $test
238 private function writeDiagnostics(PHPUnit_Framework_Test $test)
240 if (!$test instanceof PHPUnit_Framework_TestCase) {
244 if (!$test->hasOutput()) {
248 foreach (explode("\n", trim($test->getActualOutput())) as $line) {