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

标签 php unit-testing doctrine-orm phpunit symfony-2.8

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

相关文章:

php - 如何在 MySql 中更新 Json 数据类型?

php - 如何在 symfony2 Controller 中发送 JSON 响应

unit-testing - 从 Controller 单元测试中删除域类依赖项

mysql - 更新架构时 Doctrine 添加现有外键(mysql)

php - 教义不在查询中

php - 从多个时间段创建连续的时间线

php - WordPress + WooCommerce | MySQL技能不足

c# - 在报告中使用 NUnit 显示复杂类型参数的实际值

java - Maven 不运行 Spock 测试

php - 在 Symfony 2 中为集合的每个项目指定不同的验证组?