如何从EventManager模拟对象或如何在PHP单元中获取最后一个持久的对象



我正在尝试单元测试我的捆绑包,我想从EventManager模拟中获取工作单位。基本上,我想获得最后一个持久的对象。我知道在普通应用中,我可以使用Eventubscriber进行同样的操作。

我想实现的基本上是,如果其标志正在待处理,请检查上一个持久记录的状态,然后在下一个持久性中,我想将其更新到不待处理。

示例:

这是我获得事件经理的方式:

/**
 * @param EntityFriend|null $friendEntity
 * @return DoctrineORMEntityManager|PHPUnit_Framework_MockObject_MockObject
 */
private function getEntityManager(EntityFriend $friendEntity = null)
{
    $repository = $this
        ->getMockBuilder('DoctrineORMEntityRepository')
        ->disableOriginalConstructor()
        ->setMethods(['findBy'])
        ->getMock();
    $repository
        ->expects($this->any())
        ->method('findBy')
        ->will($this->returnValue(null));
    /** @var DoctrineORMEntityManager|PHPUnit_Framework_MockObject_MockObject $entityManager */
    $entityManager = $this
        ->getMockBuilder('DoctrineORMEntityManager')
        ->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 EntityFriend($this->friend, $this->user);
    $entityManager = $this->getEntityManager($friendEntity);
    $pendingRequest = new FriendService($entityManager);
    /** @var EntityFriend $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(EntityFriend::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) NalabTnahsarpFriendFollowerBundleTestsServiceFriendTest::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) NalabTnahsarpFriendFollowerBundleTestsServiceFriendTest::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);
    });

如果这不是唯一在测试代码中使用$ this-> at((而不是一次((的persist((:

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

尝试添加另一种预期的方法以使用该功能:

    $configurationMock = $this
        ->getMockBuilder('DoctrineORMMappingEntityListenerResolver')
        ->disableOriginalConstructor()
        ->getMock();
    $entityManager
        ->expects($this->any())
        ->method('getConfiguration')
        ->will($this->returnValue($configurationMock));

相关内容

  • 没有找到相关文章

最新更新