gpt4 book ai didi

php - 如何从 EventManager 模拟对象获取 UnitOfWork 或者如何获取 PHP Unit 中的最后一个持久化对象?

转载 作者:可可西里 更新时间:2023-11-01 13:27:25 28 4
gpt4 key购买 nike

我正在尝试对我的包进行单元测试,我想从 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

最佳答案

我想你想检查持久实体是否设置了一些特定的值,当然是正确的类型。

您应该像这样将其添加到实体管理器的持久化方法中:

如果 persist() 只在你轻松的时候被调用:

$entityManager
->expects($this->once())
->method('persist')
->with($theEntityYouExpectWithAllValues)
->will($this->returnValue(null));

具有更复杂的期望:

$entityManager
->expects($this->once())
->method('persist')
->willReturnCallback(function($entityToTest){
$this->assertInstanceOf(EntityClass::class, $entityToTest);
$this->assertEquals('some_value', $entityToTest->someKey);
});

如果这不是测试代码中对 persist() 的唯一调用,则必须使用 $this->at() 而不是 once():

$entityManager
->expects($this->at(3)) // 3rd call of any method on manager not just persist
->method('persist')
// ...

关于php - 如何从 EventManager 模拟对象获取 UnitOfWork 或者如何获取 PHP Unit 中的最后一个持久化对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44782639/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com