PHPUnit�ϡ����֥������ȤΥƥ��Ȥˤ����Ȥ���Τ��ȻפäƤ��ޤ�������ñ��ʴؿ���ñ�Υƥ��ȤǤ�Í���ʤ�Ǥ��͡��ʥɥ�����ȤΥ���ץ�ϡ��ؿ��Υƥ��Ȥ��ܤäƤޤ�����
�Ȥ����櫓�ǡ�PHPUnit��ñ��ʻȤ�����Ҳ𤷤ޤ���
PHPUnit�λȤ����򻲹ͤˤ��ޤ������ʤȤ��������Τޤ�ޤǤ������ߤޤ��󡣤Ȥ����������꤬�Ȥ��������ޤ�����
��1. ����ƥ��Ȥ��뤫��
������ץȤ˥������������桼���Υ�����ե�����˵�Ͽ����ؿ���ƥ��Ȥ��뤳�Ȥˤ��ޤ���
bool loginLogWriter(string user)
user������ˤȤꡢ�ե�����ˡ�user����������������ñ¤¹ï¿½ï¿½ß¤Þ¤ï¿½ï¿½ï¿½
user���������������ϡ�1��Υ����������դ���1�Ԥ�ե�����κǸ�˥��ֶ��ڤ���ɵ����ޤ���
�ñ¤¹ï¿½ï¿½ß¤ï¿½ï¿½ï¿½ï¿½ï¿½Ë½ï¿½Î»ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ë¡ï¿½true���֤��ޤ������Ԥ������ϡ�false���֤��ޤ���
����ƥ��Ȥ��뤫��ͤ��Ƥ����Ȥ��꤬����ޤ��󤬡�
���ֶ��ڤꡢ�ե��������Ȥ������Ȥǡ����ι��ܤ�ƥ��Ȥ��뤳�Ȥˤ��ޤ���
��2. �ƥ��ȤΥ�����ȥ�κ���
�ޤ����Ϥ�ˡʥƥ��Ȥ���ؿ��������ˡˡ�������ȥ����ޤ���
test.php
// run Test Suites
$suite = new PHPUnit_TestSuite("loginLogWriterTest"); //���ƥ��Ȥ��륯�饹̾(1)
$result = new PHPUnit_GUI_HTML($suite);
$result->show();
?>
test/loginLogWriterTest.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
�����ޤǤǡ��Ȥꤢ������test.php��¹Ԥ��Ƥߤޤ���
(un)check all �˥����å���Ĥ��ơ�run tests�򥯥�å����ޤ���
fail�᥽�åɤǡ�����Ū�˼��Ԥ����Ƥ��ޤ��Τǡ����Ԥ��Ƥ���С������ˤʤäƤ���С������Ǥ���
��3. �ƥ��Ȥκ���
�ؿ�������������ˡ��ޤ����ƥ��Ȥ�������ޤ���
��1. �������Ϥ��줿user����������Ͽ����Ƥ��뤳�ȡ��פ�����ޤ���
test/loginLogWriterTest.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
�ƥ��Ȥ�¹Ԥ��ޤ���
�ޤ���������񤤤Ƥ��ʤ��Τǡ����顼�ˤʤ�ޤ�����
�ƥ��ȥ�����ץȤΥ��顼��ФƤ��ޤ��������ˤ��ʤ��Ǥ����ޤ��礦��
���ơ����褤�衢�ºݤν�������ޤ���
loginLogWriter.php
�ƥ��Ȥ�¹Ԥ��ޤ���
��5. ���Υƥ��ȤǤ���
��2. user�˥��֤��ޤޤ�Ƥ��Ƥ���������Ͽ����뤳�ȡ���
Ʊ�ͤˡ��ƥ��Ȥ���ޤ���
test/loginLogWriterTest.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
�ƥ��Ȥ��ޤ���
�п��ΥС��ˤʤ�С��ƥ��ȴ�λ�Ǥ���
��6. �Ǹ�Υƥ��ȤǤ�
��3. �ñ¤¹ï¿½ï¿½ï¿½Õ¥ï¿½ï¿½ï¿½ï¿½ë¤¬Â¸ï¿½ß¤ï¿½ï¿½Ê¤ï¿½ï¿½ï¿½ï¿½ï¿½false���֤����ȡ���
Ʊ�����Ǥ���
�ƥ��Ȥ���ޤ���
test/loginLogWriterTest.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
�ƥ��Ȥ��ޤ���
�����ˤʤꡢ���Ԥ��ޤ������ؿ��ϡ�����˼¹Ԥ���Ƥ���褦�Ǥ���
������ñ¤¹ï¿½ï¿½ï¿½Õ¥ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ï¤ï¿½ï¿½Ê¤Î¤Ë¡ï¿½ï¿½È¹Í¤ï¿½ï¿½Æ¤ï¿½ï¿½ï¿½È»×¤ï¿½ï¿½Ð¤ï¿½ï¿½Þ¤ï¿½ï¿½ï¿½ï¿½ï¿½
fopen�ϡ������ץ󤹤�ե����뤬�ʤ����ˤϡ��ե������������ޤ���
�ƥ��Ȥ����Ԥξ��֤Ǥ��Τǡ��ؿ��ν����������ơ��ƥ��Ȥ��������뤳�Ȥ��ǧ���ޤ���
loginLogWriter.php
�ƥ��Ȥ�¹Ԥ��ޤ������٤��������ޤ�����
���Τ褦�ˡ��ƥ��Ȥκ������ƥ��Ȥμ��Ԥγ�ǧ�������κ������ƥ��Ȥ������γ�ǧ�����٤ⷫ���֤��ơ��ץ�������������Ƥ����Ф����ΤǤϤʤ��Ǥ��礦����