Skip to content

Instantly share code, notes, and snippets.

@tertek
Last active August 18, 2022 09:22
Show Gist options
  • Save tertek/bd25073983870873363c0d4f64f6cf0e to your computer and use it in GitHub Desktop.
Save tertek/bd25073983870873363c0d4f64f6cf0e to your computer and use it in GitHub Desktop.
PHPUnit Code-Coverage with PCOV on Windows (Local Development Environment)

PHPUnit Code-Coverage with PCOV on Windows

Download and Install PCOV

  1. Download latest release (currently 1.0.11) for Windows for your version of PHP and the correct format (ts or nts) from PECL
  2. Unzip downloaded package and copy the files php_pcov.dll and php_pcov.pdb to your PHP extensions directory, e.g. C:\bin\php\php-7.4.27-Win32-vc15-x64\ext\
  3. Modify your php.ini so that PCOV is loaded. Therefore add extension=pcov:
..
extension=fileinfo
extension=gd2
extension=pcov
..
  1. Reload your Webserver, e.g. Apache or Nginx

Configure PHPUnit

Create a PHPUnit configuration file with the wizard by running:

 .\vendor\bin\phpunit --generate-configuration

Then open the file phpunit.xml and add directories to be excluded, otherwise the command will output too many errors:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         cacheResultFile=".phpunit.cache/test-results"
         executionOrder="depends,defects"
         forceCoversAnnotation="true"
         beStrictAboutCoversAnnotation="true"
         beStrictAboutOutputDuringTests="true"
         beStrictAboutTodoAnnotatedTests="true"
         convertDeprecationsToExceptions="true"
         failOnRisky="true"
         failOnWarning="true"
         verbose="true">
    <testsuites>
        <testsuite name="default">
            <directory>tests</directory>
        </testsuite>
    </testsuites>

    <coverage cacheDirectory=".phpunit.cache/code-coverage"
              processUncoveredFiles="true">
        <include>
            <directory suffix=".php">.</directory>
        </include>
        <exclude>
            <directory>./vendor</directory>
            <directory>./tests</directory>
        </exclude>
    </coverage>
</phpunit>

Install PHPUnit Code-Coverage Package

Add PHPUnit Code Coverage package by requiring with Composer:

composer require --dev phpunit/php-code-coverage

Use phpunit with code coverage

.\vendor\bin\phpunit tests --coverage-html ./html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment