getMockBuilder(\StdClass::class) ->setMethods(['__invoke']) ->getMock(); $callable->expects($this->at(0)) ->method('__invoke') ->with(1, 0, $items) ->will($this->returnValue(1)); $callable->expects($this->at(1)) ->method('__invoke') ->with(2, 1, $items) ->will($this->returnValue(4)); $callable->expects($this->at(2)) ->method('__invoke') ->with(3, 2, $items) ->will($this->returnValue(9)); $map = new ReplaceIterator($items, $callable); $this->assertEquals([1, 4, 9], iterator_to_array($map)); } }