5 * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.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.
39 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
40 * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
41 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
43 * @link http://www.phpunit.de/
44 * @since File available since Release 3.0.0
47 require_once 'PHPUnit/Framework.php';
48 require_once 'PHPUnit/Util/Filter.php';
49 require_once 'PHPUnit/Util/Filesystem.php';
50 require_once 'PHPUnit/Util/Printer.php';
51 require_once 'PHPUnit/Util/Test.php';
53 PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
56 * A TestListener that generates maps of the executed tests
61 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
62 * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
63 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
64 * @version Release: 3.3.17
65 * @link http://www.phpunit.de/
66 * @since Class available since Release 3.0.0
68 class PHPUnit_Util_Log_GraphViz extends PHPUnit_Util_Printer implements PHPUnit_Framework_TestListener
78 protected $currentTestSuccess = TRUE;
83 protected $testSuites = array();
88 protected $testSuiteLevel = 0;
93 protected $testSuiteFailureOrErrorCount = array(0);
98 protected $testSuiteIncompleteOrSkippedCount = array(0);
105 public function __construct($out = NULL)
107 if (PHPUnit_Util_Filesystem::fileExistsInIncludePath('Image/GraphViz.php')) {
108 PHPUnit_Util_Filesystem::collectStart();
109 require_once 'Image/GraphViz.php';
111 $this->graph = new Image_GraphViz(
114 'overlap' => 'scale',
121 parent::__construct($out);
123 foreach (PHPUnit_Util_Filesystem::collectEnd() as $blacklistedFile) {
124 PHPUnit_Util_Filter::addFileToFilter($blacklistedFile, 'PHPUNIT');
127 throw new RuntimeException('Image_GraphViz is not available.');
132 * Flush buffer and close output.
135 public function flush()
137 $this->write($this->graph->parse());
145 * @param PHPUnit_Framework_Test $test
146 * @param Exception $e
149 public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
151 $this->addTestNode($test, 'red');
152 $this->testSuiteFailureOrErrorCount[$this->testSuiteLevel]++;
154 $this->currentTestSuccess = FALSE;
158 * A failure occurred.
160 * @param PHPUnit_Framework_Test $test
161 * @param PHPUnit_Framework_AssertionFailedError $e
164 public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
166 $this->addTestNode($test, 'red');
167 $this->testSuiteFailureOrErrorCount[$this->testSuiteLevel]++;
169 $this->currentTestSuccess = FALSE;
175 * @param PHPUnit_Framework_Test $test
176 * @param Exception $e
179 public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
181 $this->addTestNode($test, 'yellow');
182 $this->testSuiteIncompleteOrSkippedCount[$this->testSuiteLevel]++;
184 $this->currentTestSuccess = FALSE;
190 * @param PHPUnit_Framework_Test $test
191 * @param Exception $e
194 public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
196 $this->addTestNode($test, 'yellow');
197 $this->testSuiteIncompleteOrSkippedCount[$this->testSuiteLevel]++;
199 $this->currentTestSuccess = FALSE;
203 * A testsuite started.
205 * @param PHPUnit_Framework_TestSuite $suite
207 public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
209 $this->graph->addNode($suite->getName());
211 if ($this->testSuiteLevel > 0) {
212 $this->graph->addEdge(
214 $this->testSuites[$this->testSuiteLevel] => $suite->getName()
219 $this->testSuiteLevel++;
220 $this->testSuites[$this->testSuiteLevel] = $suite->getName();
221 $this->testSuiteFailureOrErrorCount[$this->testSuiteLevel] = 0;
222 $this->testSuiteIncompleteOrSkippedCount[$this->testSuiteLevel] = 0;
228 * @param PHPUnit_Framework_TestSuite $suite
230 public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
234 if ($this->testSuiteFailureOrErrorCount[$this->testSuiteLevel] == 0 &&
235 $this->testSuiteIncompleteOrSkippedCount[$this->testSuiteLevel] == 0) {
239 else if ($this->testSuiteFailureOrErrorCount[$this->testSuiteLevel] == 0 &&
240 $this->testSuiteIncompleteOrSkippedCount[$this->testSuiteLevel] > 0) {
244 $this->graph->addNode(
245 $this->testSuites[$this->testSuiteLevel],
246 array('color' => $color)
249 if ($this->testSuiteLevel > 1) {
250 $this->testSuiteFailureOrErrorCount[$this->testSuiteLevel - 1] += $this->testSuiteFailureOrErrorCount[$this->testSuiteLevel];
251 $this->testSuiteIncompleteOrSkippedCount[$this->testSuiteLevel - 1] += $this->testSuiteIncompleteOrSkippedCount[$this->testSuiteLevel];
254 $this->testSuiteLevel--;
260 * @param PHPUnit_Framework_Test $test
262 public function startTest(PHPUnit_Framework_Test $test)
264 $this->currentTestSuccess = TRUE;
270 * @param PHPUnit_Framework_Test $test
273 public function endTest(PHPUnit_Framework_Test $test, $time)
275 if ($this->currentTestSuccess) {
276 $this->addTestNode($test, 'green');
281 * @param PHPUnit_Framework_Test $test
282 * @param string $color
284 protected function addTestNode(PHPUnit_Framework_Test $test, $color)
286 $name = PHPUnit_Util_Test::describe($test, FALSE);
288 $this->graph->addNode(
290 array('color' => $color),
291 $this->testSuites[$this->testSuiteLevel]
294 $this->graph->addEdge(
296 $this->testSuites[$this->testSuiteLevel] => $name[1]