From 6c918a96e09100b87714a18939a78fbf3189bb52 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Sat, 21 May 2022 14:09:52 +0200 Subject: [PATCH 1/4] fix bug in log merge --- src/PhpunitMerger/Command/LogCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpunitMerger/Command/LogCommand.php b/src/PhpunitMerger/Command/LogCommand.php index d30e6ef..0d00538 100644 --- a/src/PhpunitMerger/Command/LogCommand.php +++ b/src/PhpunitMerger/Command/LogCommand.php @@ -82,7 +82,7 @@ private function addTestSuites(\DOMElement $parent, array $testSuites) foreach ($testSuites as $testSuite) { if (empty($testSuite['@attributes']['name'])) { if (!empty($testSuite['testsuite'])) { - $this->addTestSuites($parent, $testSuite['testsuite']); + $this->addTestSuites($parent, $testSuite); } continue; } From ff0c4e8590d28502c1ac1b3d6be1622bc64e1c24 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Sat, 21 May 2022 17:11:46 +0200 Subject: [PATCH 2/4] fix empty filename attributes on testsuite level --- src/PhpunitMerger/Command/LogCommand.php | 30 +++++++++++++++++------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/PhpunitMerger/Command/LogCommand.php b/src/PhpunitMerger/Command/LogCommand.php index 0d00538..dc96867 100644 --- a/src/PhpunitMerger/Command/LogCommand.php +++ b/src/PhpunitMerger/Command/LogCommand.php @@ -48,6 +48,16 @@ protected function execute(InputInterface $input, OutputInterface $output) $this->document->formatOutput = true; $root = $this->document->createElement('testsuites'); + $baseSuite = $this->document->createElement('testsuite'); + $baseSuite->setAttribute('name', ""); + $baseSuite->setAttribute('tests', "0"); + $baseSuite->setAttribute('assertions', "0"); + $baseSuite->setAttribute('errors', "0"); + $baseSuite->setAttribute('failures', "0"); + $baseSuite->setAttribute('skipped', "0"); + $baseSuite->setAttribute('time', "0"); + + $root->appendChild($baseSuite); $this->document->appendChild($root); foreach ($finder as $file) { @@ -55,7 +65,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $xml = new \SimpleXMLElement(file_get_contents($file->getRealPath())); $xmlArray = json_decode(json_encode($xml), true); if (!empty($xmlArray)) { - $this->addTestSuites($root, $xmlArray); + $this->addTestSuites($baseSuite, $xmlArray); } } catch (\Exception $exception) { // Initial fallthrough @@ -95,7 +105,6 @@ private function addTestSuites(\DOMElement $parent, array $testSuites) $element->setAttribute('parent', $parent->getAttribute('name')); $attributes = $testSuite['@attributes'] ?? []; foreach ($attributes as $key => $value) { - $value = $key === 'name' ? $value : 0; $element->setAttribute($key, (string)$value); } $parent->appendChild($element); @@ -142,13 +151,16 @@ private function addTestCases(\DOMElement $parent, array $testCases) private function addAttributeValueToTestSuite(\DOMElement $element, $key, $value) { - $currentValue = $element->hasAttribute($key) ? $element->getAttribute($key) : 0; - $element->setAttribute($key, (string)($currentValue + $value)); - - if ($element->hasAttribute('parent')) { - $parent = $element->getAttribute('parent'); - if (isset($this->domElements[$parent])) { - $this->addAttributeValueToTestSuite($this->domElements[$parent], $key, $value); + $keysToCalculate = ["assertions", "time", "tests", "errors", "failures", "skipped"]; + if (in_array($key, $keysToCalculate)) { + $currentValue = $element->hasAttribute($key) ? $element->getAttribute($key) : 0; + $element->setAttribute($key, (string)($currentValue + $value)); + + if ($element->hasAttribute('parent')) { + $parent = $element->getAttribute('parent'); + if (isset($this->domElements[$parent])) { + $this->addAttributeValueToTestSuite($this->domElements[$parent], $key, $value); + } } } } From a80b5d7b28cc3a96e62c8432f8e4f81b4b014fb0 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Sun, 22 May 2022 14:44:51 +0200 Subject: [PATCH 3/4] Add Calculations based on first level nodes. We should assume that the calculation from PHPUnit is correct so we have to recalculate the base node only. Add Subnodes of testcase node also beacuse it contains important error messages --- src/PhpunitMerger/Command/LogCommand.php | 56 +++++++++++++++++++----- 1 file changed, 45 insertions(+), 11 deletions(-) diff --git a/src/PhpunitMerger/Command/LogCommand.php b/src/PhpunitMerger/Command/LogCommand.php index dc96867..ab37e55 100644 --- a/src/PhpunitMerger/Command/LogCommand.php +++ b/src/PhpunitMerger/Command/LogCommand.php @@ -22,6 +22,8 @@ class LogCommand extends Command */ private $domElements = []; + private $keysToCalculate = ["assertions", "time", "tests", "errors", "failures", "skipped"]; + protected function configure() { $this->setName('log') @@ -49,7 +51,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $root = $this->document->createElement('testsuites'); $baseSuite = $this->document->createElement('testsuite'); - $baseSuite->setAttribute('name', ""); + $baseSuite->setAttribute('name', "All Suites"); $baseSuite->setAttribute('tests', "0"); $baseSuite->setAttribute('assertions', "0"); $baseSuite->setAttribute('errors', "0"); @@ -57,18 +59,21 @@ protected function execute(InputInterface $input, OutputInterface $output) $baseSuite->setAttribute('skipped', "0"); $baseSuite->setAttribute('time', "0"); + $this->domElements["All Suites"] = $baseSuite; + $root->appendChild($baseSuite); $this->document->appendChild($root); foreach ($finder as $file) { try { $xml = new \SimpleXMLElement(file_get_contents($file->getRealPath())); + $code = json_encode($xml); $xmlArray = json_decode(json_encode($xml), true); if (!empty($xmlArray)) { $this->addTestSuites($baseSuite, $xmlArray); } } catch (\Exception $exception) { - // Initial fallthrough + $output->writeln(sprintf("Error in file %s: %s", $file->getRealPath(), $exception->getMessage())); } } @@ -77,7 +82,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $domElement->removeAttribute('parent'); } } - + $this->calculateTopLevelStats(); $file = $input->getArgument('file'); if (!is_dir(dirname($file))) { @mkdir(dirname($file), 0777, true); @@ -135,24 +140,31 @@ private function addTestCases(\DOMElement $parent, array $testCases) if (isset($this->domElements[$name])) { continue; } - $element = $this->document->createElement('testcase'); foreach ($attributes as $key => $value) { $element->setAttribute($key, (string)$value); - if (!is_numeric($value)) { - continue; - } - $this->addAttributeValueToTestSuite($parent, $key, $value); + } + if (isset($testCase['failure']) || isset($testCase['warning']) || isset($testCase['error'])) { + $this->addChildElements($testCase, $element); } $parent->appendChild($element); $this->domElements[$name] = $element; } } - + private function addChildElements(array $tree, \DOMElement $element) + { + foreach ($tree as $key => $value) { + if ($key == "@attributes") { + continue; + } + $child = $this->document->createElement($key); + $child->nodeValue = $value; + $element->appendChild($child); + } + } private function addAttributeValueToTestSuite(\DOMElement $element, $key, $value) { - $keysToCalculate = ["assertions", "time", "tests", "errors", "failures", "skipped"]; - if (in_array($key, $keysToCalculate)) { + if (in_array($key, $this->keysToCalculate)) { $currentValue = $element->hasAttribute($key) ? $element->getAttribute($key) : 0; $element->setAttribute($key, (string)($currentValue + $value)); @@ -164,4 +176,26 @@ private function addAttributeValueToTestSuite(\DOMElement $element, $key, $value } } } + + private function calculateTopLevelStats() + { + /** @var \DOMElement $topNode */ + $suites = $this->document->getElementsByTagName("testsuites")->item(0); + $topNode = $suites->firstChild; + if ($topNode->hasChildNodes()) { + $stats = array_flip($this->keysToCalculate); + $stats = array_map(function ($_value) { return 0; }, $stats); + foreach($topNode->childNodes as $child) { + $attributes = $child->attributes; + foreach ($attributes as $key => $value) { + if (in_array($key, $this->keysToCalculate)) { + $stats[$key] += $value->nodeValue; + } + } + } + foreach ($stats as $key => $value) { + $topNode->setAttribute($key, (string)$value); + } + } + } } From 1cd43f8e65ae0ee78b6e5b3ca5aa570935acefac Mon Sep 17 00:00:00 2001 From: Sebastian Date: Sun, 22 May 2022 15:58:34 +0200 Subject: [PATCH 4/4] Unittests for log bugfixes check log directory existence fix styleci issues fix style ci issues remove .Log from gitignore fix style ci issues --- .gitignore | 1 + src/PhpunitMerger/Command/LogCommand.php | 48 +++++++------------ .../Command/AbstractCommandTest.php | 2 + .../Command/Log/LogCommandTest.php | 5 +- .../Command/Log/datasets/integration.xml | 11 +++++ .../Command/Log/datasets/unit.xml | 28 +++++++++++ .../Command/Log/expected_merge.xml | 34 +++++++++++++ 7 files changed, 97 insertions(+), 32 deletions(-) create mode 100644 tests/PhpunitMerger/Command/Log/datasets/integration.xml create mode 100644 tests/PhpunitMerger/Command/Log/datasets/unit.xml create mode 100644 tests/PhpunitMerger/Command/Log/expected_merge.xml diff --git a/.gitignore b/.gitignore index 55ad836..6f6d170 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /.Build /.idea composer.lock +.phpunit.result.cache \ No newline at end of file diff --git a/src/PhpunitMerger/Command/LogCommand.php b/src/PhpunitMerger/Command/LogCommand.php index ab37e55..862406c 100644 --- a/src/PhpunitMerger/Command/LogCommand.php +++ b/src/PhpunitMerger/Command/LogCommand.php @@ -22,7 +22,7 @@ class LogCommand extends Command */ private $domElements = []; - private $keysToCalculate = ["assertions", "time", "tests", "errors", "failures", "skipped"]; + private $keysToCalculate = ['assertions', 'time', 'tests', 'errors', 'failures', 'skipped']; protected function configure() { @@ -44,22 +44,22 @@ protected function execute(InputInterface $input, OutputInterface $output) { $finder = new Finder(); $finder->files() - ->in(realpath($input->getArgument('directory'))); + ->in(realpath($input->getArgument('directory')))->sortByName(true); $this->document = new \DOMDocument('1.0', 'UTF-8'); $this->document->formatOutput = true; $root = $this->document->createElement('testsuites'); $baseSuite = $this->document->createElement('testsuite'); - $baseSuite->setAttribute('name', "All Suites"); - $baseSuite->setAttribute('tests', "0"); - $baseSuite->setAttribute('assertions', "0"); - $baseSuite->setAttribute('errors', "0"); - $baseSuite->setAttribute('failures', "0"); - $baseSuite->setAttribute('skipped', "0"); - $baseSuite->setAttribute('time', "0"); + $baseSuite->setAttribute('name', 'All Suites'); + $baseSuite->setAttribute('tests', '0'); + $baseSuite->setAttribute('assertions', '0'); + $baseSuite->setAttribute('errors', '0'); + $baseSuite->setAttribute('failures', '0'); + $baseSuite->setAttribute('skipped', '0'); + $baseSuite->setAttribute('time', '0'); - $this->domElements["All Suites"] = $baseSuite; + $this->domElements['All Suites'] = $baseSuite; $root->appendChild($baseSuite); $this->document->appendChild($root); @@ -67,13 +67,12 @@ protected function execute(InputInterface $input, OutputInterface $output) foreach ($finder as $file) { try { $xml = new \SimpleXMLElement(file_get_contents($file->getRealPath())); - $code = json_encode($xml); $xmlArray = json_decode(json_encode($xml), true); if (!empty($xmlArray)) { $this->addTestSuites($baseSuite, $xmlArray); } } catch (\Exception $exception) { - $output->writeln(sprintf("Error in file %s: %s", $file->getRealPath(), $exception->getMessage())); + $output->writeln(sprintf('Error in file %s: %s', $file->getRealPath(), $exception->getMessage())); } } @@ -151,10 +150,11 @@ private function addTestCases(\DOMElement $parent, array $testCases) $this->domElements[$name] = $element; } } + private function addChildElements(array $tree, \DOMElement $element) { foreach ($tree as $key => $value) { - if ($key == "@attributes") { + if ($key == '@attributes') { continue; } $child = $this->document->createElement($key); @@ -162,30 +162,18 @@ private function addChildElements(array $tree, \DOMElement $element) $element->appendChild($child); } } - private function addAttributeValueToTestSuite(\DOMElement $element, $key, $value) - { - if (in_array($key, $this->keysToCalculate)) { - $currentValue = $element->hasAttribute($key) ? $element->getAttribute($key) : 0; - $element->setAttribute($key, (string)($currentValue + $value)); - - if ($element->hasAttribute('parent')) { - $parent = $element->getAttribute('parent'); - if (isset($this->domElements[$parent])) { - $this->addAttributeValueToTestSuite($this->domElements[$parent], $key, $value); - } - } - } - } private function calculateTopLevelStats() { /** @var \DOMElement $topNode */ - $suites = $this->document->getElementsByTagName("testsuites")->item(0); + $suites = $this->document->getElementsByTagName('testsuites')->item(0); $topNode = $suites->firstChild; if ($topNode->hasChildNodes()) { $stats = array_flip($this->keysToCalculate); - $stats = array_map(function ($_value) { return 0; }, $stats); - foreach($topNode->childNodes as $child) { + $stats = array_map(function ($_value) { + return 0; + }, $stats); + foreach ($topNode->childNodes as $child) { $attributes = $child->attributes; foreach ($attributes as $key => $value) { if (in_array($key, $this->keysToCalculate)) { diff --git a/tests/PhpunitMerger/Command/AbstractCommandTest.php b/tests/PhpunitMerger/Command/AbstractCommandTest.php index 68d55a4..d290874 100644 --- a/tests/PhpunitMerger/Command/AbstractCommandTest.php +++ b/tests/PhpunitMerger/Command/AbstractCommandTest.php @@ -22,6 +22,7 @@ abstract class AbstractCommandTest extends TestCase public function assertOutputFileNotExists() { $filesystem = new Filesystem(); + self::assertDirectoryExists($this->logDirectory, $this->logDirectory . ' does not exists'); $filesystem->remove($this->logDirectory . $this->outputFile); $this->assertFileNotExists($this->logDirectory . $this->outputFile); @@ -29,6 +30,7 @@ public function assertOutputFileNotExists() public function assertOutputDirectoryNotExists() { + self::assertDirectoryExists($this->logDirectory, $this->logDirectory . ' does not exists'); $filesystem = new Filesystem(); $filesystem->remove($this->logDirectory . dirname($this->outputFile)); diff --git a/tests/PhpunitMerger/Command/Log/LogCommandTest.php b/tests/PhpunitMerger/Command/Log/LogCommandTest.php index 3ebd640..08d33da 100644 --- a/tests/PhpunitMerger/Command/Log/LogCommandTest.php +++ b/tests/PhpunitMerger/Command/Log/LogCommandTest.php @@ -16,14 +16,14 @@ class LogCommandTest extends AbstractCommandTest */ protected $outputFile = 'log.xml'; - public function testRunMergesCoverage() + public function testRunMergesLogs() { $this->assertOutputFileNotExists(); $input = new ArgvInput( [ 'log', - $this->logDirectory . 'log/', + __DIR__ . '/datasets/', $this->logDirectory . $this->outputFile, ] ); @@ -33,5 +33,6 @@ public function testRunMergesCoverage() $command->run($input, $output->reveal()); $this->assertFileExists($this->logDirectory . $this->outputFile); + self::assertXmlFileEqualsXmlFile(__DIR__ . '/expected_merge.xml', $this->logDirectory . $this->outputFile); } } diff --git a/tests/PhpunitMerger/Command/Log/datasets/integration.xml b/tests/PhpunitMerger/Command/Log/datasets/integration.xml new file mode 100644 index 0000000..fc5e913 --- /dev/null +++ b/tests/PhpunitMerger/Command/Log/datasets/integration.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/tests/PhpunitMerger/Command/Log/datasets/unit.xml b/tests/PhpunitMerger/Command/Log/datasets/unit.xml new file mode 100644 index 0000000..b6c42ba --- /dev/null +++ b/tests/PhpunitMerger/Command/Log/datasets/unit.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + CasProductLabels\Tests\Unit\Components\PDFLabelTest::testGenerate + Expectation failed for method name is equal to 'createRenderer' when invoked 1 time(s) + Parameter 1 for invocation CasProductLabels\Components\RendererFactory::createRenderer(Shopware\Models\Document\Document Object (...), '<!DOCTYPE html PUBLIC "-//W3C...tml>\n', false): CasProductLabels\Components\PDFRenderer does not match expected value. + Failed asserting that a traversable contains '<html>'. + + /var/www/html/custom/project/CasProductLabels/Components/PDFLabel.php:152 + /var/www/html/custom/project/CasProductLabels/Components/PDFLabel.php:95 + /var/www/html/custom/project/CasProductLabels/Tests/Unit/Components/PDFLabelTest.php:45 + + + + + + diff --git a/tests/PhpunitMerger/Command/Log/expected_merge.xml b/tests/PhpunitMerger/Command/Log/expected_merge.xml new file mode 100644 index 0000000..96bc922 --- /dev/null +++ b/tests/PhpunitMerger/Command/Log/expected_merge.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + + + + + CasProductLabels\Tests\Unit\Components\PDFLabelTest::testGenerate + Expectation failed for method name is equal to 'createRenderer' when invoked 1 time(s) + Parameter 1 for invocation CasProductLabels\Components\RendererFactory::createRenderer(Shopware\Models\Document\Document Object (...), '<!DOCTYPE html PUBLIC "-//W3C...tml>\n', false): CasProductLabels\Components\PDFRenderer does not match expected value. + Failed asserting that a traversable contains '<html>'. + + /var/www/html/custom/project/CasProductLabels/Components/PDFLabel.php:152 + /var/www/html/custom/project/CasProductLabels/Components/PDFLabel.php:95 + /var/www/html/custom/project/CasProductLabels/Tests/Unit/Components/PDFLabelTest.php:45 + + + + + +