2012年12月8日土曜日

MacでPHPのunit testをやってみる (準備編)

動機


  • もう回帰テストを人力でやりたくありません。
  • リファクタリングのためには、テスト自動化。

自分Python使いなのになんでPHPなのよ?


人を増やすときに「Python使ったことないです。PHPならできます。」という状況で泣きをみたくないです。

環境構築

マシン

MacBookPro、OSはMountain Lion


PEARをインストール

コマンドを叩く
sudo php /usr/lib/php/install-pear-nozlib.phar
実行結果
No log handling enabled - using stderr logging
Created directory: /var/db/net-snmp
Created directory: /var/db/net-snmp/mib_indexes
[PEAR] Archive_Tar    - installed: 1.3.7
[PEAR] Console_Getopt - installed: 1.3.0
[PEAR] Structures_Graph- installed: 1.0.4
[PEAR] XML_Util       - installed: 1.2.1
[PEAR] PEAR           - installed: 1.9.4
Wrote PEAR system config file at: /private/etc/pear.conf
You may want to add: /usr/lib/php/pear to your php.ini include_path

参考 http://memorandum9.blogspot.jp/2012/11/macos-x-mountain-lionpear.html

PHP Unitをインストール

コマンドを叩く
pear config-set auto_discover 1
実行結果
config-set succeeded

もう一つ叩く
pear install pear.phpunit.de/PHPUnit
実行結果
Attempting to discover channel "pear.phpunit.de"...
downloading channel.xml ...
Starting to download channel.xml (804 bytes)
....done: 804 bytes
Auto-discovered channel "pear.phpunit.de", alias "pear.phpunit.de", adding to registry
unknown channel "pear.phpunit.de" in "pear.phpunit.de/PHPUnit"
invalid package name/package file "pear.phpunit.de/PHPUnit"
install failed
 エラーったので、sudoる

sudo pear install pear.phpunit.de/PHPUnit
実行結果
Attempting to discover channel "pear.phpunit.de"...
downloading channel.xml ...
Starting to download channel.xml (804 bytes)
....done: 804 bytes
Auto-discovered channel "pear.phpunit.de", alias "pear.phpunit.de", adding to registry
unknown channel "pear.phpunit.de" in "pear.phpunit.de/PHPUnit"
invalid package name/package file "pear.phpunit.de/PHPUnit"
install failed
watanabe-shouhito-no-MacBook-Pro:php_unit_test watanabemasato$ sudo pear install pear.phpunit.de/PHPUnit
Attempting to discover channel "pear.phpunit.de"...
downloading channel.xml ...
Starting to download channel.xml (804 bytes)
....done: 804 bytes
Auto-discovered channel "pear.phpunit.de", alias "phpunit", adding to registry
Attempting to discover channel "pear.symfony.com"...
downloading channel.xml ...
Starting to download channel.xml (811 bytes)
...done: 811 bytes
Auto-discovered channel "pear.symfony.com", alias "symfony2", adding to registry
Did not download optional dependencies: phpunit/PHP_Invoker, use --alldeps to download automatically
phpunit/PHPUnit can optionally use package "phpunit/PHP_Invoker" (version >= 1.1.0)
phpunit/PHP_CodeCoverage can optionally use PHP extension "xdebug" (version >= 2.0.5)
downloading PHPUnit-3.7.10.tgz ...
Starting to download PHPUnit-3.7.10.tgz (117,079 bytes)
...done: 117,079 bytes
downloading File_Iterator-1.3.3.tgz ...
Starting to download File_Iterator-1.3.3.tgz (5,152 bytes)
...done: 5,152 bytes
downloading Text_Template-1.1.4.tgz ...
Starting to download Text_Template-1.1.4.tgz (3,701 bytes)
...done: 3,701 bytes
downloading PHP_CodeCoverage-1.2.7.tgz ...
Starting to download PHP_CodeCoverage-1.2.7.tgz (157,806 bytes)
...done: 157,806 bytes
downloading PHP_Timer-1.0.4.tgz ...
Starting to download PHP_Timer-1.0.4.tgz (3,694 bytes)
...done: 3,694 bytes
downloading PHPUnit_MockObject-1.2.2.tgz ...
Starting to download PHPUnit_MockObject-1.2.2.tgz (20,347 bytes)
...done: 20,347 bytes
downloading Yaml-2.1.4.tgz ...
Starting to download Yaml-2.1.4.tgz (38,574 bytes)
...done: 38,574 bytes
downloading PHP_TokenStream-1.1.5.tgz ...
Starting to download PHP_TokenStream-1.1.5.tgz (9,859 bytes)
...done: 9,859 bytes
install ok: channel://pear.phpunit.de/File_Iterator-1.3.3
install ok: channel://pear.phpunit.de/Text_Template-1.1.4
install ok: channel://pear.phpunit.de/PHP_Timer-1.0.4
install ok: channel://pear.symfony.com/Yaml-2.1.4
install ok: channel://pear.phpunit.de/PHP_TokenStream-1.1.5
install ok: channel://pear.phpunit.de/PHP_CodeCoverage-1.2.7
install ok: channel://pear.phpunit.de/PHPUnit_MockObject-1.2.2
install ok: channel://pear.phpunit.de/PHPUnit-3.7.10
うまくいっているらしい。コマンド叩いて確認
phpunit --version
実行結果
 PHPUnit 3.7.10 by Sebastian Bergmann.


最初のテスト

テストコードを書く
vim sample_test_case.php
動くかどうかを知りたいので、中身は公式からコピペ
<?php
class StackTest extends PHPUnit_Framework_TestCase
{
    public function testPushAndPop()
    {
        $stack = array();
        $this->assertEquals(0, count($stack));
 
        array_push($stack, 'foo');
        $this->assertEquals('foo', $stack[count($stack)-1]);
        $this->assertEquals(1, count($stack));
 
        $this->assertEquals('foo', array_pop($stack));
        $this->assertEquals(0, count($stack));
    }
}
?> 
参考 http://www.phpunit.de/manual/current/ja/writing-tests-for-phpunit.html

実行する
phpunit sample_test_case.php
結果
PHPUnit 3.7.10 by Sebastian Bergmann.

.

Time: 0 seconds, Memory: 4.50Mb

OK (1 test, 5 assertions)
大丈夫らしい。










0 件のコメント:

コメントを投稿