Skip to content

Commit 38a1ba9

Browse files
authored
PHPLIB-582: Create JSON test result file for evergreen (#787)
* Prevent installing PHPUnit > 6 * Add test listener to write JSON result files
1 parent 3800017 commit 38a1ba9

File tree

5 files changed

+244
-11
lines changed

5 files changed

+244
-11
lines changed

.evergreen/config.yml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -253,13 +253,6 @@ functions:
253253
chmod +x $i
254254
done
255255
256-
"init test-results":
257-
- command: shell.exec
258-
params:
259-
script: |
260-
${PREPARE_SHELL}
261-
echo '{"results": [{ "status": "FAIL", "test_file": "Build", "log_raw": "No test-results.json found was created" } ]}' > ${PROJECT_DIRECTORY}/test-results.json
262-
263256
"install dependencies":
264257
- command: shell.exec
265258
params:
@@ -275,7 +268,6 @@ pre:
275268
- func: "prepare resources"
276269
- func: "windows fix"
277270
- func: "fix absolute paths"
278-
- func: "init test-results"
279271
- func: "make files executable"
280272
- func: "install dependencies"
281273

.evergreen/run-tests.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ PATH=/opt/php/${PHP_VERSION}-64bit/bin:$OLD_PATH
2626
case "$TESTS" in
2727
atlas-data-lake*)
2828
MONGODB_URI="mongodb://mhuser:[email protected]:27017"
29-
php vendor/bin/phpunit --testsuite "Atlas Data Lake Test Suite"
29+
php vendor/bin/phpunit --configuration phpunit.evergreen.xml --testsuite "Atlas Data Lake Test Suite"
3030
;;
3131

3232
*)
33-
php vendor/bin/phpunit
33+
php vendor/bin/phpunit --configuration phpunit.evergreen.xml
3434
;;
3535
esac

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"jean85/pretty-package-versions": "^1.2"
1717
},
1818
"require-dev": {
19-
"phpunit/phpunit": "^6.4 || ^8.3",
19+
"phpunit/phpunit": "^6.4",
2020
"sebastian/comparator": "^2.0 || ^3.0",
2121
"squizlabs/php_codesniffer": "^3.5, <3.5.5",
2222
"symfony/phpunit-bridge": "^4.4@dev"

phpunit.evergreen.xml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.3/phpunit.xsd"
6+
beStrictAboutOutputDuringTests="true"
7+
beStrictAboutChangesToGlobalState="true"
8+
colors="true"
9+
bootstrap="tests/bootstrap.php"
10+
defaultTestSuite="Default Test Suite"
11+
>
12+
13+
<php>
14+
<ini name="error_reporting" value="-1"/>
15+
<env name="MONGODB_URI" value="mongodb://127.0.0.1:27017/?serverSelectionTimeoutMS=100"/>
16+
<env name="MONGODB_DATABASE" value="phplib_test"/>
17+
</php>
18+
19+
<testsuites>
20+
<testsuite name="Default Test Suite">
21+
<directory>./tests/</directory>
22+
</testsuite>
23+
24+
<testsuite name="Atlas Data Lake Test Suite">
25+
<file>tests/SpecTests/AtlasDataLakeSpecTest.php</file>
26+
</testsuite>
27+
</testsuites>
28+
29+
<listeners>
30+
<listener class="MongoDB\Tests\EvergreenLogListener">
31+
<arguments>
32+
<string>test-results.json</string>
33+
</arguments>
34+
</listener>
35+
</listeners>
36+
</phpunit>

tests/EvergreenLogListener.php

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
<?php
2+
3+
namespace MongoDB\Tests;
4+
5+
use Exception;
6+
use PHPUnit\Framework\AssertionFailedError;
7+
use PHPUnit\Framework\SelfDescribing;
8+
use PHPUnit\Framework\Test;
9+
use PHPUnit\Framework\TestFailure;
10+
use PHPUnit\Framework\TestListener;
11+
use PHPUnit\Framework\TestSuite;
12+
use PHPUnit\Framework\Warning;
13+
use PHPUnit\Util\Filter;
14+
use PHPUnit\Util\Printer;
15+
use function get_class;
16+
use function json_encode;
17+
use function round;
18+
19+
// phpcs:disable SlevomatCodingStandard.Exceptions.ReferenceThrowableOnly.ReferencedGeneralException
20+
class EvergreenLogListener extends Printer implements TestListener
21+
{
22+
/** @var array[] */
23+
private $tests = [];
24+
25+
/** @var array|null */
26+
private $currentTestCase = null;
27+
28+
/**
29+
* Flush buffer and close output.
30+
*/
31+
public function flush()
32+
{
33+
$this->write($this->getJson());
34+
35+
parent::flush();
36+
}
37+
38+
/**
39+
* An error occurred.
40+
*
41+
* @param Test $test
42+
* @param Exception $e
43+
* @param float $time
44+
*/
45+
public function addError(Test $test, Exception $e, $time)
46+
{
47+
$this->doAddFault($test, $e, 'fail');
48+
}
49+
50+
/**
51+
* A warning occurred.
52+
*
53+
* @param Test $test
54+
* @param Warning $e
55+
* @param float $time
56+
*/
57+
public function addWarning(Test $test, Warning $e, $time)
58+
{
59+
// Do nothing for now
60+
}
61+
62+
/**
63+
* A failure occurred.
64+
*
65+
* @param Test $test
66+
* @param AssertionFailedError $e
67+
* @param float $time
68+
*/
69+
public function addFailure(Test $test, AssertionFailedError $e, $time)
70+
{
71+
$this->doAddFault($test, $e, 'fail');
72+
}
73+
74+
/**
75+
* Incomplete test.
76+
*
77+
* @param Test $test
78+
* @param Exception $e
79+
* @param float $time
80+
*/
81+
public function addIncompleteTest(Test $test, Exception $e, $time)
82+
{
83+
$this->doAddSkipped($test);
84+
}
85+
86+
/**
87+
* Risky test.
88+
*
89+
* @param Test $test
90+
* @param Exception $e
91+
* @param float $time
92+
*/
93+
public function addRiskyTest(Test $test, Exception $e, $time)
94+
{
95+
return;
96+
}
97+
98+
/**
99+
* Skipped test.
100+
*
101+
* @param Test $test
102+
* @param Exception $e
103+
* @param float $time
104+
*/
105+
public function addSkippedTest(Test $test, Exception $e, $time)
106+
{
107+
$this->doAddSkipped($test);
108+
}
109+
110+
/**
111+
* A testsuite started.
112+
*
113+
* @param TestSuite $suite
114+
*/
115+
public function startTestSuite(TestSuite $suite)
116+
{
117+
}
118+
119+
/**
120+
* A testsuite ended.
121+
*
122+
* @param TestSuite $suite
123+
*/
124+
public function endTestSuite(TestSuite $suite)
125+
{
126+
}
127+
128+
/**
129+
* A test started.
130+
*
131+
* @param Test $test
132+
*/
133+
public function startTest(Test $test)
134+
{
135+
$this->currentTestCase = [
136+
'status' => 'pass',
137+
'test_file' => get_class($test) . '::' . $test->getName(),
138+
];
139+
}
140+
141+
/**
142+
* A test ended.
143+
*
144+
* @param Test $test
145+
* @param float $time
146+
*/
147+
public function endTest(Test $test, $time)
148+
{
149+
if (! $this->currentTestCase) {
150+
return;
151+
}
152+
153+
$this->currentTestCase['elapsed'] = round($time, 6);
154+
155+
$this->tests[] = $this->currentTestCase;
156+
157+
$this->currentTestCase = null;
158+
}
159+
160+
/**
161+
* Returns the XML as a string.
162+
*
163+
* @return string
164+
*/
165+
public function getJson()
166+
{
167+
return json_encode(['results' => $this->tests]);
168+
}
169+
170+
/**
171+
* Method which generalizes addError() and addFailure()
172+
*
173+
* @param Test $test
174+
* @param Exception $e
175+
* @param string $type
176+
*/
177+
private function doAddFault(Test $test, Exception $e, $type)
178+
{
179+
if ($this->currentTestCase === null) {
180+
return;
181+
}
182+
183+
if ($test instanceof SelfDescribing) {
184+
$buffer = $test->toString() . "\n";
185+
} else {
186+
$buffer = '';
187+
}
188+
189+
$buffer .= TestFailure::exceptionToString($e) . "\n" .
190+
Filter::getFilteredStacktrace($e);
191+
192+
$this->currentTestCase['status'] = $type;
193+
$this->currentTestCase['raw_log'] = $buffer;
194+
}
195+
196+
private function doAddSkipped(Test $test)
197+
{
198+
if ($this->currentTestCase === null) {
199+
return;
200+
}
201+
202+
$this->currentTestCase['status'] = 'skip';
203+
}
204+
}
205+
// phpcs:enable SlevomatCodingStandard.Exceptions.ReferenceThrowableOnly.ReferencedGeneralException

0 commit comments

Comments
 (0)