2014-09-19 85 views
0

我想使用xdebug來計算超薄應用程序的代碼覆蓋率。結果似乎是錯誤的,因爲它告訴我,路由處理程序中的所有代碼不執行(和我做了一些需求...)超薄框架和代碼覆蓋率

<?php 
require 'vendor/autoload.php'; 
$app = new \Slim\Slim(); 
xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE); 
$app->get('/data', function() use ($app) { 
    echo "data"; 
}); 
$app->get('/STOP', function() use ($app) { 
    $data = xdebug_get_code_coverage(); 
    var_dump($data); 
}); 
$app->run(); 

我使用運行在服務器:

php -S localhost:8080 -t . test.php 

然後執行兩個請求:

curl http://localhost:8080/server.php/data 
curl http://localhost:8080/server.php/STOP > result.html 

result.html覆蓋率輸出告訴我:

'.../test.php' => 
array (size=11) 
    0 => int 1 
    5 => int 1 
    6 => int -1 
    7 => int 1 
    8 => int 1 
    9 => int 1 
    10 => int -1 
    11 => int 1 
    12 => int 1 
    473 => int 1 
    1267 => int 1 

第6行應爲int 1,因爲它已執行。我錯過了什麼?

回答

0

問題很明顯,輸出中顯示的覆蓋範圍僅包含第二個請求,因爲整個PHP腳本都在每個請求中運行。

簡單的解決方案包括使用兩個其他答案:enter link description hereenter link description here以使用php代碼覆蓋https://github.com/sebastianbergmann/php-code-coverage生成覆蓋率報告,然後將所有報告合併到外部腳本中。

現在服務器如下是:

<?php 
require 'vendor/autoload.php'; 
$app = new \Slim\Slim(); 
// https://stackoverflow.com/questions/19821082/collate-several-xdebug-coverage-results-into-one-report 
$coverage = new PHP_CodeCoverage; 
$coverage->start('Site coverage'); 
function shutdown() { 
    global $coverage; 
    $coverage->stop(); 
    $cov = serialize($coverage); //serialize object to disk 
    file_put_contents('coverage/data.' . date('U') . '.cov', $cov); 
} 
register_shutdown_function('shutdown'); 
$app->get('/data', function() use ($app) { 
    echo "data"; 
}); 
$app->run(); 

和合並腳本是:

#!/usr/bin/env php 
<?php 
// https://stackoverflow.com/questions/10167775/aggregating-code-coverage-from-several-executions-of-phpunit 
require 'vendor/autoload.php'; 
$coverage = new PHP_CodeCoverage; 
$blacklist = array(); 
exec("find vendor -name '*'", $blacklist); 
$coverage->filter()->addFilesToBlacklist($blacklist); 
foreach(glob('coverage/*.cov') as $filename) { 
    $cov = unserialize(file_get_contents($filename)); 
    $coverage->merge($cov); 
} 
print "\nGenerating code coverage report in HTML format ..."; 
$writer = new PHP_CodeCoverage_Report_HTML(35, 70); 
$writer->process($coverage, 'coverage'); 
print " done\n"; 
print "See coverage/index.html\n"; 

合併腳本也使一切都在vendor入黑名單。