diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index eb31f89..665cd4b 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -8,7 +8,7 @@ jobs: strategy: matrix: operating-system: ['ubuntu-latest', 'windows-latest', 'macOS-latest'] - php-version: ['7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3'] + php-version: ['8.1', '8.2', '8.3'] name: PHP ${{ matrix.php-version }} Test on ${{ matrix.operating-system }} steps: - name: Checkout code @@ -23,6 +23,7 @@ jobs: with: php-version: ${{ matrix.php-version }} tools: composer:v2 + coverage: xdebug - name: Install Composer dependencies run: composer install --prefer-dist --no-interaction --no-suggest - name: Install phpcs diff --git a/build.sh b/build.sh index 2dc41a0..9c6445e 100755 --- a/build.sh +++ b/build.sh @@ -1,12 +1,10 @@ #!/bin/sh set -e -git log origin/master... | grep -q SKIP_BUILD && exit 0 - [ -z "$UPDATE_COVERAGE" ] || composer require satooshi/php-coveralls:v1.1.0 composer install -git diff $(git merge-base origin/master HEAD) > diff.txt +[ -z "$UPDATE_COVERAGE" ] || git diff $(git merge-base origin/master HEAD) > diff.txt phpcs --standard=psr2 src phpcs --standard=psr2 --ignore=bootstrap.php,fixtures/* tests @@ -15,6 +13,6 @@ phpmd tests text cleancode,codesize,controversial,unusedcode --exclude fixtures ./vendor/bin/phpunit -bin/diffFilter --phpunit diff.txt report/coverage.xml +[ -z "$UPDATE_COVERAGE" ] || bin/diffFilter --phpunit diff.txt report/coverage.xml [ -z "$UPDATE_COVERAGE" ] || php vendor/bin/coveralls -v diff --git a/composer.json b/composer.json index 3709b8b..a38b48b 100644 --- a/composer.json +++ b/composer.json @@ -19,9 +19,9 @@ }, "bin": ["bin/diffFilter"], "require": { - "php": ">=7.0", + "php": ">=8.1", "ext-xmlreader": "*", "ext-json": "*", - "nikic/php-parser": "^3.1||^4.0" + "nikic/php-parser": "^3.1||^4.0||^5.0" } } diff --git a/phpunit.xml b/phpunit.xml index 9a3e9e6..a43e554 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,28 +1,18 @@ - - - tests - - - - - src - - - - - - + + + + + + + + + tests + + + + + src + + diff --git a/src/FileParser.php b/src/FileParser.php index ebdb393..0fe1742 100644 --- a/src/FileParser.php +++ b/src/FileParser.php @@ -18,7 +18,7 @@ class FileParser public function __construct($sourceCode) { $this->sourceCode = $sourceCode; - $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); + $parser = (new ParserFactory)->createForHostVersion(); $this->parse($parser); } @@ -56,7 +56,7 @@ protected function getCodeLimits(Node $node): CodeLimits $startLine = $node->getAttribute('startLine'); $endLine = $node->getAttribute('endLine'); if ($node->getDocComment()) { - $startLine = $node->getDocComment()->getLine(); + $startLine = $node->getDocComment()->getStartLine(); } return new CodeLimits($startLine, $endLine); diff --git a/src/functions.php b/src/functions.php index dfcbf85..d9c5684 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); - - 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 []; + ->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 []; + } ); $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); - - 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; @@ -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,19 +246,17 @@ public function testCoverageForContextLines() ->willReturn(false); $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 null; - } - - return []; + if ($file == '/full/path/to/testFile1.php' && $line == 2) { + return null; } - ) + + return []; + } ); $matcher = new FileMatchers\EndsWith; diff --git a/tests/DiffFileLoadOldVersionTest.php b/tests/DiffFileLoadOldVersionTest.php index 6ffd6ab..449c556 100644 --- a/tests/DiffFileLoadOldVersionTest.php +++ b/tests/DiffFileLoadOldVersionTest.php @@ -1,14 +1,13 @@ getChangedLines($file); @@ -16,7 +15,7 @@ public function testDiffResultsMatch($file, $expected) $this->assertEquals($expected, $changed); } - public function getResults() + public static function getResults() { return [ 'newFile' => [ diff --git a/tests/DiffFileLoadTest.php b/tests/DiffFileLoadTest.php index 9601e7d..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); @@ -26,7 +25,7 @@ public function testNonExistantFile() $this->getChangedLines('ufhbubfusdf'); } - public function getResults() + public static function getResults() { return [ 'newFile' => [ 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) {