Mercurial > hg > isophonics-drupal-site
comparison vendor/phpunit/php-code-coverage/src/CodeCoverage/Report/HTML/Renderer.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 /* | |
3 * This file is part of the PHP_CodeCoverage package. | |
4 * | |
5 * (c) Sebastian Bergmann <sebastian@phpunit.de> | |
6 * | |
7 * For the full copyright and license information, please view the LICENSE | |
8 * file that was distributed with this source code. | |
9 */ | |
10 | |
11 use SebastianBergmann\Environment\Runtime; | |
12 | |
13 /** | |
14 * Base class for PHP_CodeCoverage_Report_Node renderers. | |
15 * | |
16 * @since Class available since Release 1.1.0 | |
17 */ | |
18 abstract class PHP_CodeCoverage_Report_HTML_Renderer | |
19 { | |
20 /** | |
21 * @var string | |
22 */ | |
23 protected $templatePath; | |
24 | |
25 /** | |
26 * @var string | |
27 */ | |
28 protected $generator; | |
29 | |
30 /** | |
31 * @var string | |
32 */ | |
33 protected $date; | |
34 | |
35 /** | |
36 * @var int | |
37 */ | |
38 protected $lowUpperBound; | |
39 | |
40 /** | |
41 * @var int | |
42 */ | |
43 protected $highLowerBound; | |
44 | |
45 /** | |
46 * @var string | |
47 */ | |
48 protected $version; | |
49 | |
50 /** | |
51 * Constructor. | |
52 * | |
53 * @param string $templatePath | |
54 * @param string $generator | |
55 * @param string $date | |
56 * @param int $lowUpperBound | |
57 * @param int $highLowerBound | |
58 */ | |
59 public function __construct($templatePath, $generator, $date, $lowUpperBound, $highLowerBound) | |
60 { | |
61 $version = new SebastianBergmann\Version('2.2.4', dirname(dirname(dirname(dirname(__DIR__))))); | |
62 | |
63 $this->templatePath = $templatePath; | |
64 $this->generator = $generator; | |
65 $this->date = $date; | |
66 $this->lowUpperBound = $lowUpperBound; | |
67 $this->highLowerBound = $highLowerBound; | |
68 $this->version = $version->getVersion(); | |
69 } | |
70 | |
71 /** | |
72 * @param Text_Template $template | |
73 * @param array $data | |
74 * @return string | |
75 */ | |
76 protected function renderItemTemplate(Text_Template $template, array $data) | |
77 { | |
78 $numSeparator = ' / '; | |
79 | |
80 if (isset($data['numClasses']) && $data['numClasses'] > 0) { | |
81 $classesLevel = $this->getColorLevel($data['testedClassesPercent']); | |
82 | |
83 $classesNumber = $data['numTestedClasses'] . $numSeparator . | |
84 $data['numClasses']; | |
85 | |
86 $classesBar = $this->getCoverageBar( | |
87 $data['testedClassesPercent'] | |
88 ); | |
89 } else { | |
90 $classesLevel = 'success'; | |
91 $classesNumber = '0' . $numSeparator . '0'; | |
92 $classesBar = $this->getCoverageBar(100); | |
93 } | |
94 | |
95 if ($data['numMethods'] > 0) { | |
96 $methodsLevel = $this->getColorLevel($data['testedMethodsPercent']); | |
97 | |
98 $methodsNumber = $data['numTestedMethods'] . $numSeparator . | |
99 $data['numMethods']; | |
100 | |
101 $methodsBar = $this->getCoverageBar( | |
102 $data['testedMethodsPercent'] | |
103 ); | |
104 } else { | |
105 $methodsLevel = 'success'; | |
106 $methodsNumber = '0' . $numSeparator . '0'; | |
107 $methodsBar = $this->getCoverageBar(100); | |
108 $data['testedMethodsPercentAsString'] = '100.00%'; | |
109 } | |
110 | |
111 if ($data['numExecutableLines'] > 0) { | |
112 $linesLevel = $this->getColorLevel($data['linesExecutedPercent']); | |
113 | |
114 $linesNumber = $data['numExecutedLines'] . $numSeparator . | |
115 $data['numExecutableLines']; | |
116 | |
117 $linesBar = $this->getCoverageBar( | |
118 $data['linesExecutedPercent'] | |
119 ); | |
120 } else { | |
121 $linesLevel = 'success'; | |
122 $linesNumber = '0' . $numSeparator . '0'; | |
123 $linesBar = $this->getCoverageBar(100); | |
124 $data['linesExecutedPercentAsString'] = '100.00%'; | |
125 } | |
126 | |
127 $template->setVar( | |
128 array( | |
129 'icon' => isset($data['icon']) ? $data['icon'] : '', | |
130 'crap' => isset($data['crap']) ? $data['crap'] : '', | |
131 'name' => $data['name'], | |
132 'lines_bar' => $linesBar, | |
133 'lines_executed_percent' => $data['linesExecutedPercentAsString'], | |
134 'lines_level' => $linesLevel, | |
135 'lines_number' => $linesNumber, | |
136 'methods_bar' => $methodsBar, | |
137 'methods_tested_percent' => $data['testedMethodsPercentAsString'], | |
138 'methods_level' => $methodsLevel, | |
139 'methods_number' => $methodsNumber, | |
140 'classes_bar' => $classesBar, | |
141 'classes_tested_percent' => isset($data['testedClassesPercentAsString']) ? $data['testedClassesPercentAsString'] : '', | |
142 'classes_level' => $classesLevel, | |
143 'classes_number' => $classesNumber | |
144 ) | |
145 ); | |
146 | |
147 return $template->render(); | |
148 } | |
149 | |
150 /** | |
151 * @param Text_Template $template | |
152 * @param PHP_CodeCoverage_Report_Node $node | |
153 */ | |
154 protected function setCommonTemplateVariables(Text_Template $template, PHP_CodeCoverage_Report_Node $node) | |
155 { | |
156 $runtime = new Runtime; | |
157 | |
158 $template->setVar( | |
159 array( | |
160 'id' => $node->getId(), | |
161 'full_path' => $node->getPath(), | |
162 'path_to_root' => $this->getPathToRoot($node), | |
163 'breadcrumbs' => $this->getBreadcrumbs($node), | |
164 'date' => $this->date, | |
165 'version' => $this->version, | |
166 'runtime_name' => $runtime->getName(), | |
167 'runtime_version' => $runtime->getVersion(), | |
168 'runtime_link' => $runtime->getVendorUrl(), | |
169 'generator' => $this->generator, | |
170 'low_upper_bound' => $this->lowUpperBound, | |
171 'high_lower_bound' => $this->highLowerBound | |
172 ) | |
173 ); | |
174 } | |
175 | |
176 protected function getBreadcrumbs(PHP_CodeCoverage_Report_Node $node) | |
177 { | |
178 $breadcrumbs = ''; | |
179 $path = $node->getPathAsArray(); | |
180 $pathToRoot = array(); | |
181 $max = count($path); | |
182 | |
183 if ($node instanceof PHP_CodeCoverage_Report_Node_File) { | |
184 $max--; | |
185 } | |
186 | |
187 for ($i = 0; $i < $max; $i++) { | |
188 $pathToRoot[] = str_repeat('../', $i); | |
189 } | |
190 | |
191 foreach ($path as $step) { | |
192 if ($step !== $node) { | |
193 $breadcrumbs .= $this->getInactiveBreadcrumb( | |
194 $step, | |
195 array_pop($pathToRoot) | |
196 ); | |
197 } else { | |
198 $breadcrumbs .= $this->getActiveBreadcrumb($step); | |
199 } | |
200 } | |
201 | |
202 return $breadcrumbs; | |
203 } | |
204 | |
205 protected function getActiveBreadcrumb(PHP_CodeCoverage_Report_Node $node) | |
206 { | |
207 $buffer = sprintf( | |
208 ' <li class="active">%s</li>' . "\n", | |
209 $node->getName() | |
210 ); | |
211 | |
212 if ($node instanceof PHP_CodeCoverage_Report_Node_Directory) { | |
213 $buffer .= ' <li>(<a href="dashboard.html">Dashboard</a>)</li>' . "\n"; | |
214 } | |
215 | |
216 return $buffer; | |
217 } | |
218 | |
219 protected function getInactiveBreadcrumb(PHP_CodeCoverage_Report_Node $node, $pathToRoot) | |
220 { | |
221 return sprintf( | |
222 ' <li><a href="%sindex.html">%s</a></li>' . "\n", | |
223 $pathToRoot, | |
224 $node->getName() | |
225 ); | |
226 } | |
227 | |
228 protected function getPathToRoot(PHP_CodeCoverage_Report_Node $node) | |
229 { | |
230 $id = $node->getId(); | |
231 $depth = substr_count($id, '/'); | |
232 | |
233 if ($id != 'index' && | |
234 $node instanceof PHP_CodeCoverage_Report_Node_Directory) { | |
235 $depth++; | |
236 } | |
237 | |
238 return str_repeat('../', $depth); | |
239 } | |
240 | |
241 protected function getCoverageBar($percent) | |
242 { | |
243 $level = $this->getColorLevel($percent); | |
244 | |
245 $template = new Text_Template( | |
246 $this->templatePath . 'coverage_bar.html', | |
247 '{{', | |
248 '}}' | |
249 ); | |
250 | |
251 $template->setVar(array('level' => $level, 'percent' => sprintf('%.2F', $percent))); | |
252 | |
253 return $template->render(); | |
254 } | |
255 | |
256 /** | |
257 * @param int $percent | |
258 * @return string | |
259 */ | |
260 protected function getColorLevel($percent) | |
261 { | |
262 if ($percent <= $this->lowUpperBound) { | |
263 return 'danger'; | |
264 } elseif ($percent > $this->lowUpperBound && | |
265 $percent < $this->highLowerBound) { | |
266 return 'warning'; | |
267 } else { | |
268 return 'success'; | |
269 } | |
270 } | |
271 } |