Xdebug+Geshiでコードカバレッジ
http://d.hatena.ne.jp/bobchin/20050805 で試そうと思っていたことを試してみる。
環境は相変わらずWindowsで、PHP 4.4.0です。
Xdebugの設定
- http://xdebug.org/ の右下「BINARY SNAPSHOTS (2.0dev)」−「Modules for 4.4.x-dev:」−「Windows」から「xdebug-4.4dev-2.0dev.dll」を取得。
- PHPインストールディレクトリ下の「extensions」にコピー
- php.iniに以下を追加(PHPインストールディレクトリ = d:/php/)
zend_extension_ts="d:/php/extensions/xdebug-4.4dev-2.0dev.dll"
テスト環境
- Simpletestの取得。https://sourceforge.net/project/showfiles.php?group_id=76550&release_id=153280 から「simpletest」−「Download simpletest_1.0.tar.gz」を取得。
- GeSHiの取得。http://sourceforge.net/project/showfiles.php?group_id=114997 から「 GeSHi 1.0.7.1」−「Download GeSHi-1.0.7.1.zip」を取得
- ファイルレイアウト
テスト対象クラス
<?php class SampleObject { var $_str; function SampleObject($str = "") { $this->_str = $str; } function output() { print $this->_str . "\n"; } function setStr($str) { $this->_str = $str; } function getStr() { return $this->_str; } } ?>
テストクラス
<?php require_once('simpletest/unit_tester.php'); require_once('simpletest/reporter.php'); require_once 'SampleObject.php'; class SampleObjectTest extends UnitTestCase { var $_obj; function SampleObjectTest() { $this->UnitTestCase('SampleObjectTest'); } function setUp() { $this->_obj = new SampleObject("テスト"); } function tearDown() { unset($this->_obj); } function testOutput() { } function testSetStr() { } function testGetStr() { $result = $this->_obj->getStr(); $expect = "テスト"; $this->assertEqual($result, $expect); } } ?>
テスト実行クラス
<?php require_once 'SampleObjectTest.php'; require_once('geshi/geshi.php'); xdebug_start_code_coverage(); $test = new SampleObjectTest(); if (php_sapi_name() == "cli") { $reporter = new TextReporter(); } else { $reporter = new HtmlReporter(); } $test->run($reporter); $coverage = xdebug_get_code_coverage(); xdebug_stop_code_coverage(); $file_to_print = realpath('SampleObject.php'); $geshi = new GeSHi(file_get_contents($file_to_print), 'php', 'geshi/geshi'); $geshi->highlight_lines_extra(array_keys($coverage[$file_to_print])); echo $geshi->parse_code(); ?>