Given a simple PHP class:
class Car
{
private $speed = 0;
public getSpeed() {
return $this->speed;
}
public function accelerate($howMuch) {
$this->speed += $howMuch;
}
}
You can write a PHPUnit test which tests the behavior of the class under test by calling the public methods and check whether they function as expected:
class CarTest extends PHPUnit_Framework_TestCase
{
public function testThatInitalSpeedIsZero() {
$car = new Car();
$this->assertSame(0, $car->getSpeed());
}
public function testThatItAccelerates() {
$car = new Car();
$car->accelerate(20);
$this->assertSame(20, $car->getSpeed());
}
public function testThatSpeedSumsUp() {
$car = new Car();
$car->accelerate(30);
$car->accelerate(50);
$this->assertSame(80, $car->getSpeed());
}
}
Important parts:
$this->assert...
functions to check expected values.