From 185cea9ee4935f5c9046aee75e0ee300ffdb3897 Mon Sep 17 00:00:00 2001 From: Scott Dutton Date: Tue, 25 Jun 2024 15:31:44 +0100 Subject: [PATCH] Fix compat issues --- composer.json | 2 +- src/functions.php | 23 +++--- tests/ArgParserTest.php | 5 +- tests/CoverageCheckTest.php | 90 ++++++++++------------- tests/DiffFileLoadOldVersionTest.php | 8 +- tests/DiffFileLoadTest.php | 5 +- tests/Loaders/CheckstyleTest.php | 5 +- tests/Loaders/CodeClimateTest.php | 5 +- tests/Loaders/PhanJsonTest.php | 5 +- tests/Loaders/PhanTextTest.php | 5 +- tests/Loaders/PhpMndTest.php | 12 ++- tests/Loaders/PhpStanTest.php | 5 +- tests/Loaders/PhpcpdTest.php | 7 +- tests/Loaders/PhpmndXmlDiffFilterTest.php | 5 +- tests/Loaders/PylintTest.php | 7 +- tests/PhpunitFilterTest.php | 10 +-- 16 files changed, 90 insertions(+), 109 deletions(-) diff --git a/composer.json b/composer.json index 59178ae..4571559 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ }, "bin": ["bin/diffFilter"], "require": { - "php": ">=7.0", + "php": ">=8.0", "ext-xmlreader": "*", "ext-json": "*", "nikic/php-parser": "^3.1||^4.0||^5.0" diff --git a/src/functions.php b/src/functions.php index dfcbf85..3882675 100644 --- a/src/functions.php +++ b/src/functions.php @@ -92,7 +92,7 @@ function handleOutput(array $lines, float $minimumPercentCovered, Output $output if ($coveredLines + $uncoveredLines == 0) { error_log('No lines found!'); - + $output->output( $lines['uncoveredLines'], 100, @@ -126,14 +126,19 @@ function calculateLines(array $lines) function addExceptionHandler() { - set_exception_handler( - function ($exception) { - // @codeCoverageIgnoreStart - error_log($exception->getMessage()); - exit($exception->getCode()); - // @codeCoverageIgnoreEnd - } - ); + if ( + !defined('PHPUNIT_COMPOSER_INSTALL') && + !defined('__PHPUNIT_PHAR__') + ) { + set_exception_handler( + function ($exception) { + // @codeCoverageIgnoreStart + error_log($exception->getMessage()); + exit($exception->getCode()); + // @codeCoverageIgnoreEnd + } + ); + } } function getFileChecker( diff --git a/tests/ArgParserTest.php b/tests/ArgParserTest.php index ed78edf..7277f19 100644 --- a/tests/ArgParserTest.php +++ b/tests/ArgParserTest.php @@ -1,6 +1,7 @@ method('getErrorsOnLine') - ->will( - $this->returnCallback( - function () { - $file = func_get_arg(0); - $line = func_get_arg(1); + ->willReturnCallback( + function () { + $file = func_get_arg(0); + $line = func_get_arg(1); - if ($file == '/full/path/to/testFile1.php' && $line == 2) { - return $this->errorMessage; - } - if ($file == '/full/path/to/testFile2.php' && $line == 4) { - return $this->errorMessage; - } - - return []; + if ($file == '/full/path/to/testFile1.php' && $line == 2) { + return $this->errorMessage; + } + if ($file == '/full/path/to/testFile2.php' && $line == 4) { + return $this->errorMessage; } - ) + + return []; + } ); $matcher = new FileMatchers\EndsWith; @@ -87,19 +85,17 @@ public function testCoverageFailed() ->willReturn(null); $xmlReport->method('getErrorsOnLine') - ->will( - $this->returnCallback( - function () { - $file = func_get_arg(0); - $line = func_get_arg(1); + ->willReturnCallback( + function () { + $file = func_get_arg(0); + $line = func_get_arg(1); - if ($file == '/full/path/to/testFile1.php' && $line == 2) { - return $this->errorMessage; - } - - return []; + if ($file == '/full/path/to/testFile1.php' && $line == 2) { + return $this->errorMessage; } - ) + + return []; + } ); $matcher = new FileMatchers\EndsWith; @@ -141,19 +137,17 @@ public function testAddingAllUnknownsCovered() ->willReturn(true); $xmlReport->method('getErrorsOnLine') - ->will( - $this->returnCallback( - function () { - $file = func_get_arg(0); - $line = func_get_arg(1); + ->willReturnCallback( + function () { + $file = func_get_arg(0); + $line = func_get_arg(1); - if ($file == '/full/path/to/testFile1.php' && $line == 2) { - return $this->errorMessage; - } - - return []; + if ($file == '/full/path/to/testFile1.php' && $line == 2) { + return $this->errorMessage; } - ) + + return []; + } ); $matcher = new FileMatchers\EndsWith; @@ -196,19 +190,17 @@ public function testAddingAllUnknownsUnCovered() ->willReturn(false); $xmlReport->method('getErrorsOnLine') - ->will( - $this->returnCallback( - function () { - $file = func_get_arg(0); - $line = func_get_arg(1); - - if ($file == '/full/path/to/testFile1.php' && $line == 2) { - return $this->errorMessage; - } + ->willReturnCallback( + function () { + $file = func_get_arg(0); + $line = func_get_arg(1); - return []; + if ($file == '/full/path/to/testFile1.php' && $line == 2) { + return $this->errorMessage; } - ) + + return []; + } ); $matcher = new FileMatchers\EndsWith; @@ -254,8 +246,7 @@ public function testCoverageForContextLines() ->willReturn(false); $xmlReport->method('getErrorsOnLine') - ->will( - $this->returnCallback( + ->willReturnCallback( function () { $file = func_get_arg(0); $line = func_get_arg(1); @@ -266,7 +257,6 @@ function () { return []; } - ) ); $matcher = new FileMatchers\EndsWith; diff --git a/tests/DiffFileLoadOldVersionTest.php b/tests/DiffFileLoadOldVersionTest.php index 4ddd3bf..4ea7348 100644 --- a/tests/DiffFileLoadOldVersionTest.php +++ b/tests/DiffFileLoadOldVersionTest.php @@ -1,14 +1,14 @@ getChangedLines($file); diff --git a/tests/DiffFileLoadTest.php b/tests/DiffFileLoadTest.php index f0326e9..ffd3a2a 100644 --- a/tests/DiffFileLoadTest.php +++ b/tests/DiffFileLoadTest.php @@ -2,6 +2,7 @@ namespace exussum12\CoverageChecker\tests; use InvalidArgumentException; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use exussum12\CoverageChecker\DiffFileLoader; use exussum12\CoverageChecker\DiffFileState; @@ -9,9 +10,7 @@ class DiffFileLoadTest extends TestCase { - /** - * @dataProvider getResults - */ + #[DataProvider('getResults')] public function testDiffResultsMatch($file, $expected) { $changed = $this->getChangedLines($file); diff --git a/tests/Loaders/CheckstyleTest.php b/tests/Loaders/CheckstyleTest.php index 6c430b9..043ecbb 100644 --- a/tests/Loaders/CheckstyleTest.php +++ b/tests/Loaders/CheckstyleTest.php @@ -2,6 +2,7 @@ namespace exussum12\CoverageChecker\tests\Loaders; use exussum12\CoverageChecker\Loaders\Checkstyle; +use PHPUnit\Framework\Attributes\Before; class CheckstyleTest extends PhanTextTest { @@ -9,9 +10,7 @@ class CheckstyleTest extends PhanTextTest protected $phan; protected $prefix = ''; - /** - * @before - */ + #[Before] protected function setUpTest() { $this->phan = new Checkstyle(__DIR__ . '/../fixtures/checkstyle.xml'); diff --git a/tests/Loaders/CodeClimateTest.php b/tests/Loaders/CodeClimateTest.php index 210c2ba..623ce81 100644 --- a/tests/Loaders/CodeClimateTest.php +++ b/tests/Loaders/CodeClimateTest.php @@ -2,6 +2,7 @@ namespace exussum12\CoverageChecker\tests\Loaders; use exussum12\CoverageChecker\Loaders\CodeClimate; +use PHPUnit\Framework\Attributes\Before; class CodeClimateTest extends PhanTextTest { @@ -9,9 +10,7 @@ class CodeClimateTest extends PhanTextTest protected $phan; protected $prefix = ''; - /** - * @before - */ + #[Before] protected function setUpTest() { $this->phan = new CodeClimate(__DIR__ . '/../fixtures/codeclimate.json'); diff --git a/tests/Loaders/PhanJsonTest.php b/tests/Loaders/PhanJsonTest.php index 0ddcb80..d760ce1 100644 --- a/tests/Loaders/PhanJsonTest.php +++ b/tests/Loaders/PhanJsonTest.php @@ -2,15 +2,14 @@ namespace exussum12\CoverageChecker\tests\Loaders; use exussum12\CoverageChecker\Loaders\PhanJson; +use PHPUnit\Framework\Attributes\Before; class PhanJsonTest extends PhanTextTest { /** @var PhanJsonTest */ protected $phan; - /** - * @before - */ + #[Before] protected function setUpTest() { $this->phan = new PhanJson(__DIR__ . '/../fixtures/phan.json'); diff --git a/tests/Loaders/PhanTextTest.php b/tests/Loaders/PhanTextTest.php index e2bb002..4f40eb1 100644 --- a/tests/Loaders/PhanTextTest.php +++ b/tests/Loaders/PhanTextTest.php @@ -1,6 +1,7 @@ phan = new PhanText(__DIR__ . '/../fixtures/phan.txt'); diff --git a/tests/Loaders/PhpMndTest.php b/tests/Loaders/PhpMndTest.php index 9d39c85..e858f49 100644 --- a/tests/Loaders/PhpMndTest.php +++ b/tests/Loaders/PhpMndTest.php @@ -1,6 +1,8 @@ assertSame($expected, $this->mnd->parseLines()); } - /** - * @dataProvider fileInputs - */ + #[DataProvider('fileInputs')] public function testLinesReturnCorrect($filename, $lineNo, $expected) { $this->mnd->parseLines(); @@ -44,7 +42,7 @@ public function testInvalidFile() $this->assertTrue($this->mnd->handleNotFoundFile()); } - public function fileInputs() + public static function fileInputs() { return [ 'found file, valid line' => ['test.php', 2, []], diff --git a/tests/Loaders/PhpStanTest.php b/tests/Loaders/PhpStanTest.php index aea7647..f47b6b7 100644 --- a/tests/Loaders/PhpStanTest.php +++ b/tests/Loaders/PhpStanTest.php @@ -1,6 +1,7 @@ cpd = new Phpcpd(__DIR__ . '/../fixtures/phpcpd.txt'); diff --git a/tests/Loaders/PhpmndXmlDiffFilterTest.php b/tests/Loaders/PhpmndXmlDiffFilterTest.php index 31f193c..b29e97a 100644 --- a/tests/Loaders/PhpmndXmlDiffFilterTest.php +++ b/tests/Loaders/PhpmndXmlDiffFilterTest.php @@ -1,6 +1,7 @@ phan = new Pylint(__DIR__ . '/../fixtures/pylint.txt'); } diff --git a/tests/PhpunitFilterTest.php b/tests/PhpunitFilterTest.php index 20c7ee3..6842520 100644 --- a/tests/PhpunitFilterTest.php +++ b/tests/PhpunitFilterTest.php @@ -2,23 +2,21 @@ namespace exussum12\CoverageChecker\tests; use exussum12\CoverageChecker\PhpunitFilter; +use PHPUnit\Framework\Attributes\Before; +use PHPUnit\Framework\Attributes\RequiresPhpExtension; use PHPUnit\Framework\TestCase; use exussum12\CoverageChecker\DiffFileLoader; use exussum12\CoverageChecker\FileMatchers; use Exception; -/** - * @requires extension xdebug - */ +#[RequiresPhpExtension('xdebug')] class PhpunitFilterTest extends TestCase { protected $coverage; protected $diff; protected $matcher; - /** - * @before - */ + #[Before] public function setUpTest() { if (PHP_VERSION > 7.2) {