问题
I have several clover.xml reports of different extensions of a projects. I want to combine them into one clover.xml and then create it into a clover html. But i see no way with the phpunit classes PHP_CodeCoverage
, PHP_CodeCoverage_Report_HTML
, PHP_CodeCoverage_Report_Clover
.
None of these classes accept an existing clover.xml. I thought I might be able to work with the methods append and merge of PHP_CodeCoverage
. But that does not accept files.
回答1:
If you are running jenkins or similar include a php script in your Ant build file to merge the files using SimpleXML
An example is here http://kuttler.eu/post/merging-and-splitting-xml-files-with-simplexml/
Then in your post build actions jenkins will use the clover.xml to generate your code coverage
回答2:
As jkrnak commented above you cannot simply merge the XML files as there are computed values such as lines covered etc.. that are computed at output time. You need to "merge" while still working with native PHP code. In my case I wanted to capture the coverage of a series of web service calls executed by newman. To do this I set a flag at the beginning of execution which persists across invocations (using a cache) and then also save the PHP_CodeCoverage object in the cache as well. My implementation (in Laravel) looks something like this:
if ( isset($_GET['initCoverage']) )
{
Cache::put( 'recordCoverage', true, 1440 );
}
if ( Cache::has('recordCoverage') )
{
if ( Cache::has('coverage') )
{
$coverage = Cache::get('coverage');
}
else
{
$filter = new PHP_CodeCoverage_Filter;
$filter->addDirectoryToBlacklist( base_path() . '/vendor' );
$coverage = new PHP_CodeCoverage( null, $filter );
}
$coverage->start( Request::method() . " " . Request::path() );
if ( isset($_GET['dumpCoverage']) )
{
if ( Cache::has('coverage') )
{
// Prevent timeout as writing coverage reports takes a long time
set_time_limit( 0 );
$coverage = Cache::get( 'coverage' );
$writer = new PHP_CodeCoverage_Report_Clover;
$writer->process($coverage, 'results/coverage/clover.xml');
}
Cache::forget('recordCoverage');
Cache::forget('coverage');
}
else
{
register_shutdown_function( function($coverage)
{
$coverage->stop();
Cache::put( 'coverage', $coverage, 1440);
}, $coverage);
}
}
This captures the series of tests in a single coverage object which is then output when I make a call with the "dumpCoverage" flag.
回答3:
Years later this issue is still partly unsolved. There is a project by SB that can merge clover files, but it requires php 5.6.
None of the answers above work sufficiently well. Here is a gist of a merge thrown together. Constructive critisism welcome.
Usage:
php clover-merge.php -o merged.xml -f clover-phpunit.xml -f clover-phpspec.xml
Posting it here for posterity too:
<?php
$options = getopt("f:o:");
if (! isset($options['f'])) {
echo "Files have to be specified with -f\n";
exit(1);
}
if (! isset($options['o'])) {
echo "Output has to be specified with -o\n";
exit(1);
}
$files = $options['f'];
if (! is_array($files)) {
$files = array($files);
}
$output = $options['o'];
$buffer = '';
foreach ($files as $file) {
if (! file_exists($file)) {
echo "File '$file' doesn't exist\n";
exit(2);
}
$report = simplexml_load_file($file);
$buffer .= $report->project->asXML();
}
$fh = fopen($output ,'w');
if (! $fh) {
echo "Cannot open '$output' for writing\n";
exit(2);
}
fwrite($fh, sprintf('<?xml version="1.0" encoding="UTF-8"?><coverage>%s</coverage>', $buffer));
fclose($fh);
来源:https://stackoverflow.com/questions/13175846/phpunit-merge-two-or-more-clover-xml-reports