]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/Util/Report.php
Added unit tests.
[Github/sugarcrm.git] / tests / PHPUnit / Util / Report.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.0.0
45  */
46
47 require_once 'PHPUnit/Framework.php';
48 require_once 'PHPUnit/Util/CodeCoverage.php';
49 require_once 'PHPUnit/Util/Filter.php';
50 require_once 'PHPUnit/Util/Filesystem.php';
51 require_once 'PHPUnit/Util/Report/Node/Directory.php';
52 require_once 'PHPUnit/Util/Report/Node/File.php';
53
54 PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
55
56 /**
57  *
58  *
59  * @category   Testing
60  * @package    PHPUnit
61  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
62  * @copyright  2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
63  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
64  * @version    Release: 3.3.17
65  * @link       http://www.phpunit.de/
66  * @since      Class available since Release 3.0.0
67  * @abstract
68  */
69 abstract class PHPUnit_Util_Report
70 {
71     public static $templatePath;
72
73     /**
74      * Renders the report.
75      *
76      * @param  PHPUnit_Framework_TestResult $result
77      * @param  string                       $target
78      * @param  string                       $charset
79      * @param  boolean                      $yui
80      * @param  boolean                      $highlight
81      * @param  integer                      $lowUpperBound
82      * @param  integer                      $highLowerBound
83      */
84     public static function render(PHPUnit_Framework_TestResult $result, $target, $charset = 'ISO-8859-1', $yui = TRUE, $highlight = FALSE, $lowUpperBound = 35, $highLowerBound = 70)
85     {
86         $target = PHPUnit_Util_Filesystem::getDirectory($target);
87
88         self::$templatePath = sprintf(
89           '%s%sReport%sTemplate%s',
90
91           dirname(__FILE__),
92           DIRECTORY_SEPARATOR,
93           DIRECTORY_SEPARATOR,
94           DIRECTORY_SEPARATOR
95         );
96
97         $codeCoverageInformation = $result->getCodeCoverageInformation();
98         $files                   = PHPUnit_Util_CodeCoverage::getSummary($codeCoverageInformation);
99         $commonPath              = PHPUnit_Util_Filesystem::reducePaths($files);
100         $items                   = self::buildDirectoryStructure($files);
101
102         unset($codeCoverageInformation);
103
104         $topTestSuite = $result->topTestSuite();
105
106         if ($topTestSuite instanceof PHPUnit_Framework_TestSuite) {
107             $name = $topTestSuite->getName();
108         }
109
110         unset($result);
111
112         $root = new PHPUnit_Util_Report_Node_Directory($commonPath, NULL);
113
114         self::addItems($root, $items, $files, $yui, $highlight);
115         self::copyFiles($target);
116
117         PHPUnit_Util_CodeCoverage::clearSummary();
118
119         $root->render(
120           $target,
121           $name,
122           $charset,
123           $lowUpperBound,
124           $highLowerBound
125         );
126     }
127
128     /**
129      * @param  PHPUnit_Util_Report_Node_Directory $root
130      * @param  array   $items
131      * @param  array   $files
132      * @param  boolean $yui
133      * @param  boolean $highlight
134      */
135     protected static function addItems(PHPUnit_Util_Report_Node_Directory $root, array $items, array $files, $yui, $highlight)
136     {
137         foreach ($items as $key => $value) {
138             if (substr($key, -2) == '/f') {
139                 try {
140                     $file = $root->addFile(
141                       substr($key, 0, -2), $value, $yui, $highlight
142                     );
143                 }
144
145                 catch (RuntimeException $e) {
146                     continue;
147                 }
148             } else {
149                 $child = $root->addDirectory($key);
150                 self::addItems($child, $value, $files, $yui, $highlight);
151             }
152         }
153     }
154
155     /**
156      * Builds an array representation of the directory structure.
157      *
158      * For instance,
159      *
160      * <code>
161      * Array
162      * (
163      *     [Money.php] => Array
164      *         (
165      *             ...
166      *         )
167      *
168      *     [MoneyBag.php] => Array
169      *         (
170      *             ...
171      *         )
172      * )
173      * </code>
174      *
175      * is transformed into
176      *
177      * <code>
178      * Array
179      * (
180      *     [.] => Array
181      *         (
182      *             [Money.php] => Array
183      *                 (
184      *                     ...
185      *                 )
186      *
187      *             [MoneyBag.php] => Array
188      *                 (
189      *                     ...
190      *                 )
191      *         )
192      * )
193      * </code>
194      *
195      * @param  array $files
196      * @return array
197      */
198     protected static function buildDirectoryStructure($files)
199     {
200         $result = array();
201
202         foreach ($files as $path => $file) {
203             $path    = explode('/', $path);
204             $pointer = &$result;
205             $max     = count($path);
206
207             for ($i = 0; $i < $max; $i++) {
208                 if ($i == ($max - 1)) {
209                     $type = '/f';
210                 } else {
211                     $type = '';
212                 }
213
214                 $pointer = &$pointer[$path[$i] . $type];
215             }
216
217             $pointer = $file;
218         }
219
220         return $result;
221     }
222
223     /**
224      * @param  string $target
225      */
226     protected static function copyFiles($target)
227     {
228         $files = array(
229           'butter.png',
230           'chameleon.png',
231           'close12_1.gif',
232           'container.css',
233           'container-min.js',
234           'glass.png',
235           'scarlet_red.png',
236           'snow.png',
237           'style.css',
238           'yahoo-dom-event.js'
239         );
240
241         foreach ($files as $file) {
242             copy(self::$templatePath . $file, $target . $file);
243         }
244     }
245 }
246 ?>