I am trying to measure test coverage with no test framework using phpdbg
My test test script looks like this:
$ cat hello.php
<?php
if (0) {
echo "bye\n";
} else {
echo "hello\n";
}
so it just prints hello like this:
$ php hello.php
hello
by the way, this is my php version
$ php --version
PHP 7.4.3 (cli) (built: Aug 13 2021 05:39:12) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies
I have tried to guess how to measure test coverage of this script using phpdbg, and this is what I have come up with
$ mkdir coverage
$ phpdbg -qrr hello.php --coverage-html coverage
hello
$ find coverage
coverage/
directory is empty
I had hoped to find a coverage report in there
Does anybody know what I have to do different?
My understanding of the way code coverage works in PHP is that the debugger (either
phpdbgorXdebug) generates a log of each line, andphpunitis able to use that to synthesise the output you'd expect (the HTML page with the coloured lines indicating how many times that particular line has been hit etc). In fact, the--coverage-htmlargument isn't forphpdbg, but forphpunit. This post shows an example of the full command line, a minimal version of which would be:but obviously this involves the use of
phpunitand having tests to run, which doesn't fit your use-case.I became curious about how it all worked, and put together this toy coverage tool which would work for the very simple script you have provided. The output is something like:
but if you take a look at
Profiler::render_coverageyou can see it would not be too tricky to adapt this into whatever output format you'd like.