Doctrine ORM create method phpspec test failure

148 Views Asked by At

I try to write, at first glance, it would seem a trivial test for my repository's "update" method:

<?php

declare(strict_types=1);

namespace Paneric\Authorization\ORM\Repository;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\ORMException;
use Paneric\Authorization\DTO\FieldDTO;
use Paneric\Authorization\ORM\Entity\Field;
use Doctrine\ORM\EntityRepository;
use Paneric\Authorization\Interfaces\FieldRepositoryInterface;

class FieldRepository extends EntityRepository implements FieldRepositoryInterface
{
    const ENTITY_CLASS = Field::class;


    public function __construct(EntityManagerInterface $_em)
    {
        parent::__construct($_em, $_em->getClassMetadata(self::ENTITY_CLASS));
    }
...
    public function update(int $fieldId, FieldDTO $fieldDTO): void
    {
        try {
            $field = $this->find($fieldId);
            $field->transfer($fieldDTO);

            $this->_em->flush();
        } catch (ORMException $e) {
            echo $e->getMessage();
        }
    }
...
}

with a spec method:

<?php

namespace spec\Paneric\Authorization\ORM\Repository;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\QueryBuilder;
use Paneric\Authorization\DTO\FieldDTO;
use Paneric\Authorization\ORM\Entity\Field;
use Paneric\Authorization\ORM\Repository\FieldRepository;
use Doctrine\ORM\AbstractQuery;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class FieldRepositorySpec extends ObjectBehavior
{
    public function let(EntityManagerInterface $_em, ClassMetadata $classMetadata)
    {
        $_em->getClassMetadata(Field::class)->willReturn($classMetadata);
        $this->beConstructedWith($_em);
    }
...
    public function it_updates(Field $field, FieldDTO $fieldDTO, EntityManagerInterface $_em)
    {
        $fieldId = 1;

        $field = $this->find($fieldId);

        $field->transfer($fieldDTO)->shouldBeCalled();

        $_em->flush()->shouldBeCalled();

        $this->update($fieldId, $fieldDTO);
    }
...
}

and receive the following error:

Unexpected method call on Double\EntityManagerInterface\EntityManagerInterface\P1:
  - find(
        null,
        1,
        null,
        null
    )
expected calls were:
  - getClassMetadata(
        exact("Paneric\Authorization\ORM\Entity\Field")
    )
  - find(
        exact("Paneric\Authorization\ORM\Entity\Field"),
        exact(1)
    )
  - flush(

    )

Apparently issue is related to the call:

...
$field = $this->find($fieldId);
...

Although the second remark related to getClassMetadata, looks strange, considering the fact that my spec let method:

    public function let(EntityManagerInterface $_em, ClassMetadata $classMetadata)
    {
        $_em->getClassMetadata(Field::class)->willReturn($classMetadata);
        $this->beConstructedWith($_em);
    }

does its job in case of other spec tests.

Can anyone help me to solve this issue ? Thx in advance.

1

There are 1 best solutions below

0
user3555602 On

In my repository's "update" metod, line:

$field = $this->find($fieldId);

has to be replaced by:

$field = $this->_em->find(Field::class, $fieldId);

so the complete spec test looks like:

    public function it_updates(Field $field, FieldDTO $fieldDTO, EntityManagerInterface $_em)
    {
        $fieldId = 1;

        $_em->find(Field::class, $fieldId)->willReturn($field);

        $field->transfer($fieldDTO)->shouldBeCalled();

        $_em->flush()->shouldBeCalled();

        $this->update($fieldId, $fieldDTO);
    }