Skip to content

Commit be8b5ef

Browse files
authored
Improve testing against PHPUnit version 10, 11, and 12 (via #116)
1 parent 89404e9 commit be8b5ef

File tree

9 files changed

+179
-57
lines changed

9 files changed

+179
-57
lines changed

.github/workflows/build.yml

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ on:
1111

1212
jobs:
1313
tests:
14-
name: PHP ${{ matrix.php-version }} on ${{ matrix.os }} (${{ matrix.composer-options }})
14+
name: PHP ${{ matrix.php-version }} PHPUnit ${{matrix.phpunit-version}} on ${{ matrix.os }} (${{ matrix.prefer-lowest }})
1515
runs-on: ${{ matrix.os }}
1616
strategy:
1717
fail-fast: false
@@ -25,9 +25,34 @@ jobs:
2525
- ubuntu-latest
2626
- windows-latest
2727
- macOS-latest
28-
composer-options:
28+
phpunit-version:
29+
- "10"
30+
- "11"
31+
- "12"
32+
prefer-lowest:
2933
- ""
3034
- "--prefer-lowest"
35+
exclude:
36+
# PHPUnit ^11 requires PHP 8.2
37+
- phpunit-version: "11"
38+
php-version: "8.1"
39+
40+
# PHPUnit ^12 requires PHP 8.3
41+
- phpunit-version: "12"
42+
php-version: "8.1"
43+
- phpunit-version: "12"
44+
php-version: "8.2"
45+
include:
46+
# PHPUnit ^10.5.32 is incompatible with paratest 7.3.
47+
# Paratest 7.4.6, which fixes the incompatibility, requires PHP 8.2
48+
- php-version: "8.1"
49+
phpunit-version: "10"
50+
update-constraints: "'--with=phpunit/phpunit:<10.5.32'"
51+
52+
# PHPUnit ~11.0 || ~11.1 can't double readonly classes, which fails some tests
53+
# We constraint PHPUnit 11 to ^11.2 until we find a better solution
54+
- phpunit-version: "11"
55+
update-constraints: "'--with=phpunit/phpunit:^11.2'"
3156
steps:
3257
- name: Checkout
3358
uses: actions/checkout@v4
@@ -44,23 +69,32 @@ jobs:
4469
run: composer validate
4570

4671
- name: Install dependencies
47-
run: composer update
48-
--prefer-dist
49-
--no-progress
50-
${{ matrix.composer-options }}
72+
run: >
73+
composer update --prefer-dist --no-progress
74+
${{ matrix.prefer-lowest }}
75+
'--with=phpunit/phpunit:^${{ matrix.phpunit-version }}'
76+
${{ matrix.update-constraints }}
77+
shell: bash
78+
79+
- name: Set PHPUnit version
80+
id: set-phpunit-version
81+
run: |
82+
echo "Tested against PHPUnit **v$(composer print-phpunit-version)**" >> "$GITHUB_STEP_SUMMARY"
83+
echo "PHPUNIT_SCHEMA_VERSION=$(composer print-phpunit-schema-version)" >> "$GITHUB_OUTPUT"
84+
shell: bash
5185

5286
- name: Run tests
53-
if: ${{ matrix.os != 'windows-latest' && !contains(matrix.composer-options, '--prefer-lowest') }}
87+
if: ${{ matrix.os != 'windows-latest' && steps.set-phpunit-version.outputs.PHPUNIT_SCHEMA_VERSION != '10.0' }}
5488
run: composer test
5589

56-
- name: Run tests (lowest)
57-
if: ${{ matrix.os != 'windows-latest' && contains(matrix.composer-options, '--prefer-lowest') }}
58-
run: composer test-lowest
90+
- name: Run tests (phpunit10.0)
91+
if: ${{ matrix.os != 'windows-latest' && steps.set-phpunit-version.outputs.PHPUNIT_SCHEMA_VERSION == '10.0' }}
92+
run: composer test-phpunit10.0
5993

6094
- name: Run tests (windows)
61-
if: ${{ matrix.os == 'windows-latest' && !contains(matrix.composer-options, '--prefer-lowest') }}
95+
if: ${{ matrix.os == 'windows-latest' && steps.set-phpunit-version.outputs.PHPUNIT_SCHEMA_VERSION != '10.0' }}
6296
run: composer test-windows
6397

64-
- name: Run tests (windows-lowest)
65-
if: ${{ matrix.os == 'windows-latest' && contains(matrix.composer-options, '--prefer-lowest') }}
66-
run: composer test-windows-lowest
98+
- name: Run tests (windows-phpunit10.0)
99+
if: ${{ matrix.os == 'windows-latest' && steps.set-phpunit-version.outputs.PHPUNIT_SCHEMA_VERSION == '10.0' }}
100+
run: composer test-windows-phpunit10.0

composer.json

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,13 @@
3131
"require": {
3232
"php": "~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0",
3333
"allure-framework/allure-php-commons": "^2.0",
34-
"phpunit/phpunit": "^10 || ^11 || ^12"
34+
"phpunit/phpunit": "^10.0.5 || ^11 || ^12.0.1"
3535
},
3636
"require-dev": {
37-
"phpunit/phpunit": "~10.4.0 || ^11 || ^12",
38-
"brianium/paratest": "^7.3",
39-
"psalm/plugin-phpunit": "^0.19.3",
37+
"brianium/paratest": "^7",
38+
"psalm/plugin-phpunit": "^0.19.5",
4039
"squizlabs/php_codesniffer": "^3.7.2",
41-
"vimeo/psalm": "^6.9"
40+
"vimeo/psalm": "^6.10"
4241
},
4342
"conflict": {
4443
"amphp/byte-stream": "<1.5.1",
@@ -63,20 +62,21 @@
6362
},
6463
"psr-4": {
6564
"Qameta\\Allure\\PHPUnit\\Test\\Unit\\": "test/unit/",
66-
"Qameta\\Allure\\PHPUnit\\Test\\Report\\": "test/report/"
65+
"Qameta\\Allure\\PHPUnit\\Test\\Report\\": "test/report/",
66+
"Qameta\\Allure\\PHPUnit\\Scripts\\": "scripts/"
6767
}
6868
},
6969
"scripts": {
7070
"test-cs": "vendor/bin/phpcs -sp",
7171
"test-unit": "vendor/bin/phpunit --coverage-text",
72-
"test-unit-lowest": "vendor/bin/phpunit --configuration=phpunit.10.0.xml --coverage-text",
72+
"test-unit-phpunit10.0": "vendor/bin/phpunit --configuration=phpunit.10.0.xml --coverage-text",
7373
"clear-allure-results": "rm -rf ./build/allure-results",
7474
"test-report": [
7575
"@clear-allure-results",
7676
"vendor/bin/paratest --processes=3 --configuration=phpunit.report.xml --testsuite=positive",
7777
"vendor/bin/paratest --processes=3 --configuration=phpunit.report.xml --testsuite=negative; exit 0"
7878
],
79-
"test-report-lowest": [
79+
"test-report-phpunit10.0": [
8080
"@clear-allure-results",
8181
"vendor/bin/paratest --processes=3 --configuration=phpunit.10.0.report.xml --testsuite=positive",
8282
"vendor/bin/paratest --processes=3 --configuration=phpunit.10.0.report.xml --testsuite=negative; exit 0"
@@ -86,7 +86,7 @@
8686
"vendor/bin/paratest --processes=3 --configuration=phpunit.report.xml --testsuite=positive",
8787
"vendor/bin/paratest --processes=3 --configuration=phpunit.report.xml --testsuite=negative & exit 0"
8888
],
89-
"test-report-windows-lowest": [
89+
"test-report-windows-phpunit10.0": [
9090
"@clear-allure-results",
9191
"vendor/bin/paratest --processes=3 --configuration=phpunit.10.0.report.xml --testsuite=positive",
9292
"vendor/bin/paratest --processes=3 --configuration=phpunit.10.0.report.xml --testsuite=negative & exit 0"
@@ -98,10 +98,10 @@
9898
"@test-report",
9999
"@test-psalm"
100100
],
101-
"test-lowest": [
101+
"test-phpunit10.0": [
102102
"@test-cs",
103-
"@test-unit-lowest",
104-
"@test-report-lowest",
103+
"@test-unit-phpunit10.0",
104+
"@test-report-phpunit10.0",
105105
"@test-psalm"
106106
],
107107
"test-windows": [
@@ -110,11 +110,17 @@
110110
"@test-report-windows",
111111
"@test-psalm"
112112
],
113-
"test-windows-lowest": [
113+
"test-windows-phpunit10.0": [
114114
"@test-cs",
115-
"@test-unit-lowest",
116-
"@test-report-windows-lowest",
115+
"@test-unit-phpunit10.0",
116+
"@test-report-windows-phpunit10.0",
117117
"@test-psalm"
118+
],
119+
"print-phpunit-version": [
120+
"Qameta\\Allure\\PHPUnit\\Scripts\\CiScripts::printPhpUnitVersion"
121+
],
122+
"print-phpunit-schema-version": [
123+
"Qameta\\Allure\\PHPUnit\\Scripts\\CiScripts::printConfigSchemaVersion"
118124
]
119125
}
120126
}

phpunit.10.0.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/12.0/phpunit.xsd"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
55
colors="true"
66
displayDetailsOnIncompleteTests="true"
77
displayDetailsOnSkippedTests="true"
@@ -15,9 +15,9 @@
1515
<directory>test/unit/</directory>
1616
</testsuite>
1717
</testsuites>
18-
<source>
18+
<coverage>
1919
<include>
2020
<directory suffix=".php">src/</directory>
2121
</include>
22-
</source>
22+
</coverage>
2323
</phpunit>

phpunit.report.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/12.1/phpunit.xsd"
55
colors="true"
66
displayDetailsOnIncompleteTests="true"
77
displayDetailsOnSkippedTests="true"

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/12.1/phpunit.xsd"
55
colors="true"
66
displayDetailsOnIncompleteTests="true"
77
displayDetailsOnSkippedTests="true"

scripts/CiScripts.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Qameta\Allure\PHPUnit\Scripts;
4+
5+
final class CiScripts
6+
{
7+
/**
8+
* Prints the version of PHPUnit.
9+
*/
10+
public static function printPhpUnitVersion(): void
11+
{
12+
if (class_exists(\PHPUnit\Runner\Version::class)) {
13+
echo(\PHPUnit\Runner\Version::id());
14+
}
15+
}
16+
17+
/**
18+
* Prints the version of the PHPUnit XML schema.
19+
* We use this in the CI pipeline to deside which configuration file to use.
20+
*/
21+
public static function printConfigSchemaVersion(): void
22+
{
23+
if (class_exists(\PHPUnit\Runner\Version::class)) {
24+
echo(preg_replace('/^(\d+\.\d+)\..*$/', '$1', \PHPUnit\Runner\Version::id()));
25+
}
26+
}
27+
}

test/report/Generate/DataProviderTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ public function testNamedDataSet(int $x, int $y): void
1717
Allure::runStep(fn () => self::assertSame($x, $y));
1818
}
1919

20+
/**
21+
* @return iterable<string, array<int, int>>
22+
*/
2023
public static function providerNamed(): iterable
2124
{
2225
return [
@@ -34,6 +37,9 @@ public function testListedDataSet(int $x, int $y): void
3437
Allure::runStep(fn () => self::assertSame($x, $y));
3538
}
3639

40+
/**
41+
* @return iterable<int, array<int, int>>
42+
*/
3743
public static function providerListed(): iterable
3844
{
3945
return [

test/unit/Event/EventTestTrait.php

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,7 @@ trait EventTestTrait
3838
{
3939
protected function createTelemetryInfo(): Info
4040
{
41-
/**
42-
* @psalm-suppress MixedAssignment
43-
* @psalm-suppress TooManyArguments
44-
*/
45-
$garbageCollectorStatus = class_exists(GarbageCollectorStatus::class)
46-
? new GarbageCollectorStatus(
47-
0,
48-
0,
49-
0,
50-
0,
51-
0.0,
52-
0.0,
53-
0.0,
54-
0.0,
55-
false,
56-
false,
57-
false,
58-
0,
59-
)
60-
: null; // early PHPUnit 10
41+
$garbageCollectorStatus = self::createGarbageCollectorStatus();
6142

6243
/**
6344
* @psalm-suppress TooManyArguments
@@ -264,4 +245,51 @@ protected function createTestWarningTriggeredEvent(
264245
false,
265246
);
266247
}
248+
249+
/**
250+
* GarbageCollectorStatus doesn't exist in PHPUnit 10.0
251+
* @psalm-suppress UndefinedClass
252+
*/
253+
private static function createGarbageCollectorStatus(): ?GarbageCollectorStatus
254+
{
255+
// GarbageCollectorStatus doesn't exist in PHPUnit 10.0
256+
if (class_exists(GarbageCollectorStatus::class)) {
257+
$ctorInfo = (new \ReflectionClass(GarbageCollectorStatus::class))->getConstructor();
258+
if (isset($ctorInfo)) {
259+
/**
260+
* @psalm-suppress InvalidArgument
261+
* @psalm-suppress TooFewArguments
262+
* @psalm-suppress TooManyArguments
263+
*/
264+
return $ctorInfo->getNumberOfParameters() === 8
265+
# PHPUnit [10.1, 10.3)
266+
? new GarbageCollectorStatus(
267+
0,
268+
0,
269+
0,
270+
0,
271+
false,
272+
false,
273+
false,
274+
0,
275+
)
276+
# Extra arguments are required since 10.3.0
277+
: new GarbageCollectorStatus(
278+
0,
279+
0,
280+
0,
281+
0,
282+
0.0,
283+
0.0,
284+
0.0,
285+
0.0,
286+
false,
287+
false,
288+
false,
289+
0,
290+
);
291+
}
292+
}
293+
return null;
294+
}
267295
}

test/unit/ExceptionDetailsTraitTest.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44

55
namespace Qameta\Allure\PHPUnit\Test\Unit;
66

7+
// phpcs:disable PSR1.Classes.ClassDeclaration.MultipleClasses
8+
79
use Exception;
810
use PHPUnit\Framework\TestCase;
11+
use PHPUnit\Framework\Attributes\CoversClass;
12+
use PHPUnit\Framework\Attributes\CoversTrait;
913
use Qameta\Allure\PHPUnit\ExceptionDetailsTrait;
1014
use Qameta\Allure\PHPUnit\AllureAdapter;
1115
use Qameta\Allure\PHPUnit\AllureAdapterInterface;
1216
use Throwable;
1317

14-
// #[CoversTrait(ExceptionDetailsTrait::class)] <--- needed by phpunit 12
15-
// #[CoversClass(ExceptionDetailsTrait::class)] <--- needed by phpunit 10 + 11
16-
final class ExceptionDetailsTraitTest extends TestCase
18+
abstract class ExceptionDetailsTraitTestBase extends TestCase
1719
{
1820
public function testOnNotSuccessfulTest_GivenException_ThrowsSameException(): void
1921
{
@@ -49,3 +51,22 @@ public function testOnNotSuccessfulTest_GivenException_SetsSameExceptionAsLast()
4951
}
5052
}
5153
}
54+
55+
/**
56+
* CoversTrait was added in PHPUnit 11.2.0.
57+
* If was then deprecated in 11.4.0 but the deprecation was reverted in 11.5.4.
58+
* @psalm-suppress DeprecatedClass
59+
*/
60+
if (class_exists(CoversTrait::class)) {
61+
#[CoversTrait(ExceptionDetailsTrait::class)]
62+
final class ExceptionDetailsTraitTest extends ExceptionDetailsTraitTestBase
63+
{
64+
}
65+
66+
} else {
67+
#[CoversClass(ExceptionDetailsTrait::class)]
68+
final class ExceptionDetailsTraitTest extends ExceptionDetailsTraitTestBase
69+
{
70+
}
71+
72+
}

0 commit comments

Comments
 (0)