]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/PHP/CodeCoverage/Report/HTML/Renderer.php
Merge branch 'master' of github.com:sugarcrm/sugarcrm_dev
[Github/sugarcrm.git] / tests / PHPUnit / PHP / CodeCoverage / Report / HTML / Renderer.php
1 <?php
2 /**
3  * PHP_CodeCoverage
4  *
5  * Copyright (c) 2009-2011, 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   PHP
38  * @package    CodeCoverage
39  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
40  * @copyright  2009-2011 Sebastian Bergmann <sb@sebastian-bergmann.de>
41  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
42  * @link       http://github.com/sebastianbergmann/php-code-coverage
43  * @since      File available since Release 1.1.0
44  */
45
46 /**
47  * Base class for PHP_CodeCoverage_Report_Node renderers.
48  *
49  * @category   PHP
50  * @package    CodeCoverage
51  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
52  * @copyright  2009-2011 Sebastian Bergmann <sb@sebastian-bergmann.de>
53  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
54  * @version    Release: 1.1.1
55  * @link       http://github.com/sebastianbergmann/php-code-coverage
56  * @since      Class available since Release 1.1.0
57  */
58 abstract class PHP_CodeCoverage_Report_HTML_Renderer
59 {
60     /**
61      * @var string
62      */
63     protected $templatePath;
64
65     /**
66      * @var string
67      */
68     protected $charset;
69
70     /**
71      * @var string
72      */
73     protected $generator;
74
75     /**
76      * @var string
77      */
78     protected $date;
79
80     /**
81      * @var integer
82      */
83     protected $lowUpperBound;
84
85     /**
86      * @var integer
87      */
88     protected $highLowerBound;
89
90     /**
91      * Constructor.
92      *
93      * @param string  $templatePath
94      * @param string  $charset
95      * @param string  $generator
96      * @param string  $date
97      * @param integer $lowUpperBound
98      * @param integer $highLowerBound
99      */
100     public function __construct($templatePath, $charset, $generator, $date, $lowUpperBound, $highLowerBound)
101     {
102         $this->templatePath   = $templatePath;
103         $this->charset        = $charset;
104         $this->generator      = $generator;
105         $this->date           = $date;
106         $this->lowUpperBound  = $lowUpperBound;
107         $this->highLowerBound = $highLowerBound;
108     }
109
110     /**
111      * @param  integer $percent
112      * @return array
113      */
114     protected function getColorLevel($percent)
115     {
116         if ($percent < $this->lowUpperBound) {
117             $color = 'scarlet_red';
118             $level = 'Lo';
119         }
120
121         else if ($percent >= $this->lowUpperBound &&
122                  $percent <  $this->highLowerBound) {
123             $color = 'butter';
124             $level = 'Med';
125         }
126
127         else {
128             $color = 'chameleon';
129             $level = 'Hi';
130         }
131
132         return array($color, $level);
133     }
134
135     /**
136      * @param  Text_Template $template
137      * @param  array         $data
138      * @return string
139      */
140     protected function renderItemTemplate(Text_Template $template, array $data)
141     {
142         if (isset($data['numClasses']) && $data['numClasses'] > 0) {
143             list($classesColor, $classesLevel) = $this->getColorLevel(
144               $data['testedClassesPercent']
145             );
146
147             $classesNumber = $data['numTestedClasses'] . ' / ' .
148                              $data['numClasses'];
149         } else {
150             $classesColor  = 'snow';
151             $classesLevel  = 'None';
152             $classesNumber = '&nbsp;';
153         }
154
155         if ($data['numMethods'] > 0) {
156             list($methodsColor, $methodsLevel) = $this->getColorLevel(
157               $data['testedMethodsPercent']
158             );
159
160             $methodsNumber = $data['numTestedMethods'] . ' / ' .
161                              $data['numMethods'];
162         } else {
163             $methodsColor  = 'snow';
164             $methodsLevel  = 'None';
165             $methodsNumber = '&nbsp;';
166         }
167
168         list($linesColor, $linesLevel) = $this->getColorLevel(
169           $data['linesExecutedPercent']
170         );
171
172         $template->setVar(
173           array(
174             'itemClass' => isset($data['itemClass']) ? $data['itemClass'] : '',
175             'icon' => isset($data['icon']) ? $data['icon'] : '',
176             'crap' => isset($data['crap']) ? $data['crap'] : '',
177             'name' => $data['name'],
178             'lines_color' => $linesColor,
179             'lines_executed_width' => $data['linesExecutedPercent'],
180             'lines_not_executed_width' => 100 - $data['linesExecutedPercent'],
181             'lines_executed_percent' => $data['linesExecutedPercentAsString'],
182             'lines_level' => $linesLevel,
183             'num_executed_lines' => $data['numExecutedLines'],
184             'num_executable_lines' => $data['numExecutableLines'],
185             'methods_color' => $methodsColor,
186             'methods_tested_width' => $data['testedMethodsPercent'],
187             'methods_not_tested_width' => 100 - $data['testedMethodsPercent'],
188             'methods_tested_percent' => $data['testedMethodsPercentAsString'],
189             'methods_level' => $methodsLevel,
190             'methods_number' => $methodsNumber,
191             'classes_color' => $classesColor,
192             'classes_tested_width' => isset($data['testedClassesPercent']) ? $data['testedClassesPercent'] : '',
193             'classes_not_tested_width' => isset($data['testedClassesPercent']) ? 100 - $data['testedClassesPercent'] : '',
194             'classes_tested_percent' => isset($data['testedClassesPercentAsString']) ? $data['testedClassesPercentAsString'] : '',
195             'classes_level' => $classesLevel,
196             'classes_number' => $classesNumber
197           )
198         );
199
200         return $template->render();
201     }
202
203     /**
204      * @param Text_Template                $template
205      * @param string                       $title
206      * @param PHP_CodeCoverage_Report_Node $node
207      */
208     protected function setCommonTemplateVariables(Text_Template $template, $title, PHP_CodeCoverage_Report_Node $node = NULL)
209     {
210         $link = '';
211
212         if ($node !== NULL) {
213             $path = $node->getPathAsArray();
214
215             foreach ($path as $step) {
216                 $link .= sprintf(
217                   '%s<a href="%s.html">%s</a>',
218                   !empty($link) ? '/' : '',
219                   $step->getId(),
220                   $step->getName()
221                 );
222             }
223         }
224
225         $template->setVar(
226           array(
227             'title'            => $title,
228             'link'             => $link,
229             'charset'          => $this->charset,
230             'date'             => $this->date,
231             'version'          => '1.1.1',
232             'php_version'      => PHP_VERSION,
233             'generator'        => $this->generator,
234             'low_upper_bound'  => $this->lowUpperBound,
235             'high_lower_bound' => $this->highLowerBound
236           )
237         );
238     }
239 }