DEV Community

vuong ⛈️
vuong ⛈️

Posted on • Updated on

Working with PHPUnit in VSCode

This post is listed points that I've worked to make code coverage works for my PHP (Symfony projects). Mostly for reading again in my future but also able to share to everyone.

The VSCode extensions

Name: PHPUnit
Id: emallin.phpunit
Description: Run PHPUnit tests from VSCode.
Version: 4.1.1
Publisher: Elon Mallin
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=emallin.phpunit

Name: Coverage Gutters
Id: ryanluker.vscode-coverage-gutters
Description: Display test coverage generated by lcov or xml - works with many languages
Version: 2.10.4
Publisher: ryanluker
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ryanluker.vscode-coverage-gutters

It helps to quickly run test with shortcut, you can have a preset of settings like every setting you could have CLI command or phpunit.xml.dist.

PHPUnit & Coverage Gutters extension's setting

{
    "phpunit.args": [
        "--configuration",
        "${workspaceFolder}/www/phpunit.xml.dist",
        "--coverage-clover",
        "var/coverage/cov.xml",
    ],
    "coverage-gutters.showLineCoverage": true,
    // Would be very helpful to open Output > coverage-gutters to debug the path in every time we re-run the unit tests
    "coverage-gutters.coverageBaseDir": "var/coverage", // equivalent to ${workspaceFolder}/var/coverage
}
Enter fullscreen mode Exit fullscreen mode

To run the unit tests

Press Cmd + Shift + P to use one of these:

Image description

After the unit completed, Cmd + Shift + P again to use one of these to try with the coverage gutters:

Image description

Dealing with PHPUnit mock types

https://github.com/sebastianbergmann/phpunit/blob/1f5e1334bb5de60471251a2392a9cc8d43bc4174/.phpstorm.meta.php

Copy this file to any child folder of the project, better to any ignored folder. Then you can automatically solve the invalid type of VSCode. Which happened when you create mock from a class, and VSCode doesn't know how to detect it as original class or MockObject class, normally it will show as error if you don't do this step.

The code can't get coverage

Here are the check list for solving the problem of not showing code coverage after running test.

This post are also related to XDebug https://dev.to/arielmejiadev/set-xdebug-coverage-mode-2d9g

https://xdebug.org/docs/all_settings#mode

Top comments (0)