Zuerst phpunit2 Installieren
Code: Alles auswählen
<?PHP
class EmailAddress {
public $localPart;
public $domain;
public $address;
public function __construct($address = null)
{
if($address) {
$this->address = $address;
$this->extract();
}
}
protected function extract()
{
list($this->localPart, $this->domain) = explode("@", $this->address);
}
}
?>
Die Testklasse test.php
Code: Alles auswählen
<?PHP
require_once "email.php";
require_once 'PHPUnit2/Framework/TestCase.php';
class EmailAddressTest extends PHPUnit2_Framework_TestCase {
public function __constructor($name) {
parent::__constructor($name);
}
function testLocalPart() {
$email = new EmailAddress("ranz at domain.ch");
$this->assertTrue($email->localPart == 'franz');
}
function testDomain() {
$email = new EmailAddress("franz at domain.ch");
$this->assertTrue($email->domain == 'domain.ch');
}
}
require_once "PHPUnit2/Framework/TestSuite.php";
$suite = new PHPUnit2_Framework_TestSuite();
$suite->addTest(new EmailAddressTest('testLocalPart'));
require_once "PHPUnit2/TextUI/TestRunner.php";
PHPUnit2_TextUI_TestRunner::run($suite);
?>
Dann in die Konsolle und so ausführen:
Code: Alles auswählen
phpunit --wait /var/www/html/test/test.php
Code: Alles auswählen
PHPUnit 2.3.6 by Sebastian Bergmann.
.
Time: 0.000646
OK (1 test)
PHPUnit 2.3.6 by Sebastian Bergmann.