title: PHPUnit mocking - fail immediately when method called x times
body: <p>With PHPUnit, I am testing a sequence of method calls using ->at(), like so:</p>
  
  <pre><code>$mock-&gt;expects($this-&gt;at(0))-&gt;method('execute')-&gt;will($this-&gt;returnValue('foo'));
  $mock-&gt;expects($this-&gt;at(1))-&gt;method('execute')-&gt;will($this-&gt;returnValue('bar'));
  $mock-&gt;expects($this-&gt;at(2))-&gt;method('execute')-&gt;will($this-&gt;returnValue('baz'));
  </code></pre>
  
  <p>How can I set up the mock so that, in the above scenario, <em>if</em> execute() is called four or more times, it will immediately fail? I tried this:</p>
  
  <p><code>$mock-&gt;expects($this-&gt;at(3))-&gt;method('execute')-&gt;will($this-&gt;throwException(new Exception('Called too many times.')));</code></p>
  
  <p>But this also fails if execute() is <em>not</em> called four times. It needs to fail <strong>immediately</strong>, otherwise the system under test will produce errors of its own, which causes the resulting error message to be unclear.</p>