Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I prevent redeclaration errors when using Mock classes that implement the IteratorAggregate interface when testing with PHPUnit?

I'm writing a unit test that relies on an external class, exceptionManager. I want to be able to predict what some specific functions on this class will return, so I'm using a mock object. The code is quite straightforward:

$mockExceptionManager = $this->getMock('exceptionManager');

The trouble is, my exception manager implements the IteratorAggregate interface, which requires a method that looks like this:

public function getIterator()
{
  return new ArrayIterator($this->exceptions);
}

When I run the unit test, I get the following error:

Fatal error: Cannot redeclare Mock_exceptionManager_ae79bad2::getIterator() in /Applications/MAMP/bin/php5.2/lib/php/PEAR/PHPUnit/Framework/MockObject/Generator.php(170) : eval()'d code on line 297

I have a feeling that the PHPUnit mock object suite also implements the IteratorAggregate interface, and the two are clashing, although I'm unsure. I also tried using the Iterator interface, but ran into the same issue. How can I get around this?

like image 505
Jordan Brown Avatar asked May 23 '26 18:05

Jordan Brown


1 Answers

I disabled autoloading on the mock object which solved the issue.

$mockExceptionManager = $this->getMockBuilder('exceptionManager')
                             ->disableAutoload()
                             ->getMock();
like image 118
Jordan Brown Avatar answered May 25 '26 08:05

Jordan Brown



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!