/** * 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); }
// from now on this is a regular moodle CLI_SCRIPT require_once $CFG->libdir . '/adminlib.php'; require_once $CFG->libdir . '/upgradelib.php'; require_once $CFG->libdir . '/clilib.php'; require_once $CFG->libdir . '/installlib.php'; if ($unrecognized) { $unrecognized = implode("\n ", $unrecognized); cli_error(get_string('cliunknowoption', 'admin', $unrecognized)); } $diag = $options['diag']; $drop = $options['drop']; $install = $options['install']; $buildconfig = $options['buildconfig']; $buildcomponentconfigs = $options['buildcomponentconfigs']; if ($options['help'] or !$drop and !$install and !$buildconfig and !$buildcomponentconfigs and !$diag) { $help = "Various PHPUnit utility functions\n\nOptions:\n--drop Drop database and dataroot\n--install Install database\n--diag Diagnose installation and return error code only\n--run Execute PHPUnit tests (alternative for standard phpunit binary)\n--buildconfig Build /phpunit.xml from /phpunit.xml.dist that runs all tests\n--buildcomponentconfigs\n Build distributed phpunit.xml files for each component\n\n-h, --help Print out this help\n\nExample:\n\$ php " . testing_cli_argument_path('/admin/tool/phpunit/cli/util.php') . " --install\n"; echo $help; exit(0); } if ($diag) { list($errorcode, $message) = phpunit_util::testing_ready_problem(); if ($errorcode) { phpunit_bootstrap_error($errorcode, $message); } exit(0); } else { if ($buildconfig) { if (phpunit_util::build_config_file()) { exit(0); } else { phpunit_bootstrap_error(PHPUNIT_EXITCODE_CONFIGWARNING, 'Can not create main /phpunit.xml configuration file, verify dirroot permissions');
protected function printDefectTrace(PHPUnit_Framework_TestFailure $defect) { global $CFG; parent::printDefectTrace($defect); $failedTest = $defect->failedTest(); $testName = get_class($failedTest); $exception = $defect->thrownException(); $trace = $exception->getTrace(); if (class_exists('ReflectionClass')) { $reflection = new ReflectionClass($testName); $file = $reflection->getFileName(); } else { $file = false; $dirroot = realpath($CFG->dirroot) . DIRECTORY_SEPARATOR; $classpath = realpath("{$CFG->dirroot}/lib/phpunit/classes") . DIRECTORY_SEPARATOR; foreach ($trace as $item) { if (strpos($item['file'], $dirroot) === 0 and strpos($item['file'], $classpath) !== 0) { if ($content = file_get_contents($item['file'])) { if (preg_match('/class\\s+' . $testName . '\\s+extends/', $content)) { $file = $item['file']; break; } } } } } if ($file === false) { return; } $cwd = getcwd(); if (strpos($file, $cwd) === 0) { $file = substr($file, strlen($cwd) + 1); $file = testing_cli_fix_directory_separator($file); } $pathprefix = testing_cli_argument_path('/'); if ($pathprefix) { $pathprefix .= DIRECTORY_SEPARATOR; } // There is only vendor/bin/phpunit executable. There is no .cmd or .bat files. $executable = $pathprefix . 'vendor' . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . 'phpunit'; $executable = testing_cli_fix_directory_separator($executable); // Add server arguments to the rerun if passed. if (isset($_SERVER['argv'][0])) { if (preg_match('/phpunit(\\.bat|\\.cmd)?$/', $_SERVER['argv'][0])) { for ($i = 1; $i < count($_SERVER['argv']); $i++) { if (!isset($_SERVER['argv'][$i])) { break; } if (in_array($_SERVER['argv'][$i], array('--colors', '--verbose', '-v', '--debug'))) { $executable .= ' ' . $_SERVER['argv'][$i]; } else { if (in_array($_SERVER['argv'][$i], array('-c', '--config'))) { $executable .= ' ' . $_SERVER['argv'][$i] . ' ' . $_SERVER['argv'][++$i]; } else { if (strpos($_SERVER['argv'][$i], '--config') === 0) { $executable .= ' ' . $_SERVER['argv'][$i]; } } } } } } $this->write("\nTo re-run:\n {$executable} {$testName} {$file}\n"); }
/** * Returns the executable path * * Allows returning a customized command for cygwin when the * command is just displayed, when using exec(), system() and * friends we stay with DIRECTORY_SEPARATOR as they use the * normal cmd.exe (in Windows). * * @param bool $custombyterm If the provided command should depend on the terminal where it runs * @param bool $parallelrun If parallel run is installed. * @param bool $absolutepath return command with absolute path. * @return string */ public static final function get_behat_command($custombyterm = false, $parallerun = false, $absolutepath = false) { $separator = DIRECTORY_SEPARATOR; $exec = 'behat'; // Cygwin uses linux-style directory separators. if ($custombyterm && testing_is_cygwin()) { $separator = '/'; // MinGW can not execute .bat scripts. if (!testing_is_mingw()) { $exec = 'behat.bat'; } } // If relative path then prefix relative path. if ($absolutepath) { $pathprefix = testing_cli_argument_path('/') . $separator; } else { $pathprefix = ''; } if (!$parallerun) { $command = $pathprefix . 'vendor' . $separator . 'bin' . $separator . $exec; } else { $command = 'php ' . $pathprefix . 'admin' . $separator . 'tool' . $separator . 'behat' . $separator . 'cli' . $separator . 'run.php'; } return $command; }