]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/Util/Fileloader.php
Added unit tests.
[Github/sugarcrm.git] / tests / PHPUnit / Util / Fileloader.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 2.3.0
45  */
46
47 require_once 'PHPUnit/Util/Filter.php';
48 require_once 'PHPUnit/Util/Filesystem.php';
49
50 PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
51
52 /**
53  * Utility methods to load PHP sourcefiles.
54  *
55  * @category   Testing
56  * @package    PHPUnit
57  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
58  * @copyright  2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
59  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
60  * @version    Release: @package_version@
61  * @link       http://www.phpunit.de/
62  * @since      Class available since Release 2.3.0
63  */
64 class PHPUnit_Util_Fileloader
65 {
66     /**
67      * Path to the PHP interpreter that is to be used.
68      *
69      * @var    string $phpBinary
70      */
71     public static $phpBinary = NULL;
72
73     /**
74      * Checks if a PHP sourcefile is readable and is optionally checked for
75      * syntax errors through the syntaxCheck() method. The sourcefile is
76      * loaded through the load() method.
77      *
78      * @param  string  $filename
79      * @param  boolean $syntaxCheck
80      * @throws RuntimeException
81      */
82     public static function checkAndLoad($filename, $syntaxCheck = TRUE)
83     {
84         if (!is_readable($filename)) {
85             $filename = './' . $filename;
86         }
87
88         if (!is_readable($filename)) {
89             throw new RuntimeException(
90               sprintf(
91                 'File "%s" does not exist.',
92                 str_replace('./', '', $filename)
93               )
94             );
95         }
96
97         if ($syntaxCheck) {
98             self::syntaxCheck($filename);
99         }
100
101         self::load($filename);
102     }
103
104     /**
105      * Loads a PHP sourcefile.
106      *
107      * @param  string $filename
108      * @return mixed
109      * @since  Method available since Release 3.0.0
110      */
111     public static function load($filename)
112     {
113         $filename = PHPUnit_Util_Filesystem::fileExistsInIncludePath($filename);
114
115         if (!$filename) {
116             throw new RuntimeException(
117               sprintf(
118                 'File "%s" does not exist.',
119                 $filename
120               )
121             );
122         }
123
124         $oldVariableNames = array_keys(get_defined_vars());
125
126         include_once $filename;
127
128         $newVariables     = get_defined_vars();
129         $newVariableNames = array_diff(array_keys($newVariables), $oldVariableNames);
130
131         foreach ($newVariableNames as $variableName) {
132             if ($variableName != 'oldVariableNames') {
133                 $GLOBALS[$variableName] = $newVariables[$variableName];
134             }
135         }
136
137         return $filename;
138     }
139
140     /**
141      * Uses a separate process to perform a syntax check on a PHP sourcefile.
142      *
143      * PHPUnit_Util_Fileloader::$phpBinary contains the path to the PHP
144      * interpreter used for this. If unset, the following assumptions will
145      * be made:
146      *
147      *   1. When the PHP CLI/CGI binary configured with the PEAR Installer
148      *      (php_bin configuration value) is readable, it will be used.
149      *
150      *   2. When PHPUnit is run using the CLI SAPI and the $_SERVER['_']
151      *      variable does not contain the string "PHPUnit", $_SERVER['_']
152      *      is assumed to contain the path to the current PHP interpreter
153      *      and that will be used.
154      *
155      *   3. When PHPUnit is run using the CLI SAPI and the $_SERVER['_']
156      *      variable contains the string "PHPUnit", the file that $_SERVER['_']
157      *      points is assumed to be the PHPUnit TextUI CLI wrapper script
158      *      "phpunit" and the binary set up using #! on that file's first
159      *      line of code is assumed to contain the path to the current PHP
160      *      interpreter and that will be used.
161      *
162      *   4. The current PHP interpreter is assumed to be in the $PATH and
163      *      to be invokable through "php".
164      *
165      * @param  string $filename
166      * @throws RuntimeException
167      * @since  Method available since Release 3.0.0
168      */
169     protected static function syntaxCheck($filename)
170     {
171         if (self::$phpBinary === NULL) {
172             if (is_readable('@php_bin@')) {
173                 self::$phpBinary = '@php_bin@';
174             }
175
176             else if (PHP_SAPI == 'cli' && isset($_SERVER['_']) &&
177                      strpos($_SERVER['_'], 'phpunit') !== FALSE) {
178                 $file            = file($_SERVER['_']);
179                 $tmp             = explode(' ', $file[0]);
180                 self::$phpBinary = trim($tmp[1]);
181             }
182
183             if (!is_readable(self::$phpBinary)) {
184                 self::$phpBinary = 'php';
185             } else {
186                 self::$phpBinary = escapeshellarg(self::$phpBinary);
187             }
188         }
189
190         $command = self::$phpBinary . ' -l ' . escapeshellarg($filename);
191
192         if (DIRECTORY_SEPARATOR == '\\') {
193             $command = '"' . $command . '"';
194         }
195
196         $output = shell_exec($command);
197
198         if (strpos($output, 'Errors parsing') !== FALSE) {
199             throw new RuntimeException($output);
200         }
201     }
202 }
203 ?>