How to check Doctrine Entity Manager findOneById method with prophecy?

I use prophecy to write unit tests

"require": {
    ...,
    "phpspec/prophecy-phpunit": "~1.0"
},

and i have a call

 $dbUser = $this
            ->em
            ->getRepository('MainBundle:User')
            ->findOneById($id);

when testing, I get an error because the findOneByProperty method is not defined. Except for changing the source code to:

 $dbUser = $this
            ->em
            ->getRepository('MainBundle:User')
            ->findOneBy(array('id' => $id);

I did not find another solution. Is there a way to verify this using prophecy and keep the source code?

+4
source share
2 answers

If by "source code" you meant client code , you can work around this by creating a custom object repository.

class UserRepository extends EntityRepository
{
    public function findOneById(int $id)
    {
        return parent::findOneById($id);
    }
}

http://symfony.com/doc/current/doctrine/repository.html

$userRepository = $this->prophesize(UserRepository::class);

$em = $this->prophesize(EntityManagerInterface::class);
$em->getRepository('MainBundle:User')->willReturn($userRepository->reveal());
+1

find() . , , :

$dbUser = $this
           ->em
           ->getRepository('MainBundle:User')
           ->find($id);

. .

-1

All Articles