/** * Returns relative path against current working directory, * to be used for shell execution hints. * @param string $moodlepath starting with "/", ex: "/admin/tool/cli/init.php" * @return string path relative to current directory or absolute path */ function testing_cli_argument_path($moodlepath) { global $CFG; if (isset($CFG->admin) and $CFG->admin !== 'admin') { $moodlepath = preg_replace('|^/admin/|', "/{$CFG->admin}/", $moodlepath); } if (isset($_SERVER['REMOTE_ADDR'])) { // Web access, this should not happen often. $cwd = dirname(dirname(__DIR__)); } else { // This is the real CLI script, work with relative paths. $cwd = getcwd(); } // Remove last directory separator as $path will not contain one. if (substr($cwd, -1) === '/' || substr($cwd, -1) === '\\') { $cwd = substr($cwd, -1); } $path = realpath($CFG->dirroot . $moodlepath); // We need standrad directory seperator for path and cwd, so it can be compared. $cwd = testing_cli_fix_directory_separator($cwd); $path = testing_cli_fix_directory_separator($path); if (strpos($path, $cwd) === 0) { // Remove current working directory and directory separator. $path = substr($path, strlen($cwd) + 1); } return $path; }
/** * Test if clean features key and path is returned. * @dataProvider clean_features_path_list */ public function test_get_clean_feature_key_and_path($featurepath, $key, $cleanfeaturepath) { global $CFG; // This is a hack so directory name is correctly detected in tests. //FIXME: MDL-55722 work out why this is necessary.. $oldroot = $CFG->dirroot; $CFG->dirroot = 'C:'; $behatconfigutil = new behat_config_util(); // Fix expected directory path for OS. $cleanfeaturepath = testing_cli_fix_directory_separator($cleanfeaturepath); list($retkey, $retcleanfeaturepath) = $behatconfigutil->get_clean_feature_key_and_path($featurepath); $this->assertEquals($key, $retkey); $this->assertEquals($cleanfeaturepath, $retcleanfeaturepath); //FIXME: MDL-55722 work out why this is necessary.. $CFG->dirroot = $oldroot; }
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"); }
/** * Return feature key for featurepath * * @param string $featurepath * @return array key and featurepath. */ public function get_clean_feature_key_and_path($featurepath) { global $CFG; // Fix directory path. $featurepath = testing_cli_fix_directory_separator($featurepath); $dirroot = testing_cli_fix_directory_separator($CFG->dirroot . DIRECTORY_SEPARATOR); $key = basename($featurepath, '.feature'); // Get relative path. $featuredirname = str_replace($dirroot, '', $featurepath); // Get 5 levels of feature path to ensure we have a unique key. for ($i = 0; $i < 5; $i++) { if (($featuredirname = dirname($featuredirname)) && $featuredirname !== '.') { if ($basename = basename($featuredirname)) { $key .= '_' . $basename; } } } return array($key, $featurepath); }