如何在PHPUnit中模拟Doctrine PersistentCollection [英] How do I mock the Doctrine PersistentCollection in PHPUnit

查看:180
本文介绍了如何在PHPUnit中模拟Doctrine PersistentCollection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道如何模仿一个学说PersistentCollection?



当我使用PHPUnit和Mockery直接尝试并模拟类时,我收到一个例外:

  Mockery\Exception:类Doctrine\ORM\PersistentCollection被标记为最终

其方法无法替换。标记为final的类可以传入到\Moc
kery :: mock()作为实例化对象来创建部分模拟,但只有当m
ock不受类型提示检查时。 p>

我的研究表明,一旦方法被标记为final,Mockery和/或phpUnit就不能使用反射。



然后,我尝试创建一个stdClass,并给出迭代器将使用的方法(有效/当前/下一个),但是foreach循环将不会调用它们,除非该类实现一个迭代器



因此,以下代码无效...

  $ this  - > collectionMock = \Mockery :: mock('PersistentCollection,Traversable'); 
$ this - > collectionMock - > shouldReceive(倒带);
$ this - > collectionMock - > shouldReceive('valid') - > andReturn(真);
$这个 - > collectionMock - > shouldReceive(下一个);
$ this - > collectionMock - > shouldReceive('current') - > andReturn();

抛出似乎会导致以下致命错误:

 致命错误:无法重新声明Mockery_1670979900_PersistentCollection_Traversable :: rewind()
在C:\zendProject\zf2\vendor\mockery\mockery\library\\ \\ Mockery\Generator.php(129):
eval()'第43行代码



有没有人想出一个很好的方式来模仿这个课程

解决方案

我似乎没有可能模拟一个被宣布为最终的课程。可能有一些希望。由于 PersistentCollection 实现 Doctrine\Common\Collections\Collection Doctrine\ Common \Collections\Selectable ,可以使用Mockery来模拟实现两个接口的对象。

  Mockery :: mock(
'Doctrine\Common\Collections\Collection,Doctrine\Common\Collections\Selectable'
);

我有在我自己的一个项目中使用了这个效果。



至于为什么你不能模拟一个最后一班,这是我能找到的最好的:



Mockery



使用Mockery模拟最终课的能力是限制


同样,主要目的是确保模拟对象继承类型提示的特定类型。有一个例外,标记为final的类或标记为final的方法不能被完全模拟。在这些情况下,必须使用部分模拟(稍后解释)。

  $ mock = \Mockery :: mock('alias :MyNamespace\MyClass'); 


搜索链接的final页面。你可以找到我可以找到的所有文件。



PHPUnit



尝试在PHPUnit中模拟最后一个类通过设计抛出异常


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

解决方案

I doesn't seem like it's possible to mock a class that's been declared final. There may be some hope, however. Since PersistentCollection implements both Doctrine\Common\Collections\Collection and Doctrine\Common\Collections\Selectable, you can use Mockery to mock an object implementing both interfaces.

Mockery::mock(
    'Doctrine\Common\Collections\Collection, Doctrine\Common\Collections\Selectable'
);

I've used this to good effect in one of my own projects.

As to why you can't mock a final class, this is the best I could find:

Mockery

The ability to mock final classes with Mockery is limited:

Again, the primary purpose is to ensure the mock object inherits a specific type for type hinting. There is an exception in that classes marked final, or with methods marked final, cannot be mocked fully. In these cases a partial mock (explained later) must be utilised.

$mock = \Mockery::mock('alias:MyNamespace\MyClass');

Search the linked page for 'final'. You'll find all the documentation I could find.

PHPUnit

Attempting to mock a final class in PHPUnit throws an exception by design.

这篇关于如何在PHPUnit中模拟Doctrine PersistentCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆