# 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](https://pecl.php.net/package/pcov/1.0.11/windows)
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
..
```
4. 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
<?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
```