/** * Exits with an error code * * @param mixed $errorcode * @param string $text * @return void Stops execution with error code */ function behat_error($errorcode, $text = '') { // Adding error prefixes. switch ($errorcode) { case BEHAT_EXITCODE_CONFIG: $text = 'Behat config error: ' . $text; break; case BEHAT_EXITCODE_REQUIREMENT: $text = 'Behat requirement not satisfied: ' . $text; break; case BEHAT_EXITCODE_PERMISSIONS: $text = 'Behat permissions problem: ' . $text . ', check the permissions'; break; case BEHAT_EXITCODE_REINSTALL: $path = testing_cli_argument_path('/testing/frameworks/behat/cli/init.php'); $text = "Reinstall Behat: " . $text . ", use:\n php " . $path; break; case BEHAT_EXITCODE_INSTALL: $path = testing_cli_argument_path('/testing/frameworks/behat/cli/init.php'); $text = "Install Behat before enabling it, use:\n php " . $path; break; case BEHAT_EXITCODE_INSTALLED: $text = "The Behat site is already installed"; break; default: $text = 'Unknown error ' . $errorcode . ' ' . $text; break; } testing_error($errorcode, $text); }
/** * Print error and stop execution * @param int $errorcode The exit error code * @param string $text An error message to display * @return void stops code execution with error code */ function phpunit_bootstrap_error($errorcode, $text = '') { switch ($errorcode) { case 0: // this is not an error, just print information and exit break; case 1: $text = 'Error: ' . $text; break; case PHPUNIT_EXITCODE_PHPUNITMISSING: $text = "Can not find PHPUnit library, to install use: php composer.phar install"; break; case PHPUNIT_EXITCODE_PHPUNITWRONG: $text = 'Moodle requires PHPUnit 3.6.x, ' . $text . ' is not compatible'; break; case PHPUNIT_EXITCODE_PHPUNITEXTMISSING: $text = 'Moodle can not find required PHPUnit extension ' . $text; break; case PHPUNIT_EXITCODE_CONFIGERROR: $text = "Moodle PHPUnit environment configuration error:\n" . $text; break; case PHPUNIT_EXITCODE_CONFIGWARNING: $text = "Moodle PHPUnit environment configuration warning:\n" . $text; break; case PHPUNIT_EXITCODE_INSTALL: $path = testing_cli_argument_path('/admin/tool/phpunit/cli/init.php'); $text = "Moodle PHPUnit environment is not initialised, please use:\n php {$path}"; break; case PHPUNIT_EXITCODE_REINSTALL: $path = testing_cli_argument_path('/admin/tool/phpunit/cli/init.php'); $text = "Moodle PHPUnit environment was initialised for different version, please use:\n php {$path}"; break; default: $text = empty($text) ? '' : ': ' . $text; $text = 'Unknown error ' . $errorcode . $text; break; } testing_error($errorcode, $text); }
/** * Returns structure of all tables right after installation. * @static * @return array $table=>$records */ public static function get_tablestructure() { global $CFG; $framework = self::get_framework(); $structurefile = $CFG->dataroot . '/' . $framework . '/tablestructure.ser'; if (!file_exists($structurefile)) { // Not initialised yet. return array(); } if (!isset(self::$tablestructure)) { $data = file_get_contents($structurefile); self::$tablestructure = unserialize($data); } if (!is_array(self::$tablestructure)) { testing_error(1, 'Can not read dataroot/' . $framework . '/tablestructure.ser or invalid format, reinitialize test database.'); } return self::$tablestructure; }
/** * Returns structure of all tables right after installation. * @static * @param string $statename name of state. * @return array $table=>$records */ public static function get_table_structure($statename) { $structurefile = util::get_tool_dir() . DIRECTORY_SEPARATOR . $statename . "_structure.ser"; if (!file_exists($structurefile)) { // Not initialised yet. return array(); } if (!isset(self::$tablestructure)) { $data = file_get_contents($structurefile); self::$tablestructure = unserialize($data); } if (!is_array(self::$tablestructure)) { testing_error(1, 'Can not read dataroot/' . $statename . '_structure.ser or invalid format, reinitialize test database.'); } return self::$tablestructure; }
/** * Updates the composer installer and the dependencies. * * @return void exit() if something goes wrong */ function testing_update_composer_dependencies() { // To restore the value after finishing. $cwd = getcwd(); // Set some paths. $dirroot = dirname(dirname(__DIR__)); $composerpath = $dirroot . DIRECTORY_SEPARATOR . 'composer.phar'; $composerurl = 'https://getcomposer.org/composer.phar'; // Switch to Moodle's dirroot for easier path handling. chdir($dirroot); // Download or update composer.phar. Unfortunately we can't use the curl // class in filelib.php as we're running within one of the test platforms. if (!file_exists($composerpath)) { $file = @fopen($composerpath, 'w'); if ($file === false) { $errordetails = error_get_last(); $error = sprintf("Unable to create composer.phar\nPHP error: %s", $errordetails['message']); testing_error(TESTING_EXITCODE_COMPOSER, $error); } $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $composerurl); curl_setopt($curl, CURLOPT_FILE, $file); $result = curl_exec($curl); $curlerrno = curl_errno($curl); $curlerror = curl_error($curl); $curlinfo = curl_getinfo($curl); curl_close($curl); fclose($file); if (!$result) { $error = sprintf("Unable to download composer.phar\ncURL error (%d): %s", $curlerrno, $curlerror); testing_error(TESTING_EXITCODE_COMPOSER, $error); } else { if ($curlinfo['http_code'] === 404) { if (file_exists($composerpath)) { // Deleting the resource as it would contain HTML. unlink($composerpath); } $error = sprintf("Unable to download composer.phar\n" . "404 http status code fetching {$composerurl}"); testing_error(TESTING_EXITCODE_COMPOSER, $error); } } } else { passthru("php composer.phar self-update", $code); if ($code != 0) { exit($code); } } // Update composer dependencies. passthru("php composer.phar install", $code); if ($code != 0) { exit($code); } // Return to our original location. chdir($cwd); }