Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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