diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml
index b5482b5..c0adb06 100644
--- a/.github/workflows/push.yml
+++ b/.github/workflows/push.yml
@@ -10,14 +10,14 @@ jobs:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
php-versions: ["7.3", "7.4"]
- phpunit-version: ["7", "8", "9"]
+ phpunit-version: ["6", "7", "8", "9"]
name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.os }} (PHPUnit ${{ matrix.phpunit-version }})
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup PHP
- uses: shivammathur/setup-php@v1
+ uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
extensions: mbstring
diff --git a/composer.json b/composer.json
index 1f4c881..ba9e60d 100644
--- a/composer.json
+++ b/composer.json
@@ -10,6 +10,7 @@
}
],
"autoload": {
+ "files": ["src/Functions/helpers.php"],
"psr-4": {
"mheap\\GithubActionsReporter\\": "src"
}
diff --git a/phpunit.xml.dist b/phpunit.xml.dist
index 2b2002c..c31b621 100644
--- a/phpunit.xml.dist
+++ b/phpunit.xml.dist
@@ -14,6 +14,9 @@
./test/
+
+ ./test/Unit/
+
diff --git a/src/Functions/helpers.php b/src/Functions/helpers.php
new file mode 100644
index 0000000..4167bc5
--- /dev/null
+++ b/src/Functions/helpers.php
@@ -0,0 +1,123 @@
+=') == true &&
+ ($upperVersion === true || version_compare($version, $upperVersion, '<=') == true)
+ ) {
+ return $class;
+ }
+ }
+
+ return null;
+}
+
+/**
+ * @param TestFailure $defect
+ * @param string $defectType
+ *
+ * @return string
+ * @throws \ReflectionException
+ * @internal
+ */
+function printDefectTrace($defect, $defectType)
+{
+ $e = $defect->thrownException();
+
+ $errorLines = array_filter(
+ explode("\n", (string)$e),
+ static function ($l) {
+ return $l;
+ }
+ );
+
+ $error = end($errorLines);
+ $lineIndex = strrpos($error, ":");
+ $path = substr($error, 0, $lineIndex);
+ $line = substr($error, $lineIndex + 1);
+
+ list($reflectedPath, $reflectedLine) = getReflectionFromTest(
+ $defect->getTestName()
+ );
+
+ if ($path !== $reflectedPath) {
+ $path = $reflectedPath;
+ $line = $reflectedLine;
+ }
+
+ $message = explode("\n", $defect->getExceptionAsString());
+ $message = implode('%0A', $message);
+
+ // Some messages might contain paths. Let's convert thost to relative paths too
+ $message = relativePath($message);
+ $message = preg_replace('/%0A$/', '', $message);
+
+ $path = relativePath($path);
+ $file = "file={$path}";
+ $line = "line={$line}";
+
+ return "::{$defectType} $file,$line::{$message}\n";
+}
+
+/**
+ * @param string $path
+ *
+ * @return mixed
+ * @internal
+ */
+function relativePath($path)
+{
+ $relative = str_replace(getcwd() . DIRECTORY_SEPARATOR, '', $path);
+
+ // Translate \ in to / for Windows
+ return str_replace('\\', '/', $relative);
+}
+
+/**
+ * @param string $name
+ *
+ * @return array
+ * @throws \ReflectionException
+ * @internal
+ */
+function getReflectionFromTest($name)
+{
+ list($klass, $method) = explode('::', $name);
+
+ // Handle data providers
+ $parts = explode(" ", $method, 2);
+ if (count($parts) > 1) {
+ $method = $parts[0];
+ }
+
+ $c = new ReflectionClass($klass);
+ $m = $c->getMethod($method);
+
+ return [$m->getFileName(), $m->getStartLine()];
+}
diff --git a/src/Printer.php b/src/Printer.php
index 28774a5..14a3057 100644
--- a/src/Printer.php
+++ b/src/Printer.php
@@ -5,31 +5,15 @@
namespace mheap\GithubActionsReporter;
use PHPUnit\Runner\Version;
-use PHPUnit_TextUI_ResultPrinter;
-$low = version_compare(Version::series(), '7.0', '>=');
-$high = version_compare(Version::series(), '7.99.99', '<=');
+use function mheap\GithubActionsReporter\Functions\determinePrinter;
-if ($low && $high) {
- class Printer extends Printer7
- {
- }
-}
-
-$low = version_compare(Version::series(), '8.0', '>=');
-$high = version_compare(Version::series(), '8.99.99', '<=');
+$class = determinePrinter(Version::series());
-if ($low && $high) {
- class Printer extends Printer8
- {
- }
+if ($class === null) {
+ throw new \RuntimeException('Unable to find supporting PHPUnit print for your version');
}
-$low = version_compare(Version::series(), '9.0', '>=');
-$high = true; // version_compare(Version::series(),'8.99.99','<=');
-
-if ($low && $high) {
- class Printer extends Printer9
- {
- }
+if (class_alias($class, '\mheap\GithubActionsReporter\Printer') === false) {
+ throw new \RuntimeException('Unable to setup autoloading alias for printer');
}
diff --git a/src/Printer6.php b/src/Printer6.php
new file mode 100644
index 0000000..e442f2d
--- /dev/null
+++ b/src/Printer6.php
@@ -0,0 +1,50 @@
+currentType = (in_array($type, ['error', 'failure']) === true) ? 'error' : 'warning';
+
+ foreach ($defects as $i => $defect) {
+ $this->printDefect($defect, $i);
+ }
+ }
+
+ protected function printDefectHeader(TestFailure $defect, $count): void
+ {
+ }
+
+ /**
+ * @throws \ReflectionException
+ */
+ protected function printDefectTrace(TestFailure $defect): void
+ {
+ $this->write(printDefectTrace($defect, $this->currentType));
+ }
+}
diff --git a/src/Printer7.php b/src/Printer7.php
index 6ad7a21..ee9f9c5 100644
--- a/src/Printer7.php
+++ b/src/Printer7.php
@@ -6,9 +6,14 @@
use PHPUnit\Framework\TestFailure;
use PHPUnit\Framework\TestResult;
+use function mheap\GithubActionsReporter\Functions\printDefectTrace;
+
class Printer7 extends ResultPrinter
{
- protected $currentType = null;
+ /**
+ * @var null|string
+ */
+ private $currentType;
protected function printHeader(): void
{
@@ -24,7 +29,7 @@ protected function printFooter(TestResult $result): void
protected function printDefects(array $defects, string $type): void
{
- $this->currentType = $type;
+ $this->currentType = (in_array($type, ['error', 'failure']) === true) ? 'error' : 'warning';
foreach ($defects as $i => $defect) {
$this->printDefect($defect, $i);
@@ -37,73 +42,6 @@ protected function printDefectHeader(TestFailure $defect, int $count): void
protected function printDefectTrace(TestFailure $defect): void
{
- $e = $defect->thrownException();
-
- $errorLines = array_filter(
- explode("\n", (string)$e),
- function ($l) {
- return $l;
- }
- );
-
- $error = end($errorLines);
- $lineIndex = strrpos($error, ":");
- $path = substr($error, 0, $lineIndex);
- $line = substr($error, $lineIndex + 1);
-
- list($reflectedPath, $reflectedLine) = $this->getReflectionFromTest(
- $defect->getTestName()
- );
-
- if ($path !== $reflectedPath) {
- $path = $reflectedPath;
- $line = $reflectedLine;
- }
-
- $message = explode("\n", $defect->getExceptionAsString());
- $message = implode('%0A', $message);
-
- // Some messages might contain paths. Let's convert thost to relative paths too
- $message = $this->relativePath($message);
-
- $message = preg_replace('/%0A$/', '', $message);
-
- $type = $this->getCurrentType();
- $file = "file={$this->relativePath($path)}";
- $line = "line={$line}";
- $this->write("::{$type} $file,$line::{$message}\n");
- }
-
- protected function getCurrentType()
- {
- if (in_array($this->currentType, ['error', 'failure'])) {
- return 'error';
- }
-
- return 'warning';
- }
-
- protected function relativePath(string $path)
- {
- $relative = str_replace(getcwd() . DIRECTORY_SEPARATOR, '', $path);
- // Translate \ in to / for Windows
- $relative = str_replace('\\', '/', $relative);
- return $relative;
- }
-
- protected function getReflectionFromTest(string $name)
- {
- list($klass, $method) = explode('::', $name);
-
- // Handle data providers
- $parts = explode(" ", $method, 2);
- if (count($parts) > 1) {
- $method = $parts[0];
- }
-
- $c = new \ReflectionClass($klass);
- $m = $c->getMethod($method);
-
- return [$m->getFileName(), $m->getStartLine()];
+ $this->write(printDefectTrace($defect, $this->currentType));
}
}
diff --git a/src/Printer8.php b/src/Printer8.php
index f5b2336..ed51138 100644
--- a/src/Printer8.php
+++ b/src/Printer8.php
@@ -2,9 +2,48 @@
namespace mheap\GithubActionsReporter;
+use PHPUnit\Framework\TestFailure;
+use PHPUnit\Framework\TestResult;
use PHPUnit\TextUI\ResultPrinter;
+use function mheap\GithubActionsReporter\Functions\getCurrentType;
+use function mheap\GithubActionsReporter\Functions\printDefects;
+use function mheap\GithubActionsReporter\Functions\printDefectTrace;
+
class Printer8 extends ResultPrinter
{
- use Trait8;
+ /**
+ * @var null|string
+ */
+ private $currentType;
+
+ protected function printHeader(TestResult $result): void
+ {
+ }
+
+ protected function writeProgress(string $progress): void
+ {
+ }
+
+ protected function printFooter(TestResult $result): void
+ {
+ }
+
+ protected function printDefects(array $defects, string $type): void
+ {
+ $this->currentType = (in_array($type, ['error', 'failure']) === true) ? 'error' : 'warning';
+
+ foreach ($defects as $i => $defect) {
+ $this->printDefect($defect, $i);
+ }
+ }
+
+ protected function printDefectHeader(TestFailure $defect, int $count): void
+ {
+ }
+
+ protected function printDefectTrace(TestFailure $defect): void
+ {
+ $this->write(printDefectTrace($defect, $this->currentType));
+ }
}
diff --git a/src/Printer9.php b/src/Printer9.php
index 6b5cc7e..89c31bc 100644
--- a/src/Printer9.php
+++ b/src/Printer9.php
@@ -2,9 +2,46 @@
namespace mheap\GithubActionsReporter;
+use PHPUnit\Framework\TestFailure;
+use PHPUnit\Framework\TestResult;
use PHPUnit\TextUI\DefaultResultPrinter;
+use function mheap\GithubActionsReporter\Functions\printDefectTrace;
+
class Printer9 extends DefaultResultPrinter
{
- use Trait8;
+ /**
+ * @var null|string
+ */
+ private $currentType;
+
+ protected function printHeader(TestResult $result): void
+ {
+ }
+
+ protected function writeProgress(string $progress): void
+ {
+ }
+
+ protected function printFooter(TestResult $result): void
+ {
+ }
+
+ protected function printDefects(array $defects, string $type): void
+ {
+ $this->currentType = (in_array($type, ['error', 'failure']) === true) ? 'error' : 'warning';
+
+ foreach ($defects as $i => $defect) {
+ $this->printDefect($defect, $i);
+ }
+ }
+
+ protected function printDefectHeader(TestFailure $defect, int $count): void
+ {
+ }
+
+ protected function printDefectTrace(TestFailure $defect): void
+ {
+ $this->write(printDefectTrace($defect, $this->currentType));
+ }
}
diff --git a/src/Trait8.php b/src/Trait8.php
deleted file mode 100644
index 0ef2a2f..0000000
--- a/src/Trait8.php
+++ /dev/null
@@ -1,108 +0,0 @@
-currentType = $type;
-
- foreach ($defects as $i => $defect) {
- $this->printDefect($defect, $i);
- }
- }
-
- protected function printDefectHeader(TestFailure $defect, int $count): void
- {
- }
-
- protected function printDefectTrace(TestFailure $defect): void
- {
- $e = $defect->thrownException();
-
- $errorLines = array_filter(
- explode("\n", (string)$e),
- function ($l) {
- return $l;
- }
- );
-
- $error = end($errorLines);
- $lineIndex = strrpos($error, ":");
- $path = substr($error, 0, $lineIndex);
- $line = substr($error, $lineIndex + 1);
-
- list($reflectedPath, $reflectedLine) = $this->getReflectionFromTest(
- $defect->getTestName()
- );
-
- if ($path !== $reflectedPath) {
- $path = $reflectedPath;
- $line = $reflectedLine;
- }
-
- $message = explode("\n", $defect->getExceptionAsString());
- $message = implode('%0A', $message);
-
- // Some messages might contain paths. Let's convert thost to relative paths too
- $message = $this->relativePath($message);
-
- $message = preg_replace('/%0A$/', '', $message);
-
- $type = $this->getCurrentType();
- $file = "file={$this->relativePath($path)}";
- $line = "line={$line}";
- $this->write("::{$type} $file,$line::{$message}\n");
- }
-
- protected function getCurrentType()
- {
- if (in_array($this->currentType, ['error', 'failure'])) {
- return 'error';
- }
-
- return 'warning';
- }
-
- protected function relativePath(string $path)
- {
- $relative = str_replace(getcwd() . DIRECTORY_SEPARATOR, '', $path);
- // Translate \ in to / for Windows
- $relative = str_replace('\\', '/', $relative);
- return $relative;
- }
-
- protected function getReflectionFromTest(string $name)
- {
- list($klass, $method) = explode('::', $name);
-
- // Handle data providers
- $parts = explode(" ", $method, 2);
- if (count($parts) > 1) {
- $method = $parts[0];
- }
-
- $c = new \ReflectionClass($klass);
- $m = $c->getMethod($method);
-
- return [$m->getFileName(), $m->getStartLine()];
- }
-}
diff --git a/test/Unit/HelperFunctionTest.php b/test/Unit/HelperFunctionTest.php
new file mode 100644
index 0000000..ebb9bfb
--- /dev/null
+++ b/test/Unit/HelperFunctionTest.php
@@ -0,0 +1,64 @@
+ [
+ 'version' => 'aaa',
+ 'expected' => null
+ ];
+
+ yield 'minor version 9.4' => [
+ 'version' => '9.4',
+ 'expected' => Printer9::class
+ ];
+
+ yield 'major version 9' => [
+ 'version' => '9.0',
+ 'expected' => Printer9::class
+ ];
+
+ yield 'minor version 9.1' => [
+ 'version' => '9.1',
+ 'expected' => Printer9::class
+ ];
+
+ yield 'minor version 8.2' => [
+ 'version' => '8.2',
+ 'expected' => Printer8::class
+ ];
+
+ yield 'patch version 7.0.1' => [
+ 'version' => '7.0.1',
+ 'expected' => Printer7::class
+ ];
+
+ yield 'minor version 6.5' => [
+ 'version' => '6.5',
+ 'expected' => Printer6::class
+ ];
+
+ yield 'minor version 5' => [
+ 'version' => '5.0',
+ 'expected' => null
+ ];
+ }
+ /**
+ * @dataProvider getInputsForVersionSelection()
+ */
+ public function testVersionSelector($version, $expected): void
+ {
+ $result = determinePrinter($version);
+
+ self::assertSame($expected, $result);
+ }
+}
diff --git a/test/states-test-phpunit6.phpt b/test/states-test-phpunit6.phpt
new file mode 100644
index 0000000..57b321e
--- /dev/null
+++ b/test/states-test-phpunit6.phpt
@@ -0,0 +1,29 @@
+--TEST--
+phpunit -c tests/_files/phpunit.xml tests/_files/PrinterStatesTest.php
+--SKIPIF--
+=') === true) echo 'skip';
+?>
+--FILE--
+
+--EXPECTF--
+%%VERSION%%
+
+::error file=test/_files/PrinterStatesTest.php,line=17::strpos() expects at least 2 parameters, 0 given
+::error file=test/_files/PrinterStatesTest.php,line=22::Error: Call to undefined method PrinterStatesTest::isMissing()
+::warning file=test/_files/PrinterStatesTest.php,line=32::This is a test warning
+::error file=test/_files/PrinterStatesTest.php,line=12::Failed asserting that false is true.
+::error file=test/_files/PrinterStatesTest.php,line=54::Failed asserting that false is true.
+::warning file=test/_files/PrinterStatesTest.php,line=37::This is a risky test
+::warning file=test/_files/PrinterStatesTest.php,line=40::This test did not perform any assertions
diff --git a/test/states-test.phpt b/test/states-test.phpt
index acf678e..329b543 100644
--- a/test/states-test.phpt
+++ b/test/states-test.phpt
@@ -1,5 +1,10 @@
--TEST--
phpunit -c tests/_files/phpunit.xml tests/_files/PrinterStatesTest.php
+--SKIPIF--
+=') === false) echo 'skip';
+?>
--FILE--