Say you have a method which ultimately boils down to
class Pager
{
private $i;
public function next()
{
if ($this->i >= 3) {
throw new OutOfBoundsException();
}
$this->i++;
}
}
How would you unit test this class. I.e. test whether the exception gets thrown on the third call of next() using PHPUnit? I’ve added my attempt as an answer, but I’m not sure whether this really is the way to go.
What about testing for
nullon the first two calls and also test for the exception being thrown like follows: