PHPUnit: how to check that methods are being called in the wrong order?

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!

+7
source share
1 answer

To fulfill your expectations, you must use any InvocationMocker . 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(); } 
+7
source

All Articles