2017-06-27 29 views
6

我想单元测试我的包,我想从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 
+1

请勿模拟第三方代码。它已经被解释了数千次了。例如:https://stackoverflow.com/a/29471209/330267技术可能不同,但适用相同的规则。 –

+0

@JakubZalas,所以你的意思是我不应该做单元测试,而是做功能测试?我会喜欢在代码中进行内存检查,而不是涉及测试数据库。 **因为这个图书馆我想与社区分享,并且不希望他们安装数据库来执行测试。** – Gaurav

回答

0

尝试添加另一种预期的方法来使用该函数是这样的:

$configurationMock = $this 
     ->getMockBuilder('\Doctrine\ORM\Mapping\EntityListenerResolver') 
     ->disableOriginalConstructor() 
     ->getMock(); 

    $entityManager 
     ->expects($this->any()) 
     ->method('getConfiguration') 
     ->will($this->returnValue($configurationMock)); 
+0

Tests \ Service \ FriendTest :: testAddFriendPending BadMethodCallException:未定义的方法'getEntityListenerResolver'。方法名称必须以findBy或findOneBy开头! /Users/Sites/app/vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php:226 /用户/网站/应用/供应商/教义/ ORM/lib中/教义/ ORM /事件/ ListenersInvoker。 php:59 /Users/Sites/app/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:305 – Gaurav

+0

不起作用。 ** ** BadMethodCallException。 – Gaurav

+0

更新回答现在尝试,但我想你会得到更多的模拟 –

2

我想你想检查持久实体具有设定的某个特定值,当然是正确的类型。一旦你拥有了它容易

如果坚持()只叫:

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

凭借更先进的期望:

你应该这样添加此到persist方法的实体管理器

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

如果不坚持(),你必须使用$这 - >在(测试代码的唯一调用),而不是一次():

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

我不知道我该怎么做。你可以帮我吗?这里是我的存储库的链接。 https://开头github上。COM /拉夫 - 加里/朋友跟随束 – Gaurav

+0

我仍然收到以下错误: 有2个错误: 1)NalabTnahsarp \ FriendFollowerBundle \测试\服务\ FriendTest :: testAddFriendNotPending 错误:调用一个成员函数getUnitOfWork ()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 – Gaurav

+0

@Gaurav:With这个你不需要getLastPersistedEntity()了。这种方法不会产生你的错误。不要打电话给我,我的建议已经对您想要的实体进行检查。 – colburton

相关问题