php去掉对象最后一个,如何从EventManager Mock对象获取UnitOfWork或者如何在PHP单元中获取最后一个持久化对象?...

这篇博客探讨了如何在单元测试中从EventManagerMock获取工作单元,特别是如何检查并更新先前持久记录的状态。作者遇到一个错误,尝试在测试`addFriend`方法时调用`getUnitOfWork()`,但遇到了`null`对象错误。博客内容涉及到模拟 Doctrine 的 EntityManager 和 EntityRepository,以及在测试场景中处理持久化操作。
摘要由CSDN通过智能技术生成

我正在尝试对我的捆绑进行单元测试,我想从EventManager Mock中获取工作单元.基本上,我想获得最后一个持久化对象.我知道在正常的应用程序中,我可以使用EventSubscriber执行相同的操作.

基本上我想要实现的是,如果其标志处于挂起状态,则检查先前持久记录的状态,然后在下一个持久化中,我想将其更新为未挂起.

例:

以下是我获得活动管理器的方法:

/**

* @param Entity\Friend|null $friendEntity

* @return \Doctrine\ORM\EntityManager|\PHPUnit_Framework_MockObject_MockObject

*/

private function getEntityManager(Entity\Friend $friendEntity = null)

{

$repository = $this

->getMockBuilder('\Doctrine\ORM\EntityRepository')

->disableOriginalConstructor()

->setMethods(['findBy'])

->getMock();

$repository

->expects($this->any())

->method('findBy')

->will($this->returnValue(null));

/** @var \Doctrine\ORM\EntityManager|\PHPUnit_Framework_MockObject_MockObject $entityManager */

$entityManager = $this

->getMockBuilder('\Doctrine\ORM\EntityManager')

->setMethods(['getRepository', 'getUnitOfWork', 'persist'])

->disableOriginalConstructor()

->getMock();

$entityManager

->expects($this->any())

->method('getRepository')

->will($this->returnValue($repository));

$entityManager

->expects($this->any())

->method('getUnitOfWork')

->will($this->returnValue($repository));

$entityManager

->expects($this->any())

->method('persist')

->with($friendEntity)

->will($this->returnValue(null));

return $entityManager;

}

在我的测试中:

/**

* Test add friend when friend pending

*/

public function testAddFriendPending()

{

$friendEntity = new Entity\Friend($this->friend, $this->user);

$entityManager = $this->getEntityManager($friendEntity);

$pendingRequest = new FriendService($entityManager);

/** @var Entity\Friend $lastInsert */

$lastInsert = $pendingRequest->addFriend($this->friend, $this->user);

$lastInsert->setId(1);

$friendAddRequest = new FriendService($entityManager);

$friendAddRequest->addFriend($this->user, $this->friend);

$response = $friendAddRequest->getStatus();

$this->assertEquals(Entity\Friend::FRIEND_ADD_STATUS_COMPLETED, $response);

}

编辑

仍然收到错误:

vendor/phpunit/phpunit/phpunit src/NalabTnahsarp/FriendFollowerBundle/Tests/Service/

PHPUnit 5.7.21 by Sebastian Bergmann and contributors.

EE 2 / 2 (100%)

Time: 102 ms, Memory: 4.00MB

There were 2 errors:

1) NalabTnahsarp\FriendFollowerBundle\Tests\Service\FriendTest::testAddFriendNotPending

Error: Call to a member function getUnitOfWork() on null

/Users/Sites/app/vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php:194

/Users/Sites/app/src/NalabTnahsarp/FriendFollowerBundle/Service/Friend.php:140

/Users/Sites/app/src/NalabTnahsarp/FriendFollowerBundle/Service/Friend.php:63

/Users/Sites/app/src/NalabTnahsarp/FriendFollowerBundle/Tests/Service/FriendTest.php:43

2) NalabTnahsarp\FriendFollowerBundle\Tests\Service\FriendTest::testAddFriendPending

Error: Call to a member function getUnitOfWork() on null

/Users/Sites/app/vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php:194

/Users/Sites/app/src/NalabTnahsarp/FriendFollowerBundle/Service/Friend.php:140

/Users/Sites/app/src/NalabTnahsarp/FriendFollowerBundle/Service/Friend.php:63

/Users/Sites/app/src/NalabTnahsarp/FriendFollowerBundle/Tests/Service/FriendTest.php:57

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值