title: PHPUnit: how to test that methods are called in incorrect order?
body: <p>I want to use PHPUnit to test that methods are called in the right order.</p>
  
  <p>My first attempt, using <code>-&gt;at()</code> on a mock object, did not work. For example, I expected the following to fail, but it does not:</p>
  
  <pre><code>  public function test_at_constraint()
    {
      $x = $this-&gt;getMock('FirstSecond', array('first', 'second'));
      $x-&gt;expects($this-&gt;at(0))-&gt;method('first');
      $x-&gt;expects($this-&gt;at(1))-&gt;method('second');
  
      $x-&gt;second();
      $x-&gt;first();
    }      
  </code></pre>
  
  <p>The only way I could think of that forced a failure if things were called in the wrong order was something like this:</p>
  
  <pre><code>  public function test_at_constraint_with_exception()
    { 
      $x = $this-&gt;getMock('FirstSecond', array('first', 'second'));
  
      $x-&gt;expects($this-&gt;at(0))-&gt;method('first');
      $x-&gt;expects($this-&gt;at(1))-&gt;method('first')
        -&gt;will($this-&gt;throwException(new Exception("called at wrong index")));
  
      $x-&gt;expects($this-&gt;at(1))-&gt;method('second');
      $x-&gt;expects($this-&gt;at(0))-&gt;method('second')
        -&gt;will($this-&gt;throwException(new Exception("called at wrong index")));
  
      $x-&gt;second();
      $x-&gt;first();
    }
  </code></pre>
  
  <p>Is there a more elegant way to do this? Thanks!</p>