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