I am teaching myself PHP as well as TDD (Using PHP and PHPUnit). I am working through the book Test-driven Development by Example by Kent Beck.
In Chapter 3, for example, He suggests this for the equality test:
public void testEquality() {
assertTrue(new Dollar(5).equals(new Dollar(5)));
}
In the Dollar class he rewrites the equals method as such:
public boolean equals(Object object) {
Dollar dollar = (Dollar) object;
return amount == dollar.amount;
}
It probably does not help that I am new to PHP, but I am not sure how to translate that into PHP.
For the first function I tried:
public function testEquality(){
$a = new Dollar(5);
$this->assertTrue($a->equals($b = new Dollar(5)));
}
Is this the right track? As much as I know of PHP right now objects have to be assigned to a variable, correct? Before that route I explored
$this->assertTrue(new Dollar(5)->equals(new Dollar(5)));
threw a syntax error. Surprising as refactoring the earlier testMultiplication method with $this->assertEquals(new Dollar(10), $five->times(2));
passed, however.
As far as the equals
method is concerned, that's completely foreign to me, and I just don't know where to start.
How can I correctly reconstruct the above in PHP? If I can get a few right I think I can handle the rest of the examples.
As a side question, does anyone know if this Money example has been approached in PHP and is the code out there?