Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHPUnit's way to mock Doctrine2 Entity Manager
<?php
class AbstractManagerBase extends \PHPUnit_Framework_TestCase
{
protected function getEmMock()
{
$emMock = $this->getMock('\Doctrine\ORM\EntityManager',
array('getRepository', 'getClassMetadata', 'persist', 'flush'), array(), '', false);
$emMock->expects($this->any())
->method('getRepository')
->will($this->returnValue(new FakeRepository()));
$emMock->expects($this->any())
->method('getClassMetadata')
->will($this->returnValue((object)array('name' => 'aClass')));
$emMock->expects($this->any())
->method('persist')
->will($this->returnValue(null));
$emMock->expects($this->any())
->method('flush')
->will($this->returnValue(null));
return $emMock; // it tooks 13 lines to achieve mock!
}
}
@jsifalda

This comment has been minimized.

Copy link

jsifalda commented Dec 24, 2012

Please give me example of "FakeRepository" which you use.

@jchan-gcn

This comment has been minimized.

Copy link

jchan-gcn commented Jan 18, 2013

@jsifalda Here's my example from the code:

public function createLoadedMockedDoctrineRepository($repository, $repositoryName,$repositoryMethod,$repositoryMethodReturnVal) {
        $mockEM=$this->getMock('\Doctrine\ORM\EntityManager',
            array('getRepository', 'getClassMetadata', 'persist', 'flush'), array(), '', false);
        $mockSVRepo=$this->getMock($repository,array($repositoryMethod),array(),'',false);

        $mockEM->expects($this->any())
            ->method('getClassMetadata')
            ->will($this->returnValue((object)array('name' => 'aClass')));
        $mockEM->expects($this->any())
            ->method('persist')
            ->will($this->returnValue(null));
        $mockEM->expects($this->any())
            ->method('flush')
            ->will($this->returnValue(null));

        $mockSVRepo
            ->expects($this->once())
            ->method($repositoryMethod)
            ->will($this->returnValue($repositoryMethodReturnVal));

        $mockEM->expects($this->once())
            ->method('getRepository')
            ->with($repositoryName)
            ->will($this->returnValue($mockSVRepo));

        return $mockEM;
    }
@mageekguy

This comment has been minimized.

Copy link

mageekguy commented Apr 11, 2013

<?php

namespace vendor\project\tests\units;

use atoum;

// with atoum (http://www.atoum.org/atoum)

class Test extends atoum\test
{
   protected function getEmMock()
   {   
        $emMock  = new \mock\Doctrine\ORM\EntityManager();
        $this->calling($emMock)->getRepository = new FakeRepository();
        $this->calling($emMock)->getClassMetadata = (object) array('name' => 'aClass');

        return $emMock;
   }
}
@monbro

This comment has been minimized.

Copy link

monbro commented Jan 29, 2014

You could also work with

use Doctrine\ODM\MongoDB\Tests\Mocks\DocumentManagerMock;
use Doctrine\ODM\MongoDB\Tests\Mocks\ConnectionMock;

public function setUp()
{
        parent::setUp();
        $this->dm = DocumentManagerMock::create(new ConnectionMock());
        $this->uow = $this->dm->getUnitOfWork();
}
@AbderrEdrs

This comment has been minimized.

Copy link

AbderrEdrs commented Mar 2, 2015

How about remove ?? can i use mock or not ?

        $emMock->expects($this->any())->method('remove')
            ->with($this->setMockedData(null))
            ->will($this->returnValue(true));
@func0der

This comment has been minimized.

Copy link

func0der commented Jan 13, 2016

Good and fine.

My problem is: How do I get this mock into the controller I am currently testing?

I have a ToTestController::setEntityManager() method (not static) that would be used to inject the mock. But how do I call it from the controller test case?

@norbineogen

This comment has been minimized.

Copy link

norbineogen commented Jan 8, 2018

Another trick for mocking:

use Prophecy\Argument;
use Prophecy\Prophecy\ProphecyInterface;

     /**
     * @var ProphecyInterface|ObjectRepository
     */
    $prophet = $this->prophesize(ObjectRepository::class);
    //mocked repository with exact parameter - if findOneBy is called with another parameter test will fail
    $prophet->findOneBy(['something' => '123123'])->willReturn(null);
   // instance mocked repository
   $objectRepoMock =  $prophet->reveal(); 

    /**
     * mock em
     * @var ProphecyInterface|EntityManager $prophet
     */
    $prophet = $this->prophesize(EntityManager::class);
    //mock with exact parameter
    $prophet->getRepository('MyBundle:SomeClass')->willReturn($someRepoMock);
    //mock with any parameter
    $prophet->getRepository(Argument::any())->willReturn($objectRepoMock);
    $prophet->persist(Argument::any())->willReturn(null);
    $prophet->flush(Argument::any())->willReturn(null);
    $emMock = $prophet->reveal();

  /** 
     * mock registry
     * @var ProphecyInterface|Registry $prophet
     */
    $prophet = $this->prophesize(Registry::class);
    $prophet->getManager()->willReturn($emMock);
    $registryMock = $prophet->reveal();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.