2004ǯ11��18��

PHPUnit�λȤ���

PHPUnit�ϡ����֥������ȤΥƥ��Ȥˤ����Ȥ���Τ��ȻפäƤ��ޤ�������ñ��ʴؿ���ñ�Υƥ��ȤǤ�ͭ���ʤ�Ǥ��͡��ʥɥ�����ȤΥ���ץ�ϡ��ؿ��Υƥ��Ȥ��ܤäƤޤ�����

�Ȥ����櫓�ǡ�PHPUnit��ñ��ʻȤ�����Ҳ𤷤ޤ���

PHPUnit�λȤ����򻲹ͤˤ��ޤ������ʤȤ��������Τޤ�ޤǤ������ߤޤ��󡣤Ȥ����������꤬�Ȥ��������ޤ�����


��1. ����ƥ��Ȥ��뤫��
������ץȤ˥������������桼���Υ�����ե�����˵�Ͽ����ؿ���ƥ��Ȥ��뤳�Ȥˤ��ޤ���

bool loginLogWriter(string user)

user������ˤȤꡢ�ե�����ˡ�user����������������񤭹��ߤޤ���
user���������������ϡ�1��Υ����������դ���1�Ԥ�ե�����κǸ�˥��ֶ��ڤ���ɵ����ޤ���
�񤭹��ߤ�����˽�λ�������ˡ�true���֤��ޤ������Ԥ������ϡ�false���֤��ޤ���


����ƥ��Ȥ��뤫��ͤ��Ƥ����Ȥ��꤬����ޤ��󤬡�
���ֶ��ڤꡢ�ե��������Ȥ������Ȥǡ����ι��ܤ�ƥ��Ȥ��뤳�Ȥˤ��ޤ���


  1. �������Ϥ��줿user����������Ͽ����Ƥ��뤳�ȡ�

  2. user�˥��֤��ޤޤ�Ƥ��Ƥ���������Ͽ����뤳�ȡ�

  3. �񤭹���ե����뤬¸�ߤ��ʤ�����false���֤����ȡ�

��2. �ƥ��ȤΥ�����ȥ�κ���

�ޤ����Ϥ�ˡʥƥ��Ȥ���ؿ��������ˡˡ�������ȥ����ޤ���

test.php

<?php
require_once 'PHPUnit/GUI/HTML.php';
// Test Suites
require_once 'test/loginLogWriterTest.php'; //���ƥ��Ȥ��񤫤줿������ץ�

// run Test Suites
$suite = new PHPUnit_TestSuite("loginLogWriterTest"); //���ƥ��Ȥ��륯�饹̾(1)
$result = new PHPUnit_GUI_HTML($suite);
$result->show();
?>


test/loginLogWriterTest.php

<?php
require_once 'PHPUnit.php';
require 'loginLogWriter.php'; //���ƥ��Ȥ��륹����ץȤ򥤥󥯥롼��

class loginLogWriterTest extends PHPUnit_TestCase { //��(1)�Υƥ��Ȥ��륯�饹̾
    function loginLogWriterTest($name) //�����󥹥ȥ饯��
    {
        $this->PHPUnit_TestCase($name);
    }

    function setUp()
    {
    }

    function tearDown()
    {
    }

    function testSetup() //��test�ǻϤޤ�᥽�åɤ��ƥ��ȤȤ��Ƽ¹Ԥ���ޤ���
    {
        $this->fail("no implementation"); //��assert�Υ᥽�åɤˤĤ��Ƥϡ�PHPUnit_Assert�ò»²¾ï¿½
    }
}
?>

loginLogWriter.php

<?php
?>

�Ȥꤢ�����ե�����������ޤ���

�����ޤǤǡ��Ȥꤢ������test.php��¹Ԥ��Ƥߤޤ���
(un)check all �˥����å���Ĥ��ơ�run tests�򥯥�å����ޤ���

fail�᥽�åɤǡ�����Ū�˼��Ԥ����Ƥ��ޤ��Τǡ����Ԥ��Ƥ���С������ˤʤäƤ���С������Ǥ���


��3. �ƥ��Ȥκ���
�ؿ�������������ˡ��ޤ����ƥ��Ȥ�������ޤ���
��1. �������Ϥ��줿user����������Ͽ����Ƥ��뤳�ȡ��פ�����ޤ���


test/loginLogWriterTest.php

<?php
require_once 'PHPUnit.php';
require 'loginLogWriter.php';

class loginLogWriterTest extends PHPUnit_TestCase {
    var $logFile;
    function loginLogWriterTest($name)
    {
        $this->PHPUnit_TestCase($name);
    }

    function setUp() //���ƥ��Ȥ����˸ƤӽФ���ޤ�
    {
        $this->logFile = "/tmp/test_log.log";
        if ( !file_exists($this->logFile) ) {
            touch($this->logFile); //��������ñ¤­½Ð¤ï¿½ï¿½Õ¥ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Æ¤ï¿½ï¿½Þ¤ï¿½
        }
    }

    function tearDown() //���ƥ��Ȥν�λ��˸ƤӽФ���ޤ�
    {
        unlink($this->logFile); //�����������ե�����������Ƥ��ޤ�
    }

    function testWriteLog() //��test�ǻϤޤ�᥽�åɤ��ƥ��ȤȤʤ�ޤ�
    {
        $user = "hoge";
        $regexp = "|^".date("Y/m/d")."\t".date("H:i:").".*".$user."\t\n$|";
        loginLogWriter($user);
        $out = file($this->logFile); //���ե���������Ƥ�����˼��Ф��ޤ�
        $this->assertRegExp($regexp, $out[count($out)-1]); //������ɽ����Ȥäƽ��Ϸ�̤�����å����ޤ�
    }
}
?>

�����ޤǤǡ��ƥ��Ȥ�¹Ԥ��ޤ���

�ޤ���loginLogWriter()��������Ƥ��ʤ��Τǡ����顼�ˤʤ�ޤ���


��4. �ؿ��κ���

�ޤ����ؿ�����������򵭽Ҥ��ޤ���

loginLogWriter.php

<?php
function loginLogWriter($user){
}
?>

�ƥ��Ȥ�¹Ԥ��ޤ���

�ޤ���������񤤤Ƥ��ʤ��Τǡ����顼�ˤʤ�ޤ�����
�ƥ��ȥ�����ץȤΥ��顼��ФƤ��ޤ��������ˤ��ʤ��Ǥ����ޤ��礦��

���ơ����褤�衢�ºݤν�������ޤ���

loginLogWriter.php

<?php
function loginLogWriter($user){
    $logFile = "/tmp/test_log.log";
    $logData = array(date("Y/m/d"), date("H:i:s"), $user, "\n");
    $logData = implode("\t", $logData); //�����ڤ�ϥ��֤Ȥ��ޤ�
    $fp = fopen($logFile,"a");
    flock($fp,2);
    fwrite($fp, $logData);
    fclose($fp);
}
?>

�ƥ��Ȥ�¹Ԥ��ޤ���


��5. ���Υƥ��ȤǤ���
��2. user�˥��֤��ޤޤ�Ƥ��Ƥ���������Ͽ����뤳�ȡ���

Ʊ�ͤˡ��ƥ��Ȥ���ޤ���

test/loginLogWriterTest.php

<?php
require_once 'PHPUnit.php';
require 'loginLogWriter.php';

class loginLogWriterTest extends PHPUnit_TestCase {
    var $logFile;
    function loginLogWriterTest($name)
    {
        $this->PHPUnit_TestCase($name);
    }

    function setUp()
    {
        $this->logFile = "/tmp/test_log.log";
        if ( !file_exists($this->logFile) ) {
            touch($this->logFile);
        }
    }

    function tearDown()
    {
        unlink($this->logFile);
    }

    function testWriteLog()
    {
        $user = "hoge";
        $regexp = "|^".date("Y/m/d")."\t".date("H:i:").".*".$user."\t\n$|";
        loginLogWriter($user);
        $out = file($this->logFile);
        $this->assertRegExp($regexp, $out[count($out)-1]);
    }

    function testReplaceTab() //���ɲä����ƥ��ȤǤ�
    {
        $user = "hoge\thoge";
        $repUser = str_replace("\t", "  ", $user); //�����֤����2�Ĥ��ִ����ޤ�
        $regexp = "|^".date("Y/m/d")."\t".date("H:i:").".*".$repUser."\t\n$|";
        loginLogWriter($user);
        $out = file($this->logFile);
        $this->assertRegExp($regexp, $out[count($out)-1]);
    }
}
?>

�ƥ��Ȥ�¹Ԥ��ޤ���

���顼�ˤʤ�ޤ���


���ˡ��ؿ��ν����������ޤ���

loginLogWriter.php

<?php
function loginLogWriter($user){
    $logFile = "/tmp/test_log.log";
    $user = str_replace("\t", "  ", $user); //���ɲä��ޤ���
    $logData = array(date("Y/m/d"), date("H:i:s"), $user, "\n");
    $logData = implode("\t", $logData);
    $fp = fopen($logFile,"a");
    flock($fp,2);
    fwrite($fp, $logData);
    fclose($fp);
}
?>

�ƥ��Ȥ��ޤ���

�п��ΥС��ˤʤ�С��ƥ��ȴ�λ�Ǥ���

��6. �Ǹ�Υƥ��ȤǤ�
��3. �񤭹���ե����뤬¸�ߤ��ʤ�����false���֤����ȡ���

Ʊ�����Ǥ���
�ƥ��Ȥ���ޤ���

test/loginLogWriterTest.php

<?php
require_once 'PHPUnit.php';
require 'loginLogWriter.php';

class loginLogWriterTest extends PHPUnit_TestCase {
    var $logFile;
    function loginLogWriterTest($name)
    {
        $this->PHPUnit_TestCase($name);
    }

    function setUp()
    {
        $this->logFile = "/tmp/test_log.log";
        if ( !file_exists($this->logFile) ) {
            touch($this->logFile);
        }
    }

    function tearDown()
    {
        unlink($this->logFile);
    }

    function testWriteLog()
    {
        $user = "hoge";
        $regexp = "|^".date("Y/m/d")."\t".date("H:i:").".*".$user."\t\n$|";
        loginLogWriter($user);
        $out = file($this->logFile);
        $this->assertRegExp($regexp, $out[count($out)-1]);
    }

    function testReplaceTab()
    {
        $user = "hoge\thoge";
        $repUser = str_replace("\t", " ", $user);
        $regexp = "|^".date("Y/m/d")."\t".date("H:i:").".*".$repUser."\t\n$|";
        loginLogWriter($user);
        $out = file($this->logFile);
        $this->assertRegExp($regexp, $out[count($out)-1]);
    }

    function testFileExists() //���ɲä��ޤ���
    {
        unlink($this->logFile);
        $user = "hoge";
        $this->assertFalse(loginLogWriter($user));
    }
}
?>

�ƥ��Ȥ�¹Ԥ��ޤ���

�ƥ��Ȥ����Ԥ��뤳�Ȥ���Ԥ����ΤǤ������п��ΥС��ˤʤ��������ޤ�����
��ǧ�Τ���ˡ��ؿ��������true������Ƥߤޤ���

loginLogWriter.php

<?php
function loginLogWriter($user){
    $logFile = "/tmp/test_log.log";
    $user = str_replace("\t", "  ", $user);
    $logData = array(date("Y/m/d"), date("H:i:s"), $user, "\n");
    $logData = implode("\t", $logData);
    $fp = fopen($logFile,"a");
    flock($fp,2);
    fwrite($fp, $logData);
    fclose($fp);
    return true; //���ɲä��ޤ���
}

�ƥ��Ȥ��ޤ���

�����ˤʤꡢ���Ԥ��ޤ������ؿ��ϡ�����˼¹Ԥ���Ƥ���褦�Ǥ���
������񤭹���ե�������������Ϥ��ʤΤˡ��ȹͤ��Ƥ���Ȼפ��Ф��ޤ�����
fopen�ϡ������ץ󤹤�ե����뤬�ʤ����ˤϡ��ե������������ޤ���

�ƥ��Ȥ����Ԥξ��֤Ǥ��Τǡ��ؿ��ν����������ơ��ƥ��Ȥ��������뤳�Ȥ��ǧ���ޤ���

loginLogWriter.php

<?php
function loginLogWriter($user){
    $logFile = "/tmp/test_log.log";
    if ( file_exists($logFile) ) {
        $user = str_replace("\t", "  ", $user);
        $logData = array(date("Y/m/d"), date("H:i:s"), $user, "\n");
        $logData = implode("\t", $logData);
        $fp = fopen($logFile,"a");
        flock($fp,2);
        fwrite($fp, $logData);
        fclose($fp);
        return true;
    }
    else {
        return false;
    }
?>

�ƥ��Ȥ�¹Ԥ��ޤ������٤��������ޤ�����


���Τ褦�ˡ��ƥ��Ȥκ������ƥ��Ȥμ��Ԥγ�ǧ�������κ������ƥ��Ȥ������γ�ǧ�����٤ⷫ���֤��ơ��ץ�������������Ƥ����Ф����ΤǤϤʤ��Ǥ��礦����

Posted by hosco at 2004ǯ11��18�� 22:44