I want to use PHPUnit to verify that methods are called in the correct order.
My first attempt using ->at() for a mock object did not work. For example, I expected the following to happen, but it is 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 having to fail if everything was 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!
des4maisons
source share