diff --git a/README.md b/README.md index a18711a..91f6ec1 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,5 @@ Enable in phpunit.xml with - ``` diff --git a/src/Poster.php b/src/Poster.php index 9d695b0..e028243 100644 --- a/src/Poster.php +++ b/src/Poster.php @@ -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--; } - } } diff --git a/src/ResultPrinter.php b/src/ResultPrinter.php index 5797281..891d8f7 100644 --- a/src/ResultPrinter.php +++ b/src/ResultPrinter.php @@ -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); } - + }