How do I mock the Doctrine PersistentCollection in PHPUnit

Viewed 2457

Does anyone know how to mock a Doctrine PersistentCollection?

When I try and mock the class directly using PHPUnit and Mockery I get an exception that reads:

Mockery\Exception: The class Doctrine\ORM\PersistentCollection is marked final a

nd its methods cannot be replaced. Classes marked final can be passed in to \Moc kery::mock() as instantiated objects to create a partial mock, but only if the m ock is not subject to type hinting checks.

My research indicates that Mockery and/or phpUnit cannot use reflection once the methods are marked as final.

I then tried to create a stdClass and give it the methods an iterator would use (valid/current/next) but a foreach loop will not call these unless the class implements an iterator.

Thus, the following code does not work...

$this -> collectionMock = \Mockery::mock('PersistentCollection, Traversable');
$this -> collectionMock -> shouldReceive('rewind');
$this -> collectionMock -> shouldReceive('valid') -> andReturn('true');
$this -> collectionMock -> shouldReceive('next');
$this -> collectionMock -> shouldReceive('current') ->andReturn();

And throws seems to throw the following fatal error:

Fatal error: Cannot redeclare Mockery_1670979900_PersistentCollection_Traversable::rewind() 
in C:\zendProject\zf2\vendor\mockery\mockery\library\Mockery\Generator.php(129) :
eval()'d code on line 43

Has anyone come up with a good way to mock this class

1 Answers
Related