From 9ce4a71bc30d6b6c2251b985e11cdf64af6ee1f0 Mon Sep 17 00:00:00 2001 From: Michael Heap Date: Fri, 3 Jul 2020 22:03:58 +0100 Subject: [PATCH] Add support for PHPUnit 9 --- .gitattributes | 1 + .github/workflows/push.yml | 36 +++++++++++-- .gitignore | 3 +- composer.json | 2 +- src/Printer.php | 99 +++++------------------------------- src/Printer8.php | 10 ++++ src/Printer9.php | 10 ++++ src/Trait8.php | 101 +++++++++++++++++++++++++++++++++++++ test/states-test.phpt | 6 +-- 9 files changed, 175 insertions(+), 93 deletions(-) create mode 100644 src/Printer8.php create mode 100644 src/Printer9.php create mode 100644 src/Trait8.php diff --git a/.gitattributes b/.gitattributes index fcadb2c..e8733ac 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1,2 @@ * text eol=lf +test/ export-ignore diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index 51f4498..bfc52ab 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -2,13 +2,16 @@ name: CI on: push jobs: run: - runs-on: ${{ matrix.operating-system }} + env: + ACTIONS_STEP_DEBUG: true + runs-on: ${{ matrix.os }} strategy: fail-fast: false matrix: - operating-system: [ubuntu-latest, windows-latest, macos-latest] + os: [ubuntu-latest, windows-latest, macos-latest] php-versions: ['7.3', '7.4'] - name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }} + phpunit-version: ['8', '9'] + name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.os }} (PHPUnit ${{ matrix.phpunit-version }}) steps: - name: Checkout uses: actions/checkout@v2 @@ -20,9 +23,36 @@ jobs: extensions: mbstring coverage: xdebug + - name: Set PHPUnit Version (Non-Windows) + run: | + sed -i.bak 's#"phpunit/phpunit": "^9"#"phpunit/phpunit": "^${{ matrix.phpunit-version }}"#' composer.json + if: "!startsWith(matrix.os, 'windows')" + + - name: Set PHPUnit Version (Windows) + run: | + $content = Get-Content -Path 'composer.json' + $newContent = $content -replace '"phpunit/phpunit": "\^9"', '"phpunit/phpunit": "^${{ matrix.phpunit-version }}"' + $newContent | Set-Content -Path 'composer.json' + if: "startsWith(matrix.os, 'windows')" + - name: Composer dependencies run: composer install --no-ansi --no-interaction --no-scripts --no-suggest --no-progress --prefer-dist + - name: Configure tests for the current PHPUnit version (Non-Windows) + run: | + V=$(./vendor/bin/phpunit --version) + sed -i.bak "s/%%VERSION%%/$V/" test/states-test.phpt + if: "!startsWith(matrix.os, 'windows')" + + - name: Configure tests for the current PHPUnit version (Windows) + id: phpunit-windows + run: | + $V = (.\vendor\bin\phpunit --version | Out-String).trim() + $content = Get-Content -Path 'test/states-test.phpt' + $newContent = $content -replace '%%VERSION%%', $V + $newContent | Set-Content -Path 'test/states-test.phpt' + if: "startsWith(matrix.os, 'windows')" + - name: Run phpunit run: ./vendor/bin/phpunit diff --git a/.gitignore b/.gitignore index dfd6caa..612c601 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /vendor -composer.lock \ No newline at end of file +composer.lock +.phpunit.result.cache diff --git a/composer.json b/composer.json index 61767be..1f4c881 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ }, "require": {}, "require-dev": { - "phpunit/phpunit": "^8", + "phpunit/phpunit": "^9", "squizlabs/php_codesniffer": "3.*" } } diff --git a/src/Printer.php b/src/Printer.php index e40655b..8d5c3a6 100644 --- a/src/Printer.php +++ b/src/Printer.php @@ -1,97 +1,26 @@ -currentType = $type; - - foreach ($defects as $i => $defect) { - $this->printDefect($defect, $i); - } - } +// phpcs:disable PSR1.Classes.ClassDeclaration.MultipleClasses - 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; - } +namespace mheap\GithubActionsReporter; - $message = explode("\n", $defect->getExceptionAsString()); - $message = implode('%0A', $message); +use PHPUnit\Runner\Version; +use PHPUnit_TextUI_ResultPrinter; - $type = $this->getCurrentType(); - $file = "file={$this->relativePath($path)}"; - $line = "line={$line}"; - $this->write("::{$type} $file,$line::{$message}\n"); - } +$low = version_compare(Version::series(), '8.0', '>='); +$high = version_compare(Version::series(), '8.99.99', '<='); - protected function getCurrentType() +if ($low && $high) { + class Printer extends Printer8 { - 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; - } +$low = version_compare(Version::series(), '9.0', '>='); +$high = true; // version_compare(Version::series(),'8.99.99','<='); - protected function getReflectionFromTest(string $name) +if ($low && $high) { + class Printer extends Printer9 { - list($klass, $method) = explode('::', $name); - $c = new \ReflectionClass($klass); - $m = $c->getMethod($method); - - return [$m->getFileName(), $m->getStartLine()]; } } diff --git a/src/Printer8.php b/src/Printer8.php new file mode 100644 index 0000000..f5b2336 --- /dev/null +++ b/src/Printer8.php @@ -0,0 +1,10 @@ +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()]; + } +} diff --git a/test/states-test.phpt b/test/states-test.phpt index b7d2eba..5c9af3b 100644 --- a/test/states-test.phpt +++ b/test/states-test.phpt @@ -13,11 +13,11 @@ require_once(dirname(dirname(__FILE__))).'/vendor/autoload.php'; PHPUnit\TextUI\Command::main(); ?> --EXPECTF-- -PHPUnit 8.5.8 by Sebastian Bergmann and contributors. +%%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::Call to undefined method PrinterStatesTest::isMissing() +::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. ::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 +::warning file=test/_files/PrinterStatesTest.php,line=40::This test did not perform any assertions%0A%0Atest/_files/PrinterStatesTest.php:40