PageRenderTime 60ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/phpunit/bootstrap.php

https://github.com/jamiepratt/moodle
PHP | 236 lines | 154 code | 33 blank | 49 comment | 34 complexity | 35d874281c73a8e268c6cb2e719a2566 MD5 | raw file
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Prepares PHPUnit environment, the phpunit.xml configuration
  18. * must specify this file as bootstrap.
  19. *
  20. * Exit codes: {@see phpunit_bootstrap_error()}
  21. *
  22. * @package core
  23. * @category phpunit
  24. * @copyright 2012 Petr Skoda {@link http://skodak.org}
  25. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26. */
  27. // we want to know about all problems
  28. error_reporting(E_ALL | E_STRICT);
  29. ini_set('display_errors', '1');
  30. ini_set('log_errors', '1');
  31. // Make sure OPcache does not strip comments, we need them in phpunit!
  32. if (ini_get('opcache.enable') and strtolower(ini_get('opcache.enable')) !== 'off') {
  33. if (!ini_get('opcache.save_comments') or strtolower(ini_get('opcache.save_comments')) === 'off') {
  34. ini_set('opcache.enable', 0);
  35. } else {
  36. ini_set('opcache.load_comments', 1);
  37. }
  38. }
  39. if (!defined('IGNORE_COMPONENT_CACHE')) {
  40. define('IGNORE_COMPONENT_CACHE', true);
  41. }
  42. require_once(__DIR__.'/bootstraplib.php');
  43. require_once(__DIR__.'/../testing/lib.php');
  44. require_once(__DIR__.'/classes/autoloader.php');
  45. if (isset($_SERVER['REMOTE_ADDR'])) {
  46. phpunit_bootstrap_error(1, 'Unit tests can be executed only from command line!');
  47. }
  48. if (defined('PHPUNIT_TEST')) {
  49. phpunit_bootstrap_error(1, "PHPUNIT_TEST constant must not be manually defined anywhere!");
  50. }
  51. /** PHPUnit testing framework active */
  52. define('PHPUNIT_TEST', true);
  53. if (!defined('PHPUNIT_UTIL')) {
  54. /** Identifies utility scripts - the database does not need to be initialised */
  55. define('PHPUNIT_UTIL', false);
  56. }
  57. if (defined('CLI_SCRIPT')) {
  58. phpunit_bootstrap_error(1, 'CLI_SCRIPT must not be manually defined in any PHPUnit test scripts');
  59. }
  60. define('CLI_SCRIPT', true);
  61. $phpunitversion = PHPUnit_Runner_Version::id();
  62. if ($phpunitversion === '@package_version@') {
  63. // library checked out from git, let's hope dev knows that 3.6.0 is required
  64. } else if (version_compare($phpunitversion, '3.6.0', 'lt')) {
  65. phpunit_bootstrap_error(PHPUNIT_EXITCODE_PHPUNITWRONG, $phpunitversion);
  66. }
  67. unset($phpunitversion);
  68. if (!include_once('PHPUnit/Extensions/Database/Autoload.php')) {
  69. phpunit_bootstrap_error(PHPUNIT_EXITCODE_PHPUNITEXTMISSING, 'phpunit/DbUnit');
  70. }
  71. define('NO_OUTPUT_BUFFERING', true);
  72. // only load CFG from config.php, stop ASAP in lib/setup.php
  73. define('ABORT_AFTER_CONFIG', true);
  74. require(__DIR__ . '/../../config.php');
  75. if (!defined('PHPUNIT_LONGTEST')) {
  76. /** Execute longer version of tests */
  77. define('PHPUNIT_LONGTEST', false);
  78. }
  79. // remove error handling overrides done in config.php
  80. error_reporting(E_ALL | E_STRICT);
  81. ini_set('display_errors', '1');
  82. ini_set('log_errors', '1');
  83. set_time_limit(0); // no time limit in CLI scripts, user may cancel execution
  84. // prepare dataroot
  85. umask(0);
  86. if (isset($CFG->phpunit_directorypermissions)) {
  87. $CFG->directorypermissions = $CFG->phpunit_directorypermissions;
  88. } else {
  89. $CFG->directorypermissions = 02777;
  90. }
  91. $CFG->filepermissions = ($CFG->directorypermissions & 0666);
  92. if (!isset($CFG->phpunit_dataroot)) {
  93. phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, 'Missing $CFG->phpunit_dataroot in config.php, can not run tests!');
  94. }
  95. // Create test dir if does not exists yet.
  96. if (!file_exists($CFG->phpunit_dataroot)) {
  97. mkdir($CFG->phpunit_dataroot, $CFG->directorypermissions);
  98. }
  99. if (!is_dir($CFG->phpunit_dataroot)) {
  100. phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->phpunit_dataroot directory can not be created, can not run tests!');
  101. }
  102. // Ensure we access to phpunit_dataroot realpath always.
  103. $CFG->phpunit_dataroot = realpath($CFG->phpunit_dataroot);
  104. if (isset($CFG->dataroot) and $CFG->phpunit_dataroot === $CFG->dataroot) {
  105. phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->dataroot and $CFG->phpunit_dataroot must not be identical, can not run tests!');
  106. }
  107. if (!is_writable($CFG->phpunit_dataroot)) {
  108. // try to fix permissions if possible
  109. if (function_exists('posix_getuid')) {
  110. $chmod = fileperms($CFG->phpunit_dataroot);
  111. if (fileowner($CFG->phpunit_dataroot) == posix_getuid()) {
  112. $chmod = $chmod | 0700;
  113. chmod($CFG->phpunit_dataroot, $chmod);
  114. }
  115. }
  116. if (!is_writable($CFG->phpunit_dataroot)) {
  117. phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->phpunit_dataroot directory is not writable, can not run tests!');
  118. }
  119. }
  120. if (!file_exists("$CFG->phpunit_dataroot/phpunittestdir.txt")) {
  121. if ($dh = opendir($CFG->phpunit_dataroot)) {
  122. while (($file = readdir($dh)) !== false) {
  123. if ($file === 'phpunit' or $file === '.' or $file === '..' or $file === '.DS_Store') {
  124. continue;
  125. }
  126. phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->phpunit_dataroot directory is not empty, can not run tests! Is it used for anything else?');
  127. }
  128. closedir($dh);
  129. unset($dh);
  130. unset($file);
  131. }
  132. // now we are 100% sure this dir is used only for phpunit tests
  133. testing_initdataroot($CFG->phpunit_dataroot, 'phpunit');
  134. }
  135. // verify db prefix
  136. if (!isset($CFG->phpunit_prefix)) {
  137. phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, 'Missing $CFG->phpunit_prefix in config.php, can not run tests!');
  138. }
  139. if ($CFG->phpunit_prefix === '') {
  140. phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->phpunit_prefix can not be empty, can not run tests!');
  141. }
  142. if (isset($CFG->prefix) and $CFG->prefix === $CFG->phpunit_prefix) {
  143. phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGERROR, '$CFG->prefix and $CFG->phpunit_prefix must not be identical, can not run tests!');
  144. }
  145. // override CFG settings if necessary and throw away extra CFG settings
  146. $CFG->wwwroot = 'http://www.example.com/moodle';
  147. $CFG->dataroot = $CFG->phpunit_dataroot;
  148. $CFG->prefix = $CFG->phpunit_prefix;
  149. $CFG->dbtype = isset($CFG->phpunit_dbtype) ? $CFG->phpunit_dbtype : $CFG->dbtype;
  150. $CFG->dblibrary = isset($CFG->phpunit_dblibrary) ? $CFG->phpunit_dblibrary : $CFG->dblibrary;
  151. $CFG->dbhost = isset($CFG->phpunit_dbhost) ? $CFG->phpunit_dbhost : $CFG->dbhost;
  152. $CFG->dbname = isset($CFG->phpunit_dbname) ? $CFG->phpunit_dbname : $CFG->dbname;
  153. $CFG->dbuser = isset($CFG->phpunit_dbuser) ? $CFG->phpunit_dbuser : $CFG->dbuser;
  154. $CFG->dbpass = isset($CFG->phpunit_dbpass) ? $CFG->phpunit_dbpass : $CFG->dbpass;
  155. $CFG->prefix = isset($CFG->phpunit_prefix) ? $CFG->phpunit_prefix : $CFG->prefix;
  156. $CFG->dboptions = isset($CFG->phpunit_dboptions) ? $CFG->phpunit_dboptions : $CFG->dboptions;
  157. $allowed = array('wwwroot', 'dataroot', 'dirroot', 'admin', 'directorypermissions', 'filepermissions',
  158. 'dbtype', 'dblibrary', 'dbhost', 'dbname', 'dbuser', 'dbpass', 'prefix', 'dboptions',
  159. 'proxyhost', 'proxyport', 'proxytype', 'proxyuser', 'proxypassword', 'proxybypass', // keep proxy settings from config.php
  160. );
  161. $productioncfg = (array)$CFG;
  162. $CFG = new stdClass();
  163. foreach ($productioncfg as $key=>$value) {
  164. if (!in_array($key, $allowed) and strpos($key, 'phpunit_') !== 0) {
  165. // ignore
  166. continue;
  167. }
  168. $CFG->{$key} = $value;
  169. }
  170. unset($key);
  171. unset($value);
  172. unset($allowed);
  173. unset($productioncfg);
  174. // force the same CFG settings in all sites
  175. $CFG->debug = (E_ALL | E_STRICT); // can not use DEBUG_DEVELOPER yet
  176. $CFG->debugdeveloper = true;
  177. $CFG->debugdisplay = 1;
  178. error_reporting($CFG->debug);
  179. ini_set('display_errors', '1');
  180. ini_set('log_errors', '1');
  181. $CFG->noemailever = true; // better not mail anybody from tests, override temporarily if necessary
  182. // some ugly hacks
  183. $CFG->themerev = 1;
  184. $CFG->jsrev = 1;
  185. // load test case stub classes and other stuff
  186. require_once("$CFG->dirroot/lib/phpunit/lib.php");
  187. // finish moodle init
  188. define('ABORT_AFTER_CONFIG_CANCEL', true);
  189. require("$CFG->dirroot/lib/setup.php");
  190. raise_memory_limit(MEMORY_HUGE);
  191. if (PHPUNIT_UTIL) {
  192. // we are not going to do testing, this is 'true' in utility scripts that only init database
  193. return;
  194. }
  195. // is database and dataroot ready for testing?
  196. list($errorcode, $message) = phpunit_util::testing_ready_problem();
  197. // print some version info
  198. phpunit_util::bootstrap_moodle_info();
  199. if ($errorcode) {
  200. phpunit_bootstrap_error($errorcode, $message);
  201. }
  202. // prepare for the first test run - store fresh globals, reset database and dataroot, etc.
  203. phpunit_util::bootstrap_init();