简体   繁体   English

在使用PHPUnit进行测试时,如何在使用实现IteratorAggregate接口的Mock类时防止重新声明错误?

[英]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. 我正在编写一个依赖于外部类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: 麻烦的是,我的异常管理器实现了IteratorAggregate接口,这需要一个如下所示的方法:

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 致命错误:无法在/Applications/MAMP/bin/php5.2/lib/php/PEAR/PHPUnit/Framework/MockObject/Generator.php(170)中重新声明Mock_exceptionManager_ae79bad2 :: getIterator():eval()'d代码在线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. 我有一种感觉,PHPUnit模拟对象套件也实现了IteratorAggregate接口,并且两者发生了冲突,尽管我不确定。 I also tried using the Iterator interface, but ran into the same issue. 我也尝试使用Iterator接口,但遇到了同样的问题。 How can I get around this? 我怎么能绕过这个?

I disabled autoloading on the mock object which solved the issue. 我在模拟对象上禁用了自动加载功能,解决了这个问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM