Skip to content

Commit 91658a4

Browse files
Mitchell Macphersonmheap
Mitchell Macpherson
andauthored
PHPUnit 6 support + refactoring (#23)
Co-authored-by: Michael Heap <[email protected]>
1 parent eae84d4 commit 91658a4

13 files changed

+369
-204
lines changed

Diff for: .github/workflows/push.yml

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

1919
- name: Setup PHP
20-
uses: shivammathur/setup-php@v1
20+
uses: shivammathur/setup-php@v2
2121
with:
2222
php-version: ${{ matrix.php-versions }}
2323
extensions: mbstring

Diff for: composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
}
1111
],
1212
"autoload": {
13+
"files": ["src/Functions/helpers.php"],
1314
"psr-4": {
1415
"mheap\\GithubActionsReporter\\": "src"
1516
}

Diff for: phpunit.xml.dist

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
<testsuite name="Github Actions printer for PHPUnit Test Suite">
1515
<directory suffix=".phpt">./test/</directory>
1616
</testsuite>
17+
<testsuite name="Github Actions printer for PHPUnit Test Suite">
18+
<directory suffix=".php">./test/Unit/</directory>
19+
</testsuite>
1720
</testsuites>
1821
<filter>
1922
<whitelist>

Diff for: src/Functions/helpers.php

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
namespace mheap\GithubActionsReporter\Functions;
4+
5+
use mheap\GithubActionsReporter\Printer6;
6+
use mheap\GithubActionsReporter\Printer7;
7+
use mheap\GithubActionsReporter\Printer8;
8+
use mheap\GithubActionsReporter\Printer9;
9+
use PHPUnit\Framework\TestFailure;
10+
use ReflectionClass;
11+
12+
/**
13+
* @param $version
14+
*
15+
* @return string|null Fully Qualified Class Name, or null if no matching version
16+
*
17+
* @internal
18+
*/
19+
function determinePrinter($version)
20+
{
21+
$versionMatrix = [
22+
// greater than equals, lower than equals, printer FQCN
23+
['6.0', '6.99.99', Printer6::class],
24+
['7.0', '7.99.99', Printer7::class],
25+
['8.0', '8.99.99', Printer8::class],
26+
['9.0', true, Printer9::class],
27+
];
28+
29+
foreach ($versionMatrix as list($lowerVersion, $upperVersion, $class)) {
30+
if (
31+
version_compare($version, $lowerVersion, '>=') == true &&
32+
($upperVersion === true || version_compare($version, $upperVersion, '<=') == true)
33+
) {
34+
return $class;
35+
}
36+
}
37+
38+
return null;
39+
}
40+
41+
/**
42+
* @param TestFailure $defect
43+
* @param string $defectType
44+
*
45+
* @return string
46+
* @throws \ReflectionException
47+
* @internal
48+
*/
49+
function printDefectTrace($defect, $defectType)
50+
{
51+
$e = $defect->thrownException();
52+
53+
$errorLines = array_filter(
54+
explode("\n", (string)$e),
55+
static function ($l) {
56+
return $l;
57+
}
58+
);
59+
60+
$error = end($errorLines);
61+
$lineIndex = strrpos($error, ":");
62+
$path = substr($error, 0, $lineIndex);
63+
$line = substr($error, $lineIndex + 1);
64+
65+
list($reflectedPath, $reflectedLine) = getReflectionFromTest(
66+
$defect->getTestName()
67+
);
68+
69+
if ($path !== $reflectedPath) {
70+
$path = $reflectedPath;
71+
$line = $reflectedLine;
72+
}
73+
74+
$message = explode("\n", $defect->getExceptionAsString());
75+
$message = implode('%0A', $message);
76+
77+
// Some messages might contain paths. Let's convert thost to relative paths too
78+
$message = relativePath($message);
79+
$message = preg_replace('/%0A$/', '', $message);
80+
81+
$path = relativePath($path);
82+
$file = "file={$path}";
83+
$line = "line={$line}";
84+
85+
return "::{$defectType} $file,$line::{$message}\n";
86+
}
87+
88+
/**
89+
* @param string $path
90+
*
91+
* @return mixed
92+
* @internal
93+
*/
94+
function relativePath($path)
95+
{
96+
$relative = str_replace(getcwd() . DIRECTORY_SEPARATOR, '', $path);
97+
98+
// Translate \ in to / for Windows
99+
return str_replace('\\', '/', $relative);
100+
}
101+
102+
/**
103+
* @param string $name
104+
*
105+
* @return array
106+
* @throws \ReflectionException
107+
* @internal
108+
*/
109+
function getReflectionFromTest($name)
110+
{
111+
list($klass, $method) = explode('::', $name);
112+
113+
// Handle data providers
114+
$parts = explode(" ", $method, 2);
115+
if (count($parts) > 1) {
116+
$method = $parts[0];
117+
}
118+
119+
$c = new ReflectionClass($klass);
120+
$m = $c->getMethod($method);
121+
122+
return [$m->getFileName(), $m->getStartLine()];
123+
}

Diff for: src/Printer.php

+6-22
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,15 @@
55
namespace mheap\GithubActionsReporter;
66

77
use PHPUnit\Runner\Version;
8-
use PHPUnit_TextUI_ResultPrinter;
98

10-
$low = version_compare(Version::series(), '7.0', '>=');
11-
$high = version_compare(Version::series(), '7.99.99', '<=');
9+
use function mheap\GithubActionsReporter\Functions\determinePrinter;
1210

13-
if ($low && $high) {
14-
class Printer extends Printer7
15-
{
16-
}
17-
}
18-
19-
$low = version_compare(Version::series(), '8.0', '>=');
20-
$high = version_compare(Version::series(), '8.99.99', '<=');
11+
$class = determinePrinter(Version::series());
2112

22-
if ($low && $high) {
23-
class Printer extends Printer8
24-
{
25-
}
13+
if ($class === null) {
14+
throw new \RuntimeException('Unable to find supporting PHPUnit print for your version');
2615
}
2716

28-
$low = version_compare(Version::series(), '9.0', '>=');
29-
$high = true; // version_compare(Version::series(),'8.99.99','<=');
30-
31-
if ($low && $high) {
32-
class Printer extends Printer9
33-
{
34-
}
17+
if (class_alias($class, '\mheap\GithubActionsReporter\Printer') === false) {
18+
throw new \RuntimeException('Unable to setup autoloading alias for printer');
3519
}

Diff for: src/Printer6.php

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
use function mheap\GithubActionsReporter\Functions\printDefectTrace;
10+
11+
class Printer6 extends ResultPrinter
12+
{
13+
/**
14+
* @var null|string
15+
*/
16+
private $currentType;
17+
18+
protected function printHeader(): void
19+
{
20+
}
21+
22+
protected function writeProgress($progress): void
23+
{
24+
}
25+
26+
protected function printFooter(TestResult $result): void
27+
{
28+
}
29+
30+
protected function printDefects(array $defects, $type): void
31+
{
32+
$this->currentType = (in_array($type, ['error', 'failure']) === true) ? 'error' : 'warning';
33+
34+
foreach ($defects as $i => $defect) {
35+
$this->printDefect($defect, $i);
36+
}
37+
}
38+
39+
protected function printDefectHeader(TestFailure $defect, $count): void
40+
{
41+
}
42+
43+
/**
44+
* @throws \ReflectionException
45+
*/
46+
protected function printDefectTrace(TestFailure $defect): void
47+
{
48+
$this->write(printDefectTrace($defect, $this->currentType));
49+
}
50+
}

Diff for: src/Printer7.php

+8-70
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@
66
use PHPUnit\Framework\TestFailure;
77
use PHPUnit\Framework\TestResult;
88

9+
use function mheap\GithubActionsReporter\Functions\printDefectTrace;
10+
911
class Printer7 extends ResultPrinter
1012
{
11-
protected $currentType = null;
13+
/**
14+
* @var null|string
15+
*/
16+
private $currentType;
1217

1318
protected function printHeader(): void
1419
{
@@ -24,7 +29,7 @@ protected function printFooter(TestResult $result): void
2429

2530
protected function printDefects(array $defects, string $type): void
2631
{
27-
$this->currentType = $type;
32+
$this->currentType = (in_array($type, ['error', 'failure']) === true) ? 'error' : 'warning';
2833

2934
foreach ($defects as $i => $defect) {
3035
$this->printDefect($defect, $i);
@@ -37,73 +42,6 @@ protected function printDefectHeader(TestFailure $defect, int $count): void
3742

3843
protected function printDefectTrace(TestFailure $defect): void
3944
{
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-
98-
// Handle data providers
99-
$parts = explode(" ", $method, 2);
100-
if (count($parts) > 1) {
101-
$method = $parts[0];
102-
}
103-
104-
$c = new \ReflectionClass($klass);
105-
$m = $c->getMethod($method);
106-
107-
return [$m->getFileName(), $m->getStartLine()];
45+
$this->write(printDefectTrace($defect, $this->currentType));
10846
}
10947
}

Diff for: src/Printer8.php

+40-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,48 @@
22

33
namespace mheap\GithubActionsReporter;
44

5+
use PHPUnit\Framework\TestFailure;
6+
use PHPUnit\Framework\TestResult;
57
use PHPUnit\TextUI\ResultPrinter;
68

9+
use function mheap\GithubActionsReporter\Functions\getCurrentType;
10+
use function mheap\GithubActionsReporter\Functions\printDefects;
11+
use function mheap\GithubActionsReporter\Functions\printDefectTrace;
12+
713
class Printer8 extends ResultPrinter
814
{
9-
use Trait8;
15+
/**
16+
* @var null|string
17+
*/
18+
private $currentType;
19+
20+
protected function printHeader(TestResult $result): void
21+
{
22+
}
23+
24+
protected function writeProgress(string $progress): void
25+
{
26+
}
27+
28+
protected function printFooter(TestResult $result): void
29+
{
30+
}
31+
32+
protected function printDefects(array $defects, string $type): void
33+
{
34+
$this->currentType = (in_array($type, ['error', 'failure']) === true) ? 'error' : 'warning';
35+
36+
foreach ($defects as $i => $defect) {
37+
$this->printDefect($defect, $i);
38+
}
39+
}
40+
41+
protected function printDefectHeader(TestFailure $defect, int $count): void
42+
{
43+
}
44+
45+
protected function printDefectTrace(TestFailure $defect): void
46+
{
47+
$this->write(printDefectTrace($defect, $this->currentType));
48+
}
1049
}

0 commit comments

Comments
 (0)