-
Notifications
You must be signed in to change notification settings - Fork 266
PHPLIB-582: Create JSON test result file for evergreen #787
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,10 +26,10 @@ PATH=/opt/php/${PHP_VERSION}-64bit/bin:$OLD_PATH | |
case "$TESTS" in | ||
atlas-data-lake*) | ||
MONGODB_URI="mongodb://mhuser:[email protected]:27017" | ||
php vendor/bin/phpunit --testsuite "Atlas Data Lake Test Suite" | ||
php vendor/bin/phpunit --configuration phpunit.evergreen.xml --testsuite "Atlas Data Lake Test Suite" | ||
;; | ||
|
||
*) | ||
php vendor/bin/phpunit | ||
php vendor/bin/phpunit --configuration phpunit.evergreen.xml | ||
;; | ||
esac |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<phpunit | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.3/phpunit.xsd" | ||
beStrictAboutOutputDuringTests="true" | ||
beStrictAboutChangesToGlobalState="true" | ||
colors="true" | ||
bootstrap="tests/bootstrap.php" | ||
defaultTestSuite="Default Test Suite" | ||
> | ||
|
||
<php> | ||
<ini name="error_reporting" value="-1"/> | ||
<env name="MONGODB_URI" value="mongodb://127.0.0.1:27017/?serverSelectionTimeoutMS=100"/> | ||
<env name="MONGODB_DATABASE" value="phplib_test"/> | ||
</php> | ||
|
||
<testsuites> | ||
<testsuite name="Default Test Suite"> | ||
<directory>./tests/</directory> | ||
</testsuite> | ||
|
||
<testsuite name="Atlas Data Lake Test Suite"> | ||
<file>tests/SpecTests/AtlasDataLakeSpecTest.php</file> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<listeners> | ||
<listener class="MongoDB\Tests\EvergreenLogListener"> | ||
<arguments> | ||
<string>test-results.json</string> | ||
</arguments> | ||
</listener> | ||
</listeners> | ||
</phpunit> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
<?php | ||
|
||
namespace MongoDB\Tests; | ||
|
||
use Exception; | ||
use PHPUnit\Framework\AssertionFailedError; | ||
use PHPUnit\Framework\SelfDescribing; | ||
use PHPUnit\Framework\Test; | ||
use PHPUnit\Framework\TestFailure; | ||
use PHPUnit\Framework\TestListener; | ||
use PHPUnit\Framework\TestSuite; | ||
use PHPUnit\Framework\Warning; | ||
use PHPUnit\Util\Filter; | ||
use PHPUnit\Util\Printer; | ||
use function get_class; | ||
use function json_encode; | ||
use function round; | ||
|
||
// phpcs:disable SlevomatCodingStandard.Exceptions.ReferenceThrowableOnly.ReferencedGeneralException | ||
class EvergreenLogListener extends Printer implements TestListener | ||
{ | ||
/** @var array[] */ | ||
private $tests = []; | ||
|
||
/** @var array|null */ | ||
private $currentTestCase = null; | ||
|
||
/** | ||
* Flush buffer and close output. | ||
*/ | ||
public function flush() | ||
{ | ||
$this->write($this->getJson()); | ||
|
||
parent::flush(); | ||
} | ||
|
||
/** | ||
* An error occurred. | ||
* | ||
* @param Test $test | ||
* @param Exception $e | ||
* @param float $time | ||
*/ | ||
public function addError(Test $test, Exception $e, $time) | ||
{ | ||
$this->doAddFault($test, $e, 'fail'); | ||
} | ||
|
||
/** | ||
* A warning occurred. | ||
* | ||
* @param Test $test | ||
* @param Warning $e | ||
* @param float $time | ||
*/ | ||
public function addWarning(Test $test, Warning $e, $time) | ||
{ | ||
// Do nothing for now | ||
} | ||
|
||
/** | ||
* A failure occurred. | ||
* | ||
* @param Test $test | ||
* @param AssertionFailedError $e | ||
* @param float $time | ||
*/ | ||
public function addFailure(Test $test, AssertionFailedError $e, $time) | ||
{ | ||
$this->doAddFault($test, $e, 'fail'); | ||
} | ||
|
||
/** | ||
* Incomplete test. | ||
* | ||
* @param Test $test | ||
* @param Exception $e | ||
* @param float $time | ||
*/ | ||
public function addIncompleteTest(Test $test, Exception $e, $time) | ||
{ | ||
$this->doAddSkipped($test); | ||
} | ||
|
||
/** | ||
* Risky test. | ||
* | ||
* @param Test $test | ||
* @param Exception $e | ||
* @param float $time | ||
*/ | ||
public function addRiskyTest(Test $test, Exception $e, $time) | ||
{ | ||
return; | ||
} | ||
|
||
/** | ||
* Skipped test. | ||
* | ||
* @param Test $test | ||
* @param Exception $e | ||
* @param float $time | ||
*/ | ||
public function addSkippedTest(Test $test, Exception $e, $time) | ||
{ | ||
$this->doAddSkipped($test); | ||
} | ||
|
||
/** | ||
* A testsuite started. | ||
* | ||
* @param TestSuite $suite | ||
*/ | ||
public function startTestSuite(TestSuite $suite) | ||
{ | ||
} | ||
|
||
/** | ||
* A testsuite ended. | ||
* | ||
* @param TestSuite $suite | ||
*/ | ||
public function endTestSuite(TestSuite $suite) | ||
{ | ||
} | ||
|
||
/** | ||
* A test started. | ||
* | ||
* @param Test $test | ||
*/ | ||
public function startTest(Test $test) | ||
{ | ||
$this->currentTestCase = [ | ||
'status' => 'pass', | ||
'test_file' => get_class($test) . '::' . $test->getName(), | ||
]; | ||
} | ||
|
||
/** | ||
* A test ended. | ||
* | ||
* @param Test $test | ||
* @param float $time | ||
*/ | ||
public function endTest(Test $test, $time) | ||
{ | ||
if (! $this->currentTestCase) { | ||
return; | ||
} | ||
|
||
$this->currentTestCase['elapsed'] = round($time, 6); | ||
|
||
$this->tests[] = $this->currentTestCase; | ||
|
||
$this->currentTestCase = null; | ||
} | ||
|
||
/** | ||
* Returns the XML as a string. | ||
* | ||
* @return string | ||
*/ | ||
public function getJson() | ||
{ | ||
return json_encode(['results' => $this->tests]); | ||
} | ||
|
||
/** | ||
* Method which generalizes addError() and addFailure() | ||
* | ||
* @param Test $test | ||
* @param Exception $e | ||
* @param string $type | ||
*/ | ||
private function doAddFault(Test $test, Exception $e, $type) | ||
{ | ||
if ($this->currentTestCase === null) { | ||
return; | ||
} | ||
|
||
if ($test instanceof SelfDescribing) { | ||
$buffer = $test->toString() . "\n"; | ||
} else { | ||
$buffer = ''; | ||
} | ||
|
||
$buffer .= TestFailure::exceptionToString($e) . "\n" . | ||
Filter::getFilteredStacktrace($e); | ||
|
||
$this->currentTestCase['status'] = $type; | ||
$this->currentTestCase['raw_log'] = $buffer; | ||
} | ||
|
||
private function doAddSkipped(Test $test) | ||
{ | ||
if ($this->currentTestCase === null) { | ||
return; | ||
} | ||
|
||
$this->currentTestCase['status'] = 'skip'; | ||
} | ||
} | ||
// phpcs:enable SlevomatCodingStandard.Exceptions.ReferenceThrowableOnly.ReferencedGeneralException |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we already use symfony/phpunit-bridge, I noticed it has an alias for a SymfonyTestsListener interface. I think you could leverage that instead of implementing PHPUnit's interface directly and preserve our ability to test with PHPUnit 8.x.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, that class is not a generic implementation for test listeners, but rather a specific one to support the use-case of the PHPUnit bridge.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I just poked my head into SymfonyTestsListenerTrait.