Skip to content

Commit 95128c2

Browse files
committed
Add support for PHPUnit 9
1 parent cce752a commit 95128c2

9 files changed

+175
-93
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
* text eol=lf
2+
test/ export-ignore

.github/workflows/push.yml

+33-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ name: CI
22
on: push
33
jobs:
44
run:
5-
runs-on: ${{ matrix.operating-system }}
5+
env:
6+
ACTIONS_STEP_DEBUG: true
7+
runs-on: ${{ matrix.os }}
68
strategy:
79
fail-fast: false
810
matrix:
9-
operating-system: [ubuntu-latest, windows-latest, macos-latest]
11+
os: [ubuntu-latest, windows-latest, macos-latest]
1012
php-versions: ['7.3', '7.4']
11-
name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }}
13+
phpunit-version: ['8', '9']
14+
name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.os }} (PHPUnit ${{ matrix.phpunit-version }})
1215
steps:
1316
- name: Checkout
1417
uses: actions/checkout@v2
@@ -20,9 +23,36 @@ jobs:
2023
extensions: mbstring
2124
coverage: xdebug
2225

26+
- name: Set PHPUnit Version (Non-Windows)
27+
run: |
28+
sed -i.bak 's#"phpunit/phpunit": "^9"#"phpunit/phpunit": "^${{ matrix.phpunit-version }}"#' composer.json
29+
if: "!startsWith(matrix.os, 'windows')"
30+
31+
- name: Set PHPUnit Version (Windows)
32+
run: |
33+
$content = Get-Content -Path 'composer.json'
34+
$newContent = $content -replace '"phpunit/phpunit": "\^9"', '"phpunit/phpunit": "^${{ matrix.phpunit-version }}"'
35+
$newContent | Set-Content -Path 'composer.json'
36+
if: "startsWith(matrix.os, 'windows')"
37+
2338
- name: Composer dependencies
2439
run: composer install --no-ansi --no-interaction --no-scripts --no-suggest --no-progress --prefer-dist
2540

41+
- name: Configure tests for the current PHPUnit version (Non-Windows)
42+
run: |
43+
V=$(./vendor/bin/phpunit --version)
44+
sed -i.bak "s/%%VERSION%%/$V/" test/states-test.phpt
45+
if: "!startsWith(matrix.os, 'windows')"
46+
47+
- name: Configure tests for the current PHPUnit version (Windows)
48+
id: phpunit-windows
49+
run: |
50+
$V = (.\vendor\bin\phpunit --version | Out-String).trim()
51+
$content = Get-Content -Path 'test/states-test.phpt'
52+
$newContent = $content -replace '%%VERSION%%', $V
53+
$newContent | Set-Content -Path 'test/states-test.phpt'
54+
if: "startsWith(matrix.os, 'windows')"
55+
2656
- name: Run phpunit
2757
run: ./vendor/bin/phpunit
2858

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/vendor
2-
composer.lock
2+
composer.lock
3+
.phpunit.result.cache

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
},
1717
"require": {},
1818
"require-dev": {
19-
"phpunit/phpunit": "^8",
19+
"phpunit/phpunit": "^9",
2020
"squizlabs/php_codesniffer": "3.*"
2121
}
2222
}

src/Printer.php

+14-85
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,26 @@
1-
<?php
1+
<?php // phpcs:disable PSR1.Files.SideEffects.FoundWithSymbols
22

3-
namespace mheap\GithubActionsReporter;
4-
5-
use PHPUnit\Framework\TestResult;
6-
use PHPUnit\Framework\TestFailure;
7-
use PHPUnit\TextUI\ResultPrinter;
8-
9-
class Printer extends ResultPrinter
10-
{
11-
protected $currentType = null;
12-
13-
protected function printHeader(TestResult $result): void
14-
{
15-
}
16-
17-
protected function writeProgress(string $progress): void
18-
{
19-
}
20-
21-
protected function printFooter(TestResult $result): void
22-
{
23-
}
24-
25-
protected function printDefects(array $defects, string $type): void
26-
{
27-
$this->currentType = $type;
28-
29-
foreach ($defects as $i => $defect) {
30-
$this->printDefect($defect, $i);
31-
}
32-
}
3+
// phpcs:disable PSR1.Classes.ClassDeclaration.MultipleClasses
334

34-
protected function printDefectHeader(TestFailure $defect, int $count): void
35-
{
36-
}
37-
38-
protected function printDefectTrace(TestFailure $defect): void
39-
{
40-
$e = $defect->thrownException();
41-
42-
$errorLines = array_filter(
43-
explode("\n", (string)$e),
44-
function ($l) {
45-
return $l;
46-
}
47-
);
48-
49-
$error = end($errorLines);
50-
$lineIndex = strrpos($error, ":");
51-
$path = substr($error, 0, $lineIndex);
52-
$line = substr($error, $lineIndex + 1);
53-
54-
list($reflectedPath, $reflectedLine) = $this->getReflectionFromTest(
55-
$defect->getTestName()
56-
);
57-
58-
if($path !== $reflectedPath) {
59-
$path = $reflectedPath;
60-
$line = $reflectedLine;
61-
}
5+
namespace mheap\GithubActionsReporter;
626

63-
$message = explode("\n", $defect->getExceptionAsString());
64-
$message = implode('%0A', $message);
7+
use PHPUnit\Runner\Version;
8+
use PHPUnit_TextUI_ResultPrinter;
659

66-
$type = $this->getCurrentType();
67-
$file = "file={$this->relativePath($path)}";
68-
$line = "line={$line}";
69-
$this->write("::{$type} $file,$line::{$message}\n");
70-
}
10+
$low = version_compare(Version::series(), '8.0', '>=');
11+
$high = version_compare(Version::series(), '8.99.99', '<=');
7112

72-
protected function getCurrentType()
13+
if ($low && $high) {
14+
class Printer extends Printer8
7315
{
74-
if (in_array($this->currentType, ['error', 'failure'])) {
75-
return 'error';
76-
}
77-
78-
return 'warning';
7916
}
17+
}
8018

81-
protected function relativePath(string $path)
82-
{
83-
$relative = str_replace(getcwd() . DIRECTORY_SEPARATOR, '', $path);
84-
// Translate \ in to / for Windows
85-
$relative = str_replace('\\', '/', $relative);
86-
return $relative;
87-
}
19+
$low = version_compare(Version::series(), '9.0', '>=');
20+
$high = true; // version_compare(Version::series(),'8.99.99','<=');
8821

89-
protected function getReflectionFromTest(string $name)
22+
if ($low && $high) {
23+
class Printer extends Printer9
9024
{
91-
list($klass, $method) = explode('::', $name);
92-
$c = new \ReflectionClass($klass);
93-
$m = $c->getMethod($method);
94-
95-
return [$m->getFileName(), $m->getStartLine()];
9625
}
9726
}

src/Printer8.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace mheap\GithubActionsReporter;
4+
5+
use PHPUnit\TextUI\ResultPrinter;
6+
7+
class Printer8 extends ResultPrinter
8+
{
9+
use Trait8;
10+
}

src/Printer9.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace mheap\GithubActionsReporter;
4+
5+
use PHPUnit\TextUI\DefaultResultPrinter;
6+
7+
class Printer9 extends DefaultResultPrinter
8+
{
9+
use Trait8;
10+
}

src/Trait8.php

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
namespace mheap\GithubActionsReporter;
4+
5+
use PHPUnit\Framework\TestResult;
6+
use PHPUnit\Framework\TestFailure;
7+
8+
trait Trait8
9+
{
10+
protected $currentType = null;
11+
12+
protected function printHeader(TestResult $result): void
13+
{
14+
}
15+
16+
protected function writeProgress(string $progress): void
17+
{
18+
}
19+
20+
protected function printFooter(TestResult $result): void
21+
{
22+
}
23+
24+
protected function printDefects(array $defects, string $type): void
25+
{
26+
$this->currentType = $type;
27+
28+
foreach ($defects as $i => $defect) {
29+
$this->printDefect($defect, $i);
30+
}
31+
}
32+
33+
protected function printDefectHeader(TestFailure $defect, int $count): void
34+
{
35+
}
36+
37+
protected function printDefectTrace(TestFailure $defect): void
38+
{
39+
$e = $defect->thrownException();
40+
41+
$errorLines = array_filter(
42+
explode("\n", (string)$e),
43+
function ($l) {
44+
return $l;
45+
}
46+
);
47+
48+
$error = end($errorLines);
49+
$lineIndex = strrpos($error, ":");
50+
$path = substr($error, 0, $lineIndex);
51+
$line = substr($error, $lineIndex + 1);
52+
53+
list($reflectedPath, $reflectedLine) = $this->getReflectionFromTest(
54+
$defect->getTestName()
55+
);
56+
57+
if ($path !== $reflectedPath) {
58+
$path = $reflectedPath;
59+
$line = $reflectedLine;
60+
}
61+
62+
$message = explode("\n", $defect->getExceptionAsString());
63+
$message = implode('%0A', $message);
64+
65+
// Some messages might contain paths. Let's convert thost to relative paths too
66+
$message = $this->relativePath($message);
67+
68+
$message = preg_replace('/%0A$/', '', $message);
69+
70+
$type = $this->getCurrentType();
71+
$file = "file={$this->relativePath($path)}";
72+
$line = "line={$line}";
73+
$this->write("::{$type} $file,$line::{$message}\n");
74+
}
75+
76+
protected function getCurrentType()
77+
{
78+
if (in_array($this->currentType, ['error', 'failure'])) {
79+
return 'error';
80+
}
81+
82+
return 'warning';
83+
}
84+
85+
protected function relativePath(string $path)
86+
{
87+
$relative = str_replace(getcwd() . DIRECTORY_SEPARATOR, '', $path);
88+
// Translate \ in to / for Windows
89+
$relative = str_replace('\\', '/', $relative);
90+
return $relative;
91+
}
92+
93+
protected function getReflectionFromTest(string $name)
94+
{
95+
list($klass, $method) = explode('::', $name);
96+
$c = new \ReflectionClass($klass);
97+
$m = $c->getMethod($method);
98+
99+
return [$m->getFileName(), $m->getStartLine()];
100+
}
101+
}

test/states-test.phpt

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ require_once(dirname(dirname(__FILE__))).'/vendor/autoload.php';
1313
PHPUnit\TextUI\Command::main();
1414
?>
1515
--EXPECTF--
16-
PHPUnit 8.5.8 by Sebastian Bergmann and contributors.
16+
%%VERSION%%
1717

1818
::error file=test/_files/PrinterStatesTest.php,line=17::strpos() expects at least 2 parameters, 0 given
19-
::error file=test/_files/PrinterStatesTest.php,line=22::Call to undefined method PrinterStatesTest::isMissing()
19+
::error file=test/_files/PrinterStatesTest.php,line=22::Error: Call to undefined method PrinterStatesTest::isMissing()
2020
::warning file=test/_files/PrinterStatesTest.php,line=32::This is a test warning
2121
::error file=test/_files/PrinterStatesTest.php,line=12::Failed asserting that false is true.
2222
::warning file=test/_files/PrinterStatesTest.php,line=37::This is a risky test
23-
::warning file=test/_files/PrinterStatesTest.php,line=40::This test did not perform any assertions
23+
::warning file=test/_files/PrinterStatesTest.php,line=40::This test did not perform any assertions%0A%0Atest/_files/PrinterStatesTest.php:40

0 commit comments

Comments
 (0)