Search code examples
phpdoctrine-ormphpspec

PHPSpec: function returning by reference


I updated Doctrine 2.5 to 2.6 in my project and phpspec is broken.

The function getEntityChangeSet() is now returned by reference. It seems not to be supported by phpspec.

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

The response is returning by reference not supported

the underlying function (doctrine/doctrine2) is

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

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

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

Do you know if it's possible to bypass this or change test for make it work?


Solution

  • The answer has been given on Twitter by @Pamilme

    You have to mock UnitOfWork with Mockery. An example can be found 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);