]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/Util/Metrics/File.php
Added unit tests.
[Github/sugarcrm.git] / tests / PHPUnit / Util / Metrics / File.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/Util/Class.php';
48 require_once 'PHPUnit/Util/Metrics.php';
49 require_once 'PHPUnit/Util/Filter.php';
50
51 PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
52
53 /**
54  * File-Level Metrics.
55  *
56  * @category   Testing
57  * @package    PHPUnit
58  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
59  * @copyright  2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
60  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
61  * @version    Release: 3.3.17
62  * @link       http://www.phpunit.de/
63  * @since      Class available since Release 3.2.0
64  */
65 class PHPUnit_Util_Metrics_File extends PHPUnit_Util_Metrics
66 {
67     protected $coverage      = 0;
68     protected $loc           = 0;
69     protected $cloc          = 0;
70     protected $ncloc         = 0;
71     protected $locExecutable = 0;
72     protected $locExecuted   = 0;
73
74     protected $filename;
75     protected $classes = array();
76     protected $functions = array();
77     protected $lines = array();
78     protected $tokens = array();
79
80     protected static $cache = array();
81
82     /**
83      * Constructor.
84      *
85      * @param  string $filename
86      * @param  array  $codeCoverage
87      * @throws RuntimeException
88      */
89     protected function __construct($filename, &$codeCoverage = array())
90     {
91         if (!file_exists($filename)) {
92             throw new RuntimeException(
93               sprintf(
94                 'File "%s" not found.',
95                 $filename
96               )
97             );
98         }
99
100         $this->filename = $filename;
101         $this->lines    = file($filename);
102         $this->tokens   = token_get_all(file_get_contents($filename));
103
104         $this->countLines();
105         $this->setCoverage($codeCoverage);
106
107         foreach (PHPUnit_Util_Class::getClassesInFile($filename) as $class) {
108             $this->classes[$class->getName()] = PHPUnit_Util_Metrics_Class::factory($class, $codeCoverage);
109         }
110
111         foreach (PHPUnit_Util_Class::getFunctionsInFile($filename) as $function) {
112             $this->functions[$function->getName()] = PHPUnit_Util_Metrics_Function::factory($function, $codeCoverage);
113         }
114     }
115
116     /**
117      * Factory.
118      *
119      * @param  string $filename
120      * @param  array  $codeCoverage
121      * @return PHPUnit_Util_Metrics_File
122      */
123     public static function factory($filename, &$codeCoverage = array())
124     {
125         if (!isset(self::$cache[$filename])) {
126             self::$cache[$filename] = new PHPUnit_Util_Metrics_File($filename, $codeCoverage);
127         }
128
129         else if (!empty($codeCoverage) && self::$cache[$filename]->getCoverage() == 0) {
130             self::$cache[$filename]->setCoverage($codeCoverage);
131         }
132
133         return self::$cache[$filename];
134     }
135
136     /**
137      * @param  array $codeCoverage
138      */
139     public function setCoverage(array &$codeCoverage)
140     {
141         if (!empty($codeCoverage)) {
142             $this->calculateCodeCoverage($codeCoverage);
143
144             foreach ($this->classes as $class) {
145                 $class->setCoverage($codeCoverage);
146             }
147
148             foreach ($this->functions as $function) {
149                 $function->setCoverage($codeCoverage);
150             }
151         }
152     }
153
154     /**
155      * Returns the path to the file.
156      *
157      * @return string
158      */
159     public function getPath()
160     {
161         return $this->filename;
162     }
163
164     /**
165      * Classes.
166      *
167      * @return array
168      */
169     public function getClasses()
170     {
171         return $this->classes;
172     }
173
174     /**
175      * A class.
176      *
177      * @param  string $className
178      * @return ReflectionClass
179      */
180     public function getClass($className)
181     {
182         return $this->classes[$className];
183     }
184
185     /**
186      * Functions.
187      *
188      * @return array
189      */
190     public function getFunctions()
191     {
192         return $this->functions;
193     }
194
195     /**
196      * A function.
197      *
198      * @param  string $functionName
199      * @return ReflectionClass
200      */
201     public function getFunction($functionName)
202     {
203         return $this->functions[$functionName];
204     }
205
206     /**
207      * Lines.
208      *
209      * @return array
210      */
211     public function getLines()
212     {
213         return $this->lines;
214     }
215
216     /**
217      * Tokens.
218      *
219      * @return array
220      */
221     public function getTokens()
222     {
223         return $this->tokens;
224     }
225
226     /**
227      * Returns the Code Coverage for the file.
228      *
229      * @return float
230      */
231     public function getCoverage()
232     {
233         return $this->coverage;
234     }
235
236     /**
237      * Lines of Code (LOC).
238      *
239      * @return int
240      */
241     public function getLoc()
242     {
243         return $this->loc;
244     }
245
246     /**
247      * Executable Lines of Code (ELOC).
248      *
249      * @return int
250      */
251     public function getLocExecutable()
252     {
253         return $this->locExecutable;
254     }
255
256     /**
257      * Executed Lines of Code.
258      *
259      * @return int
260      */
261     public function getLocExecuted()
262     {
263         return $this->locExecuted;
264     }
265
266     /**
267      * Comment Lines of Code (CLOC).
268      *
269      * @return int
270      */
271     public function getCloc()
272     {
273         return $this->cloc;
274     }
275
276     /**
277      * Non-Comment Lines of Code (NCLOC).
278      *
279      * @return int
280      */
281     public function getNcloc()
282     {
283         return $this->ncloc;
284     }
285
286     /**
287      * Calculates the Code Coverage for the class.
288      *
289      * @param  array $codeCoverage
290      */
291     protected function calculateCodeCoverage(&$codeCoverage)
292     {
293         $statistics = PHPUnit_Util_CodeCoverage::getStatistics(
294           $codeCoverage,
295           $this->filename,
296           1,
297           count($this->lines)
298         );
299
300         $this->coverage      = $statistics['coverage'];
301         $this->loc           = $statistics['loc'];
302         $this->locExecutable = $statistics['locExecutable'];
303         $this->locExecuted   = $statistics['locExecuted'];
304     }
305
306     /**
307      */
308     protected function countLines()
309     {
310         $this->loc  = count($this->lines);
311         $this->cloc = 0;
312
313         foreach ($this->tokens as $i => $token) {
314             if (is_string($token)) {
315                 continue;
316             }
317
318             list ($token, $value) = $token;
319
320             if ($token == T_COMMENT || $token == T_DOC_COMMENT) {
321                 $this->cloc += count(explode("\n", $value));
322             }
323         }
324
325         $this->ncloc = $this->loc - $this->cloc;
326     }
327 }
328 ?>