[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/phpunit/ -> bootstrap.php (source)

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


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1