/**
* Mock iterator
*
* This attaches all the required expectations in the right order so that
* our iterator will act like an iterator!
*
* @param Iterator $iterator The iterator object; this is what we attach
* all the expectations to
* @param array An array of items that we will mock up, we will use the
* keys (if needed) and values of this array to return
* @param boolean $includeCallsToKey Whether we want to mock up the calls
* to "key"; only needed if you are doing foreach ($foo as $k => $v)
* as opposed to foreach ($foo as $v)
*/
private function mockIterator(
Iterator $iterator,
array $items,
$includeCallsToKey = FALSE
)
{
$iterator->expects($this->at(0))
->method('rewind');
$counter = 1;
foreach ($items as $k => $v)
{
$iterator->expects($this->at($counter++))
->method('valid')
->will($this->returnValue(TRUE));
$iterator->expects($this->at($counter++))
->method('current')
->will($this->returnValue($v));
if ($includeCallsToKey)
{
$iterator->expects($this->at($counter++))
->method('key')
->will($this->returnValue($k));
}
$iterator->expects($this->at($counter++))
->method('next');
}
$iterator->expects($this->at($counter))
->method('valid')
->will($this->returnValue(FALSE));
}
Last active
June 17, 2016 13:35
-
-
Save matejb/6a5210be2b3ffa261e49e2964b2a9b2d to your computer and use it in GitHub Desktop.
PHP Mock iterator
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment