2

我正在使用php-code-coverage从一个 HTTP 请求(通过 apache)收集覆盖率信息。我想存储来自多个请求的覆盖率数据,然后将结果整理成一份综合报告。

这很容易做到吗?我希望有一些类似的东西:

<?php
require 'PHP/CodeCoverage/Autoload.php';

$coverage = new PHP_CodeCoverage;
$coverage->start('<some request>');

// ...

$coverage->stop();

$writer = new PHP_CodeCoverage_Report_XXX;
$writer->process($coverage, '/tmp/reportYYY.xml');


// at some later date, collate all the results.
$writer = new PHP_CodeCoverage_Report_HTML;
$writer->process('/tmp/reportX.xml');
// ...
$writer->process('/tmp/reportZ.xml');
4

1 回答 1

2

如前所述,我已经设法使用phpcov(通过 composer 安装)运行它:

使用 Apache .htaccess 将 PHP_CodeCoverage 对象序列化到文件的前置/附加 php 脚本,您需要适当地调整路径:

.ht 访问:

# Prepend the file
php_value auto_prepend_file "prepend.php" 

# Append file to bottom of page
php_value auto_append_file "append.php" 

prepend.php:

<?php
require_once '../vendor/autoload.php';
$coverage = new PHP_CodeCoverage;
$coverage->start('Site coverage');

追加.php:

<?php

$coverage->stop();
$cov = serialize($coverage); //serialize object to disk
file_put_contents('../cov/site.' . date('U') . '.cov', $cov);

因为我正在序列化我必须编辑/vendor/phpunit/phpcov/src/MergeCommand.php 的对象:

protected function execute(InputInterface $input, OutputInterface $output)
{
    $mergedCoverage = new PHP_CodeCoverage;

    $finder = new FinderFacade(
        array($input->getArgument('directory')),
        array(),
        array('*.cov')
    );

    foreach ($finder->findFiles() as $file) {
        print "Merging $file" . PHP_EOL;
        //$_coverage = include($file);
        $_coverage = unserialize(file_get_contents($file));
        $mergedCoverage->merge($_coverage);
        unset($_coverage);
    }

    $this->handleReports($mergedCoverage, $input, $output);
}

然后使用 phpcov,创建报告:

./vendor/bin/phpcov merge --html="./cov/report/" ./cov -vvv
于 2014-03-12T22:46:02.457 回答