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 * Base class for printers of TestDox documentation.
14 * @since Class available since Release 2.1.0
16 abstract class PHPUnit_Util_TestDox_ResultPrinter extends PHPUnit_Util_Printer implements PHPUnit_Framework_TestListener
19 * @var PHPUnit_Util_TestDox_NamePrettifier
21 protected $prettifier;
26 protected $testClass = '';
31 protected $testStatus = false;
36 protected $tests = array();
41 protected $successful = 0;
46 protected $failed = 0;
56 protected $skipped = 0;
61 protected $incomplete = 0;
66 protected $currentTestClassPrettified;
71 protected $currentTestMethodPrettified;
76 * @param resource $out
78 public function __construct($out = null)
80 parent::__construct($out);
82 $this->prettifier = new PHPUnit_Util_TestDox_NamePrettifier;
87 * Flush buffer and close output.
89 public function flush()
100 * @param PHPUnit_Framework_Test $test
101 * @param Exception $e
104 public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
106 if (!$this->isOfInterest($test)) {
110 $this->testStatus = PHPUnit_Runner_BaseTestRunner::STATUS_ERROR;
115 * A failure occurred.
117 * @param PHPUnit_Framework_Test $test
118 * @param PHPUnit_Framework_AssertionFailedError $e
121 public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
123 if (!$this->isOfInterest($test)) {
127 $this->testStatus = PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE;
134 * @param PHPUnit_Framework_Test $test
135 * @param Exception $e
138 public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
140 if (!$this->isOfInterest($test)) {
144 $this->testStatus = PHPUnit_Runner_BaseTestRunner::STATUS_INCOMPLETE;
151 * @param PHPUnit_Framework_Test $test
152 * @param Exception $e
155 * @since Method available since Release 4.0.0
157 public function addRiskyTest(PHPUnit_Framework_Test $test, Exception $e, $time)
159 if (!$this->isOfInterest($test)) {
163 $this->testStatus = PHPUnit_Runner_BaseTestRunner::STATUS_RISKY;
170 * @param PHPUnit_Framework_Test $test
171 * @param Exception $e
174 * @since Method available since Release 3.0.0
176 public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
178 if (!$this->isOfInterest($test)) {
182 $this->testStatus = PHPUnit_Runner_BaseTestRunner::STATUS_SKIPPED;
187 * A testsuite started.
189 * @param PHPUnit_Framework_TestSuite $suite
191 * @since Method available since Release 2.2.0
193 public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
200 * @param PHPUnit_Framework_TestSuite $suite
202 * @since Method available since Release 2.2.0
204 public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
211 * @param PHPUnit_Framework_Test $test
213 public function startTest(PHPUnit_Framework_Test $test)
215 if (!$this->isOfInterest($test)) {
219 $class = get_class($test);
221 if ($this->testClass != $class) {
222 if ($this->testClass != '') {
226 $this->currentTestClassPrettified = $this->prettifier->prettifyTestClass($class);
227 $this->startClass($class);
229 $this->testClass = $class;
230 $this->tests = array();
235 $annotations = $test->getAnnotations();
237 if (isset($annotations['method']['testdox'][0])) {
238 $this->currentTestMethodPrettified = $annotations['method']['testdox'][0];
243 $this->currentTestMethodPrettified = $this->prettifier->prettifyTestMethod($test->getName(false));
246 $this->testStatus = PHPUnit_Runner_BaseTestRunner::STATUS_PASSED;
252 * @param PHPUnit_Framework_Test $test
255 public function endTest(PHPUnit_Framework_Test $test, $time)
257 if (!$this->isOfInterest($test)) {
261 if (!isset($this->tests[$this->currentTestMethodPrettified])) {
262 if ($this->testStatus == PHPUnit_Runner_BaseTestRunner::STATUS_PASSED) {
263 $this->tests[$this->currentTestMethodPrettified]['success'] = 1;
264 $this->tests[$this->currentTestMethodPrettified]['failure'] = 0;
266 $this->tests[$this->currentTestMethodPrettified]['success'] = 0;
267 $this->tests[$this->currentTestMethodPrettified]['failure'] = 1;
270 if ($this->testStatus == PHPUnit_Runner_BaseTestRunner::STATUS_PASSED) {
271 $this->tests[$this->currentTestMethodPrettified]['success']++;
273 $this->tests[$this->currentTestMethodPrettified]['failure']++;
277 $this->currentTestClassPrettified = null;
278 $this->currentTestMethodPrettified = null;
282 * @since Method available since Release 2.3.0
284 protected function doEndClass()
286 foreach ($this->tests as $name => $data) {
287 $this->onTest($name, $data['failure'] == 0);
290 $this->endClass($this->testClass);
294 * Handler for 'start run' event.
296 protected function startRun()
301 * Handler for 'start class' event.
303 * @param string $name
305 protected function startClass($name)
310 * Handler for 'on test' event.
312 * @param string $name
313 * @param bool $success
315 protected function onTest($name, $success = true)
320 * Handler for 'end class' event.
322 * @param string $name
324 protected function endClass($name)
329 * Handler for 'end run' event.
331 protected function endRun()
335 private function isOfInterest(PHPUnit_Framework_Test $test)
337 return $test instanceof PHPUnit_Framework_TestCase && get_class($test) != 'PHPUnit_Framework_Warning';