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.2.0
44
 */
45
 
46
require_once 'PHPUnit/Runner/Version.php';
47
require_once 'PHPUnit/Util/Metrics/Project.php';
48
require_once 'PHPUnit/Util/Class.php';
49
require_once 'PHPUnit/Util/CodeCoverage.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 software metrics information.
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.2.0
66
 */
67
class PHPUnit_Util_Log_Metrics extends PHPUnit_Util_Printer
68
{
69
    /**
70
     * @param  PHPUnit_Framework_TestResult $result
71
     */
72
    public function process(PHPUnit_Framework_TestResult $result)
73
    {
74
        $codeCoverage   = $result->getCodeCoverageInformation();
75
        $summary        = PHPUnit_Util_CodeCoverage::getSummary($codeCoverage);
76
        $files          = array_keys($summary);
77
        $projectMetrics = new PHPUnit_Util_Metrics_Project($files, $summary);
78
 
79
        $document = new DOMDocument('1.0', 'UTF-8');
80
        $document->formatOutput = TRUE;
81
 
82
        $metrics = $document->createElement('metrics');
83
        $metrics->setAttribute('files', count($projectMetrics->getFiles()));
84
        $metrics->setAttribute('functions', count($projectMetrics->getFunctions()));
85
        $metrics->setAttribute('cls', $projectMetrics->getCLS());
86
        $metrics->setAttribute('clsa', $projectMetrics->getCLSa());
87
        $metrics->setAttribute('clsc', $projectMetrics->getCLSc());
88
        $metrics->setAttribute('roots', $projectMetrics->getRoots());
89
        $metrics->setAttribute('leafs', $projectMetrics->getLeafs());
90
        $metrics->setAttribute('interfs', $projectMetrics->getInterfs());
91
        $metrics->setAttribute('maxdit', $projectMetrics->getMaxDit());
92
 
93
        $document->appendChild($metrics);
94
 
95
        foreach ($projectMetrics->getFiles() as $fileName => $fileMetrics) {
96
            $xmlFile = $metrics->appendChild(
97
              $document->createElement('file')
98
            );
99
 
100
            $xmlFile->setAttribute('name', $fileName);
101
            $xmlFile->setAttribute('classes', count($fileMetrics->getClasses()));
102
            $xmlFile->setAttribute('functions', count($fileMetrics->getFunctions()));
103
            $xmlFile->setAttribute('loc', $fileMetrics->getLoc());
104
            $xmlFile->setAttribute('cloc', $fileMetrics->getCloc());
105
            $xmlFile->setAttribute('ncloc', $fileMetrics->getNcloc());
106
            $xmlFile->setAttribute('locExecutable', $fileMetrics->getLocExecutable());
107
            $xmlFile->setAttribute('locExecuted', $fileMetrics->getLocExecuted());
108
            $xmlFile->setAttribute('coverage', sprintf('%F', $fileMetrics->getCoverage()));
109
 
110
            foreach ($fileMetrics->getClasses() as $className => $classMetrics) {
111
                if (!$classMetrics->getClass()->implementsInterface('PHPUnit_Framework_Test')) {
112
                    $xmlClass = $document->createElement('class');
113
 
114
                    $xmlClass->setAttribute('name', $classMetrics->getClass()->getName());
115
                    $xmlClass->setAttribute('loc', $classMetrics->getLoc());
116
                    $xmlClass->setAttribute('locExecutable', $classMetrics->getLocExecutable());
117
                    $xmlClass->setAttribute('locExecuted', $classMetrics->getLocExecuted());
118
                    $xmlClass->setAttribute('aif', sprintf('%F', $classMetrics->getAIF()));
119
                    $xmlClass->setAttribute('ahf', sprintf('%F', $classMetrics->getAHF()));
120
                    $xmlClass->setAttribute('ca', $classMetrics->getCa());
121
                    $xmlClass->setAttribute('ce', $classMetrics->getCe());
122
                    $xmlClass->setAttribute('csz', $classMetrics->getCSZ());
123
                    $xmlClass->setAttribute('cis', $classMetrics->getCIS());
124
                    $xmlClass->setAttribute('coverage', sprintf('%F', $classMetrics->getCoverage()));
125
                    $xmlClass->setAttribute('dit', $classMetrics->getDIT());
126
                    $xmlClass->setAttribute('i', sprintf('%F', $classMetrics->getI()));
127
                    $xmlClass->setAttribute('impl', $classMetrics->getIMPL());
128
                    $xmlClass->setAttribute('mif', sprintf('%F', $classMetrics->getMIF()));
129
                    $xmlClass->setAttribute('mhf', sprintf('%F', $classMetrics->getMHF()));
130
                    $xmlClass->setAttribute('noc', $classMetrics->getNOC());
131
                    $xmlClass->setAttribute('pf', sprintf('%F', $classMetrics->getPF()));
132
                    $xmlClass->setAttribute('vars', $classMetrics->getVARS());
133
                    $xmlClass->setAttribute('varsnp', $classMetrics->getVARSnp());
134
                    $xmlClass->setAttribute('varsi', $classMetrics->getVARSi());
135
                    $xmlClass->setAttribute('wmc', $classMetrics->getWMC());
136
                    $xmlClass->setAttribute('wmcnp', $classMetrics->getWMCnp());
137
                    $xmlClass->setAttribute('wmci', $classMetrics->getWMCi());
138
 
139
                    foreach ($classMetrics->getMethods() as $methodName => $methodMetrics) {
140
                        $xmlMethod = $xmlClass->appendChild(
141
                          $document->createElement('method')
142
                        );
143
 
144
                        $this->processFunctionOrMethod($methodMetrics, $xmlMethod);
145
                    }
146
 
147
                    $xmlFile->appendChild($xmlClass);
148
                }
149
            }
150
 
151
            foreach ($fileMetrics->getFunctions() as $functionName => $functionMetrics) {
152
                $xmlFunction = $xmlFile->appendChild(
153
                  $document->createElement('function')
154
                );
155
 
156
                $this->processFunctionOrMethod($functionMetrics, $xmlFunction);
157
            }
158
        }
159
 
160
        $this->write($document->saveXML());
161
        $this->flush();
162
    }
163
 
164
    /**
165
     * @param  PHPUnit_Util_Metrics_Function $metrics
166
     * @param  DOMElement                    $element
167
     */
168
    protected function processFunctionOrMethod($metrics, DOMElement $element)
169
    {
170
        $element->setAttribute('name', $metrics->getFunction()->getName());
171
        $element->setAttribute('loc', $metrics->getLoc());
172
        $element->setAttribute('locExecutable', $metrics->getLocExecutable());
173
        $element->setAttribute('locExecuted', $metrics->getLocExecuted());
174
        $element->setAttribute('coverage', sprintf('%F', $metrics->getCoverage()));
175
        $element->setAttribute('ccn', $metrics->getCCN());
176
        $element->setAttribute('crap', sprintf('%F', $metrics->getCrapIndex()));
177
        $element->setAttribute('npath', $metrics->getNPath());
178
        $element->setAttribute('parameters', $metrics->getParameters());
179
    }
180
}
181
?>