1

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?

Alex028502
  • 3,486
  • 2
  • 23
  • 50

1 Answers1

2

My understanding of the way code coverage works in PHP is that the debugger (either phpdbg or Xdebug) generates a log of each line, and phpunit is 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-html argument isn't for phpdbg, but for phpunit. This post shows an example of the full command line, a minimal version of which would be:

phpdbg \
  -qrr vendor/phpunit/phpunit/phpunit \
  -c phpunit.xml \
  --coverage-html coverage

but obviously this involves the use of phpunit and 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:

 | <?php
 | 
1| if (0) {
0|     echo "bye\n";
 | } else {
1|     echo "hello\n";
 | }

but if you take a look at Profiler::render_coverage you can see it would not be too tricky to adapt this into whatever output format you'd like.

msbit
  • 4,152
  • 2
  • 9
  • 22
  • wow thanks! I was hoping for a different answer (missing argument) (because I thought it would work more like `nyc` or the python one) but this is great too! – Alex028502 Nov 04 '21 at 07:32
  • You can also take a look at the output of `phpdbg -qrr -v` or `-O logfile` which will output the lines, opcodes and filenames. Won't output the PHP source though. – msbit Nov 04 '21 at 07:43