title: PHPUnit - multiple stubs of same class
body: <p>I'm building unit tests for class <code>Foo</code>, and I'm fairly new to unit testing.</p>
  
  <p>A key component of my class is an instance of <code>BarCollection</code> which contains a number of <code>Bar</code> objects.  One method in <code>Foo</code> iterates through the collection and calls a couple methods on each <code>Bar</code> object in the collection.  I want to use stub objects to generate a series of responses for my test class.  How do I make the <code>Bar</code> stub class return different values as I iterate?  I'm trying to do something along these lines:</p>
  
  <pre><code>$stubs = array();
  foreach ($array as $value) {
      $barStub = $this-&gt;getMock('Bar');
      $barStub-&gt;expects($this-&gt;any())
              -&gt;method('GetValue')
              -&gt;will($this-&gt;returnValue($value));
      $stubs[] = $barStub;
  }
  // populate stubs into `Foo`
  
  // assert results from `Foo-&gt;someMethod()`
  </code></pre>
  
  <p>So <code>Foo-&gt;someMethod()</code> will produce data based on the results it receives from the <code>Bar</code> objects.  But this gives me the following error whenever the array is longer than one:</p>
  
  <pre><code>There was 1 failure:
  
  1) testMyTest(FooTest) with data set #2 (array(0.5, 0.5))
  Expectation failed for method name is equal to &lt;string:GetValue&gt; when invoked zero or more times.
  Mocked method does not exist.
  /usr/share/php/PHPUnit/Framework/MockObject/Mock.php(193) : eval()'d code:25
  </code></pre>
  
  <p>One thought I had was to use <code>-&gt;will($this-&gt;returnCallback())</code> to invoke a callback method, but I don't know how to indicate to the callback which <code>Bar</code> object is making the call (and consequently what response to give).</p>
  
  <p>Another idea is to use the <code>onConsecutiveCalls()</code> method, or something like it, to tell my stub to return 1 the first time, 2 the second time, etc, but I'm not sure exactly how to do this.  I'm also concerned that if my class ever does anything other than ordered iteration on the collection, I won't have a way to test it.</p>