LoginSignup
1
0

More than 5 years have passed since last update.

Apache上で動いたPhpのcode coverageをPhpStormで見る方法

Posted at

前提条件

  • windows10で動かして
  • apacheの設定は特にいらない
  • xdebugが動くこと
  • ローカルで動かす事
  • php-7.2.9-Win32-VC15-x64
  • Apache 2.4.34 win64 VC15(スレッドセーフ)

準備

composer require phpunit/php-code-coverage

で落としてくる。

以下のサンプルはPHP_CodeCoverage 2.2なのでクラス名とか違うので最新のREADME.mdをみてちょと書き直せばいい。

githubのサンプルで示すような感じでindex.phpから動かすようにする

$coverage = new PHP_CodeCoverage();

$coverage->start('<name of test>');

CodeCoverageXX::$coverage=$coverage;


function shutdown()
{
    $coverage = CodeCoverageXX::$coverage;
    $coverage->stop();

    $writer = new PHP_CodeCoverage_Report_Clover;
    $writer->process($coverage, 'D:\link\var\coverage\\'.date('Ymd_His').'_'.getmypid(). '_Apach.xml');

}

register_shutdown_function('shutdown');

/** shutdownにわたすため **/
class CodeCoverageXX
{
    /* @var PHP_CodeCoverage */
    public static $coverage;
}


このコードを常時動かすと遅いのでon/offができるようにしておくこと

Apacheで実行後に

read01.jpg

Show Code Coverage Dataで取得する

read02.png

+で追加してチェックボックスをつけて表示する。
たくさんのカバレッジをこれで表示するのは大変なので以下のように書き換える
シリアライズして保存するコード。

$coverage = new PHP_CodeCoverage();

$coverage->start('<name of test>');

CodeCoverageXX::$coverage=$coverage;
function shutdown()
{
    $coverage = CodeCoverageXX::$coverage;
    $coverage->stop();

    file_put_contents('D:\link\var\coverage\\'.date('Ymd_His').'_'.getmypid().'_Apach.txt', serialize($coverage));

}

register_shutdown_function('shutdown');

複数のシリアライスされたオブジェクトが保存されるので

以下のコードでマージする

<?php


require __DIR__.'/../../bootstrap/autoload.php';
$list = glob('D:/link/var/coverage/*.txt');

/* @var $coverage PHP_CodeCoverage */
$coverage = null;
foreach ($list as $filePath) {
    echo $filePath . PHP_EOL;
    $c = unserialize(file_get_contents($filePath));
    if ($coverage === null) {
        $coverage = $c;
    } else {
        $coverage->merge($c);
    }
}

if ($coverage === null) {
    $coverage = new PHP_CodeCoverage();
}

$writer = new PHP_CodeCoverage_Report_Clover;
$writer->process($coverage, 'D:\link\var\coverage\\'.date('Ymd_His').'_'.getmypid().'_Apach.xml');

foreach ($list as $filePath) {
    unlink($filePath);
    echo $filePath .'.....unlink'. PHP_EOL;
}

実行後Show Code Coverage Dataで取得すれば複数のイベントをまとめて表示できる。

WebDriverでテストを実行してPhpStormで見ることができる!
こりゃ便利(´,,・ω・,,`)

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0