Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   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   * PHPUnit related utilities.
  19   *
  20   * Exit codes: {@see phpunit_bootstrap_error()}
  21   *
  22   * @package    tool_phpunit
  23   * @copyright  2012 Petr Skoda {@link http://skodak.org}
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  if (isset($_SERVER['REMOTE_ADDR'])) {
  28      die; // no access from web!
  29  }
  30  
  31  define('IGNORE_COMPONENT_CACHE', true);
  32  
  33  require_once (__DIR__.'/../../../../lib/clilib.php');
  34  require_once (__DIR__.'/../../../../lib/phpunit/bootstraplib.php');
  35  require_once (__DIR__.'/../../../../lib/testing/lib.php');
  36  
  37  // now get cli options
  38  list($options, $unrecognized) = cli_get_params(
  39      array(
  40          'drop'                  => false,
  41          'install'               => false,
  42          'buildconfig'           => false,
  43          'buildcomponentconfigs' => false,
  44          'diag'                  => false,
  45          'run'                   => false,
  46          'help'                  => false,
  47      ),
  48      array(
  49          'h' => 'help'
  50      )
  51  );
  52  
  53  if (file_exists(__DIR__.'/../../../../vendor/phpunit/phpunit/composer.json')) {
  54      // Composer packages present.
  55      require_once(__DIR__.'/../../../../vendor/autoload.php');
  56  
  57  } else {
  58      // Note: installation via PEAR is not supported any more.
  59      phpunit_bootstrap_error(PHPUNIT_EXITCODE_PHPUNITMISSING);
  60  }
  61  
  62  if ($options['install'] or $options['drop']) {
  63      define('CACHE_DISABLE_ALL', true);
  64  }
  65  
  66  if ($options['run']) {
  67      unset($options);
  68      unset($unrecognized);
  69  
  70      foreach ($_SERVER['argv'] as $k=>$v) {
  71          if (strpos($v, '--run') === 0) {
  72              unset($_SERVER['argv'][$k]);
  73              $_SERVER['argc'] = $_SERVER['argc'] - 1;
  74          }
  75      }
  76      $_SERVER['argv'] = array_values($_SERVER['argv']);
  77      PHPUnit\TextUI\Command::main();
  78      exit(0);
  79  }
  80  
  81  define('PHPUNIT_UTIL', true);
  82  
  83  require (__DIR__ . '/../../../../lib/phpunit/bootstrap.php');
  84  
  85  // from now on this is a regular moodle CLI_SCRIPT
  86  
  87  require_once($CFG->libdir.'/adminlib.php');
  88  require_once($CFG->libdir.'/upgradelib.php');
  89  require_once($CFG->libdir.'/clilib.php');
  90  require_once($CFG->libdir.'/installlib.php');
  91  
  92  if ($unrecognized) {
  93      $unrecognized = implode("\n  ", $unrecognized);
  94      cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
  95  }
  96  
  97  $diag = $options['diag'];
  98  $drop = $options['drop'];
  99  $install = $options['install'];
 100  $buildconfig = $options['buildconfig'];
 101  $buildcomponentconfigs = $options['buildcomponentconfigs'];
 102  
 103  if ($options['help'] or (!$drop and !$install and !$buildconfig and !$buildcomponentconfigs and !$diag)) {
 104      $help = "Various PHPUnit utility functions
 105  
 106  Options:
 107  --drop         Drop database and dataroot
 108  --install      Install database
 109  --diag         Diagnose installation and return error code only
 110  --run          Execute PHPUnit tests (alternative for standard phpunit binary)
 111  --buildconfig  Build /phpunit.xml from /phpunit.xml.dist that runs all tests
 112  --buildcomponentconfigs
 113                 Build distributed phpunit.xml files for each component
 114  
 115  -h, --help     Print out this help
 116  
 117  Example:
 118  \$ php ".testing_cli_argument_path('/admin/tool/phpunit/cli/util.php')." --install
 119  ";
 120      echo $help;
 121      exit(0);
 122  }
 123  
 124  if ($diag) {
 125      list($errorcode, $message) = phpunit_util::testing_ready_problem();
 126      if ($errorcode) {
 127          phpunit_bootstrap_error($errorcode, $message);
 128      }
 129      exit(0);
 130  
 131  } else if ($buildconfig) {
 132      if (phpunit_util::build_config_file()) {
 133          exit(0);
 134      } else {
 135          phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGWARNING, 'Can not create main /phpunit.xml configuration file, verify dirroot permissions');
 136      }
 137  
 138  } else if ($buildcomponentconfigs) {
 139      phpunit_util::build_component_config_files();
 140      exit(0);
 141  
 142  } else if ($drop) {
 143      // make sure tests do not run in parallel
 144      test_lock::acquire('phpunit');
 145      phpunit_util::drop_site(true);
 146      // note: we must stop here because $CFG is messed up and we can not reinstall, sorry
 147      exit(0);
 148  
 149  } else if ($install) {
 150      phpunit_util::install_site();
 151      exit(0);
 152  }