PHP代碼覆蓋率

一  安裝php環境php

二 統計php代碼覆蓋率html

1 須要安裝xdebugpython

安裝步驟:linux

http://www.jb51.net/article/116419.htmnginx

測試環境c++

  • LNMP 軍哥一鍵包1.3版本
  • PHP 7.0.7
  • Xdebug 2.6

配置步驟git

 

1
2
3
4
5
6
7
8
git clone git: //github .com /xdebug/xdebug .git
cd xdebug
find / -name phpize
/usr/bin/phpize
find / -name php-config
. /configure -- enable -xdebug --with-php-config= /usr/local/php/bin/php-config
make
make install

 

開啓擴展github

?
1
2
3
4
find / -name php.ini
vi /usr/local/php/etc/php .ini
添加 extension=xdebug.so

[Xdebug]
xdebug.collect_params=on
xdebug.collect_return=on
xdebug.remote_autostart=onapache

service restart php-fpm

成功驗證:①在linux輸入php -version,以下:json

② 訪問index.php(phpinfo())

 

2 安裝composer

curl -sS https://getcomposer.org/installer | php
php composer.phar --version Composer version 1.6.5 2018-05-04 11:44:59

3 安裝phpcov 和 phpunit

此處選了phpunit 6.5.0 和phpcov 4.0.8,編輯composer.json文件(phpcov是根據phpunit自動匹配的,php和phpunit對應關係可百度或看底部)

#composer.json
{
    "name": "root/php-code-coverage",
    "require-dev": {
        "phpunit/phpunit":"6.5.0",
        "phpunit/phpcov": "*" 
}

執行命令安裝   php composer.phar install

安裝完成後校驗 以下便可(phpunit和phpcov必定要在這個目錄下使用)

vendor/bin
[root@mt-jry-01 bin]# ll
lrwxrwxrwx 1 root root      24 Jul 13 10:22 phpcov -> ../phpunit/phpcov/phpcov
lrwxrwxrwx 1 root root      26 Jul 13 10:21 phpunit -> ../phpunit/phpunit/phpunit
[root@mt-jry-01 bin]# .vendor/bin/phpunit --version
PHPUnit 6.5.0 by Sebastian Bergmann and contributors.

[root@mt-jry-01 bin]# .vendor/bin/phpcov --version
phpcov 4.0.5 by Sebastian Bergmann.

  

4 編寫測試代碼

#userinfo.php
<?php
include_once("*****/prepend.php");  
$id = $_POST["user_id"];
if ($id != 10086){
    exit();
}
$userinfo = array(
    'username'=>'jason',
    'password'=>'123456',
);
$result = array(
          'code'=>10000,
          'message'=>"success",
          'data'=>$userinfo,
        );
echo json_encode($result);

  

#prepend.php
<?php
require_once dirname(__FILE__).'/vendor/autoload.php';  # 在composer生成的vender同級目錄
use SebastianBergmann\CodeCoverage\CodeCoverage;
$coverage = new CodeCoverage;

$coverage->filter()->addDirectoryToWhitelist('/var/www/html/userinfo.php');  # 白名單

$coverage->start('<Site coverage>');#開始統計
register_shutdown_function('__coverage_stop',$coverage);#註冊關閉方法
 
function __coverage_stop(CodeCoverage $coverage){
  $coverage->stop();#中止統計
  $cov = '<?php return unserialize(' . var_export(serialize($coverage), true) . ');';#獲取覆蓋結果,注意使用了反序列化
  //echo $cov;
  file_put_contents(dirname(__FILE__).'/cov/site.' . date('U') .'.'.uniqid(). '.cov', $cov);#將結果寫入到文件中
}

若多個域名或者接口請求要在同一個prepend文件裏分別統計,在新建$coverage前加if條件便可,如
if(strpos($_SERVER['HTTP_HOST'],'www.baidu.com') === true){}

  

5  測試

執行命令

[root@mt-jry-01 html]# curl -d "user_id=10086" "127.0.0.1/userinfo.php"
{"code":10000,"message":"success","data":{"username":"jason","password":"123456"}}

查看prepend.php統計目錄cov下

-rw-r--r-- 1 apache apache   4609 Jul 13 14:45 site.1531464305.5b484a71c0a1c.cov

生成xml或者html報告命令以下:

./vendor/bin/phpcov merge --clover cov/coverage.xml cov/ -vvv   # 在cov目錄下生成xml報告
./vendor/bin/phpcov merge --html="cov/coverage_html" cov/ -vvv  # 在cov目錄下生成html報告 

6 查看報告結果

8 工程配置

在實際項目中有三種配置方式

  1. 在php.ini中引入prepend文件:auto_prepend_file = /***/prepend.php (配置後重啓php) --- 全部php請求均會預加載該文件,文件有錯誤時影響整個php服務
  2. 在文件入口文件中引入prepend文件:include_once(/www/***/prepend.conf); (通常爲index.php) --- 效果同3,從新部署清掉配置
  3. 在nginx.conf中引入prepend文件 --- 對於該域名的請求會加載該文件(配置後重啓nginx)

 

location ~ .*\.php?$
        {   
                fastcgi_pass  127.0.0.1:9200;
                fastcgi_index index.php;
                include common/fastcgi.conf;
                fastcgi_param MY_ENV pre;
                fastcgi_param PHP_VALUE 'auto_prepend_file=/www/data/phpcoverage_wallet/prepend.php';
        }

  

7 問題:

① 開始使用的phpcov 2.0.2 & phpunit 4.8.7 生成的報告數據全爲0 - phpunit4 不能支持 php7,對應版本見⑦

② 開始老是報錯PHP Fatal error:  Uncaught Error: Class 'SebastianBergmann\CodeCoverage\CodeCoverage' not found in

     是由於沒有引用vender目錄,在prepend.php里加一句require_once dirname(__FILE__).'/vendor/autoload.php';  便可

③ 配置nginx

④ 請求域名沒有生成site文件:請求權限不夠,不能在對應目錄下寫文件

     chmod 777 -R 域名請求是apache權限,若是與cov文件夾權限不一致則不可寫入

 ⑤ 生成覆蓋率文件有要統計的代碼文件,可是命中狀況count全爲0,有兩種可能

      A xdebug的collect_param 與collect_return沒打開,致使未收集到數據,須要在php.ini裏配置

      B 若是在php.ini裏配置了auto_prepend_file=‘**/prepend.php’,則只有用指定目錄下的prepend.php文件才能生成覆蓋率數據,不然覆蓋行全爲0

 ⑥ 將xml報告集成到jenkins

      注意:go 和 c++ 的xml報告能夠用 Cobertura 統計到jenkins展現,php 的要用Clover PHP 插件統計,phpcov生成的xml格式Cobertura解析不了會報錯  

 ⑦ php 和 phpunit 的對應關係 https://phpunit.de/supported-versions.html

      

 

 

 

https://github.com/sebastianbergmann/phpcov

相關文章
相關標籤/搜索