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>
<log type="junit" target="/tmp/logfile.xml"/>
<log type="testdox-text" target="/tmp/testdox.txt"/>
</logging>
```

View File

@ -5,56 +5,72 @@ use \JHodges\GiteaBot\Client;
final class Poster{
private $report;
private $report;
private $level=0;
private $total_fail=0;
function __construct($testdox_path,$xml_path){
$testdox=file_get_contents($testdox_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
]);
function __construct($xml_path){
$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);
private function process($item){
if(isset($item->testcase)){
foreach($item->testcase as $testcase) {
foreach($testcase as $k=>$v){
$img='';
$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.="\n------\n# $name\n$img\n```plain\n".print_r($testcase,true)."\n```";
$this->process($this->xml); //process xml and upload screenshots
// create the issue
if(trim($this->report)){
$issue=$this->repo->createIssue([
'title'=>"$title ({$this->total_fail} fail)",
'body'=>$this->report
]);
}
}
}
if(isset($item->testsuite)){
foreach($item->testsuite as $testsuite) {
$this->process($testsuite);
}
private function process($item){
$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){
parent::printResult($result);
$poster=new \JHodges\GiteaBotPHPUnit\Poster('/tmp/testdox.txt','/tmp/logfile.xml');
$poster=new \JHodges\GiteaBotPHPUnit\Poster('/tmp/logfile.xml');
$poster->post(
getenv('GiteaUrl'),
getenv('GiteaUser'),
getenv('GiteaPass'),
getenv('GiteaRepoUser'),
getenv('GiteaRepo'),
'Test Results '.getenv('SeleniumBrowserUrl')
getenv('GiteaTitle')
);
}
@ -31,5 +31,5 @@ class ResultPrinter extends \PHPUnit\TextUI\ResultPrinter{
public function suiteNameProcessor(array $record){
parent::suiteNameProcessor($record);
}
}