我试图让 PCOV 与在带有 XAMPP 的 Windows 机器上的 PHP 7.4.13 上运行的 PHP 应用程序 (Drupal) 一起工作。
我从https://windows.php.net/downloads/pecl/releases/pcov/1.0.9/php_pcov-1.0.9-7.4-ts-vc15-x64.zip安装了 PCOV 1.0.9 线程安全版本并复制过来php_pcov.dll 到 xampp 下的 php/ext 文件夹,并在 php.ini 中启用它。我在 php.ini 中有以下配置
extension=pcov
pcov.enabled = 1
pcov.directory = "<full_local_path_to_web>\web\modules\custom"
我的 index.php 中有以下代码
use SebastianBergmann\CodeCoverage\Filter;
use SebastianBergmann\CodeCoverage\Driver\Selector;
use SebastianBergmann\CodeCoverage\CodeCoverage;
use SebastianBergmann\CodeCoverage\Driver\PhpdbgDriver;
use SebastianBergmann\CodeCoverage\Report\Html\Facade as HtmlReport;
$autoloader = require_once 'autoload.php';
$current_dir = __DIR__;
$filter = new Filter;
$filter->includeDirectory('./modules/custom/*');
$selector = new Selector();
$coverage = new CodeCoverage(
$selector->forLineCoverage($filter),
$filter
);
$test_name = preg_replace("/[^A-Za-z0-9]/", '-', $_SERVER['REQUEST_URI']);
$coverage->start($test_name);
function end_coverage()
{
global $test_name;
global $coverage;
global $filter;
global $current_dir;
$coverageName = $current_dir . '/sites/default/files/private/code_coverage/coverage-' . $test_name . '-' . microtime(true);
try
{
$coverage->stop();
$writer = new \SebastianBergmann\CodeCoverage\Report\Html\Facade;
$writer->process($coverage, $current_dir . '/report/');
$writer = new SebastianBergmann\CodeCoverage\Report\PHP();
$coverageName = $current_dir . '/sites/default/files/private/code_coverage/coverage-'. $test_name . '-' . microtime(true);
$writer->process($coverage, $coverageName . '.php');
}
catch (Exception $ex)
{
file_put_contents($coverageName . '.ex', $ex);
}
}
register_shutdown_function('end_coverage');
我看到外观 php 文件是在相应的文件夹中创建的,但是当我尝试使用以下代码创建组合报告时,我看到 0 代码覆盖率,当我查看源视图时,所有行都显示为未执行。
<?php
include_once("autoload.php");
$coverages = glob("sites/default/files/private/code_coverage/*.php");
use SebastianBergmann\CodeCoverage\Filter;
use SebastianBergmann\CodeCoverage\Driver\Selector;
use SebastianBergmann\CodeCoverage\CodeCoverage;
use SebastianBergmann\CodeCoverage\Driver\PhpdbgDriver;
use SebastianBergmann\CodeCoverage\Report\Html\Facade as HtmlReport;
#increase the memory in multiples of 128M in case of memory error
ini_set('memory_limit', '1800M');
$filter = new Filter;
$filter->includeDirectory('./modules/custom/*');
$selector = new Selector();
$coverage = new CodeCoverage(
$selector->forLineCoverage($filter),
$filter
);
$final_coverage = $coverage;
$count = count($coverages);
$i = 0;
foreach ($coverages as $coverage_file)
{
$i++;
echo "Processing coverage ($i/$count) from $coverage_file<br>". PHP_EOL;
require_once($coverage_file);
$final_coverage->merge($coverage);
}
echo "Generating final report...<br>" . PHP_EOL;
$report = new \SebastianBergmann\CodeCoverage\Report\Html\Facade;
$report->process($final_coverage,"sites/default/files/private/code_coverage_reports");
echo "Report generated succesfully<br>". PHP_EOL;
为什么 PCOV 报告 0 代码覆盖率?PCOV 工作正常吗?还是我必须做更多的事情来启用手动测试的代码覆盖率测试?提前感谢您的投入。
该方法基于这篇文章 - https://tarunlalwani.com/post/php-code-coverage-web-selenium/