
Mock IteratorAggregate with Mockery to Fix foreach in Tests
Originally published at recca0120.github.io While developing Google Firebase-related code, I needed to mock QuerySnapshot , which implements IteratorAggregate . It wasn't immediately obvious how to make foreach work with Mockery . What is IteratorAggregate IteratorAggregate is a built-in PHP interface. By implementing it and providing a getIterator() method, an object becomes iterable with foreach . The key point: foreach iterates over whatever getIterator() returns, not the object itself -- which is exactly what makes it mockable: class myData implements IteratorAggregate { public $property1 = "Public property one" ; public $property2 = "Public property two" ; public $property3 = "Public property three" ; public function __construct () { $this -> property4 = "last property" ; } public function getIterator () { return new ArrayIterator ( $this ); } } $obj = new myData ; foreach ( $obj as $key => $value ) { var_dump ( $key , $value ); echo " \n " ; } How to Mock It Just mock getIterator
Continue reading on Dev.to
Opens in a new tab


