Certainly! The message "No filter is configured, code coverage will not be processed" indicates that PHPUnit is unable to process code coverage because no filter is configured to specify which files or directories should be included in the code coverage report.
In PHPUnit 10.5, code coverage configuration has been updated, and you need to explicitly define filters in your phpunit.xml or phpunit.xml.dist configuration file. Here’s how you can configure it:
Step-by-Step Guide to Configure Code Coverage in PHPUnit 10.5
-
Open your phpunit.xml or phpunit.xml.dist file.
-
Add a <coverage> section to your configuration file if it doesn't already exist.
-
Inside the <coverage> section, add a <filter> section to specify which files or directories to include in the code coverage report.
Here is an example configuration:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<coverage>
<filter>
<whitelist>
<directory suffix=".php">src</directory>
<!-- You can add more directories or exclude some files -->
<!-- <exclude>
<directory suffix=".php">src/ExcludedDirectory</directory>
<file>src/ExcludedFile.php</file>
</exclude> -->
</whitelist>
</filter>
</coverage>
<testsuites>
<testsuite name="Application Test Suite">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>
</phpunit>
Explanation:
<coverage>: This section is where you configure code coverage settings.
<filter>: This section is used to define which files or directories should be included or excluded from code coverage.
<whitelist>: This tag is used to specify the files or directories to include in the code coverage report.
<directory suffix=".php">src</directory>: This includes all .php files in the src directory. Adjust the directory path to match your source code directory.
- You can also use the
<file> tag to include specific files.
- (Optional)
<exclude>: If there are specific files or directories you want to exclude from code coverage, you can use the <exclude> tag.
<directory suffix=".php">src/ExcludedDirectory</directory>: Excludes all .php files in the src/ExcludedDirectory.
<file>src/ExcludedFile.php</file>: Excludes a specific file.
Running PHPUnit with Code Coverage
To generate a code coverage report, run PHPUnit with the --coverage-text, --coverage-html, or --coverage-clover option:
# For text coverage report
vendor/bin/phpunit --coverage-text
# For HTML coverage report
vendor/bin/phpunit --coverage-html coverage-report
# For Clover XML coverage report
vendor/bin/phpunit --coverage-clover coverage.xml
Additional Tips
- Bootstrap File: Ensure that the
bootstrap attribute points to your autoload.php file, usually located in the vendor directory.
- Test Suites: Make sure your
<testsuites> section correctly points to your test files.
By following these steps, you should no longer see the "No filter is configured, code coverage will not be processed" message, and PHPUnit will generate the code coverage report as expected.