refactor to use xml and not testdox for generating the report

also add custim GiteaTitle env var for setting the title
This commit is contained in:
James 2020-02-09 12:24:14 +00:00
parent 05d4e2e3c1
commit 1d2ca239e7
3 changed files with 63 additions and 48 deletions

View File

@ -15,6 +15,5 @@ Enable in phpunit.xml with
<logging> <logging>
<log type="junit" target="/tmp/logfile.xml"/> <log type="junit" target="/tmp/logfile.xml"/>
<log type="testdox-text" target="/tmp/testdox.txt"/>
</logging> </logging>
``` ```

View File

@ -5,56 +5,72 @@ use \JHodges\GiteaBot\Client;
final class Poster{ final class Poster{
private $report; private $report;
private $level=0;
private $total_fail=0;
function __construct($testdox_path,$xml_path){ function __construct($xml_path){
$testdox=file_get_contents($testdox_path); $this->xml = simplexml_load_file($xml_path);
$testdox=preg_replace('# \[#',' * [',$testdox);
$this->report.=$testdox;
$this->xml = simplexml_load_file($xml_path);
}
public function post($url, $user, $pass, $repoUser, $repo, $title='Test Results'){
if(!$url) return;
// open connection and repo
$this->client=new Client($url, $user, $pass);
$this->repo=$this->client->getRepo($repoUser, $repo);
$this->process($this->xml); //process xml and upload screenshots
// create the issue
if(trim($this->report)){
$issue=$this->repo->createIssue([
'title'=>$title,
'body'=>$this->report
]);
} }
} public function post($url, $user, $pass, $repoUser, $repo, $title='Test Results'){
if(!$url) return;
// open connection and repo
$this->client=new Client($url, $user, $pass);
$this->repo=$this->client->getRepo($repoUser, $repo);
private function process($item){ $this->process($this->xml); //process xml and upload screenshots
if(isset($item->testcase)){ // create the issue
foreach($item->testcase as $testcase) { if(trim($this->report)){
foreach($testcase as $k=>$v){ $issue=$this->repo->createIssue([
$img=''; 'title'=>"$title ({$this->total_fail} fail)",
$name=$testcase->attributes()->class[0]."::".$testcase->attributes()->name[0]; 'body'=>$this->report
if(file_exists("/tmp/$name.png")){ ]);
$data=$this->repo->addReleaseAttachment(1,"/tmp/$name.png");
$url=$data->browser_download_url;
$img="\n![]($url)\n";
unlink("/tmp/$name.png");
}
$this->report.="\n------\n# $name\n$img\n```plain\n".print_r($testcase,true)."\n```";
} }
}
} }
if(isset($item->testsuite)){
foreach($item->testsuite as $testsuite) { private function process($item){
$this->process($testsuite); $this->level++;
} if(isset($item->testcase)){
foreach($item->testcase as $testcase) {
$att=$testcase->attributes();
$failmsg='';
foreach($testcase as $k=>$v){
$failmsg="(**$k**)\n";
if(trim($v)) $failmsg.="```plain\n$v\n```\n";
}
if($failmsg){
$this->total_fail++;
$this->report.=" * [ ] {$att->name[0]} $failmsg\n";
}else{
$this->report.=" * [x] {$att->name[0]}\n";
}
$name=$testcase->attributes()->class[0].'::'.$testcase->attributes()->name[0];
if(file_exists("/tmp/$name.png")){
$data=$this->repo->addReleaseAttachment(1,"/tmp/$name.png");
$url=$data->browser_download_url;
$img="\n![]($url)\n";
unlink("/tmp/$name.png");
$this->report.="$img\n";
}
}
}
if(isset($item->testsuite)){
foreach($item->testsuite as $testsuite) {
$att=$testsuite->attributes();
$this->report.=str_repeat('#',$this->level)." {$att->name[0]}\n".
"tests: {$att->tests[0]}".
", assertions: {$att->assertions[0]}".
", errors: {$att->errors[0]}".
", failures: {$att->failures[0]}".
", skipped: {$att->skipped[0]}".
", time: {$att->time[0]}".
"\n";
$this->process($testsuite);
}
}
$this->level--;
} }
}
} }

View File

@ -9,14 +9,14 @@ class ResultPrinter extends \PHPUnit\TextUI\ResultPrinter{
public function printResult(\PHPUnit\Framework\TestResult $result){ public function printResult(\PHPUnit\Framework\TestResult $result){
parent::printResult($result); parent::printResult($result);
$poster=new \JHodges\GiteaBotPHPUnit\Poster('/tmp/testdox.txt','/tmp/logfile.xml'); $poster=new \JHodges\GiteaBotPHPUnit\Poster('/tmp/logfile.xml');
$poster->post( $poster->post(
getenv('GiteaUrl'), getenv('GiteaUrl'),
getenv('GiteaUser'), getenv('GiteaUser'),
getenv('GiteaPass'), getenv('GiteaPass'),
getenv('GiteaRepoUser'), getenv('GiteaRepoUser'),
getenv('GiteaRepo'), getenv('GiteaRepo'),
'Test Results '.getenv('SeleniumBrowserUrl') getenv('GiteaTitle')
); );
} }
@ -31,5 +31,5 @@ class ResultPrinter extends \PHPUnit\TextUI\ResultPrinter{
public function suiteNameProcessor(array $record){ public function suiteNameProcessor(array $record){
parent::suiteNameProcessor($record); parent::suiteNameProcessor($record);
} }
} }