Search code examples
symfonydoctrine-ormphpunittdd

PHPUnit Doctrine Repository magic methods


I need some help to fully test my class that depends on Doctrine.

I have one method in my class that uses that magic method findOneBy... from the Doctrine EntityRepository, as showed below:

enter image description here

But when I run my test I always have this warning:

enter image description here

How can I mock that call? Below I put how EntityManager magic __call method supposed to work:

enter image description here


Solution

  • You can mock magic method __call which is invoked when invoking unacessible methods.

    $mock
        ->expects($this->once())
        ->method('__call')
       ->with(
          $this->equalTo('findOneByIdShopUrl'), //
          $this->equalTo(['5'])
       )
       ->willReturn(['shop' => 'shop info']); // return shop object
    

    Check also http://php.net/manual/en/language.oop5.overloading.php#object.call

    The other option is to use setMethods()

    $this->getMockBuilder('ShopRepository')->setMethods(['findOneByShopId'])->getMock(); 
    

    and then the rest logic with methods like will, with etc.