Skip to content

Commit 8fdacfd

Browse files
Centralised repeated code
Simplified 'Printer' class designator Added PHPUnit 6 support
1 parent eae84d4 commit 8fdacfd

File tree

10 files changed

+266
-209
lines changed

10 files changed

+266
-209
lines changed

.github/workflows/push.yml

Lines changed: 1 addition & 1 deletion
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: ["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

composer.json

Lines changed: 1 addition & 0 deletions
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
}

src/Functions/helpers.php

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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 (version_compare($version, $lowerVersion, '>=') == true &&
31+
($upperVersion === true || version_compare($version, $upperVersion, '<=') == true)
32+
) {
33+
return $class;
34+
}
35+
}
36+
37+
return null;
38+
}
39+
40+
/**
41+
* @param TestFailure $defect
42+
*
43+
* @return string
44+
* @throws \ReflectionException
45+
* @internal
46+
*/
47+
function printDefectTrace($defect)
48+
{
49+
$e = $defect->thrownException();
50+
51+
$errorLines = array_filter(
52+
explode("\n", (string)$e),
53+
static function ($l) {
54+
return $l;
55+
}
56+
);
57+
58+
$error = end($errorLines);
59+
$lineIndex = strrpos($error, ":");
60+
$path = substr($error, 0, $lineIndex);
61+
$line = substr($error, $lineIndex + 1);
62+
63+
list($reflectedPath, $reflectedLine) = getReflectionFromTest(
64+
$defect->getTestName()
65+
);
66+
67+
if ($path !== $reflectedPath) {
68+
$path = $reflectedPath;
69+
$line = $reflectedLine;
70+
}
71+
72+
$message = explode("\n", $defect->getExceptionAsString());
73+
$message = implode('%0A', $message);
74+
75+
// Some messages might contain paths. Let's convert thost to relative paths too
76+
$message = relativePath($message);
77+
$message = preg_replace('/%0A$/', '', $message);
78+
79+
$type = $defect->isFailure() ? 'error' : 'warning';
80+
$path = relativePath($path);
81+
$file = "file={$path}";
82+
$line = "line={$line}";
83+
84+
return "::{$type} $file,$line::{$message}\n";
85+
}
86+
87+
/**
88+
* @param string $path
89+
*
90+
* @return mixed
91+
* @internal
92+
*/
93+
function relativePath($path)
94+
{
95+
$relative = str_replace(getcwd() . DIRECTORY_SEPARATOR, '', $path);
96+
97+
// Translate \ in to / for Windows
98+
return str_replace('\\', '/', $relative);
99+
}
100+
101+
/**
102+
* @param string $name
103+
*
104+
* @return array
105+
* @throws \ReflectionException
106+
* @internal
107+
*/
108+
function getReflectionFromTest($name)
109+
{
110+
list($klass, $method) = explode('::', $name);
111+
112+
// Handle data providers
113+
$parts = explode(" ", $method, 2);
114+
if (count($parts) > 1) {
115+
$method = $parts[0];
116+
}
117+
118+
$c = new ReflectionClass($klass);
119+
$m = $c->getMethod($method);
120+
121+
return [$m->getFileName(), $m->getStartLine()];
122+
}

src/Printer.php

Lines changed: 6 additions & 22 deletions
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
}

src/Printer6.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
protected function printHeader(): void
14+
{
15+
}
16+
17+
protected function writeProgress($progress): void
18+
{
19+
}
20+
21+
protected function printFooter(TestResult $result): void
22+
{
23+
}
24+
25+
protected function printDefects(array $defects, $type): void
26+
{
27+
}
28+
29+
protected function printDefectHeader(TestFailure $defect, $count): void
30+
{
31+
}
32+
33+
protected function printDefectTrace(TestFailure $defect): void
34+
{
35+
$this->write(printDefectTrace($defect));
36+
}
37+
}

src/Printer7.php

Lines changed: 3 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
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
{
1113
protected $currentType = null;
@@ -24,11 +26,6 @@ protected function printFooter(TestResult $result): void
2426

2527
protected function printDefects(array $defects, string $type): void
2628
{
27-
$this->currentType = $type;
28-
29-
foreach ($defects as $i => $defect) {
30-
$this->printDefect($defect, $i);
31-
}
3229
}
3330

3431
protected function printDefectHeader(TestFailure $defect, int $count): void
@@ -37,73 +34,6 @@ protected function printDefectHeader(TestFailure $defect, int $count): void
3734

3835
protected function printDefectTrace(TestFailure $defect): void
3936
{
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()];
37+
$this->write(printDefectTrace($defect));
10838
}
10939
}

src/Printer8.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,39 @@
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+
712
class Printer8 extends ResultPrinter
813
{
9-
use Trait8;
14+
protected $currentType = null;
15+
16+
protected function printHeader(TestResult $result): void
17+
{
18+
}
19+
20+
protected function writeProgress(string $progress): void
21+
{
22+
}
23+
24+
protected function printFooter(TestResult $result): void
25+
{
26+
}
27+
28+
protected function printDefects(array $defects, string $type): void
29+
{
30+
$this->write(printDefects($defects, $type));
31+
}
32+
33+
protected function printDefectHeader(TestFailure $defect, int $count): void
34+
{
35+
}
36+
37+
protected function printDefectTrace(TestFailure $defect): void
38+
{
39+
}
1040
}

src/Printer9.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
namespace mheap\GithubActionsReporter;
44

5-
use PHPUnit\TextUI\DefaultResultPrinter;
6-
7-
class Printer9 extends DefaultResultPrinter
5+
class Printer9 extends Printer8
86
{
9-
use Trait8;
107
}

0 commit comments

Comments
 (0)