phpのユニットテスト(PHPUnit)とデバッグ環境(xdebug)

最近PHPをまた使っている。PHPUnitのバージョンがかなりアップされていたので再度メモ。あと、エラー発生時にスタックトレースと変数の表示などをしてくれる xdebug も格段に簡単に入るようになっていたのでメモ。

  • インストール
# yum install php-pear
# pear channel-discover pear.phpunit.de; pear install phpunit/PHPUnit
# pecl install xdebug

/etc/php.d/xdebug.ini を以下のように作成

zend_extension=/usr/lib/php/modules/xdebug.so

HelloWorld.php を作成

<?php
class HelloWorld {
	public function hello($name) {
		return "hello $name";
	}
}
?>

ケルトンの生成

# phpunit --skeleton HelloWorld

HelloWorldTest.php が自動生成されるので、以下のように編集

	public function testHello() {
		$this->assertEquals("hello yoshifumi1975", $this->object->hello("yoshifumi1975"));
	}

ユニットテストの実行

# phpunit HelloWorld
or
# php HelloWorld.php