Skip to content
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

Fix JUnit missing PHPT results #3437

Merged
merged 6 commits into from Dec 6, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 34 additions & 5 deletions src/Runner/PhptTestCase.php
Expand Up @@ -62,6 +62,11 @@ class PhptTestCase implements Test, SelfDescribing
*/
private $phpUtil;

/**
* @var string
*/
private $output = '';

/**
* Constructs a test case with the given filename.
*
Expand Down Expand Up @@ -155,8 +160,9 @@ public function run(TestResult $result = null): TestResult

Timer::start();

$jobResult = $this->phpUtil->runJob($code, $this->stringifyIni($settings));
$time = Timer::stop();
$jobResult = $this->phpUtil->runJob($code, $this->stringifyIni($settings));
$time = Timer::stop();
$this->output = $jobResult['stdout'] ?? '';

if ($result->getCollectCodeCoverageInformation() && ($coverage = $this->cleanupForCoverage())) {
$result->getCodeCoverage()->append($coverage, $this, true, [], [], true);
Expand Down Expand Up @@ -202,6 +208,26 @@ public function toString(): string
return $this->filename;
}

public function usesDataProvider(): bool
{
return false;
}

public function getNumAssertions(): int
{
return 1;
}

public function getActualOutput(): string
{
return $this->output;
}

public function hasOutput(): bool
{
return !empty($this->output);
}

/**
* Parse --INI-- section key value pairs and return as array.
*
Expand Down Expand Up @@ -475,8 +501,8 @@ private function render(string $code): string

private function getCoverageFiles(): array
{
$baseDir = \dirname(\realpath($this->filename)) . \DIRECTORY_SEPARATOR;
$basename = \basename($this->filename, 'phpt');
$baseDir = \dirname(\realpath($this->filename)) . \DIRECTORY_SEPARATOR;
$basename = \basename($this->filename, 'phpt');

return [
'coverage' => $baseDir . $basename . 'coverage',
Expand Down Expand Up @@ -507,7 +533,10 @@ private function renderForCoverage(string &$job): void
$globals = '';

if (!empty($GLOBALS['__PHPUNIT_BOOTSTRAP'])) {
$globals = '$GLOBALS[\'__PHPUNIT_BOOTSTRAP\'] = ' . \var_export($GLOBALS['__PHPUNIT_BOOTSTRAP'], true) . ";\n";
$globals = '$GLOBALS[\'__PHPUNIT_BOOTSTRAP\'] = ' . \var_export(
$GLOBALS['__PHPUNIT_BOOTSTRAP'],
true
) . ";\n";
}

$template->setVar(
Expand Down
9 changes: 0 additions & 9 deletions src/Util/Log/JUnit.php
Expand Up @@ -15,7 +15,6 @@
use PHPUnit\Framework\ExceptionWrapper;
use PHPUnit\Framework\SelfDescribing;
use PHPUnit\Framework\Test;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestFailure;
use PHPUnit\Framework\TestListener;
use PHPUnit\Framework\TestSuite;
Expand Down Expand Up @@ -291,10 +290,6 @@ public function endTestSuite(TestSuite $suite): void
*/
public function startTest(Test $test): void
{
if (!$test instanceof TestCase) {
return;
}

$testCase = $this->document->createElement('testcase');
$testCase->setAttribute('name', $test->getName());

Expand All @@ -318,10 +313,6 @@ public function startTest(Test $test): void
*/
public function endTest(Test $test, float $time): void
{
if (!$test instanceof TestCase) {
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change fixes phpttestcase, but it will cause fatal errors for third party implementations of Test interface having no getNumAssertions or hasOutput methods.
It would be the best to check if those methods exist before calling them.
Up until 7.3.5 here was a condition to check if hasOutput method exists.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, now I understand the history here a bit better. Other implementations can run into the exact same problem of course.

I will add some safety around this.


$numAssertions = $test->getNumAssertions();
$this->testSuiteAssertions[$this->testSuiteLevel] += $numAssertions;

Expand Down
28 changes: 28 additions & 0 deletions tests/end-to-end/log-junit-phpt.phpt
@@ -0,0 +1,28 @@
--TEST--
phpunit --log-junit php://stdout ../end-to-end/phpt-stderr.phpt
--FILE--
<?php
$_SERVER['argv'][1] = '--no-configuration';
$_SERVER['argv'][2] = '--log-junit';
$_SERVER['argv'][3] = 'php://stdout';
$_SERVER['argv'][4] = \realpath(__DIR__ . '/../end-to-end/phpt-stderr.phpt');

require __DIR__ . '/../bootstrap.php';
PHPUnit\TextUI\Command::main();
?>
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

. 1 / 1 (100%)<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite name="" tests="1" assertions="1" errors="0" failures="0" skipped="0" time="%s">
<testcase name="%send-to-end%ephpt-stderr.phpt" assertions="1" time="%s">
<system-out>PHPUnit must look at STDERR when running PHPT tests.</system-out>
</testcase>
</testsuite>
</testsuites>


Time: %s, Memory: %s

OK (1 test, 1 assertion)
25 changes: 25 additions & 0 deletions tests/end-to-end/log-teamcity-phpt.phpt
@@ -0,0 +1,25 @@
--TEST--
phpunit --log-teamcity php://stdout ../end-to-end/phpt-stderr.phpt
--FILE--
<?php
$_SERVER['argv'][1] = '--no-configuration';
$_SERVER['argv'][2] = '--log-teamcity';
$_SERVER['argv'][3] = 'php://stdout';
$_SERVER['argv'][4] = \realpath(__DIR__ . '/../end-to-end/phpt-stderr.phpt');

require __DIR__ . '/../bootstrap.php';
PHPUnit\TextUI\Command::main();
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.


##teamcity[testCount count='1' flowId='%d']

##teamcity[testStarted name='%send-to-end%ephpt-stderr.phpt' flowId='%d']
. 1 / 1 (100%)
##teamcity[testFinished name='%send-to-end%ephpt-stderr.phpt' duration='%d' flowId='%d']


Time: %s, Memory: %s

OK (1 test, 1 assertion)
10 changes: 10 additions & 0 deletions tests/unit/Runner/PhptTestCaseTest.php
Expand Up @@ -84,6 +84,16 @@ protected function tearDown(): void
$this->testCase = null;
}

public function testAlwaysReportsNumberOfAssertionsIsOne(): void
{
$this->assertSame(1, $this->testCase->getNumAssertions());
}

public function testAlwaysReportsItDoesNotUseADataprovider(): void
{
$this->assertSame(false, $this->testCase->usesDataProvider());
}

public function testShouldRunFileSectionAsTest(): void
{
$this->setPhpContent($this->ensureCorrectEndOfLine(self::EXPECT_CONTENT));
Expand Down