/** * To find all existing PHPUnit results xml-files. * * @param string $targetPath (Option) new path to PHPUnit results xml-files. * @return hash Array of testSuites structure: * @code * Array ( [someFile.xml] => Array ( [@attributes] => Array ( [name] => someTest [file] => someFile.xml [tests] => [assertions] => [failures] => 1 [errors] => [time] => [base] => basePath ) [testcase] => Array ( [0] => Array ( [@attributes] => Array ( [name] => [class] => [file] => [line] => [assertions] => [time] => ) [failure] => some failure ) ) [someFile2.xml] => Array( ... ) ... * @endcode */ public function findAll($targetPath = NULL) { //--- Find all xml files of results: $targetPath = $this->getTargetPath($targetPath); $files = Files::globFiles($targetPath, "\\.xml\$", NULL, true); $this->testsStructure = Files::getFilesTree($files); $testSuites = array(); //--- Load results to common array: if (is_array($files)) { foreach ($files as $fileName => $basePath) { $data = Data::load($fileName, false); if (!empty($data["testsuite"]["@attributes"])) { $data["testsuite"]["@attributes"]["base"] = $basePath; $testSuites[$fileName] = $data["testsuite"]; } } } //echo "<listing>"; print_r($testSuites); exit; $this->testSuites = $testSuites; return $this->testSuites; }
/** * To create an application structure from templates * * @param string $config Array of config params. * ~~~ * $config = [ * 'basePath' => '/path/to/copy/template/files', * 'appTemplates' => [ * 'templatesPath' => '/path/to/templates/directory', * 'params' => [ an optional array of any extra params ], * ], * 'appInfo' => [ an optional array of any extra params ] * ] * ~~~ * @return null */ public static function createApplication($config) { //--- Check config: if (!is_array($config)) { self::log(_('Config mast be an array!'), 'error'); return -1; } //--- Include application templates: if ($config['appTemplates'] && is_string($config['appTemplates'])) { $config['appTemplates'] = (require $config['appTemplates']); } if (!is_array($config['appTemplates'])) { self::log(_('appTemplates mast be an array!'), 'error'); return -1; } //--- Extract and prepare expected params: $params = array_merge(Basic::pathValue($config['appInfo'], null, '[', ']'), Basic::pathValue($config['appTemplates']['params'], null, '[', ']')); //print_r($params); //--- Get Application templates structure: $applicationBase = $config['basePath']; $templateBase = $config['appTemplates']['templatesPath']; $templateFiles = Files::glob($templateBase, '{,.}*', GLOB_BRACE, true); foreach ($templateFiles as $srcFile) { $file = preg_replace('/^' . preg_quote($templateBase . '/', '/') . '/', '', $srcFile); $dstFile = $applicationBase . '/' . $file; $result = null; try { //--- Skip an exists file(dir): if (file_exists($dstFile)) { throw new \Exception(_('SKIPPED') . ' ' . _('destination is exists'), -2); } //--- Copy file: if (is_file($srcFile)) { $dstDir = dirname($dstFile); if (!file_exists($dstDir) && !mkdir($dstDir, 0775, true)) { throw new \Exception(_('cant create a directory')); } //--- Load source content: $content = file_get_contents($srcFile); //--- Replace shortcodes [...key...] to values: $content = strtr($content, $params); //--- Save content to destination: if (file_put_contents($dstFile, $content) === false) { throw new \Exception(_('cant copy a file')); } $message = _('OK') . ' ' . _('file copied'); } //--- Create a directory: if (is_dir($srcFile)) { if (mkdir($dstFile, 0775, true)) { $message = _('OK') . ' ' . _('directory created'); } else { throw new \Exception(_('cant create a directory')); } } } catch (\Exception $e) { $message = $e->getMessage(); $result = 'error'; if ($e->getCode() == -2) { $result = 'warn'; } } self::log(_('source') . ': [' . $file . "]\t" . $message . "\t[" . $dstFile . ']', $result); } }
/** * Returns HTML code list contains tree of files spesified by class properties. * * @return string HTML code */ public function getNavigation() { //--- Create real path: $this->path = realpath($this->path); //--- Create filePatterns from given stripExtention: if ($this->stripExtention && !$this->filePatterns) { $this->filePatterns = '.*' . preg_quote($this->stripExtention) . '$'; } //--- Select all files by patterns: $files = Files::globFiles($this->path, $this->filePatterns, $this->excludePatterns, $this->recursive); //--- Get requested file: $activeFile = $_REQUEST[$this->routerPrefix]; //--- Add file extention: if ($activeFile && $this->stripExtention) { $activeFile .= $this->stripExtention; } //--- Check: Is it my file?: if (!isset($files[rtrim($this->path, '/') . '/' . $activeFile])) { $activeFile = ''; } //--- Get default file (first): if (!$activeFile) { $name = array_keys($files)[0]; $base = array_values($files)[0]; $activeFile = preg_replace('/^' . preg_quote($base . '/', '/') . '/', '', $name); } //--- Set current item: $this->currentItem = $activeFile; //--- Create HTML code of tree: return Html::arrayToList(Files::getFilesTree($files), $this->listType, null, function (&$tag, &$body, &$attr) { if ($tag == 'li' && preg_match('/(.*)\\.\\w+$/', $body, $m)) { if ($body == $this->currentItem) { $attr['class'] = $this->cssActive; } if ($this->stripExtention) { $body = preg_replace('/' . preg_quote($this->stripExtention) . '$/', '', $body); } $body = Html::getElement('a', basename($m[1]), ['href' => '?' . $this->routerPrefix . '=' . $body]); } }); }
/** * Generate common file body to execute all tests * * @param array $destinations List of destination file names. * @param string $destinationRoot Root path for destinations. * @return string A source code (text) of run-file. */ public function generateRunFile($destinations, $destinationRoot) { if (!is_array($destinations)) { return false; } $destinationRoot = realpath($destinationRoot); $body = Basic::replace($this->runFileHeaderTemplate, array("DATE" => date("Y.m.d H:i"), "GENERATOR_NAME" => get_class(), "RUN_DIR" => $destinationRoot)); foreach ($destinations as $file => $_) { $testStruct = SimpleCodeParser::parseCode(file_get_contents($file)); if (is_array($testStruct)) { foreach ($testStruct as $nameSpace) { if (is_array($nameSpace["classes"])) { foreach ($nameSpace["classes"] as $className => $value) { $testName = $className; $path = Files::getRelativePath(dirname($file), $destinationRoot); $fileName = "." . $path["relative"] . "/" . basename($file); $logName = "." . $path["relative"] . "/" . $testName; $body .= Basic::replace($this->runFileLineTemplate, array("TEST_TITLE" => $testName, "TEST_NAME" => $testName, "TEST_FILE" => $fileName, "TEST_LOG" => $logName, "PHPUNIT" => $this->phpUnitFile)); } } } } } return $body; }