I was going through the book named “Test-Driven Development By Example” By the author Kent Beck.
I am trying to write a similar function in php but not understanding the steps.
Original function:
Test function:
public void testEquality() {
assertTrue(new Dollar(5).equals(new Dollar(5)));
assertFalse(new Dollar(5).equals(new Dollar(6)));
}
Class function:
public boolean equals(Object object) {
Dollar dollar = (Dollar) object;
return amount == dollar.amount;
}
My code:
Test function:
public function setup() {
$this->dollarFive = new Dollar(5);
}
public function testEquality() {
$this->assertTrue($this->dollarFive->equals(new Dollar(5)));
}
Class Function:
class Dollar
{
public function __construct($amount) {
$this->amount = (int) $amount;
}
public function equals(Dollar $object) {
$this->Object = $object;
return $this->amount == $this->Object;
}
}
While executing the test case i am getting the following error.
Object of class Dollar could not be converted to int
Need some help on this. How can i fix this?
$this->amountis an int,$this->Objectisn’t an int. You tried to compare each other, thus you’ll getYou probably mean
However, there is also something curious in your class
you’ll probably want just