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

PhpUnit XML reporter #53

Merged
merged 1 commit into from
Dec 31, 2018
Merged
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: php

env:
CODECEPTION_VERSION: '2.4.x-dev'
CODECEPTION_VERSION: 'dev-phpunit-xml-reports'

php:
- 7.1
Expand Down
116 changes: 116 additions & 0 deletions src/Log/PhpUnit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php
namespace Codeception\PHPUnit\Log;

use Codeception\Configuration;
use Codeception\Test\Interfaces\Reported;
use Codeception\Test\Test;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestSuite;

class PhpUnit extends \PHPUnit\Util\Log\JUnit
{
const SUITE_LEVEL = 1;
const FILE_LEVEL = 2;

protected $strictAttributes = ['file', 'name', 'class'];

private $currentFile;
private $currentFileSuite;

public function startTest(\PHPUnit\Framework\Test $test):void
{
if (method_exists($test, 'getFileName') ) {
$filename = $test->getFileName();
} else {
$reflector = new \ReflectionClass($test);
$filename = $reflector->getFileName();
}

if ($filename !== $this->currentFile) {
if ($this->currentFile !== null) {
parent::endTestSuite(new TestSuite());
}

//initialize all values to avoid warnings
$this->testSuiteAssertions[self::FILE_LEVEL] = 0;
$this->testSuiteTests[self::FILE_LEVEL] = 0;
$this->testSuiteTimes[self::FILE_LEVEL] = 0;
$this->testSuiteErrors[self::FILE_LEVEL] = 0;
$this->testSuiteFailures[self::FILE_LEVEL] = 0;
$this->testSuiteSkipped[self::FILE_LEVEL] = 0;

$this->testSuiteLevel = self::FILE_LEVEL;

$this->currentFile = $filename;
$this->currentFileSuite = $this->document->createElement('testsuite');
$this->currentFileSuite->setAttribute('file', $filename);

$this->testSuites[self::SUITE_LEVEL]->appendChild($this->currentFileSuite);
$this->testSuites[self::FILE_LEVEL] = $this->currentFileSuite;
}

if (!$test instanceof Reported) {
parent::startTest($test);
return;
}

$this->currentTestCase = $this->document->createElement('testcase');

$isStrict = Configuration::config()['settings']['strict_xml'];

foreach ($test->getReportFields() as $attr => $value) {
if ($isStrict and !in_array($attr, $this->strictAttributes)) {
continue;
}
$this->currentTestCase->setAttribute($attr, $value);
}
}

public function endTest(\PHPUnit\Framework\Test $test, float $time):void
{
if ($this->currentTestCase !== null && $test instanceof Test) {
$numAssertions = $test->getNumAssertions();
$this->testSuiteAssertions[$this->testSuiteLevel] += $numAssertions;

$this->currentTestCase->setAttribute(
'assertions',
$numAssertions
);
}

if ($test instanceof TestCase) {
parent::endTest($test, $time);
return;
}

// In PhpUnit 7.4.*, parent::endTest ignores tests that aren't instances of TestCase
// so I copied this code from PhpUnit 7.3.5

$this->currentTestCase->setAttribute(
'time',
\sprintf('%F', $time)
);
$this->testSuites[$this->testSuiteLevel]->appendChild(
$this->currentTestCase
);
$this->testSuiteTests[$this->testSuiteLevel]++;
$this->testSuiteTimes[$this->testSuiteLevel] += $time;
$this->currentTestCase = null;
}

/**
* Cleans the mess caused by test suite manipulation in startTest
*/
public function endTestSuite(TestSuite $suite): void
{
if ($suite->getName()) {
if ($this->currentFile) {
//close last file in the test suite
parent::endTestSuite(new TestSuite());
$this->currentFile = null;
}
$this->testSuiteLevel = self::SUITE_LEVEL;
}
parent::endTestSuite($suite);
}
}
18 changes: 13 additions & 5 deletions src/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ class Runner extends \PHPUnit\TextUI\TestRunner
public static $persistentListeners = [];

protected $defaultListeners = [
'xml' => false,
'html' => false,
'tap' => false,
'json' => false,
'report' => false
'xml' => false,
'phpunit-xml' => false,
'html' => false,
'tap' => false,
'json' => false,
'report' => false
];

protected $config = [];
Expand Down Expand Up @@ -156,6 +157,13 @@ protected function applyReporters(\PHPUnit\Framework\TestResult $result, array $
[$this->absolutePath($arguments['xml']), (bool)$arguments['log_incomplete_skipped']]
);
}
if ($arguments['phpunit-xml']) {
codecept_debug('Printing PHPUNIT report into ' . $arguments['phpunit-xml']);
self::$persistentListeners[] = $this->instantiateReporter(
'phpunit-xml',
[$this->absolutePath($arguments['phpunit-xml']), (bool)$arguments['log_incomplete_skipped']]
);
}
if ($arguments['tap']) {
codecept_debug('Printing TAP report into ' . $arguments['tap']);
self::$persistentListeners[] = $this->instantiateReporter('tap', [$this->absolutePath($arguments['tap'])]);
Expand Down