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.3.0
44
 */
45
 
46
require_once 'PHPUnit/Runner/Version.php';
47
require_once 'PHPUnit/Util/Class.php';
48
require_once 'PHPUnit/Util/CodeCoverage.php';
49
require_once 'PHPUnit/Util/File.php';
50
require_once 'PHPUnit/Util/Filter.php';
51
require_once 'PHPUnit/Util/Printer.php';
52
 
53
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
54
 
55
/**
56
 * Generates an XML logfile with code coverage information using the
57
 * Clover format "documented" at
58
 * http://svn.atlassian.com/svn/public/contrib/bamboo/bamboo-coverage-plugin/trunk/src/test/resources/test-clover-report.xml
59
 *
60
 * @category   Testing
61
 * @package    PHPUnit
62
 * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
63
 * @copyright  2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
64
 * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
65
 * @version    Release: 3.4.15
66
 * @link       http://www.phpunit.de/
67
 * @since      Class available since Release 3.1.4
68
 */
69
class PHPUnit_Util_Log_CodeCoverage_XML_Clover extends PHPUnit_Util_Printer
70
{
71
    /**
72
     * @param  PHPUnit_Framework_TestResult $result
73
     * @todo   Count conditionals.
74
     */
75
    public function process(PHPUnit_Framework_TestResult $result)
76
    {
77
        $time = time();
78
 
79
        $document = new DOMDocument('1.0', 'UTF-8');
80
        $document->formatOutput = TRUE;
81
 
82
        $coverage = $document->createElement('coverage');
83
        $coverage->setAttribute('generated', $time);
84
        $coverage->setAttribute('phpunit', PHPUnit_Runner_Version::id());
85
        $document->appendChild($coverage);
86
 
87
        $project = $document->createElement('project');
88
        $project->setAttribute('name', $result->topTestSuite()->getName());
89
        $project->setAttribute('timestamp', $time);
90
        $coverage->appendChild($project);
91
 
92
        $codeCoverageInformation = $result->getCodeCoverageInformation();
93
        $files                   = PHPUnit_Util_CodeCoverage::getSummary(
94
                                     $codeCoverageInformation
95
                                   );
96
        $packages                = array();
97
 
98
        $projectStatistics = array(
99
          'files'               => 0,
100
          'loc'                 => 0,
101
          'ncloc'               => 0,
102
          'classes'             => 0,
103
          'methods'             => 0,
104
          'coveredMethods'      => 0,
105
          'conditionals'        => 0,
106
          'coveredConditionals' => 0,
107
          'statements'          => 0,
108
          'coveredStatements'   => 0
109
        );
110
 
111
        foreach ($files as $filename => $data) {
112
            $namespace = 'global';
113
 
114
            if (file_exists($filename)) {
115
                $fileStatistics = array(
116
                  'classes'             => 0,
117
                  'methods'             => 0,
118
                  'coveredMethods'      => 0,
119
                  'conditionals'        => 0,
120
                  'coveredConditionals' => 0,
121
                  'statements'          => 0,
122
                  'coveredStatements'   => 0
123
                );
124
 
125
                $file = $document->createElement('file');
126
                $file->setAttribute('name', $filename);
127
 
128
                $classesInFile = PHPUnit_Util_File::getClassesInFile($filename);
129
                $lines         = array();
130
 
131
                foreach ($classesInFile as $className => $_class) {
132
                    $classStatistics = array(
133
                      'methods'             => 0,
134
                      'coveredMethods'      => 0,
135
                      'conditionals'        => 0,
136
                      'coveredConditionals' => 0,
137
                      'statements'          => 0,
138
                      'coveredStatements'   => 0
139
                    );
140
 
141
                    foreach ($_class['methods'] as $methodName => $method) {
142
                        $classStatistics['methods']++;
143
 
144
                        $methodCount = 0;
145
 
146
                        for ($i = $method['startLine']; $i <= $method['endLine']; $i++) {
147
                            $add   = TRUE;
148
                            $count = 0;
149
 
150
                            if (isset($files[$filename][$i])) {
151
                                if ($files[$filename][$i] != -2) {
152
                                    $classStatistics['statements']++;
153
                                }
154
 
155
                                if (is_array($files[$filename][$i])) {
156
                                    $classStatistics['coveredStatements']++;
157
                                    $count = count($files[$filename][$i]);
158
                                }
159
 
160
                                else if ($files[$filename][$i] == -2) {
161
                                    $add = FALSE;
162
                                }
163
                            } else {
164
                                $add = FALSE;
165
                            }
166
 
167
                            $methodCount = max($methodCount, $count);
168
 
169
                            if ($add) {
170
                                $lines[$i] = array(
171
                                  'count' => $count,
172
                                  'type'  => 'stmt'
173
                                );
174
                            }
175
                        }
176
 
177
                        if ($methodCount > 0) {
178
                            $classStatistics['coveredMethods']++;
179
                        }
180
 
181
                        $lines[$method['startLine']] = array(
182
                          'count' => $methodCount,
183
                          'type'  => 'method',
184
                          'name'  => $methodName
185
                        );
186
                    }
187
 
188
                    $packageInformation = PHPUnit_Util_Class::getPackageInformation(
189
                      $className, $_class['docComment']
190
                    );
191
 
192
                    if (!empty($packageInformation['namespace'])) {
193
                        $namespace = $packageInformation['namespace'];
194
                    }
195
 
196
                    $class = $document->createElement('class');
197
                    $class->setAttribute('name', $className);
198
                    $class->setAttribute('namespace', $namespace);
199
 
200
                    if (!empty($packageInformation['fullPackage'])) {
201
                        $class->setAttribute(
202
                          'fullPackage', $packageInformation['fullPackage']
203
                        );
204
                    }
205
 
206
                    if (!empty($packageInformation['category'])) {
207
                        $class->setAttribute(
208
                          'category', $packageInformation['category']
209
                        );
210
                    }
211
 
212
                    if (!empty($packageInformation['package'])) {
213
                        $class->setAttribute(
214
                          'package', $packageInformation['package']
215
                        );
216
                    }
217
 
218
                    if (!empty($packageInformation['subpackage'])) {
219
                        $class->setAttribute(
220
                          'subpackage', $packageInformation['subpackage']
221
                        );
222
                    }
223
 
224
                    $file->appendChild($class);
225
 
226
                    $metrics = $document->createElement('metrics');
227
                    $metrics->setAttribute('methods', $classStatistics['methods']);
228
                    $metrics->setAttribute('coveredmethods', $classStatistics['coveredMethods']);
229
                    //$metrics->setAttribute('conditionals', $classStatistics['conditionals']);
230
                    //$metrics->setAttribute('coveredconditionals', $classStatistics['coveredConditionals']);
231
                    $metrics->setAttribute('statements', $classStatistics['statements']);
232
                    $metrics->setAttribute('coveredstatements', $classStatistics['coveredStatements']);
233
                    $metrics->setAttribute('elements', $classStatistics['conditionals'] + $classStatistics['statements'] + $classStatistics['methods']);
234
                    $metrics->setAttribute('coveredelements', $classStatistics['coveredConditionals'] + $classStatistics['coveredStatements'] + $classStatistics['coveredMethods']);
235
                    $class->appendChild($metrics);
236
 
237
                    $fileStatistics['methods']             += $classStatistics['methods'];
238
                    $fileStatistics['coveredMethods']      += $classStatistics['coveredMethods'];
239
                    $fileStatistics['conditionals']        += $classStatistics['conditionals'];
240
                    $fileStatistics['coveredConditionals'] += $classStatistics['coveredConditionals'];
241
                    $fileStatistics['statements']          += $classStatistics['statements'];
242
                    $fileStatistics['coveredStatements']   += $classStatistics['coveredStatements'];
243
                    $fileStatistics['classes']++;
244
                }
245
 
246
                foreach ($data as $_line => $_data) {
247
                    if (isset($lines[$_line])) {
248
                        continue;
249
                    }
250
 
251
                    if ($_data != -2) {
252
                        $fileStatistics['statements']++;
253
 
254
                        if (is_array($_data)) {
255
                            $count = count($_data);
256
                            $fileStatistics['coveredStatements']++;
257
                        } else {
258
                            $count = 0;
259
                        }
260
 
261
                        $lines[$_line] = array(
262
                          'count' => $count,
263
                          'type' => 'stmt'
264
                        );
265
                    }
266
                }
267
 
268
                ksort($lines);
269
 
270
                foreach ($lines as $_line => $_data) {
271
                    $line = $document->createElement('line');
272
                    $line->setAttribute('num', $_line);
273
                    $line->setAttribute('type', $_data['type']);
274
 
275
                    if (isset($_data['name'])) {
276
                        $line->setAttribute('name', $_data['name']);
277
                    }
278
 
279
                    $line->setAttribute('count', $_data['count']);
280
 
281
                    $file->appendChild($line);
282
                }
283
 
284
                $count = PHPUnit_Util_File::countLines($filename);
285
 
286
                $metrics = $document->createElement('metrics');
287
                $metrics->setAttribute('loc', $count['loc']);
288
                $metrics->setAttribute('ncloc', $count['ncloc']);
289
                $metrics->setAttribute('classes', $fileStatistics['classes']);
290
                $metrics->setAttribute('methods', $fileStatistics['methods']);
291
                $metrics->setAttribute('coveredmethods', $fileStatistics['coveredMethods']);
292
                //$metrics->setAttribute('conditionals', $fileStatistics['conditionals']);
293
                //$metrics->setAttribute('coveredconditionals', $fileStatistics['coveredConditionals']);
294
                $metrics->setAttribute('statements', $fileStatistics['statements']);
295
                $metrics->setAttribute('coveredstatements', $fileStatistics['coveredStatements']);
296
                $metrics->setAttribute('elements', $fileStatistics['conditionals'] + $fileStatistics['statements'] + $fileStatistics['methods']);
297
                $metrics->setAttribute('coveredelements', $fileStatistics['coveredConditionals'] + $fileStatistics['coveredStatements'] + $fileStatistics['coveredMethods']);
298
 
299
                $file->appendChild($metrics);
300
 
301
                if ($namespace == 'global') {
302
                    $project->appendChild($file);
303
                } else {
304
                    if (!isset($packages[$namespace])) {
305
                        $packages[$namespace] = $document->createElement(
306
                          'package'
307
                        );
308
 
309
                        $packages[$namespace]->setAttribute('name', $namespace);
310
                        $project->appendChild($packages[$namespace]);
311
                    }
312
 
313
                    $packages[$namespace]->appendChild($file);
314
                }
315
 
316
                $projectStatistics['loc']                 += $count['loc'];
317
                $projectStatistics['ncloc']               += $count['ncloc'];
318
                $projectStatistics['classes']             += $fileStatistics['classes'];
319
                $projectStatistics['methods']             += $fileStatistics['methods'];
320
                $projectStatistics['coveredMethods']      += $fileStatistics['coveredMethods'];
321
                $projectStatistics['conditionals']        += $fileStatistics['conditionals'];
322
                $projectStatistics['coveredConditionals'] += $fileStatistics['coveredConditionals'];
323
                $projectStatistics['statements']          += $fileStatistics['statements'];
324
                $projectStatistics['coveredStatements']   += $fileStatistics['coveredStatements'];
325
                $projectStatistics['files']++;
326
            }
327
        }
328
 
329
        $metrics = $document->createElement('metrics');
330
        $metrics->setAttribute('files', $projectStatistics['files']);
331
        $metrics->setAttribute('loc', $projectStatistics['loc']);
332
        $metrics->setAttribute('ncloc', $projectStatistics['ncloc']);
333
        $metrics->setAttribute('classes', $projectStatistics['classes']);
334
        $metrics->setAttribute('methods', $projectStatistics['methods']);
335
        $metrics->setAttribute('coveredmethods', $projectStatistics['coveredMethods']);
336
        //$metrics->setAttribute('conditionals', $projectStatistics['conditionals']);
337
        //$metrics->setAttribute('coveredconditionals', $projectStatistics['coveredConditionals']);
338
        $metrics->setAttribute('statements', $projectStatistics['statements']);
339
        $metrics->setAttribute('coveredstatements', $projectStatistics['coveredStatements']);
340
        $metrics->setAttribute('elements', $projectStatistics['conditionals'] + $projectStatistics['statements'] + $projectStatistics['methods']);
341
        $metrics->setAttribute('coveredelements', $projectStatistics['coveredConditionals'] + $projectStatistics['coveredStatements'] + $projectStatistics['coveredMethods']);
342
        $project->appendChild($metrics);
343
 
344
        $this->write($document->saveXML());
345
        $this->flush();
346
    }
347
}
348
?>