How to use PHP_CodeCoverage without PHPUnit?

For some reason, I would like to analyze my application code coverage using PHP_CodeCoverage, but not using the PHP module (with manual testing).

I know how to use XDebug in the long run (through several queries, while maintaining the analysis of code coverage in shared memory). Building simple XML files, such as clover.xml (or any other format) after some tests, is not difficult with the output of XDebug (it is a simple associative array and it is documented).

Is there a way to instruct PHP_CodeCoverage to use clover.xml (or another file format) to generate an HTML report (which is very good). Or you know a simple hack that will rely on the PHP_CodeCoverage internal function to work directly with the XDebug output to generate an HTML report.

+4
source share
1 answer

After some experimentation, this is what I developed.

PHP_CodeCoverage, . PHP_CodeCoverage . uniqid, . script ( , , , ):

require_once "phpcov/vendor/autoload.php";
$filter = new PHP_CodeCoverage_Filter();
$filter->addDirectoryToBlacklist(__DIR__ . "/phpcov");
$token = uniqid();
$coverage = new PHP_CodeCoverage(null, $filter);
$coverage->start($token);

//Do something

$coverage->stop();

$s = serialize($coverage);
file_put_contents('tmp/' . $token, $s);

, PHP_CodeCoverage_Report_HTML:

require_once "phpcov/vendor/autoload.php";
$coverage = new PHP_CodeCoverage();

$files = glob('tmp/*');
foreach($files as $file) {
    $s = file_get_contents($file);
    $data = unserialize($s);
    $coverage->merge($data);
}

$writer = new PHP_CodeCoverage_Report_HTML;
$writer->process($coverage, 'report-coverage');

PHP_CodeCoverage_Filter, , .

CodeIgniter, , .

+1

Source: https://habr.com/ru/post/1609432/


All Articles