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