Skip to content

Emit warning when OPCache is enabled while collecting coverage #6299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,73 @@ 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 with Coverage Driver

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

if: github.event_name != 'schedule'

needs:
- end-to-end-tests
- end-to-end-tests-with-coverage-driver

runs-on: ubuntu-latest
timeout-minutes: 10
Expand Down
4 changes: 4 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
<exclude>tests/end-to-end/self-direct-indirect/_files</exclude>
<exclude>tests/end-to-end/testdox/_files</exclude>
</testsuite>

<testsuite name="end-to-end-with-coverage-driver">
<directory suffix=".phpt">tests/end-to-end-with-coverage-driver/warn-when-opcache-enabled</directory>
</testsuite>
</testsuites>

<source ignoreIndirectDeprecations="true" restrictNotices="true" restrictWarnings="true">
Expand Down
9 changes: 9 additions & 0 deletions src/Runner/CodeCoverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,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;

Expand Down Expand Up @@ -124,6 +125,14 @@ public function init(Configuration $configuration, CodeCoverageFilterRegistry $c
$this->codeCoverage()->excludeUncoveredFiles();
}

$runtime = new Runtime;

if ($runtime->isOpcacheActive()) {
EventFacade::emitter()->testRunnerTriggeredPhpunitWarning(
'Code coverage might produce unreliable results when OPCache is enabled',
);
}

if ($codeCoverageFilterRegistry->get()->isEmpty()) {
if (!$codeCoverageFilterRegistry->configured()) {
EventFacade::emitter()->testRunnerTriggeredPhpunitWarning(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <[email protected]>
*
* 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!';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types=1);

/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
require_once __DIR__ . '/Greeter.php';
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <[email protected]>
*
* 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());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
--TEST--
https://github.com/sebastianbergmann/php-code-coverage/issues/1022
--INI--
opcache.enable_cli=1
opcache.jit=disable
--ENV--
XDEBUG_MODE=coverage
--SKIPIF--
<?php declare(strict_types=1);
require __DIR__ . '/../../_files/skip-if-requires-code-coverage-driver.php';

if (!extension_loaded('Zend OPcache')) {
echo 'skip: opcache extension is not loaded';
}
--FILE--
<?php declare(strict_types=1);
$_SERVER['argv'][] = '--do-not-cache-result';
$_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__.'/tests/GreeterTest.php';

require_once __DIR__ . '/../../bootstrap.php';

(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

Runtime: %s
Configuration: %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
%A
Loading