I have quite a few objects in my system that implement the PHP SPL Iterator interface.
As I write them I also write tests.
I know that writing tests is generally NOT a cut 'n paste job.
But, when it comes to testing classes that implement Standard PHP Library interfaces, surely it makes sense to have a few script snippets that can be borrowed and dropped in to a Test class - purely to test that particular interface.
It seems sensible to have these publicly available. So, I was wondering if you knew of any?
A quick and dirty trick for mocking an iterator is just to stock an ArrayIterator with mock objects and use that as your mock Iterator
$mockIt = new ArrayIterator;
$mockIt->append($mock1);
$mockIt->append($mock2);
$mockIt->append($mock3);
$sut = new SystemExpectingAnIterator($mockIt);
$this->assertTrue($sut->doSomethingWithIterator());
It's a bit smelly, but more straightforward than mocking all the SPL Iterator methods.