From 582529377b0222a0a62087a9e6c82f7addcd21d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20N=C3=BCrnberger?= Date: Sun, 28 Jan 2024 08:52:46 +0100 Subject: [PATCH 1/3] add test for junit output --- tests/Visual/JUnit.php | 75 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 tests/Visual/JUnit.php diff --git a/tests/Visual/JUnit.php b/tests/Visual/JUnit.php new file mode 100644 index 00000000..46b1071a --- /dev/null +++ b/tests/Visual/JUnit.php @@ -0,0 +1,75 @@ + 'DefaultPrinter', 'COLLISION_IGNORE_DURATION' => 'true'], + ); + + $process->run(); + + $rawXmlContent = file_get_contents($junitLogFile); + unlink($junitLogFile); + + // convert xml to array + try { + $xml = new SimpleXMLElement(preg_replace("/(<\/?)(\w+):([^>]*>)/", '$1$2$3', $rawXmlContent)); + + return json_decode(json_encode((array) $xml), true); + } catch (Exception $exception) { + throw new XmlParseException($exception->getMessage(), $exception->getCode(), $exception->getPrevious()); + } +}; + +test('junit output', function () use ($run) { + $result = $run('tests/.tests/SuccessOnly.php'); + + expect($result['testsuite']['@attributes']) + ->name->toBe('Tests\tests\SuccessOnly') + ->file->toBe('tests/.tests/SuccessOnly.php') + ->tests->toBe('2') + ->assertions->toBe('2') + ->errors->toBe('0') + ->failures->toBe('0') + ->skipped->toBe('0'); + + expect($result['testsuite']['testcase']) + ->toHaveCount(2); + + expect($result['testsuite']['testcase'][0]['@attributes']) + ->name->toBe('it can pass with comparison') + ->file->toBe('tests/.tests/SuccessOnly.php::it can pass with comparison') + ->class->toBe('Tests\tests\SuccessOnly') + ->classname->toBe('Tests.tests.SuccessOnly') + ->assertions->toBe('1') + ->time->toStartWith('0.0'); +}); + +test('junit with parallel', function () use ($run) { + $result = $run('tests/.tests/SuccessOnly.php', '--parallel', '--processes=1', '--filter', 'can pass with comparison'); + + expect($result['testsuite']['@attributes']) + ->name->toBe('Tests\tests\SuccessOnly') + ->file->toBe('tests/.tests/SuccessOnly.php') + ->tests->toBe('1') + ->assertions->toBe('1') + ->errors->toBe('0') + ->failures->toBe('0') + ->skipped->toBe('0'); + + expect($result['testsuite']['testcase']) + ->toHaveCount(1); + + expect($result['testsuite']['testcase']['@attributes']) + ->name->toBe('it can pass with comparison') + ->file->toBe('tests/.tests/SuccessOnly.php::it can pass with comparison') + ->class->toBe('Tests\tests\SuccessOnly') + ->classname->toBe('Tests.tests.SuccessOnly') + ->assertions->toBe('1') + ->time->toStartWith('0.0'); +}); From b1558ddde565520641c3c823a6e3babf00991a2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20N=C3=BCrnberger?= Date: Sun, 28 Jan 2024 09:04:42 +0100 Subject: [PATCH 2/3] update snapshot --- tests/.snapshots/success.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 71c65700..c339f9fe 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -1379,6 +1379,10 @@ PASS Tests\Visual\Help ✓ visual snapshot of help command output + PASS Tests\Visual\JUnit + ✓ junit output + ✓ junit with parallel + PASS Tests\Visual\Parallel ✓ parallel ✓ a parallel test can extend another test with same name @@ -1404,4 +1408,4 @@ WARN Tests\Visual\Version - visual snapshot of help command output - Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 20 skipped, 999 passed (2359 assertions) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 20 skipped, 1001 passed (2387 assertions) \ No newline at end of file From e70edbfa38460f20a339071ac2ae07b1cf30f97b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20N=C3=BCrnberger?= Date: Sun, 28 Jan 2024 09:11:06 +0100 Subject: [PATCH 3/3] normalize path for windows --- tests/Visual/JUnit.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/tests/Visual/JUnit.php b/tests/Visual/JUnit.php index 46b1071a..59562b1d 100644 --- a/tests/Visual/JUnit.php +++ b/tests/Visual/JUnit.php @@ -26,12 +26,16 @@ $run = function () { } }; -test('junit output', function () use ($run) { +$normalizedPath = function (string $path) { + return str_replace('/', DIRECTORY_SEPARATOR, $path); +}; + +test('junit output', function () use ($normalizedPath, $run) { $result = $run('tests/.tests/SuccessOnly.php'); expect($result['testsuite']['@attributes']) ->name->toBe('Tests\tests\SuccessOnly') - ->file->toBe('tests/.tests/SuccessOnly.php') + ->file->toBe($normalizedPath('tests/.tests/SuccessOnly.php')) ->tests->toBe('2') ->assertions->toBe('2') ->errors->toBe('0') @@ -43,19 +47,19 @@ test('junit output', function () use ($run) { expect($result['testsuite']['testcase'][0]['@attributes']) ->name->toBe('it can pass with comparison') - ->file->toBe('tests/.tests/SuccessOnly.php::it can pass with comparison') + ->file->toBe($normalizedPath('tests/.tests/SuccessOnly.php::it can pass with comparison')) ->class->toBe('Tests\tests\SuccessOnly') ->classname->toBe('Tests.tests.SuccessOnly') ->assertions->toBe('1') ->time->toStartWith('0.0'); }); -test('junit with parallel', function () use ($run) { +test('junit with parallel', function () use ($normalizedPath, $run) { $result = $run('tests/.tests/SuccessOnly.php', '--parallel', '--processes=1', '--filter', 'can pass with comparison'); expect($result['testsuite']['@attributes']) ->name->toBe('Tests\tests\SuccessOnly') - ->file->toBe('tests/.tests/SuccessOnly.php') + ->file->toBe($normalizedPath('tests/.tests/SuccessOnly.php')) ->tests->toBe('1') ->assertions->toBe('1') ->errors->toBe('0') @@ -67,7 +71,7 @@ test('junit with parallel', function () use ($run) { expect($result['testsuite']['testcase']['@attributes']) ->name->toBe('it can pass with comparison') - ->file->toBe('tests/.tests/SuccessOnly.php::it can pass with comparison') + ->file->toBe($normalizedPath('tests/.tests/SuccessOnly.php::it can pass with comparison')) ->class->toBe('Tests\tests\SuccessOnly') ->classname->toBe('Tests.tests.SuccessOnly') ->assertions->toBe('1')