我想单元测试我的包,我想从EventManager模拟获得工作单元。基本上,我想获得最后一个持久对象。我知道在正常的应用程序中,我可以使用EventSubscriber执行相同的操作。如何从EventManager Mock对象获取UnitOfWork或者如何获取PHP单元中的最后一个持久对象?
我想要实现的基本上是检查以前持久记录的状态,如果它的标志挂起,那么在下一次持续时,我想将其更新为未挂起。
例子:
下面是如何得到事件管理器:
/**
* @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
请勿模拟第三方代码。它已经被解释了数千次了。例如:https://stackoverflow.com/a/29471209/330267技术可能不同,但适用相同的规则。 –
@JakubZalas,所以你的意思是我不应该做单元测试,而是做功能测试?我会喜欢在代码中进行内存检查,而不是涉及测试数据库。 **因为这个图书馆我想与社区分享,并且不希望他们安装数据库来执行测试。** – Gaurav