]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/Util/Filesystem.php
Added unit tests.
[Github/sugarcrm.git] / tests / PHPUnit / Util / Filesystem.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/Util/Filter.php';
48
49 PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
50
51 /**
52  * Filesystem helpers.
53  *
54  * @category   Testing
55  * @package    PHPUnit
56  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
57  * @copyright  2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
58  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
59  * @version    Release: 3.3.17
60  * @link       http://www.phpunit.de/
61  * @since      Class available since Release 3.0.0
62  * @abstract
63  */
64 class PHPUnit_Util_Filesystem
65 {
66     protected static $buffer = array();
67
68     /**
69      * Starts the collection of loaded files.
70      *
71      * @since  Method available since Release 3.3.0
72      */
73     public static function collectStart()
74     {
75         self::$buffer = get_included_files();
76     }
77
78     /**
79      * Stops the collection of loaded files and
80      * returns the names of the loaded files.
81      *
82      * @return array
83      * @since  Method available since Release 3.3.0
84      */
85     public static function collectEnd()
86     {
87         return array_values(
88           array_diff(get_included_files(), self::$buffer)
89         );
90     }
91
92     /**
93      * Wrapper for file_exists() that searches the include_path.
94      *
95      * @param  string $file
96      * @return mixed
97      * @author Mattis Stordalen Flister <mattis@xait.no>
98      * @since  Method available since Release 3.2.9
99      */
100     public static function fileExistsInIncludePath($file)
101     {
102         if (file_exists($file)) {
103             return realpath($file);
104         }
105
106         $paths = explode(PATH_SEPARATOR, get_include_path());
107
108         foreach ($paths as $path) {
109             $fullpath = $path . DIRECTORY_SEPARATOR . $file;
110
111             if (file_exists($fullpath)) {
112                 return realpath($fullpath);
113             }
114         }
115
116         return FALSE;
117     }
118
119     /**
120      * Returns the common path of a set of files.
121      *
122      * @param  array $paths
123      * @return string
124      * @since  Method available since Release 3.1.0
125      */
126     public static function getCommonPath(array $paths)
127     {
128         $count = count($paths);
129
130         if ($count == 1) {
131             return dirname($paths[0]) . DIRECTORY_SEPARATOR;
132         }
133
134         $_paths = array();
135
136         for ($i = 0; $i < $count; $i++) {
137             $_paths[$i] = explode(DIRECTORY_SEPARATOR, $paths[$i]);
138
139             if (empty($_paths[$i][0])) {
140                 $_paths[$i][0] = DIRECTORY_SEPARATOR;
141             }
142         }
143
144         $common = '';
145         $done   = FALSE;
146         $j      = 0;
147         $count--;
148
149         while (!$done) {
150             for ($i = 0; $i < $count; $i++) {
151                 if ($_paths[$i][$j] != $_paths[$i+1][$j]) {
152                     $done = TRUE;
153                     break;
154                 }
155             }
156
157             if (!$done) {
158                 $common .= $_paths[0][$j];
159
160                 if ($j > 0) {
161                     $common .= DIRECTORY_SEPARATOR;
162                 }
163             }
164
165             $j++;
166         }
167
168         return $common;
169     }
170
171     /**
172      * @param  string $directory
173      * @return string
174      * @throws RuntimeException
175      * @since  Method available since Release 3.3.0
176      */
177     public static function getDirectory($directory)
178     {
179         if (substr($directory, -1, 1) != DIRECTORY_SEPARATOR) {
180             $directory .= DIRECTORY_SEPARATOR;
181         }
182
183         if (is_dir($directory) || mkdir($directory, 0777, TRUE)) {
184             return $directory;
185         } else {
186             throw new RuntimeException(
187               sprintf(
188                 'Directory "%s" does not exist.',
189                 $directory
190               )
191             );
192         }
193     }
194
195     /**
196      * Returns a filesystem safe version of the passed filename.
197      * This function does not operate on full paths, just filenames.
198      *
199      * @param  string $filename
200      * @return string
201      * @author Michael Lively Jr. <m@digitalsandwich.com>
202      */
203     public static function getSafeFilename($filename)
204     {
205         /* characters allowed: A-Z, a-z, 0-9, _ and . */
206         return preg_replace('#[^\w.]#', '_', $filename);
207     }
208
209     /**
210      * Reduces the paths by cutting the longest common start path.
211      *
212      * For instance,
213      *
214      * <code>
215      * Array
216      * (
217      *     [/home/sb/PHPUnit/Samples/Money/Money.php] => Array
218      *         (
219      *             ...
220      *         )
221      *
222      *     [/home/sb/PHPUnit/Samples/Money/MoneyBag.php] => Array
223      *         (
224      *             ...
225      *         )
226      * )
227      * </code>
228      *
229      * is reduced to
230      *
231      * <code>
232      * Array
233      * (
234      *     [Money.php] => Array
235      *         (
236      *             ...
237      *         )
238      *
239      *     [MoneyBag.php] => Array
240      *         (
241      *             ...
242      *         )
243      * )
244      * </code>
245      *
246      * @param  array $files
247      * @return string
248      * @since  Method available since Release 3.3.0
249      */
250     public static function reducePaths(&$files)
251     {
252         if (empty($files)) {
253             return '.';
254         }
255
256         $commonPath = '';
257         $paths      = array_keys($files);
258
259         if (count($files) == 1) {
260             $commonPath                 = dirname($paths[0]);
261             $files[basename($paths[0])] = $files[$paths[0]];
262
263             unset($files[$paths[0]]);
264
265             return $commonPath;
266         }
267
268         $max = count($paths);
269
270         for ($i = 0; $i < $max; $i++) {
271             $paths[$i] = explode(DIRECTORY_SEPARATOR, $paths[$i]);
272
273             if (empty($paths[$i][0])) {
274                 $paths[$i][0] = DIRECTORY_SEPARATOR;
275             }
276         }
277
278         $done = FALSE;
279         $max  = count($paths);
280
281         while (!$done) {
282             for ($i = 0; $i < $max - 1; $i++) {
283                 if (!isset($paths[$i][0]) ||
284                     !isset($paths[$i+1][0]) ||
285                     $paths[$i][0] != $paths[$i+1][0]) {
286                     $done = TRUE;
287                     break;
288                 }
289             }
290
291             if (!$done) {
292                 $commonPath .= $paths[0][0] . (($paths[0][0] != DIRECTORY_SEPARATOR) ? DIRECTORY_SEPARATOR : '');
293
294                 for ($i = 0; $i < $max; $i++) {
295                     array_shift($paths[$i]);
296                 }
297             }
298         }
299
300         $original = array_keys($files);
301         $max      = count($original);
302
303         for ($i = 0; $i < $max; $i++) {
304             $files[join('/', $paths[$i])] = $files[$original[$i]];
305             unset($files[$original[$i]]);
306         }
307
308         ksort($files);
309
310         return $commonPath;
311     }
312 }
313 ?>