PHPSpec:通过引用返回的函数

PHPSpec: function returning by reference

我在我的项目中将 Doctrine 2.5 更新为 2.6,但 phpspec 已损坏。

函数 getEntityChangeSet() 现在通过引用返回。 phpspec好像不支持

$unitOfWork
    ->getEntityChangeSet($site)
    ->willReturn(['_dataParent' => [0 => 2, 1 => 3]]);

响应是 returning by reference not supported

底层函数(doctrine/doctrine2)是

public function & getEntityChangeSet($entity)
{
    $oid  = spl_object_hash($entity);
    $data = [];

    if (!isset($this->entityChangeSets[$oid])) {
        return $data;
    }

    return $this->entityChangeSets[$oid];
}

您知道是否可以绕过这个或更改测试以使其工作吗?

@Pamilme

在 Twitter 上给出了答案

你必须用 Mockery 模拟 UnitOfWork。可以找到一个示例 here:

    /** @var UnitOfWork|MockInterface $unitOfWork */
    $unitOfWork = Mockery::mock(UnitOfWork::class);
    $unitOfWork->shouldReceive('getEntityChangeSet')->withArgs([$productAttribute->getWrappedObject()])->andReturn([
        'configuration' => [
            ['choices' => [
                '8ec40814-adef-4194-af91-5559b5f19236' => 'Banana',
                '1739bc61-9e42-4c80-8b9a-f97f0579cccb' => 'Pineapple',
            ]],
            ['choices' => [
                '8ec40814-adef-4194-af91-5559b5f19236' => 'Banana',
            ]],
        ],
    ]);
    $entityManager->getUnitOfWork()->willReturn($unitOfWork);

如果您在测试中扩展 TestCase class,您还可以执行以下操作:

$uow = $this->createMock(UnitOfWork::class);
$uow->method('getEntityChangeSet')->willReturn(['_dataParent' => [0 => 2, 1 => 3]);