5
* Copyright (c) 2002-2010, 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-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
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';
53
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
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
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.14
66
* @link http://www.phpunit.de/
67
* @since Class available since Release 3.1.4
69
class PHPUnit_Util_Log_CodeCoverage_XML_Clover extends PHPUnit_Util_Printer
72
* @param PHPUnit_Framework_TestResult $result
73
* @todo Count conditionals.
75
public function process(PHPUnit_Framework_TestResult $result)
79
$document = new DOMDocument('1.0', 'UTF-8');
80
$document->formatOutput = TRUE;
82
$coverage = $document->createElement('coverage');
83
$coverage->setAttribute('generated', $time);
84
$coverage->setAttribute('phpunit', PHPUnit_Runner_Version::id());
85
$document->appendChild($coverage);
87
$project = $document->createElement('project');
88
$project->setAttribute('name', $result->topTestSuite()->getName());
89
$project->setAttribute('timestamp', $time);
90
$coverage->appendChild($project);
92
$codeCoverageInformation = $result->getCodeCoverageInformation();
93
$files = PHPUnit_Util_CodeCoverage::getSummary(
94
$codeCoverageInformation
98
$projectStatistics = array(
104
'coveredMethods' => 0,
106
'coveredConditionals' => 0,
108
'coveredStatements' => 0
111
foreach ($files as $filename => $data) {
112
$namespace = 'global';
114
if (file_exists($filename)) {
115
$fileStatistics = array(
118
'coveredMethods' => 0,
120
'coveredConditionals' => 0,
122
'coveredStatements' => 0
125
$file = $document->createElement('file');
126
$file->setAttribute('name', $filename);
128
$classesInFile = PHPUnit_Util_File::getClassesInFile($filename);
131
foreach ($classesInFile as $className => $_class) {
132
$classStatistics = array(
134
'coveredMethods' => 0,
136
'coveredConditionals' => 0,
138
'coveredStatements' => 0
141
foreach ($_class['methods'] as $methodName => $method) {
142
$classStatistics['methods']++;
146
for ($i = $method['startLine']; $i <= $method['endLine']; $i++) {
150
if (isset($files[$filename][$i])) {
151
if ($files[$filename][$i] != -2) {
152
$classStatistics['statements']++;
155
if (is_array($files[$filename][$i])) {
156
$classStatistics['coveredStatements']++;
157
$count = count($files[$filename][$i]);
160
else if ($files[$filename][$i] == -2) {
167
$methodCount = max($methodCount, $count);
177
if ($methodCount > 0) {
178
$classStatistics['coveredMethods']++;
181
$lines[$method['startLine']] = array(
182
'count' => $methodCount,
184
'name' => $methodName
188
$packageInformation = PHPUnit_Util_Class::getPackageInformation(
189
$className, $_class['docComment']
192
if (!empty($packageInformation['namespace'])) {
193
$namespace = $packageInformation['namespace'];
196
$class = $document->createElement('class');
197
$class->setAttribute('name', $className);
198
$class->setAttribute('namespace', $namespace);
200
if (!empty($packageInformation['fullPackage'])) {
201
$class->setAttribute(
202
'fullPackage', $packageInformation['fullPackage']
206
if (!empty($packageInformation['category'])) {
207
$class->setAttribute(
208
'category', $packageInformation['category']
212
if (!empty($packageInformation['package'])) {
213
$class->setAttribute(
214
'package', $packageInformation['package']
218
if (!empty($packageInformation['subpackage'])) {
219
$class->setAttribute(
220
'subpackage', $packageInformation['subpackage']
224
$file->appendChild($class);
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);
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']++;
246
foreach ($data as $_line => $_data) {
247
if (isset($lines[$_line])) {
252
$fileStatistics['statements']++;
254
if (is_array($_data)) {
255
$count = count($_data);
256
$fileStatistics['coveredStatements']++;
261
$lines[$_line] = array(
270
foreach ($lines as $_line => $_data) {
271
$line = $document->createElement('line');
272
$line->setAttribute('num', $_line);
273
$line->setAttribute('type', $_data['type']);
275
if (isset($_data['name'])) {
276
$line->setAttribute('name', $_data['name']);
279
$line->setAttribute('count', $_data['count']);
281
$file->appendChild($line);
284
$count = PHPUnit_Util_File::countLines($filename);
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']);
299
$file->appendChild($metrics);
301
if ($namespace == 'global') {
302
$project->appendChild($file);
304
if (!isset($packages[$namespace])) {
305
$packages[$namespace] = $document->createElement(
309
$packages[$namespace]->setAttribute('name', $namespace);
310
$project->appendChild($packages[$namespace]);
313
$packages[$namespace]->appendChild($file);
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']++;
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);
344
$this->write($document->saveXML());
b'\\ No newline at end of file'