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