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 d30e6ef..862406c 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') @@ -42,12 +44,24 @@ 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'); + + $this->domElements['All Suites'] = $baseSuite; + + $root->appendChild($baseSuite); $this->document->appendChild($root); foreach ($finder as $file) { @@ -55,10 +69,10 @@ 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 + $output->writeln(sprintf('Error in file %s: %s', $file->getRealPath(), $exception->getMessage())); } } @@ -67,7 +81,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); @@ -82,7 +96,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; } @@ -95,7 +109,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); @@ -126,29 +139,50 @@ 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 addAttributeValueToTestSuite(\DOMElement $element, $key, $value) + private function addChildElements(array $tree, \DOMElement $element) { - $currentValue = $element->hasAttribute($key) ? $element->getAttribute($key) : 0; - $element->setAttribute($key, (string)($currentValue + $value)); + foreach ($tree as $key => $value) { + if ($key == '@attributes') { + continue; + } + $child = $this->document->createElement($key); + $child->nodeValue = $value; + $element->appendChild($child); + } + } - 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); + $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); } } } 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 + + + + + +