Skip to content

Bugfix/log merging #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.Build
/.idea
composer.lock
.phpunit.result.cache
70 changes: 52 additions & 18 deletions src/PhpunitMerger/Command/LogCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class LogCommand extends Command
*/
private $domElements = [];

private $keysToCalculate = ['assertions', 'time', 'tests', 'errors', 'failures', 'skipped'];

protected function configure()
{
$this->setName('log')
Expand All @@ -42,23 +44,35 @@ 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) {
try {
$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>Error in file %s: %s</error>', $file->getRealPath(), $exception->getMessage()));
}
}

Expand All @@ -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);
Expand All @@ -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;
}
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions tests/PhpunitMerger/Command/AbstractCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ 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);
}

public function assertOutputDirectoryNotExists()
{
self::assertDirectoryExists($this->logDirectory, $this->logDirectory . ' does not exists');
$filesystem = new Filesystem();
$filesystem->remove($this->logDirectory . dirname($this->outputFile));

Expand Down
5 changes: 3 additions & 2 deletions tests/PhpunitMerger/Command/Log/LogCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]
);
Expand All @@ -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);
}
}
11 changes: 11 additions & 0 deletions tests/PhpunitMerger/Command/Log/datasets/integration.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite name="" tests="2" assertions="3" errors="0" failures="0" skipped="0" time="8.728057">
<testsuite name="Integration Tests" tests="2" assertions="3" errors="0" failures="0" skipped="0" time="8.728057">
<testsuite name="ForestsoftMerchant\tests\functional\Components\FTPFiles\FTPFilesOrderImportNotificationTest" file="/var/www/html/custom/project/ForestsoftMerchant/tests/functional/Components/FTPFiles/FTPFilesOrderImportNotificationTest.php" tests="2" assertions="3" errors="0" failures="0" skipped="0" time="0.032107">
<testcase name="test__toString" class="ForestsoftMerchant\tests\functional\Components\FTPFiles\FTPFilesOrderImportNotificationTest" classname="ForestsoftMerchant.tests.functional.Components.FTPFiles.FTPFilesOrderImportNotificationTest" file="/var/www/html/custom/project/ForestsoftMerchant/tests/functional/Components/FTPFiles/FTPFilesOrderImportNotificationTest.php" line="13" assertions="1" time="0.006198"/>
<testcase name="test__toString_with_ordermessages" class="ForestsoftMerchant\tests\functional\Components\FTPFiles\FTPFilesOrderImportNotificationTest" classname="ForestsoftMerchant.tests.functional.Components.FTPFiles.FTPFilesOrderImportNotificationTest" file="/var/www/html/custom/project/ForestsoftMerchant/tests/functional/Components/FTPFiles/FTPFilesOrderImportNotificationTest.php" line="22" assertions="2" time="0.025909"/>
</testsuite>
</testsuite>
</testsuite>
</testsuites>
28 changes: 28 additions & 0 deletions tests/PhpunitMerger/Command/Log/datasets/unit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite name="" tests="6" assertions="6" errors="0" failures="1" skipped="5" time="10.119620">
<testsuite name="Unit Tests" tests="6" assertions="6" errors="0" failures="1" skipped="5" time="10.119620">
<testsuite name="ForestsoftOrderBenchmark\Tests\Unit\InstallerTest" file="/var/www/html/custom/project/ForestsoftOrderBenchmark/Tests/Unit/InstallerTest.php" tests="4" assertions="5" errors="0" failures="0" skipped="0" time="0.077074">
<testcase name="should_skip_install_mail_model_if_it_exist" class="ForestsoftOrderBenchmark\Tests\Unit\InstallerTest" classname="ForestsoftOrderBenchmark.Tests.Unit.InstallerTest" file="/var/www/html/custom/project/ForestsoftOrderBenchmark/Tests/Unit/InstallerTest.php" line="105" assertions="2" time="0.066342"/>
<testcase name="should_install_mail_model" class="ForestsoftOrderBenchmark\Tests\Unit\InstallerTest" classname="ForestsoftOrderBenchmark.Tests.Unit.InstallerTest" file="/var/www/html/custom/project/ForestsoftOrderBenchmark/Tests/Unit/InstallerTest.php" line="90" assertions="1" time="0.004449"/>
<testcase name="test_should_return_schema_tool" class="ForestsoftOrderBenchmark\Tests\Unit\InstallerTest" classname="ForestsoftOrderBenchmark.Tests.Unit.InstallerTest" file="/var/www/html/custom/project/ForestsoftOrderBenchmark/Tests/Unit/InstallerTest.php" line="118" assertions="1" time="0.001749"/>
<testcase name="test_install_should_create_tables" class="ForestsoftOrderBenchmark\Tests\Unit\InstallerTest" classname="ForestsoftOrderBenchmark.Tests.Unit.InstallerTest" file="/var/www/html/custom/project/ForestsoftOrderBenchmark/Tests/Unit/InstallerTest.php" line="61" assertions="1" time="0.004534"/>
</testsuite>
<testsuite name="CasProductLabels\Tests\Unit\Components\PDFLabelTest" file="/var/www/html/custom/project/CasProductLabels/Tests/Unit/Components/PDFLabelTest.php" tests="2" assertions="1" errors="0" failures="1" skipped="0" time="0.104663">
<testcase name="testCanCreatedByDI" class="CasProductLabels\Tests\Unit\Components\PDFLabelTest" classname="CasProductLabels.Tests.Unit.Components.PDFLabelTest" file="/var/www/html/custom/project/CasProductLabels/Tests/Unit/Components/PDFLabelTest.php" line="49" assertions="1" time="0.003620"/>
<testcase name="testGenerate" class="CasProductLabels\Tests\Unit\Components\PDFLabelTest" classname="CasProductLabels.Tests.Unit.Components.PDFLabelTest" file="/var/www/html/custom/project/CasProductLabels/Tests/Unit/Components/PDFLabelTest.php" line="33" assertions="0" time="0.101043">
<failure type="PHPUnit\Framework\ExpectationFailedException">
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 (...), '&lt;!DOCTYPE html PUBLIC "-//W3C...tml&gt;\n', false): CasProductLabels\Components\PDFRenderer does not match expected value.
Failed asserting that a traversable contains '&lt;html&gt;'.

/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
</failure>
</testcase>
</testsuite>
</testsuite>
</testsuite>
</testsuites>
34 changes: 34 additions & 0 deletions tests/PhpunitMerger/Command/Log/expected_merge.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite name="All Suites" tests="8" assertions="9" errors="0" failures="1" skipped="5" time="18.847677">
<testsuite name="Integration Tests" tests="2" assertions="3" errors="0" failures="0" skipped="0" time="8.728057">
<testsuite name="ForestsoftMerchant\tests\functional\Components\FTPFiles\FTPFilesOrderImportNotificationTest" file="/var/www/html/custom/project/ForestsoftMerchant/tests/functional/Components/FTPFiles/FTPFilesOrderImportNotificationTest.php" tests="2" assertions="3" errors="0" failures="0" skipped="0" time="0.032107">
<testcase name="test__toString" class="ForestsoftMerchant\tests\functional\Components\FTPFiles\FTPFilesOrderImportNotificationTest" classname="ForestsoftMerchant.tests.functional.Components.FTPFiles.FTPFilesOrderImportNotificationTest" file="/var/www/html/custom/project/ForestsoftMerchant/tests/functional/Components/FTPFiles/FTPFilesOrderImportNotificationTest.php" line="13" assertions="1" time="0.006198"/>
<testcase name="test__toString_with_ordermessages" class="ForestsoftMerchant\tests\functional\Components\FTPFiles\FTPFilesOrderImportNotificationTest" classname="ForestsoftMerchant.tests.functional.Components.FTPFiles.FTPFilesOrderImportNotificationTest" file="/var/www/html/custom/project/ForestsoftMerchant/tests/functional/Components/FTPFiles/FTPFilesOrderImportNotificationTest.php" line="22" assertions="2" time="0.025909"/>
</testsuite>
</testsuite>
<testsuite name="Unit Tests" tests="6" assertions="6" errors="0" failures="1" skipped="5" time="10.119620">
<testsuite name="ForestsoftOrderBenchmark\Tests\Unit\InstallerTest" file="/var/www/html/custom/project/ForestsoftOrderBenchmark/Tests/Unit/InstallerTest.php" tests="4" assertions="5" errors="0" failures="0" skipped="0" time="0.077074">
<testcase name="should_skip_install_mail_model_if_it_exist" class="ForestsoftOrderBenchmark\Tests\Unit\InstallerTest" classname="ForestsoftOrderBenchmark.Tests.Unit.InstallerTest" file="/var/www/html/custom/project/ForestsoftOrderBenchmark/Tests/Unit/InstallerTest.php" line="105" assertions="2" time="0.066342"/>
<testcase name="should_install_mail_model" class="ForestsoftOrderBenchmark\Tests\Unit\InstallerTest" classname="ForestsoftOrderBenchmark.Tests.Unit.InstallerTest" file="/var/www/html/custom/project/ForestsoftOrderBenchmark/Tests/Unit/InstallerTest.php" line="90" assertions="1" time="0.004449"/>
<testcase name="test_should_return_schema_tool" class="ForestsoftOrderBenchmark\Tests\Unit\InstallerTest" classname="ForestsoftOrderBenchmark.Tests.Unit.InstallerTest" file="/var/www/html/custom/project/ForestsoftOrderBenchmark/Tests/Unit/InstallerTest.php" line="118" assertions="1" time="0.001749"/>
<testcase name="test_install_should_create_tables" class="ForestsoftOrderBenchmark\Tests\Unit\InstallerTest" classname="ForestsoftOrderBenchmark.Tests.Unit.InstallerTest" file="/var/www/html/custom/project/ForestsoftOrderBenchmark/Tests/Unit/InstallerTest.php" line="61" assertions="1" time="0.004534"/>
</testsuite>
<testsuite name="CasProductLabels\Tests\Unit\Components\PDFLabelTest" file="/var/www/html/custom/project/CasProductLabels/Tests/Unit/Components/PDFLabelTest.php" tests="2" assertions="1" errors="0" failures="1" skipped="0" time="0.104663">
<testcase name="testCanCreatedByDI" class="CasProductLabels\Tests\Unit\Components\PDFLabelTest" classname="CasProductLabels.Tests.Unit.Components.PDFLabelTest" file="/var/www/html/custom/project/CasProductLabels/Tests/Unit/Components/PDFLabelTest.php" line="49" assertions="1" time="0.003620"/>
<testcase name="testGenerate" class="CasProductLabels\Tests\Unit\Components\PDFLabelTest" classname="CasProductLabels.Tests.Unit.Components.PDFLabelTest" file="/var/www/html/custom/project/CasProductLabels/Tests/Unit/Components/PDFLabelTest.php" line="33" assertions="0" time="0.101043">
<failure>
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 (...), '&lt;!DOCTYPE html PUBLIC "-//W3C...tml&gt;\n', false): CasProductLabels\Components\PDFRenderer does not match expected value.
Failed asserting that a traversable contains '&lt;html&gt;'.

/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
</failure>
</testcase>
</testsuite>
</testsuite>
</testsuite>
</testsuites>