Skip to content
This repository was archived by the owner on Mar 12, 2025. It is now read-only.

Commit d25db25

Browse files
authored
Merge pull request #55 from Codeception/6.0-phpunit-xml-reporter
6.0 phpunit xml reporter
2 parents d9f02b6 + 5a3ec80 commit d25db25

File tree

3 files changed

+130
-6
lines changed

3 files changed

+130
-6
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
language: php
22

33
env:
4-
CODECEPTION_VERSION: '2.5.x-dev'
4+
CODECEPTION_VERSION: 'dev-phpunit-xml-reports'
55

66
php:
77
- 5.6

src/Log/PhpUnit.php

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
namespace Codeception\PHPUnit\Log;
3+
4+
use Codeception\Configuration;
5+
use Codeception\Test\Interfaces\Reported;
6+
use Codeception\Test\Test;
7+
use PHPUnit\Framework\TestCase;
8+
use PHPUnit\Framework\TestSuite;
9+
10+
class PhpUnit extends \PHPUnit\Util\Log\JUnit
11+
{
12+
const SUITE_LEVEL = 1;
13+
const FILE_LEVEL = 2;
14+
15+
protected $strictAttributes = ['file', 'name', 'class'];
16+
17+
private $currentFile;
18+
private $currentFileSuite;
19+
20+
public function startTest(\PHPUnit\Framework\Test $test)
21+
{
22+
if (method_exists($test, 'getFileName') ) {
23+
$filename = $test->getFileName();
24+
} else {
25+
$reflector = new \ReflectionClass($test);
26+
$filename = $reflector->getFileName();
27+
}
28+
29+
if ($filename !== $this->currentFile) {
30+
if ($this->currentFile !== null) {
31+
parent::endTestSuite(new TestSuite());
32+
}
33+
34+
//initialize all values to avoid warnings
35+
$this->testSuiteAssertions[self::FILE_LEVEL] = 0;
36+
$this->testSuiteTests[self::FILE_LEVEL] = 0;
37+
$this->testSuiteTimes[self::FILE_LEVEL] = 0;
38+
$this->testSuiteErrors[self::FILE_LEVEL] = 0;
39+
$this->testSuiteFailures[self::FILE_LEVEL] = 0;
40+
$this->testSuiteSkipped[self::FILE_LEVEL] = 0;
41+
42+
$this->testSuiteLevel = self::FILE_LEVEL;
43+
44+
$this->currentFile = $filename;
45+
$this->currentFileSuite = $this->document->createElement('testsuite');
46+
$this->currentFileSuite->setAttribute('file', $filename);
47+
48+
$this->testSuites[self::SUITE_LEVEL]->appendChild($this->currentFileSuite);
49+
$this->testSuites[self::FILE_LEVEL] = $this->currentFileSuite;
50+
}
51+
52+
if (!$test instanceof Reported) {
53+
parent::startTest($test);
54+
return;
55+
}
56+
57+
$this->currentTestCase = $this->document->createElement('testcase');
58+
59+
$isStrict = Configuration::config()['settings']['strict_xml'];
60+
61+
foreach ($test->getReportFields() as $attr => $value) {
62+
if ($isStrict and !in_array($attr, $this->strictAttributes)) {
63+
continue;
64+
}
65+
$this->currentTestCase->setAttribute($attr, $value);
66+
}
67+
}
68+
69+
public function endTest(\PHPUnit\Framework\Test $test, $time)
70+
{
71+
if ($this->currentTestCase !== null && $test instanceof Test) {
72+
$numAssertions = $test->getNumAssertions();
73+
$this->testSuiteAssertions[$this->testSuiteLevel] += $numAssertions;
74+
75+
$this->currentTestCase->setAttribute(
76+
'assertions',
77+
$numAssertions
78+
);
79+
}
80+
81+
if ($test instanceof TestCase) {
82+
parent::endTest($test, $time);
83+
return;
84+
}
85+
86+
// In PhpUnit 7.4.*, parent::endTest ignores tests that aren't instances of TestCase
87+
// so I copied this code from PhpUnit 7.3.5
88+
89+
$this->currentTestCase->setAttribute(
90+
'time',
91+
\sprintf('%F', $time)
92+
);
93+
$this->testSuites[$this->testSuiteLevel]->appendChild(
94+
$this->currentTestCase
95+
);
96+
$this->testSuiteTests[$this->testSuiteLevel]++;
97+
$this->testSuiteTimes[$this->testSuiteLevel] += $time;
98+
$this->currentTestCase = null;
99+
}
100+
101+
/**
102+
* Cleans the mess caused by test suite manipulation in startTest
103+
*/
104+
public function endTestSuite(TestSuite $suite)
105+
{
106+
if ($suite->getName()) {
107+
if ($this->currentFile) {
108+
//close last file in the test suite
109+
parent::endTestSuite(new TestSuite());
110+
$this->currentFile = null;
111+
}
112+
$this->testSuiteLevel = self::SUITE_LEVEL;
113+
}
114+
parent::endTestSuite($suite);
115+
}
116+
}

src/Runner.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ class Runner extends \PHPUnit\TextUI\TestRunner
99
public static $persistentListeners = [];
1010

1111
protected $defaultListeners = [
12-
'xml' => false,
13-
'html' => false,
14-
'tap' => false,
15-
'json' => false,
16-
'report' => false
12+
'xml' => false,
13+
'phpunit-xml' => false,
14+
'html' => false,
15+
'tap' => false,
16+
'json' => false,
17+
'report' => false
1718
];
1819

1920
protected $config = [];
@@ -148,6 +149,13 @@ protected function applyReporters(\PHPUnit\Framework\TestResult $result, array $
148149
[$this->absolutePath($arguments['xml']), (bool)$arguments['log_incomplete_skipped']]
149150
);
150151
}
152+
if ($arguments['phpunit-xml']) {
153+
codecept_debug('Printing PHPUNIT report into ' . $arguments['phpunit-xml']);
154+
self::$persistentListeners[] = $this->instantiateReporter(
155+
'phpunit-xml',
156+
[$this->absolutePath($arguments['phpunit-xml']), (bool)$arguments['log_incomplete_skipped']]
157+
);
158+
}
151159
if ($arguments['tap']) {
152160
codecept_debug('Printing TAP report into ' . $arguments['tap']);
153161
self::$persistentListeners[] = $this->instantiateReporter('tap', [$this->absolutePath($arguments['tap'])]);

0 commit comments

Comments
 (0)