Skip to content

Commit 93107fd

Browse files
authored
Add PHPUnit 7 support (#20)
1 parent 5c2511a commit 93107fd

File tree

3 files changed

+112
-1
lines changed

3 files changed

+112
-1
lines changed

.github/workflows/push.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
matrix:
1111
os: [ubuntu-latest, windows-latest, macos-latest]
1212
php-versions: ["7.3", "7.4"]
13-
phpunit-version: ["8", "9"]
13+
phpunit-version: ["7", "8", "9"]
1414
name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.os }} (PHPUnit ${{ matrix.phpunit-version }})
1515
steps:
1616
- name: Checkout

src/Printer.php

+9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@
77
use PHPUnit\Runner\Version;
88
use PHPUnit_TextUI_ResultPrinter;
99

10+
$low = version_compare(Version::series(), '7.0', '>=');
11+
$high = version_compare(Version::series(), '7.99.99', '<=');
12+
13+
if ($low && $high) {
14+
class Printer extends Printer7
15+
{
16+
}
17+
}
18+
1019
$low = version_compare(Version::series(), '8.0', '>=');
1120
$high = version_compare(Version::series(), '8.99.99', '<=');
1221

src/Printer7.php

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
namespace mheap\GithubActionsReporter;
4+
5+
use PHPUnit\TextUI\ResultPrinter;
6+
use PHPUnit\Framework\TestFailure;
7+
use PHPUnit\Framework\TestResult;
8+
9+
class Printer7 extends ResultPrinter
10+
{
11+
protected $currentType = null;
12+
13+
protected function printHeader(): 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+
}
33+
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+
}
62+
63+
$message = explode("\n", $defect->getExceptionAsString());
64+
$message = implode('%0A', $message);
65+
66+
// Some messages might contain paths. Let's convert thost to relative paths too
67+
$message = $this->relativePath($message);
68+
69+
$message = preg_replace('/%0A$/', '', $message);
70+
71+
$type = $this->getCurrentType();
72+
$file = "file={$this->relativePath($path)}";
73+
$line = "line={$line}";
74+
$this->write("::{$type} $file,$line::{$message}\n");
75+
}
76+
77+
protected function getCurrentType()
78+
{
79+
if (in_array($this->currentType, ['error', 'failure'])) {
80+
return 'error';
81+
}
82+
83+
return 'warning';
84+
}
85+
86+
protected function relativePath(string $path)
87+
{
88+
$relative = str_replace(getcwd() . DIRECTORY_SEPARATOR, '', $path);
89+
// Translate \ in to / for Windows
90+
$relative = str_replace('\\', '/', $relative);
91+
return $relative;
92+
}
93+
94+
protected function getReflectionFromTest(string $name)
95+
{
96+
list($klass, $method) = explode('::', $name);
97+
$c = new \ReflectionClass($klass);
98+
$m = $c->getMethod($method);
99+
100+
return [$m->getFileName(), $m->getStartLine()];
101+
}
102+
}

0 commit comments

Comments
 (0)