I want to use PHPUnit to test that methods are called in the right order.
My first attempt, using ->at()
on a mock object, did not work. For example, I expected the following to fail, but it does not:
public function test_at_constraint()
{
$x = $this->getMock('FirstSecond', array('first', 'second'));
$x->expects($this->at(0))->method('first');
$x->expects($this->at(1))->method('second');
$x->second();
$x->first();
}
The only way I could think of that forced a failure if things were called in the wrong order was something like this:
public function test_at_constraint_with_exception()
{
$x = $this->getMock('FirstSecond', array('first', 'second'));
$x->expects($this->at(0))->method('first');
$x->expects($this->at(1))->method('first')
->will($this->throwException(new Exception("called at wrong index")));
$x->expects($this->at(1))->method('second');
$x->expects($this->at(0))->method('second')
->will($this->throwException(new Exception("called at wrong index")));
$x->second();
$x->first();
}
Is there a more elegant way to do this? Thanks!
You need involve any InvocationMocker
to make your expectations work. For example this should work:
public function test_at_constraint()
{
$x = $this->getMock('FirstSecond', array('first', 'second'));
$x->expects($this->at(0))->method('first')->with();
$x->expects($this->at(1))->method('second')->with();
$x->second();
$x->first();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With