]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/PHP/CodeCoverage/Report/HTML/Node/Directory.php
Release 6.2.0
[Github/sugarcrm.git] / tests / PHPUnit / PHP / CodeCoverage / Report / HTML / Node / Directory.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.0.0
44  */
45
46 require_once 'PHP/CodeCoverage/Report/HTML/Node/Iterator.php';
47
48 /**
49  * Represents a directory in the code coverage information tree.
50  *
51  * @category   PHP
52  * @package    CodeCoverage
53  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
54  * @copyright  2009-2011 Sebastian Bergmann <sb@sebastian-bergmann.de>
55  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
56  * @version    Release: 1.0.4
57  * @link       http://github.com/sebastianbergmann/php-code-coverage
58  * @since      Class available since Release 1.0.0
59  */
60 class PHP_CodeCoverage_Report_HTML_Node_Directory extends PHP_CodeCoverage_Report_HTML_Node implements IteratorAggregate
61 {
62     /**
63      * @var PHP_CodeCoverage_Report_HTML_Node[]
64      */
65     protected $children = array();
66
67     /**
68      * @var PHP_CodeCoverage_Report_HTML_Node_Directory[]
69      */
70     protected $directories = array();
71
72     /**
73      * @var PHP_CodeCoverage_Report_HTML_Node_File[]
74      */
75     protected $files = array();
76
77     /**
78      * @var array
79      */
80     protected $classes;
81
82     /**
83      * @var integer
84      */
85     protected $numExecutableLines = -1;
86
87     /**
88      * @var integer
89      */
90     protected $numExecutedLines = -1;
91
92     /**
93      * @var integer
94      */
95     protected $numClasses = -1;
96
97     /**
98      * @var integer
99      */
100     protected $numTestedClasses = -1;
101
102     /**
103      * @var integer
104      */
105     protected $numMethods = -1;
106
107     /**
108      * @var integer
109      */
110     protected $numTestedMethods = -1;
111
112     /**
113      * Returns an iterator for this node.
114      *
115      * @return RecursiveIteratorIterator
116      */
117     public function getIterator()
118     {
119         return new RecursiveIteratorIterator(
120           new PHP_CodeCoverage_Report_HTML_Node_Iterator($this),
121           RecursiveIteratorIterator::SELF_FIRST
122         );
123     }
124
125     /**
126      * Adds a new directory.
127      *
128      * @return PHP_CodeCoverage_Report_HTML_Node_Directory
129      */
130     public function addDirectory($name)
131     {
132         $directory = new PHP_CodeCoverage_Report_HTML_Node_Directory(
133           $name, $this
134         );
135
136         $this->children[]    = $directory;
137         $this->directories[] = &$this->children[count($this->children) - 1];
138
139         return $directory;
140     }
141
142     /**
143      * Adds a new file.
144      *
145      * @param  string  $name
146      * @param  array   $lines
147      * @param  boolean $yui
148      * @param  boolean $highlight
149      * @return PHP_CodeCoverage_Report_HTML_Node_File
150      * @throws RuntimeException
151      */
152     public function addFile($name, array $lines, $yui, $highlight)
153     {
154         $file = new PHP_CodeCoverage_Report_HTML_Node_File(
155           $name, $this, $lines, $yui, $highlight
156         );
157
158         $this->children[] = $file;
159         $this->files[]    = &$this->children[count($this->children) - 1];
160
161         $this->numExecutableLines = -1;
162         $this->numExecutedLines   = -1;
163
164         return $file;
165     }
166
167     /**
168      * Returns the directories in this directory.
169      *
170      * @return array
171      */
172     public function getDirectories()
173     {
174         return $this->directories;
175     }
176
177     /**
178      * Returns the files in this directory.
179      *
180      * @return array
181      */
182     public function getFiles()
183     {
184         return $this->files;
185     }
186
187     /**
188      * Returns the child nodes of this node.
189      *
190      * @return array
191      */
192     public function getChildNodes()
193     {
194         return $this->children;
195     }
196
197     /**
198      * Returns the classes of this node.
199      *
200      * @return array
201      */
202     public function getClasses()
203     {
204         if ($this->classes === NULL) {
205             $this->classes = array();
206
207             foreach ($this->children as $child) {
208                 $this->classes = array_merge(
209                   $this->classes, $child->getClasses()
210                 );
211             }
212         }
213
214         return $this->classes;
215     }
216
217     /**
218      * Returns the number of executable lines.
219      *
220      * @return integer
221      */
222     public function getNumExecutableLines()
223     {
224         if ($this->numExecutableLines == -1) {
225             $this->numExecutableLines = 0;
226
227             foreach ($this->children as $child) {
228                 $this->numExecutableLines += $child->getNumExecutableLines();
229             }
230         }
231
232         return $this->numExecutableLines;
233     }
234
235     /**
236      * Returns the number of executed lines.
237      *
238      * @return integer
239      */
240     public function getNumExecutedLines()
241     {
242         if ($this->numExecutedLines == -1) {
243             $this->numExecutedLines = 0;
244
245             foreach ($this->children as $child) {
246                 $this->numExecutedLines += $child->getNumExecutedLines();
247             }
248         }
249
250         return $this->numExecutedLines;
251     }
252
253     /**
254      * Returns the number of classes.
255      *
256      * @return integer
257      */
258     public function getNumClasses()
259     {
260         if ($this->numClasses == -1) {
261             $this->numClasses = 0;
262
263             foreach ($this->children as $child) {
264                 $this->numClasses += $child->getNumClasses();
265             }
266         }
267
268         return $this->numClasses;
269     }
270
271     /**
272      * Returns the number of tested classes.
273      *
274      * @return integer
275      */
276     public function getNumTestedClasses()
277     {
278         if ($this->numTestedClasses == -1) {
279             $this->numTestedClasses = 0;
280
281             foreach ($this->children as $child) {
282                 $this->numTestedClasses += $child->getNumTestedClasses();
283             }
284         }
285
286         return $this->numTestedClasses;
287     }
288
289     /**
290      * Returns the number of methods.
291      *
292      * @return integer
293      */
294     public function getNumMethods()
295     {
296         if ($this->numMethods == -1) {
297             $this->numMethods = 0;
298
299             foreach ($this->children as $child) {
300                 $this->numMethods += $child->getNumMethods();
301             }
302         }
303
304         return $this->numMethods;
305     }
306
307     /**
308      * Returns the number of tested methods.
309      *
310      * @return integer
311      */
312     public function getNumTestedMethods()
313     {
314         if ($this->numTestedMethods == -1) {
315             $this->numTestedMethods = 0;
316
317             foreach ($this->children as $child) {
318                 $this->numTestedMethods += $child->getNumTestedMethods();
319             }
320         }
321
322         return $this->numTestedMethods;
323     }
324
325     /**
326      * Renders this node.
327      *
328      * @param string  $target
329      * @param string  $title
330      * @param string  $charset
331      * @param integer $lowUpperBound
332      * @param integer $highLowerBound
333      * @param string  $generator
334      */
335     public function render($target, $title, $charset = 'UTF-8', $lowUpperBound = 35, $highLowerBound = 70, $generator = '')
336     {
337         $this->doRender(
338           $target, $title, $charset, $lowUpperBound, $highLowerBound, $generator
339         );
340
341         foreach ($this->children as $child) {
342             $child->render(
343               $target,
344               $title,
345               $charset,
346               $lowUpperBound,
347               $highLowerBound,
348               $generator
349             );
350         }
351
352         $this->children = array();
353     }
354
355     /**
356      * @param string  $target
357      * @param string  $title
358      * @param string  $charset
359      * @param integer $lowUpperBound
360      * @param integer $highLowerBound
361      * @param string  $generator
362      */
363     protected function doRender($target, $title, $charset, $lowUpperBound, $highLowerBound, $generator)
364     {
365         $cleanId = PHP_CodeCoverage_Util::getSafeFilename($this->getId());
366         $file    = $target . $cleanId . '.html';
367
368         $template = new Text_Template(
369           PHP_CodeCoverage_Report_HTML::$templatePath . 'directory.html'
370         );
371
372         $this->setTemplateVars($template, $title, $charset, $generator);
373
374         $template->setVar(
375           array(
376             'total_item'       => $this->renderTotalItem(
377                                     $lowUpperBound, $highLowerBound
378                                   ),
379             'items'            => $this->renderItems(
380                                     $lowUpperBound, $highLowerBound
381                                   ),
382             'low_upper_bound'  => $lowUpperBound,
383             'high_lower_bound' => $highLowerBound
384           )
385         );
386
387         $template->renderTo($file);
388
389         $this->directories = array();
390         $this->files       = array();
391     }
392
393     /**
394      * @param  float  $lowUpperBound
395      * @param  float  $highLowerBound
396      * @return string
397      */
398     protected function renderItems($lowUpperBound, $highLowerBound)
399     {
400         $items = $this->doRenderItems(
401           $this->directories, $lowUpperBound, $highLowerBound, 'coverDirectory'
402         );
403
404         $items .= $this->doRenderItems(
405           $this->files, $lowUpperBound, $highLowerBound, 'coverFile'
406         );
407
408         return $items;
409     }
410
411     /**
412      * @param  array  $items
413      * @param  float  $lowUpperBound
414      * @param  float  $highLowerBound
415      * @param  string $itemClass
416      * @return string
417      */
418     protected function doRenderItems(array $items, $lowUpperBound, $highLowerBound, $itemClass)
419     {
420         $result = '';
421
422         foreach ($items as $item) {
423             $result .= $this->doRenderItemObject(
424               $item, $lowUpperBound, $highLowerBound, NULL, $itemClass
425             );
426         }
427
428         return $result;
429     }
430 }