A brief introduction to the test infrastructure
PHPUnit . PHPUnit JUnit, eXtreme Programming. XP , API , . XP, XP, PHPUnit. PHPUnit .
, echo() var_dump(). , . PHPUnit, . :
, , . PHPUnit .
: . :
---- string.php ----
<?php
class String
{
var $data;
function String($data) {
$this->data = $data;
}
function copy() {
}
function add($string) {
}
function toString($format) {
}
}
?>
, . - PHP, PHPUnit_TestCase, , "" . . assert*()-family, , .
---- testcase.php ----
<?php
require_once 'string.php';
require_once 'PHPUnit.php';
class StringTest extends PHPUnit_TestCase
{
var $abc;
function StringTest($name) {
$this->PHPUnit_TestCase($name);
}
function setUp() {
$this->abc = new String("abc");
}
function tearDown() {
unset($this->abc);
}
function testToString() {
$result = $this->abc->toString('contains %s');
$expected = 'contains abc';
$this->assertTrue($result == $expected);
}
function testCopy() {
$abc2 = $this->abc->copy();
$this->assertEquals($abc2, $this->abc);
}
function testAdd() {
$abc2 = new String('123');
$this->abc->add($abc2);
$result = $this->abc->toString("%s");
$expected = "abc123";
$this->assertTrue($result == $expected);
}
}
?>
. , , PHP.
---- stringtest.php ----
<?php
require_once 'testcase.php';
require_once 'PHPUnit.php';
$suite = new PHPUnit_TestSuite("StringTest");
$result = PHPUnit::run($suite);
echo $result -> toString();
?>
script , :
TestCase stringtest->testtostring() failed: expected true, actual false
TestCase stringtest->testcopy() failed: expected , actual Object
TestCase stringtest->testadd() failed: expected true, actual false
, , .
script , script html $result->toHTML() $result->toString().
, .
---- string.php ----
<?php
class String
{
var $data;
function String($data) {
$this->data = $data;
}
function copy() {
$ret = new String($this->data);
return $ret;
}
function add($string) {
$this->data = $this->data.$string->toString("%ss");
}
function toString($format) {
$ret = sprintf($format, $this->data);
return $ret;
}
}
?>
, :
~>
php -f stringtest.php
TestCase stringtest->testtostring() passed
TestCase stringtest->testcopy() passed
TestCase stringtest->testadd() failed: expected true, actual false
D'! ! . 16 string.php
<?php
$this->data = $this->data.$string->toString("%s");
?>
:
~>
php -f stringtest.php
TestCase stringtest->testtostring() passed
TestCase stringtest->testcopy() passed
TestCase stringtest->testadd() passed
!
, ? , . , API, . PHPUnit - .
, . , - , , . , , , , , , , .
: http://pear.php.net