From 93107fd9eec7e5bffd122be8f1e6988cc525ac59 Mon Sep 17 00:00:00 2001 From: Michael Heap Date: Sun, 13 Sep 2020 21:37:37 +0100 Subject: [PATCH] Add PHPUnit 7 support (#20) --- .github/workflows/push.yml | 2 +- src/Printer.php | 9 ++++ src/Printer7.php | 102 +++++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 src/Printer7.php diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index 877f9f5..b5482b5 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -10,7 +10,7 @@ jobs: matrix: os: [ubuntu-latest, windows-latest, macos-latest] php-versions: ["7.3", "7.4"] - phpunit-version: ["8", "9"] + phpunit-version: ["7", "8", "9"] name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.os }} (PHPUnit ${{ matrix.phpunit-version }}) steps: - name: Checkout diff --git a/src/Printer.php b/src/Printer.php index 8d5c3a6..28774a5 100644 --- a/src/Printer.php +++ b/src/Printer.php @@ -7,6 +7,15 @@ use PHPUnit\Runner\Version; use PHPUnit_TextUI_ResultPrinter; +$low = version_compare(Version::series(), '7.0', '>='); +$high = version_compare(Version::series(), '7.99.99', '<='); + +if ($low && $high) { + class Printer extends Printer7 + { + } +} + $low = version_compare(Version::series(), '8.0', '>='); $high = version_compare(Version::series(), '8.99.99', '<='); diff --git a/src/Printer7.php b/src/Printer7.php new file mode 100644 index 0000000..4a10bc1 --- /dev/null +++ b/src/Printer7.php @@ -0,0 +1,102 @@ +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); + $c = new \ReflectionClass($klass); + $m = $c->getMethod($method); + + return [$m->getFileName(), $m->getStartLine()]; + } +}