src.dualinventive.com/dinet/sec-multi-proxy/common/ditest-php/TestReport.php

112 lines
3.5 KiB
PHP

<?php
class TestReport {
private $__testName = "";
private $__goal = "";
private $__testLabel = "";
private $__start = 0;
private $__end = 0;
private $__steps = [];
function __construct($testName, $urlSafeName) {
$this->__testName = $testName;
$this->__testLabel = $urlSafeName;
$this->__start = microtime(true);
}
public function goal($goalStr) {
$this->__goal = $goalStr;
}
public function stepFinish($name, $result) {
$this->__steps[$this->__sanitizeLeadingNumber($name)] = $result;
}
public function writeReport($summary) {
$this->__end = microtime(true);
$f = fopen($this->__testLabel . '.tex', "w");
fwrite($f, '\chapter{' . $this->__texT($this->__sanitizeLeadingNumber($this->__testName)) . '}');
fwrite($f, '\label{' . $this->__texL($this->__testLabel) . $this->randomStr() . '}' . PHP_EOL);
fwrite($f, PHP_EOL);
fwrite($f, '\begin{longtable}[c]{@{}lllll@{}}' . PHP_EOL);
fwrite($f, '\toprule\addlinespace' . PHP_EOL);
fwrite($f, 'Time & Duration & Passes & Failures & Skips' . PHP_EOL); // headers
fwrite($f, '\\\\\addlinespace' . PHP_EOL);
fwrite($f, '\midrule\endhead' . PHP_EOL);
date_default_timezone_set('Europe/Amsterdam');
$durationMs = round(($this->__end - $this->__start) * 1000, 2);
fwrite($f, date('c', $this->__start) . ' & '.$durationMs.' ms & '.$summary['pass'].' & '.$summary['fail'].' & ' . $summary['skip'] . PHP_EOL); // values
fwrite($f, '\\\\\addlinespace' . PHP_EOL);
fwrite($f, '\bottomrule' . PHP_EOL);
fwrite($f, '\end{longtable}' . PHP_EOL);
fwrite($f, PHP_EOL);
fwrite($f, PHP_EOL);
if (strlen($this->__goal)) {
fwrite($f, '\\begin{flushleft}' . PHP_EOL);
fwrite($f, '\\textbf{Goal:} \\\\'.PHP_EOL);
fwrite($f, $this->__texT(iconv("UTF-8", "ASCII//TRANSLIT", $this->__goal)));
fwrite($f, '\\end{flushleft}' . PHP_EOL);
fwrite($f, PHP_EOL);
}
fwrite($f, '\begin{longtable}{p{380pt} p{50pt}}' . PHP_EOL);
fwrite($f, '\toprule\addlinespace' . PHP_EOL);
fwrite($f, 'Test & Result' . PHP_EOL);
fwrite($f, '\\\\\addlinespace' . PHP_EOL);
fwrite($f, '\midrule\endhead' . PHP_EOL);
foreach ($this->__steps as $step => $result) {
fwrite($f, $this->__texT(iconv("UTF-8", "ASCII//TRANSLIT", $step)));
fwrite($f, ' & ');
if ($result) {
fwrite($f, '\PASS{[PASS]}' . PHP_EOL);
} else {
fwrite($f, '\FAIL{[FAIL]}' . PHP_EOL);
}
fwrite($f, '\\\\ \hline \addlinespace' . PHP_EOL);// \addlinespace' . PHP_EOL);
}
fwrite($f, '\bottomrule' . PHP_EOL);
fwrite($f, '\end{longtable}' . PHP_EOL);
if (file_exists('report.tex')) {
$t = file_get_contents('report.tex');
} else {
$t = '% Report generated by ditest-php at ' . date('c') . PHP_EOL;
}
$file = getcwd() . '/' . $this->__testLabel . '.tex';
if (strpos($t, $file) === false) {
$t .= '\input{\detokenize{' . getcwd() . '/' . $this->__testLabel . '.tex}}' . PHP_EOL;
file_put_contents('report.tex', $t);
}
}
/**
* Texify Text
*/
private function __texT($txt) {
return str_replace(['_', '&'], ['\_', '\&'], $txt);
}
private function __texL($txt) {
return str_replace(["_", '-', '.', ',', '&'], "", $txt);
}
private function __sanitizeLeadingNumber($str) {
return trim(preg_replace('/^([0-9x\.]*) /', '', $str));
}
private function randomStr($length = 5) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyz';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
}