From e4febf6d25fb57f57c81f07538d68c1cfa295db6 Mon Sep 17 00:00:00 2001 From: SimonHeimberg Date: Wed, 21 Feb 2018 17:20:22 +0100 Subject: [PATCH 1/4] PhpUnitJunit loads file in a separate function --- src/Plugin/Util/PhpUnitResultJunit.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Plugin/Util/PhpUnitResultJunit.php b/src/Plugin/Util/PhpUnitResultJunit.php index 3f41cb8f..3aa29cee 100644 --- a/src/Plugin/Util/PhpUnitResultJunit.php +++ b/src/Plugin/Util/PhpUnitResultJunit.php @@ -17,13 +17,13 @@ class PhpUnitResultJunit extends PhpUnitResult */ public function parse() { - $suites = simplexml_load_file($this->outputFile); - // Reset the parsing variables $this->results = []; $this->errors = []; $this->failures = 0; + $suites = $this->loadResultFile(); + foreach ($suites->xpath('//testcase') as $testCase) { $this->parseTestcase($testCase); } @@ -127,4 +127,16 @@ class PhpUnitResultJunit extends PhpUnitResult return $msg; } + + /** + * @return \SimpleXMLElement + */ + private function loadResultFile() + { + if (true) { + $suites = simplexml_load_file($this->outputFile); + } + + return $suites; + } } From 30349014b7420591120ce7b9c0d74f0097863172 Mon Sep 17 00:00:00 2001 From: SimonHeimberg Date: Wed, 21 Feb 2018 17:21:53 +0100 Subject: [PATCH 2/4] PhpUnitJunit explains clearly an empty output file --- src/Plugin/Util/PhpUnitResultJunit.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Plugin/Util/PhpUnitResultJunit.php b/src/Plugin/Util/PhpUnitResultJunit.php index 3aa29cee..776e6513 100644 --- a/src/Plugin/Util/PhpUnitResultJunit.php +++ b/src/Plugin/Util/PhpUnitResultJunit.php @@ -133,10 +133,23 @@ class PhpUnitResultJunit extends PhpUnitResult */ private function loadResultFile() { + if (!file_exists($this->outputFile) || 0 === filesize($this->outputFile)) { + $this->internalProblem('empty output file'); + + return new \SimpleXMLElement(''); // new empty element + } + if (true) { $suites = simplexml_load_file($this->outputFile); } return $suites; } + + private function internalProblem($description) + { + throw new \Exception($description); + + // alternative to error throwing: append to $this->errors + } } From 4c8398f68f0cc61df6f76d03c922361cf137965a Mon Sep 17 00:00:00 2001 From: SimonHeimberg Date: Sat, 3 Feb 2018 17:48:11 +0100 Subject: [PATCH 3/4] PhpUnitJunit tries to load invalid xml file Work around outup from phpunit versions before 6.5.7/7.0.2 (before sebastianbergmann/phpunit#2974 was fixed). --- src/Plugin/Util/PhpUnitResultJunit.php | 35 +++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/Plugin/Util/PhpUnitResultJunit.php b/src/Plugin/Util/PhpUnitResultJunit.php index 776e6513..c16a4ebc 100644 --- a/src/Plugin/Util/PhpUnitResultJunit.php +++ b/src/Plugin/Util/PhpUnitResultJunit.php @@ -139,8 +139,41 @@ class PhpUnitResultJunit extends PhpUnitResult return new \SimpleXMLElement(''); // new empty element } - if (true) { + try { $suites = simplexml_load_file($this->outputFile); + } catch (\Exception $ex) { + $suites = null; + } catch (\Throwable $ex) { // since php7 + $suites = null; + } + if (!$suites) { + // from https://stackoverflow.com/questions/7766455/how-to-handle-invalid-unicode-with-simplexml/8092672#8092672 + $oldUse = libxml_use_internal_errors(true); + libxml_clear_errors(); + $dom = new \DOMDocument("1.0", "UTF-8"); + $dom->strictErrorChecking = false; + $dom->validateOnParse = false; + $dom->recover = true; + $dom->loadXML(strtr( + file_get_contents($this->outputFile), + array('"' => "'") // " in attribute names may mislead the parser + )); + + /** + * @var \LibXMLError + */ + $xmlError = libxml_get_last_error(); + if ($xmlError) { + $warning = sprintf('L%s C%s: %s', $xmlError->line, $xmlError->column, $xmlError->message); + print 'WARNING: ignored errors while reading phpunit result, '.$warning."\n"; + } + if (!$dom->hasChildNodes()) { + $this->internalProblem('xml file with no content'); + } + $suites = simplexml_import_dom($dom); + + libxml_clear_errors(); + libxml_use_internal_errors($oldUse); } return $suites; From 60b715e4bd4ba71f762944f53340a8c9a1455137 Mon Sep 17 00:00:00 2001 From: SimonHeimberg Date: Sun, 25 Feb 2018 17:17:26 +0100 Subject: [PATCH 4/4] cleanup doc blocks of PhpUnit --- src/Plugin/PhpUnit.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/Plugin/PhpUnit.php b/src/Plugin/PhpUnit.php index f7988b2e..d45b347d 100644 --- a/src/Plugin/PhpUnit.php +++ b/src/Plugin/PhpUnit.php @@ -42,10 +42,8 @@ class PhpUnit extends Plugin implements ZeroConfigPluginInterface */ protected $buildBranchLocation; - /** - * @var string[] Raw options from the config file - */ - protected $options = []; + /** @var PhpUnitOptions*/ + protected $options; /** * @return string @@ -83,7 +81,7 @@ class PhpUnit extends Plugin implements ZeroConfigPluginInterface /** * Check if the plugin can be executed without any configurations * - * @param $stage + * @param string $stage * @param Builder $builder * @param Build $build * @@ -140,9 +138,9 @@ class PhpUnit extends Plugin implements ZeroConfigPluginInterface /** * Run the tests defined in a PHPUnit config file or in a specific directory. * - * @param $directory - * @param $configFile - * @param string $logFormat + * @param string $directory + * @param string|null $configFile + * @param string $logFormat * * @return bool|mixed *