From 948ae069c83f6efff97d50d66d306e99baa1250e Mon Sep 17 00:00:00 2001 From: Michael Heap Date: Fri, 3 Jul 2020 21:26:18 +0100 Subject: [PATCH 01/14] Make tests pass again --- src/Printer.php | 2 +- test/states-test.phpt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Printer.php b/src/Printer.php index ff4e06a..8bbb551 100644 --- a/src/Printer.php +++ b/src/Printer.php @@ -10,7 +10,7 @@ class Printer extends ResultPrinter { protected $currentType = null; - protected function printHeader(): void + protected function printHeader(TestResult $result): void { } diff --git a/test/states-test.phpt b/test/states-test.phpt index a639ffd..b7d2eba 100644 --- a/test/states-test.phpt +++ b/test/states-test.phpt @@ -13,7 +13,7 @@ require_once(dirname(dirname(__FILE__))).'/vendor/autoload.php'; PHPUnit\TextUI\Command::main(); ?> --EXPECTF-- -PHPUnit 8.5.2 by Sebastian Bergmann and contributors. +PHPUnit 8.5.8 by Sebastian Bergmann and contributors. ::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() From 5238cde4e5a027e9bd219ee5dcc16cadb24b82c6 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Fri, 27 Mar 2020 21:43:34 -0700 Subject: [PATCH 02/14] Enable multiline messages in annotations Uses '%0A' for newline. --- src/Printer.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Printer.php b/src/Printer.php index 8bbb551..20c105c 100644 --- a/src/Printer.php +++ b/src/Printer.php @@ -57,7 +57,8 @@ function ($l) { ); } - $message = explode("\n", $e->getMessage())[0]; + $message = explode("\n", $defect->getExceptionAsString()); + $message = implode('%0A', $message); $type = $this->getCurrentType(); $file = "file={$this->relativePath($path)}"; From cce752a249474088842ef3b6e7038af0eaf581ba Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Sun, 22 Mar 2020 17:28:00 -0700 Subject: [PATCH 03/14] Always use reflection to check class filename But use the original line if the path was already correct. --- src/Printer.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Printer.php b/src/Printer.php index 20c105c..e40655b 100644 --- a/src/Printer.php +++ b/src/Printer.php @@ -51,10 +51,13 @@ function ($l) { $path = substr($error, 0, $lineIndex); $line = substr($error, $lineIndex + 1); - if (!$path) { - list($path, $line) = $this->getReflectionFromTest( - $defect->getTestName() - ); + list($reflectedPath, $reflectedLine) = $this->getReflectionFromTest( + $defect->getTestName() + ); + + if($path !== $reflectedPath) { + $path = $reflectedPath; + $line = $reflectedLine; } $message = explode("\n", $defect->getExceptionAsString()); From 95128c2a9b14c75047ddb627eb96e9d6e0845c10 Mon Sep 17 00:00:00 2001 From: Michael Heap Date: Fri, 3 Jul 2020 22:03:58 +0100 Subject: [PATCH 04/14] 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 From 5c2511af15591c4e9484e28db8d6e050737aa6d7 Mon Sep 17 00:00:00 2001 From: Michael Heap Date: Thu, 16 Jul 2020 15:52:36 +0100 Subject: [PATCH 05/14] Update PHPUnit version replacement to always use powershell --- .github/workflows/push.yml | 89 ++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 47 deletions(-) diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index bfc52ab..877f9f5 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -9,52 +9,47 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest, windows-latest, macos-latest] - php-versions: ['7.3', '7.4'] - phpunit-version: ['8', '9'] + php-versions: ["7.3", "7.4"] + phpunit-version: ["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 - with: - php-version: ${{ matrix.php-versions }} - 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 - - - name: Run phpcs - run: ./vendor/bin/phpcs + - name: Checkout + uses: actions/checkout@v2 + + - name: Setup PHP + uses: shivammathur/setup-php@v1 + with: + php-version: ${{ matrix.php-versions }} + extensions: mbstring + coverage: xdebug + + - name: Set PHPUnit Version + run: | + $content = Get-Content -Path 'composer.json' | ConvertFrom-Json + $content.{require-dev}.{phpunit/phpunit} = "^${{ matrix.phpunit-version }}" + $content | ConvertTo-Json | Set-Content -Path 'composer.json' + shell: pwsh + + - 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 + + - name: Run phpcs + run: ./vendor/bin/phpcs From 93107fd9eec7e5bffd122be8f1e6988cc525ac59 Mon Sep 17 00:00:00 2001 From: Michael Heap Date: Sun, 13 Sep 2020 21:37:37 +0100 Subject: [PATCH 06/14] 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()]; + } +} From eae84d4931979f741a804e0503357490f5e5feb6 Mon Sep 17 00:00:00 2001 From: Michael Heap Date: Mon, 9 Nov 2020 13:36:21 +0000 Subject: [PATCH 07/14] Add support for data providers --- src/Printer7.php | 7 +++++++ src/Trait8.php | 7 +++++++ test/_files/PrinterStatesTest.php | 15 +++++++++++++++ test/states-test.phpt | 1 + 4 files changed, 30 insertions(+) diff --git a/src/Printer7.php b/src/Printer7.php index 4a10bc1..6ad7a21 100644 --- a/src/Printer7.php +++ b/src/Printer7.php @@ -94,6 +94,13 @@ protected function relativePath(string $path) 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); diff --git a/src/Trait8.php b/src/Trait8.php index 68eee61..0ef2a2f 100644 --- a/src/Trait8.php +++ b/src/Trait8.php @@ -93,6 +93,13 @@ protected function relativePath(string $path) 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); diff --git a/test/_files/PrinterStatesTest.php b/test/_files/PrinterStatesTest.php index 3bb686d..8b32a1a 100644 --- a/test/_files/PrinterStatesTest.php +++ b/test/_files/PrinterStatesTest.php @@ -45,5 +45,20 @@ public function testIncomplete() { $this->markTestIncomplete('Incomplete'); } + + /** + * @dataProvider demoProvider + */ + public function testProvider($v) + { + $this->assertTrue($v); + } + + public function demoProvider() + { + return [ + [false] + ]; + } } diff --git a/test/states-test.phpt b/test/states-test.phpt index 5c9af3b..acf678e 100644 --- a/test/states-test.phpt +++ b/test/states-test.phpt @@ -19,5 +19,6 @@ PHPUnit\TextUI\Command::main(); ::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%0A%0Atest/_files/PrinterStatesTest.php:40 From 91658a4ccbc7bff9eb66c43e05520b7e3eab2c3c Mon Sep 17 00:00:00 2001 From: Mitchell Macpherson Date: Sat, 21 Nov 2020 07:21:01 +1100 Subject: [PATCH 08/14] PHPUnit 6 support + refactoring (#23) Co-authored-by: Michael Heap --- .github/workflows/push.yml | 4 +- composer.json | 1 + phpunit.xml.dist | 3 + src/Functions/helpers.php | 123 +++++++++++++++++++++++++++++++ src/Printer.php | 28 ++----- src/Printer6.php | 50 +++++++++++++ src/Printer7.php | 78 ++------------------ src/Printer8.php | 41 ++++++++++- src/Printer9.php | 39 +++++++++- src/Trait8.php | 108 --------------------------- test/Unit/HelperFunctionTest.php | 64 ++++++++++++++++ test/states-test-phpunit6.phpt | 29 ++++++++ test/states-test.phpt | 5 ++ 13 files changed, 369 insertions(+), 204 deletions(-) create mode 100644 src/Functions/helpers.php create mode 100644 src/Printer6.php delete mode 100644 src/Trait8.php create mode 100644 test/Unit/HelperFunctionTest.php create mode 100644 test/states-test-phpunit6.phpt 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-- Date: Fri, 20 Nov 2020 20:59:50 +0000 Subject: [PATCH 09/14] Fix CI + run on pull_request (#24) --- .github/workflows/{push.yml => ci.yml} | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) rename .github/workflows/{push.yml => ci.yml} (68%) diff --git a/.github/workflows/push.yml b/.github/workflows/ci.yml similarity index 68% rename from .github/workflows/push.yml rename to .github/workflows/ci.yml index c0adb06..ac907a6 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/ci.yml @@ -1,5 +1,8 @@ name: CI -on: push +on: + push: {} + pull_request: + types: [opened, reopened, synchronize] jobs: run: env: @@ -33,20 +36,23 @@ jobs: - 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) + - name: Set correct phpt file name + id: phpt-filename run: | - V=$(./vendor/bin/phpunit --version) - sed -i.bak "s/%%VERSION%%/$V/" test/states-test.phpt - if: "!startsWith(matrix.os, 'windows')" + If (${{matrix.phpunit-version}} -eq 6) { + '##[set-output name=version;]-phpunit6' + } Else { + '##[set-output name=version;]' + } + shell: pwsh - - name: Configure tests for the current PHPUnit version (Windows) - id: phpunit-windows + - name: Configure tests for the current PHPUnit version run: | $V = (.\vendor\bin\phpunit --version | Out-String).trim() - $content = Get-Content -Path 'test/states-test.phpt' + $content = Get-Content -Path 'test/states-test${{ steps.phpt-filename.outputs.version}}.phpt' $newContent = $content -replace '%%VERSION%%', $V - $newContent | Set-Content -Path 'test/states-test.phpt' - if: "startsWith(matrix.os, 'windows')" + $newContent | Set-Content -Path 'test/states-test${{ steps.phpt-filename.outputs.version}}.phpt' + shell: pwsh - name: Run phpunit run: ./vendor/bin/phpunit From 877ab6e5f9d552f83101ffa19bbd2a50f2feed44 Mon Sep 17 00:00:00 2001 From: y_ahiru Date: Tue, 24 Nov 2020 08:40:24 +0900 Subject: [PATCH 10/14] fix signature of printHeader for 8.0.0 ~ 8.5.6 --- src/Printer8.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Printer8.php b/src/Printer8.php index ed51138..cf88392 100644 --- a/src/Printer8.php +++ b/src/Printer8.php @@ -17,7 +17,7 @@ class Printer8 extends ResultPrinter */ private $currentType; - protected function printHeader(TestResult $result): void + protected function printHeader(TestResult $result = null): void { } From 660400ad34426ce377e73e87b37b72752e1b8f55 Mon Sep 17 00:00:00 2001 From: Michael Heap Date: Sun, 28 Feb 2021 16:57:57 +0000 Subject: [PATCH 11/14] Fix xdebug error --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ac907a6..6fc49e8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,7 +24,7 @@ jobs: with: php-version: ${{ matrix.php-versions }} extensions: mbstring - coverage: xdebug + coverage: xdebug2 - name: Set PHPUnit Version run: | From 138aef8fff0159dc21d23a5a230df614a0e873e4 Mon Sep 17 00:00:00 2001 From: Michael Heap Date: Sun, 28 Feb 2021 17:23:35 +0000 Subject: [PATCH 12/14] Only run on push to default branch --- .github/workflows/ci.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6fc49e8..d50904c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,6 +1,8 @@ name: CI on: - push: {} + push: + branches: + - $default-branch pull_request: types: [opened, reopened, synchronize] jobs: From a2ddbb3137da135bfe0fad92a858eb0b2c13cfa0 Mon Sep 17 00:00:00 2001 From: Michael Heap Date: Mon, 6 Sep 2021 13:02:04 +0100 Subject: [PATCH 13/14] Enable test run on workflow_dispatch --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d50904c..d6b6453 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,5 +1,6 @@ name: CI on: + workflow_dispatch: push: branches: - $default-branch From 19b34d79a6abfaa362debcd25dd7bd11e82576b8 Mon Sep 17 00:00:00 2001 From: Michael Heap Date: Mon, 6 Sep 2021 13:04:20 +0100 Subject: [PATCH 14/14] Fix default-branch CI run --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d6b6453..8c40ae0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,7 +3,7 @@ on: workflow_dispatch: push: branches: - - $default-branch + - master pull_request: types: [opened, reopened, synchronize] jobs: