MENU

TOP

Personal

Linux
CD-RW

ncftp

PHP
PHPLIB

PHPUNIT

PostgreSQL
PostgreSQL7.0

Oracle
Oracle8iR2

Windows

Java

XML

Network

資格

Link

 
 
 
 
 
 
PhpUnitチュートリアル
☆「eXtreame Programing」は、テストに始まり、テストに終わる開発方法だ。
そこで、テスト用のクラスライブラリが無いか、Web上を探し廻ってみたところ、
PHPUNITなるものが存在することがわかった、PHPUnitは、幾つかあるみたいだが、
以下のように取ってきたものをここでは解説します。
環境は以下のようになっています。

OS:RedHat7.1J
PHP:PHP-4.0.6(DSO)

1.PhpUnitのインストール
    適当なディレクトリ(ここでは、/usr/local/apacheとする)で以下のコマンドを実行。

  # cd /usr/local/apache
  #
cvs -d:pserver:anonymous@cvs.phpunit.sourceforge.net:/cvsroot/phpunit login
  (Logging in to anonymous@cvs.phpunit.sourceforge.net)
  CVS password:                          <- Enterを入力
  # cvs -z8 -d:pserver:anonymous@cvs.phpunit.sourceforge.net:/cvsroot/phpunit co phpunit
  # ls -F phpunit
  CVS/ phpunit.php phpunit_test.php

2.環境設定
    php.iniのinclude_pathに/usr/local/apache/phpunitを追加

  # vi /usr/local/lib/php.ini
  include_path = ".:/usr/local/apache/phpunit"     <-- コメントを外して、追加

3.テスト用のクラスを作成。
   (PHPBuilder.comで解説されていた、サンプルを使っています。)

  # mkdir /usr/local/apache/htdocs/mytest
  # cd /usr/local/apache/htdocs/mytest
  # vi Account.php

<?php
// PhpUnitサンプルクラス

class Account {

  var $balance;

  function Account($initialBalance=0){
    $this->balance = $initialBalance;       // コンストラクタ
  }

  function withdraw($amount){
    $this->balance -= $amount;     // $balance = $balance - $amountを計算
  }

  function deposit($amount){
    $this->balance += $amount;     // $balance = $balance + $amountを計算
  }

  function getBalance(){
    return $this->balance;            // $balanceを取得
  }

  function transferFrom(&$sourceAccount,$amount){
    $sourceAccount->withdraw($amount);
    $this->deposit($amount);
  }

}
?>

4.テスタークラスを作成。

  # vi AccountTester.php

<?php

require("phpunit.php");
require("Account.php");

class AccountTester extends TestCase{

  var $_ac1;
  var $_ac2;
  var $_ac3;
  var $_ac4;

  function AccountTester($name){
    $this->TestCase($name);             // phpunit.phpで定義されている親クラスを呼び出す
  }

  function setUp(){

    // data for testWithdraw
    $this->_ac1 = new Account(100);  // $_ac1->balance=100を代入

    // data for testDeposit
    $this->_ac2 = new Account(20);   // $_ac2->balance=20を代入

    // data for testTransferFrom
    $this->_ac3 = new Account(30);   // $_ac3->balance=30を代入
    $this->_ac4 = new Account(50);   // $_ac4->balacne=50を代入
  }

  // $_ac1オブジェクトのwithdraw()メソッドに25を渡す。
  // $_ac1の$balance初期値は100


  function testWithdraw(){
    $this->_ac1->withdraw(25);
    $this->assert($this->_ac1->getBalance() == 75);   // 計算結果 100-25 = 75で妥当性を検証
  }

  // $_ac2オブジェクトのdeposit()メソッドに10を渡す。
  // $_ac2の$balacne初期値は20

  function testDeposit(){
    $this->_ac2->deposit(10);
    $this->assertEquals(30,$this->_ac2->getBalance()); // 計算結果 20+10 = 30 で妥当性を検証
  }

  // Tranfers 10 units from _ac3 to _ac4
  // _ac3's initial balance is 30
  // _ac4's initial balance is 50


  function testTransferFrom(){
    $this->_ac4->transferFrom(&$this->_ac3,10);
    $this->assertEquals(20,$this->_ac3->getBalance(),"Source account balance incorrect"); // 30 - 10 = 20
    $this->assertEquals(60,$this->_ac4->getBalance(),"Target account balance incorrect"); // 50 + 10 = 60
  }
}
?>

5.テストを実行するPHPスクリプトを作成。

# vi runtest.php

<?php
require("AccountTester.php");

//phpunit.phpで定義されているTestSuiteクラスをインスタンス化
$tSuite = new TestSuite();

//単体テストを追加します。
$tSuite->addtest(new AccountTester("testWithdraw"));
$tSuite->addtest(new AccountTester("testDeposit"));
$tSuite->addtest(new AccountTester("testTransferFrom"));

//テスト結果クラスをインスタンス化
$res = new TextTestResult();

//テストを実行
$tSuite->run(&$res);

//テスト結果の表示
$res->report();

?>

6.runtest.phpを実行

AccountTester.phpの値を変えて、わざと間違えたりして、結果が変わるのを確認して下さい。
とりあえず、今回は簡単な例を付けてみました。
基本は、phpunit.phpを読む事です。

TOPに戻る


[PR]女性が輝く公文の先生募集中!:全国で教室開設説明会開催