]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/Util/Log/GraphViz.php
Added unit tests.
[Github/sugarcrm.git] / tests / PHPUnit / Util / Log / GraphViz.php
1 <?php
2 /**
3  * PHPUnit
4  *
5  * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  *   * Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *
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
18  *     distribution.
19  *
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.
23  *
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.
36  *
37  * @category   Testing
38  * @package    PHPUnit
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
42
43  * @link       http://www.phpunit.de/
44  * @since      File available since Release 3.0.0
45  */
46
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';
52
53 PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
54
55 /**
56  * A TestListener that generates maps of the executed tests
57  * in GraphViz markup.
58  *
59  * @category   Testing
60  * @package    PHPUnit
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
67  */
68 class PHPUnit_Util_Log_GraphViz extends PHPUnit_Util_Printer implements PHPUnit_Framework_TestListener
69 {
70     /**
71      * @var    Image_GraphViz
72      */
73     protected $graph;
74
75     /**
76      * @var    boolean
77      */
78     protected $currentTestSuccess = TRUE;
79
80     /**
81      * @var    string[]
82      */
83     protected $testSuites = array();
84
85     /**
86      * @var    integer
87      */
88     protected $testSuiteLevel = 0;
89
90     /**
91      * @var    integer[]
92      */
93     protected $testSuiteFailureOrErrorCount = array(0);
94
95     /**
96      * @var    integer[]
97      */
98     protected $testSuiteIncompleteOrSkippedCount = array(0);
99
100     /**
101      * Constructor.
102      *
103      * @param  mixed $out
104      */
105     public function __construct($out = NULL)
106     {
107         if (PHPUnit_Util_Filesystem::fileExistsInIncludePath('Image/GraphViz.php')) {
108             PHPUnit_Util_Filesystem::collectStart();
109             require_once 'Image/GraphViz.php';
110
111             $this->graph = new Image_GraphViz(
112               TRUE,
113               array(
114                 'overlap'  => 'scale',
115                 'splines'  => 'true',
116                 'sep'      => '.1',
117                 'fontsize' => '8'
118               )
119             );
120
121             parent::__construct($out);
122
123             foreach (PHPUnit_Util_Filesystem::collectEnd() as $blacklistedFile) {
124                 PHPUnit_Util_Filter::addFileToFilter($blacklistedFile, 'PHPUNIT');
125             }
126         } else {
127             throw new RuntimeException('Image_GraphViz is not available.');
128         }
129     }
130
131     /**
132      * Flush buffer and close output.
133      *
134      */
135     public function flush()
136     {
137         $this->write($this->graph->parse());
138
139         parent::flush();
140     }
141
142     /**
143      * An error occurred.
144      *
145      * @param  PHPUnit_Framework_Test $test
146      * @param  Exception              $e
147      * @param  float                  $time
148      */
149     public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
150     {
151         $this->addTestNode($test, 'red');
152         $this->testSuiteFailureOrErrorCount[$this->testSuiteLevel]++;
153
154         $this->currentTestSuccess = FALSE;
155     }
156
157     /**
158      * A failure occurred.
159      *
160      * @param  PHPUnit_Framework_Test                 $test
161      * @param  PHPUnit_Framework_AssertionFailedError $e
162      * @param  float                                  $time
163      */
164     public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
165     {
166         $this->addTestNode($test, 'red');
167         $this->testSuiteFailureOrErrorCount[$this->testSuiteLevel]++;
168
169         $this->currentTestSuccess = FALSE;
170     }
171
172     /**
173      * Incomplete test.
174      *
175      * @param  PHPUnit_Framework_Test $test
176      * @param  Exception              $e
177      * @param  float                  $time
178      */
179     public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
180     {
181         $this->addTestNode($test, 'yellow');
182         $this->testSuiteIncompleteOrSkippedCount[$this->testSuiteLevel]++;
183
184         $this->currentTestSuccess = FALSE;
185     }
186
187     /**
188      * Skipped test.
189      *
190      * @param  PHPUnit_Framework_Test $test
191      * @param  Exception              $e
192      * @param  float                  $time
193      */
194     public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
195     {
196         $this->addTestNode($test, 'yellow');
197         $this->testSuiteIncompleteOrSkippedCount[$this->testSuiteLevel]++;
198
199         $this->currentTestSuccess = FALSE;
200     }
201
202     /**
203      * A testsuite started.
204      *
205      * @param  PHPUnit_Framework_TestSuite $suite
206      */
207     public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
208     {
209         $this->graph->addNode($suite->getName());
210
211         if ($this->testSuiteLevel > 0) {
212             $this->graph->addEdge(
213               array(
214                 $this->testSuites[$this->testSuiteLevel] => $suite->getName()
215               )
216             );
217         }
218
219         $this->testSuiteLevel++;
220         $this->testSuites[$this->testSuiteLevel]                        = $suite->getName();
221         $this->testSuiteFailureOrErrorCount[$this->testSuiteLevel]      = 0;
222         $this->testSuiteIncompleteOrSkippedCount[$this->testSuiteLevel] = 0;
223     }
224
225     /**
226      * A testsuite ended.
227      *
228      * @param  PHPUnit_Framework_TestSuite $suite
229      */
230     public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
231     {
232         $color = 'red';
233
234         if ($this->testSuiteFailureOrErrorCount[$this->testSuiteLevel] == 0 &&
235             $this->testSuiteIncompleteOrSkippedCount[$this->testSuiteLevel] == 0) {
236             $color = 'green';
237         }
238
239         else if ($this->testSuiteFailureOrErrorCount[$this->testSuiteLevel] == 0 &&
240                  $this->testSuiteIncompleteOrSkippedCount[$this->testSuiteLevel] > 0) {
241             $color = 'yellow';
242         }
243
244         $this->graph->addNode(
245           $this->testSuites[$this->testSuiteLevel],
246           array('color' => $color)
247         );
248
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];
252         }
253
254         $this->testSuiteLevel--;
255     }
256
257     /**
258      * A test started.
259      *
260      * @param  PHPUnit_Framework_Test $test
261      */
262     public function startTest(PHPUnit_Framework_Test $test)
263     {
264         $this->currentTestSuccess = TRUE;
265     }
266
267     /**
268      * A test ended.
269      *
270      * @param  PHPUnit_Framework_Test $test
271      * @param  float                  $time
272      */
273     public function endTest(PHPUnit_Framework_Test $test, $time)
274     {
275         if ($this->currentTestSuccess) {
276             $this->addTestNode($test, 'green');
277         }
278     }
279
280     /**
281      * @param  PHPUnit_Framework_Test $test
282      * @param  string                  $color
283      */
284     protected function addTestNode(PHPUnit_Framework_Test $test, $color)
285     {
286         $name = PHPUnit_Util_Test::describe($test, FALSE);
287
288         $this->graph->addNode(
289           $name[1],
290           array('color' => $color),
291           $this->testSuites[$this->testSuiteLevel]
292         );
293
294         $this->graph->addEdge(
295           array(
296             $this->testSuites[$this->testSuiteLevel] => $name[1]
297           )
298         );
299     }
300 }
301 ?>