From cc3287f79b4612b6c57819b487f5256575bb953c Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 8 Aug 2025 19:19:54 +0200 Subject: [PATCH 01/16] Emit warning when OPCache is enabled while collecting coverage --- .../warn-when-opcache-enabled.phpt | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 tests/end-to-end/code-coverage/warn-when-opcache-enabled.phpt diff --git a/tests/end-to-end/code-coverage/warn-when-opcache-enabled.phpt b/tests/end-to-end/code-coverage/warn-when-opcache-enabled.phpt new file mode 100644 index 0000000000..e341655667 --- /dev/null +++ b/tests/end-to-end/code-coverage/warn-when-opcache-enabled.phpt @@ -0,0 +1,43 @@ +--TEST-- +https://github.com/sebastianbergmann/php-code-coverage/issues/1022 +--INI-- +opcache.enable_cli=1 +--SKIPIF-- +run($_SERVER['argv']); +--EXPECTF-- +PHPUnit %s by Sebastian Bergmann and contributors. + +Runtime: %s + +. 1 / 1 (100%) + +Time: %s, Memory: %s MB + +There was 1 PHPUnit test runner warning: + +1) Code coverage might produce unreliable results when OPCache is enabled. + +OK, but there were issues! +Tests: 1, Assertions: 1, PHPUnit Warnings: 1. +%A From 89ea6a45694b44e1612029bb70db21468416d1ce Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 8 Aug 2025 19:39:52 +0200 Subject: [PATCH 02/16] Run coverage job also on PHP8.5, which has opcache builtin by default --- .github/workflows/ci.yaml | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 0ae0b5c4cb..70f82a067c 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -207,6 +207,13 @@ jobs: runs-on: ubuntu-latest timeout-minutes: 10 + strategy: + fail-fast: false + matrix: + php-version: + - "8.4" + - "8.5" + steps: - name: Checkout uses: actions/checkout@v4 @@ -225,7 +232,7 @@ jobs: - name: Install PHP with extensions uses: shivammathur/setup-php@v2 with: - php-version: ${{ env.PHP_VERSION }} + php-version: ${{ matrix.php-version }} coverage: xdebug extensions: none, ctype, curl, dom, json, libxml, mbstring, pdo, phar, tokenizer, xml, xmlwriter ini-values: zend.assertions=1, error_reporting=-1, log_errors_max_len=0, display_errors=On From b5b5fe5f2047c3628ab8055efd9cc2f5435bc78f Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 8 Aug 2025 19:51:40 +0200 Subject: [PATCH 03/16] Emit: "Code coverage might produce unreliable results when OPCache is enabled" --- src/Runner/CodeCoverage.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Runner/CodeCoverage.php b/src/Runner/CodeCoverage.php index e84a5fc5fb..68bd941fec 100644 --- a/src/Runner/CodeCoverage.php +++ b/src/Runner/CodeCoverage.php @@ -11,6 +11,8 @@ use function assert; use function file_put_contents; +use function function_exists; +use function ini_get; use function sprintf; use function sys_get_temp_dir; use PHPUnit\Event\Facade as EventFacade; @@ -124,6 +126,16 @@ public function init(Configuration $configuration, CodeCoverageFilterRegistry $c $this->codeCoverage()->excludeUncoveredFiles(); } + if ( + // opcache is built-in since 8.5 + function_exists('opcache_compile_file') && + ini_get('opcache.enable_cli') === '1' + ) { + EventFacade::emitter()->testRunnerTriggeredPhpunitWarning( + 'Code coverage might produce unreliable results when OPCache is enabled.', + ); + } + if ($codeCoverageFilterRegistry->get()->isEmpty()) { if (!$codeCoverageFilterRegistry->configured()) { EventFacade::emitter()->testRunnerTriggeredPhpunitWarning( From 574af8f544e461a6c6e2a9cad561127b47f62192 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 8 Aug 2025 20:00:43 +0200 Subject: [PATCH 04/16] Disable jit for test Prevents "Warning: JIT is incompatible with third party extensions that override zend_execute_ex(). JIT disabled. in Unknown on line 0" --- tests/end-to-end/code-coverage/warn-when-opcache-enabled.phpt | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/end-to-end/code-coverage/warn-when-opcache-enabled.phpt b/tests/end-to-end/code-coverage/warn-when-opcache-enabled.phpt index e341655667..551bd285be 100644 --- a/tests/end-to-end/code-coverage/warn-when-opcache-enabled.phpt +++ b/tests/end-to-end/code-coverage/warn-when-opcache-enabled.phpt @@ -2,6 +2,7 @@ https://github.com/sebastianbergmann/php-code-coverage/issues/1022 --INI-- opcache.enable_cli=1 +opcache.jit=disable --SKIPIF-- Date: Sat, 9 Aug 2025 10:56:50 +0200 Subject: [PATCH 05/16] uniform error message format --- src/Runner/CodeCoverage.php | 2 +- tests/end-to-end/code-coverage/warn-when-opcache-enabled.phpt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Runner/CodeCoverage.php b/src/Runner/CodeCoverage.php index 68bd941fec..7b430e614f 100644 --- a/src/Runner/CodeCoverage.php +++ b/src/Runner/CodeCoverage.php @@ -132,7 +132,7 @@ function_exists('opcache_compile_file') && ini_get('opcache.enable_cli') === '1' ) { EventFacade::emitter()->testRunnerTriggeredPhpunitWarning( - 'Code coverage might produce unreliable results when OPCache is enabled.', + 'Code coverage might produce unreliable results when OPCache is enabled', ); } diff --git a/tests/end-to-end/code-coverage/warn-when-opcache-enabled.phpt b/tests/end-to-end/code-coverage/warn-when-opcache-enabled.phpt index 551bd285be..bdf0c64bf5 100644 --- a/tests/end-to-end/code-coverage/warn-when-opcache-enabled.phpt +++ b/tests/end-to-end/code-coverage/warn-when-opcache-enabled.phpt @@ -37,7 +37,7 @@ Time: %s, Memory: %s MB There was 1 PHPUnit test runner warning: -1) Code coverage might produce unreliable results when OPCache is enabled. +1) Code coverage might produce unreliable results when OPCache is enabled OK, but there were issues! Tests: 1, Assertions: 1, PHPUnit Warnings: 1. From 31169da49868290ee8de4864bfb778c91a13e78b Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 9 Aug 2025 11:02:17 +0200 Subject: [PATCH 06/16] Discard changes to .github/workflows/ci.yaml --- .github/workflows/ci.yaml | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 70f82a067c..0ae0b5c4cb 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -207,13 +207,6 @@ jobs: runs-on: ubuntu-latest timeout-minutes: 10 - strategy: - fail-fast: false - matrix: - php-version: - - "8.4" - - "8.5" - steps: - name: Checkout uses: actions/checkout@v4 @@ -232,7 +225,7 @@ jobs: - name: Install PHP with extensions uses: shivammathur/setup-php@v2 with: - php-version: ${{ matrix.php-version }} + php-version: ${{ env.PHP_VERSION }} coverage: xdebug extensions: none, ctype, curl, dom, json, libxml, mbstring, pdo, phar, tokenizer, xml, xmlwriter ini-values: zend.assertions=1, error_reporting=-1, log_errors_max_len=0, display_errors=On From 335f16f5184c6170a2e33ba7cae67757b042cada Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 9 Aug 2025 11:19:45 +0200 Subject: [PATCH 07/16] move test into end-to-end/phar/tests/code-coverage --- tests/end-to-end/phar/phpunit.xml | 1 + .../warn-when-opcache-enabled.phpt | 13 +++++------ .../warn-when-opcache-enabled/phpunit.xml | 22 +++++++++++++++++++ .../warn-when-opcache-enabled/src/Greeter.php | 18 +++++++++++++++ .../tests/GreeterTest.php | 22 +++++++++++++++++++ 5 files changed, 69 insertions(+), 7 deletions(-) rename tests/end-to-end/{ => phar/tests}/code-coverage/warn-when-opcache-enabled.phpt (71%) create mode 100644 tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled/phpunit.xml create mode 100644 tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled/src/Greeter.php create mode 100644 tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled/tests/GreeterTest.php diff --git a/tests/end-to-end/phar/phpunit.xml b/tests/end-to-end/phar/phpunit.xml index 538ee05c4c..de37f9a920 100644 --- a/tests/end-to-end/phar/phpunit.xml +++ b/tests/end-to-end/phar/phpunit.xml @@ -7,6 +7,7 @@ tests/standard + tests/code-coverage tests/phpt diff --git a/tests/end-to-end/code-coverage/warn-when-opcache-enabled.phpt b/tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled.phpt similarity index 71% rename from tests/end-to-end/code-coverage/warn-when-opcache-enabled.phpt rename to tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled.phpt index bdf0c64bf5..49f2768536 100644 --- a/tests/end-to-end/code-coverage/warn-when-opcache-enabled.phpt +++ b/tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled.phpt @@ -3,6 +3,7 @@ https://github.com/sebastianbergmann/php-code-coverage/issues/1022 --INI-- opcache.enable_cli=1 opcache.jit=disable +pcov.directory=tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled/src/ --SKIPIF-- run($_SERVER['argv']); --EXPECTF-- PHPUnit %s by Sebastian Bergmann and contributors. Runtime: %s +Configuration: %s . 1 / 1 (100%) diff --git a/tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled/phpunit.xml b/tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled/phpunit.xml new file mode 100644 index 0000000000..5f8be9d009 --- /dev/null +++ b/tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled/phpunit.xml @@ -0,0 +1,22 @@ + + + + + tests + + + + + + src + + + diff --git a/tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled/src/Greeter.php b/tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled/src/Greeter.php new file mode 100644 index 0000000000..768e37a01f --- /dev/null +++ b/tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled/src/Greeter.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\TestFixture\WarnWhenOpcacheEnabled; + +final class Greeter +{ + public function greet(): string + { + return 'Hello world!'; + } +} diff --git a/tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled/tests/GreeterTest.php b/tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled/tests/GreeterTest.php new file mode 100644 index 0000000000..eac01a800e --- /dev/null +++ b/tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled/tests/GreeterTest.php @@ -0,0 +1,22 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\TestFixture\WarnWhenOpcacheEnabled; + +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\TestCase; + +#[CoversClass(Greeter::class)] +final class GreeterTest extends TestCase +{ + public function testGreets(): void + { + $this->assertSame('Hello world!', (new Greeter)->greet()); + } +} From fb152d13b2b8f929f503e0d375a11d21c01aebc0 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 9 Aug 2025 11:47:55 +0200 Subject: [PATCH 08/16] Update warn-when-opcache-enabled.phpt --- .../phar/tests/code-coverage/warn-when-opcache-enabled.phpt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled.phpt b/tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled.phpt index 49f2768536..a58a92d4ad 100644 --- a/tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled.phpt +++ b/tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled.phpt @@ -21,6 +21,8 @@ $_SERVER['argv'][] = '--coverage-text'; $_SERVER['argv'][] = '--configuration'; $_SERVER['argv'][] = __DIR__ . '/warn-when-opcache-enabled/phpunit.xml'; +require_once __DIR__ . '/warn-when-opcache-enabled/src/Greeter.php'; + require_once __DIR__ . '/../../../../bootstrap.php'; (new PHPUnit\TextUI\Application)->run($_SERVER['argv']); From d5acc5dd6254566158271a14c835c15cdc7e3402 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 9 Aug 2025 12:10:38 +0200 Subject: [PATCH 09/16] simplify test --- .../warn-when-opcache-enabled.phpt | 4 ---- .../warn-when-opcache-enabled/phpunit.xml | 22 ------------------- .../tests/GreeterTest.php | 2 ++ 3 files changed, 2 insertions(+), 26 deletions(-) delete mode 100644 tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled/phpunit.xml diff --git a/tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled.phpt b/tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled.phpt index a58a92d4ad..f308efbc6b 100644 --- a/tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled.phpt +++ b/tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled.phpt @@ -18,10 +18,6 @@ if (!function_exists('opcache_compile_file')) { $_SERVER['argv'][] = '--do-not-cache-result'; $_SERVER['argv'][] = '--colors=never'; $_SERVER['argv'][] = '--coverage-text'; -$_SERVER['argv'][] = '--configuration'; -$_SERVER['argv'][] = __DIR__ . '/warn-when-opcache-enabled/phpunit.xml'; - -require_once __DIR__ . '/warn-when-opcache-enabled/src/Greeter.php'; require_once __DIR__ . '/../../../../bootstrap.php'; diff --git a/tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled/phpunit.xml b/tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled/phpunit.xml deleted file mode 100644 index 5f8be9d009..0000000000 --- a/tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled/phpunit.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - tests - - - - - - src - - - diff --git a/tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled/tests/GreeterTest.php b/tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled/tests/GreeterTest.php index eac01a800e..b0959d8da6 100644 --- a/tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled/tests/GreeterTest.php +++ b/tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled/tests/GreeterTest.php @@ -17,6 +17,8 @@ final class GreeterTest extends TestCase { public function testGreets(): void { + require_once __DIR__ . '/../src/Greeter.php'; + $this->assertSame('Hello world!', (new Greeter)->greet()); } } From 5a1b72e3f2ae4a283ef2c071a7f363ea48733749 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 9 Aug 2025 12:27:35 +0200 Subject: [PATCH 10/16] simplify test further --- tests/end-to-end/phar/phpunit.xml | 1 - .../warn-when-opcache-enabled/src/Greeter.php | 18 -------------- .../tests/GreeterTest.php | 24 ------------------- .../warn-when-opcache-enabled.phpt | 10 ++++---- 4 files changed, 5 insertions(+), 48 deletions(-) delete mode 100644 tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled/src/Greeter.php delete mode 100644 tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled/tests/GreeterTest.php rename tests/end-to-end/phar/tests/{code-coverage => phpt}/warn-when-opcache-enabled.phpt (77%) diff --git a/tests/end-to-end/phar/phpunit.xml b/tests/end-to-end/phar/phpunit.xml index de37f9a920..538ee05c4c 100644 --- a/tests/end-to-end/phar/phpunit.xml +++ b/tests/end-to-end/phar/phpunit.xml @@ -7,7 +7,6 @@ tests/standard - tests/code-coverage tests/phpt diff --git a/tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled/src/Greeter.php b/tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled/src/Greeter.php deleted file mode 100644 index 768e37a01f..0000000000 --- a/tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled/src/Greeter.php +++ /dev/null @@ -1,18 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace PHPUnit\TestFixture\WarnWhenOpcacheEnabled; - -final class Greeter -{ - public function greet(): string - { - return 'Hello world!'; - } -} diff --git a/tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled/tests/GreeterTest.php b/tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled/tests/GreeterTest.php deleted file mode 100644 index b0959d8da6..0000000000 --- a/tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled/tests/GreeterTest.php +++ /dev/null @@ -1,24 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace PHPUnit\TestFixture\WarnWhenOpcacheEnabled; - -use PHPUnit\Framework\Attributes\CoversClass; -use PHPUnit\Framework\TestCase; - -#[CoversClass(Greeter::class)] -final class GreeterTest extends TestCase -{ - public function testGreets(): void - { - require_once __DIR__ . '/../src/Greeter.php'; - - $this->assertSame('Hello world!', (new Greeter)->greet()); - } -} diff --git a/tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled.phpt b/tests/end-to-end/phar/tests/phpt/warn-when-opcache-enabled.phpt similarity index 77% rename from tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled.phpt rename to tests/end-to-end/phar/tests/phpt/warn-when-opcache-enabled.phpt index f308efbc6b..38b69300a5 100644 --- a/tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled.phpt +++ b/tests/end-to-end/phar/tests/phpt/warn-when-opcache-enabled.phpt @@ -3,7 +3,7 @@ https://github.com/sebastianbergmann/php-code-coverage/issues/1022 --INI-- opcache.enable_cli=1 opcache.jit=disable -pcov.directory=tests/end-to-end/phar/tests/code-coverage/warn-when-opcache-enabled/src/ +pcov.directory=tests/end-to-end/phar/src/ --SKIPIF-- Date: Sat, 9 Aug 2025 12:43:45 +0200 Subject: [PATCH 11/16] Update warn-when-opcache-enabled.phpt --- .../end-to-end/phar/tests/phpt/warn-when-opcache-enabled.phpt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/end-to-end/phar/tests/phpt/warn-when-opcache-enabled.phpt b/tests/end-to-end/phar/tests/phpt/warn-when-opcache-enabled.phpt index 38b69300a5..79a28e2081 100644 --- a/tests/end-to-end/phar/tests/phpt/warn-when-opcache-enabled.phpt +++ b/tests/end-to-end/phar/tests/phpt/warn-when-opcache-enabled.phpt @@ -20,6 +20,8 @@ $_SERVER['argv'][] = '--colors=never'; $_SERVER['argv'][] = '--coverage-text'; $_SERVER['argv'][] = '--bootstrap'; $_SERVER['argv'][] = __DIR__.'/../../src/autoload.php'; +$_SERVER['argv'][] = '--coverage-filter'; +$_SERVER['argv'][] = __DIR__.'/../../src/'; $_SERVER['argv'][] = __DIR__.'/../standard/GreeterTest.php'; require_once __DIR__ . '/../../../../bootstrap.php'; @@ -31,7 +33,7 @@ PHPUnit %s by Sebastian Bergmann and contributors. Runtime: %s Configuration: %s -WW 2 / 2 (100%) +.. 2 / 2 (100%) Time: %s, Memory: %s MB From f3d108f9c0297f5f4b7c96e5a9f01f2f52a2a34b Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Tue, 12 Aug 2025 15:17:58 +0200 Subject: [PATCH 12/16] move test into new CI job --- .github/workflows/ci.yaml | 60 +++++++++++++++++++ phpunit.xml | 4 ++ .../warn-when-opcache-enabled/src/Greeter.php | 18 ++++++ .../src/autoload.php | 11 ++++ .../tests/GreeterTest.php | 25 ++++++++ .../warn-when-opcache-enabled.phpt | 13 ++-- 6 files changed, 124 insertions(+), 7 deletions(-) create mode 100644 tests/end-to-end-with-coverage-driver/warn-when-opcache-enabled/src/Greeter.php create mode 100644 tests/end-to-end-with-coverage-driver/warn-when-opcache-enabled/src/autoload.php create mode 100644 tests/end-to-end-with-coverage-driver/warn-when-opcache-enabled/tests/GreeterTest.php rename tests/{end-to-end/phar/tests/phpt => end-to-end-with-coverage-driver/warn-when-opcache-enabled}/warn-when-opcache-enabled.phpt (68%) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 0ae0b5c4cb..f875efb7c4 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -196,6 +196,65 @@ jobs: - name: Run tests with PHPUnit run: php ./phpunit --testsuite end-to-end --order-by depends,random + end-to-end-tests-with-coverage-driver: + name: End-to-End Tests + + needs: + - unit-tests + + runs-on: ${{ matrix.os }} + timeout-minutes: 5 + + env: + PHP_EXTENSIONS: none, ctype, curl, dom, json, libxml, mbstring, openssl, pdo, phar, tokenizer, xml, xmlwriter + PHP_INI_VALUES: zend.assertions=1, error_reporting=-1, log_errors_max_len=0, display_errors=On + + strategy: + fail-fast: false + matrix: + os: + - ubuntu-latest + - windows-latest + + php-version: + - "8.3" + - "8.4" + - "8.5" + + steps: + - name: Configure Git to avoid issues with line endings + if: matrix.os == 'windows-latest' + run: git config --global core.autocrlf false + + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + ref: ${{ github.event.pull_request.head.sha || github.sha }} + + - name: Use local branch + shell: bash + run: | + BRANCH=$([ "${{ github.event_name }}" == "pull_request" ] && echo "${{ github.head_ref }}" || echo "${{ github.ref_name }}") + git branch -D $BRANCH 2>/dev/null || true + git branch $BRANCH HEAD + git checkout $BRANCH + + - name: Install PHP with extensions + uses: shivammathur/setup-php@v2 + with: + coverage: xdebug + php-version: ${{ matrix.php-version }} + extensions: ${{ env.PHP_EXTENSIONS }} + ini-values: ${{ env.PHP_INI_VALUES }} + tools: none + + - name: Install dependencies with Composer + run: php ./tools/composer install --no-ansi --no-interaction --no-progress + + - name: Run tests with PHPUnit + run: php ./phpunit --testsuite end-to-end-with-coverage-driver --order-by depends,random + code-coverage: name: Code Coverage @@ -203,6 +262,7 @@ jobs: needs: - end-to-end-tests + - end-to-end-tests-with-coverage-driver runs-on: ubuntu-latest timeout-minutes: 10 diff --git a/phpunit.xml b/phpunit.xml index 9d543c2e8f..8702c8c6df 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -47,6 +47,10 @@ tests/end-to-end/self-direct-indirect/_files tests/end-to-end/testdox/_files + + + tests/end-to-end-with-coverage-driver/warn-when-opcache-enabled + diff --git a/tests/end-to-end-with-coverage-driver/warn-when-opcache-enabled/src/Greeter.php b/tests/end-to-end-with-coverage-driver/warn-when-opcache-enabled/src/Greeter.php new file mode 100644 index 0000000000..081835d281 --- /dev/null +++ b/tests/end-to-end-with-coverage-driver/warn-when-opcache-enabled/src/Greeter.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\TestFixture\CoverageWithOpcacheEnabled; + +final class Greeter +{ + public function greet(): string + { + return 'Hello world!'; + } +} diff --git a/tests/end-to-end-with-coverage-driver/warn-when-opcache-enabled/src/autoload.php b/tests/end-to-end-with-coverage-driver/warn-when-opcache-enabled/src/autoload.php new file mode 100644 index 0000000000..c66cf5e50f --- /dev/null +++ b/tests/end-to-end-with-coverage-driver/warn-when-opcache-enabled/src/autoload.php @@ -0,0 +1,11 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +require_once __DIR__ . '/Greeter.php'; diff --git a/tests/end-to-end-with-coverage-driver/warn-when-opcache-enabled/tests/GreeterTest.php b/tests/end-to-end-with-coverage-driver/warn-when-opcache-enabled/tests/GreeterTest.php new file mode 100644 index 0000000000..c17d46abe6 --- /dev/null +++ b/tests/end-to-end-with-coverage-driver/warn-when-opcache-enabled/tests/GreeterTest.php @@ -0,0 +1,25 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\TestFixture\CoverageWithOpcacheEnabled; + +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\RunInSeparateProcess; +use PHPUnit\Framework\Attributes\Ticket; +use PHPUnit\Framework\TestCase; + +#[CoversClass(Greeter::class)] +final class GreeterTest extends TestCase +{ + public function testGreets(): void + { + $this->assertSame('Hello world!', (new Greeter)->greet()); + } + +} diff --git a/tests/end-to-end/phar/tests/phpt/warn-when-opcache-enabled.phpt b/tests/end-to-end-with-coverage-driver/warn-when-opcache-enabled/warn-when-opcache-enabled.phpt similarity index 68% rename from tests/end-to-end/phar/tests/phpt/warn-when-opcache-enabled.phpt rename to tests/end-to-end-with-coverage-driver/warn-when-opcache-enabled/warn-when-opcache-enabled.phpt index 79a28e2081..a066b56382 100644 --- a/tests/end-to-end/phar/tests/phpt/warn-when-opcache-enabled.phpt +++ b/tests/end-to-end-with-coverage-driver/warn-when-opcache-enabled/warn-when-opcache-enabled.phpt @@ -3,10 +3,9 @@ https://github.com/sebastianbergmann/php-code-coverage/issues/1022 --INI-- opcache.enable_cli=1 opcache.jit=disable -pcov.directory=tests/end-to-end/phar/src/ --SKIPIF-- run($_SERVER['argv']); --EXPECTF-- @@ -33,7 +32,7 @@ PHPUnit %s by Sebastian Bergmann and contributors. Runtime: %s Configuration: %s -.. 2 / 2 (100%) +. 1 / 1 (100%) Time: %s, Memory: %s MB From 0015b990cdf64d506c2e7a813c6eb5733db72cbc Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Tue, 12 Aug 2025 15:23:37 +0200 Subject: [PATCH 13/16] fix test --- .github/workflows/ci.yaml | 2 +- .../warn-when-opcache-enabled/warn-when-opcache-enabled.phpt | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index f875efb7c4..f4de0fb2b9 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -197,7 +197,7 @@ jobs: run: php ./phpunit --testsuite end-to-end --order-by depends,random end-to-end-tests-with-coverage-driver: - name: End-to-End Tests + name: End-to-End Tests with Coverage Driver needs: - unit-tests diff --git a/tests/end-to-end-with-coverage-driver/warn-when-opcache-enabled/warn-when-opcache-enabled.phpt b/tests/end-to-end-with-coverage-driver/warn-when-opcache-enabled/warn-when-opcache-enabled.phpt index a066b56382..36e00b7be5 100644 --- a/tests/end-to-end-with-coverage-driver/warn-when-opcache-enabled/warn-when-opcache-enabled.phpt +++ b/tests/end-to-end-with-coverage-driver/warn-when-opcache-enabled/warn-when-opcache-enabled.phpt @@ -3,6 +3,8 @@ https://github.com/sebastianbergmann/php-code-coverage/issues/1022 --INI-- opcache.enable_cli=1 opcache.jit=disable +--ENV-- +XDEBUG_MODE=coverage --SKIPIF-- Date: Tue, 12 Aug 2025 15:29:45 +0200 Subject: [PATCH 14/16] fix skipif condition --- .../warn-when-opcache-enabled/warn-when-opcache-enabled.phpt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/end-to-end-with-coverage-driver/warn-when-opcache-enabled/warn-when-opcache-enabled.phpt b/tests/end-to-end-with-coverage-driver/warn-when-opcache-enabled/warn-when-opcache-enabled.phpt index 36e00b7be5..3c8bb29a84 100644 --- a/tests/end-to-end-with-coverage-driver/warn-when-opcache-enabled/warn-when-opcache-enabled.phpt +++ b/tests/end-to-end-with-coverage-driver/warn-when-opcache-enabled/warn-when-opcache-enabled.phpt @@ -9,7 +9,7 @@ XDEBUG_MODE=coverage Date: Tue, 12 Aug 2025 15:37:00 +0200 Subject: [PATCH 15/16] fix SKIPIF --- .../warn-when-opcache-enabled.phpt | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/end-to-end-with-coverage-driver/warn-when-opcache-enabled/warn-when-opcache-enabled.phpt b/tests/end-to-end-with-coverage-driver/warn-when-opcache-enabled/warn-when-opcache-enabled.phpt index 3c8bb29a84..df3eb2cd53 100644 --- a/tests/end-to-end-with-coverage-driver/warn-when-opcache-enabled/warn-when-opcache-enabled.phpt +++ b/tests/end-to-end-with-coverage-driver/warn-when-opcache-enabled/warn-when-opcache-enabled.phpt @@ -7,12 +7,10 @@ opcache.jit=disable XDEBUG_MODE=coverage --SKIPIF-- Date: Tue, 12 Aug 2025 16:32:49 +0200 Subject: [PATCH 16/16] Utilize SebastianBergmann\Environment\Runtime --- src/Runner/CodeCoverage.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/Runner/CodeCoverage.php b/src/Runner/CodeCoverage.php index 7b430e614f..1576151f1a 100644 --- a/src/Runner/CodeCoverage.php +++ b/src/Runner/CodeCoverage.php @@ -11,8 +11,6 @@ use function assert; use function file_put_contents; -use function function_exists; -use function ini_get; use function sprintf; use function sys_get_temp_dir; use PHPUnit\Event\Facade as EventFacade; @@ -42,6 +40,7 @@ use SebastianBergmann\CodeCoverage\Test\TestSize\TestSize; use SebastianBergmann\CodeCoverage\Test\TestStatus\TestStatus; use SebastianBergmann\Comparator\Comparator; +use SebastianBergmann\Environment\Runtime; use SebastianBergmann\Timer\NoActiveTimerException; use SebastianBergmann\Timer\Timer; @@ -126,11 +125,9 @@ public function init(Configuration $configuration, CodeCoverageFilterRegistry $c $this->codeCoverage()->excludeUncoveredFiles(); } - if ( - // opcache is built-in since 8.5 - function_exists('opcache_compile_file') && - ini_get('opcache.enable_cli') === '1' - ) { + $runtime = new Runtime; + + if ($runtime->isOpcacheActive()) { EventFacade::emitter()->testRunnerTriggeredPhpunitWarning( 'Code coverage might produce unreliable results when OPCache is enabled', );