Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wowo/1331789 to your computer and use it in GitHub Desktop.
Save wowo/1331789 to your computer and use it in GitHub Desktop.
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!
}
}
@norbineogen
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();

@ScottA38
Copy link

ScottA38 commented May 4, 2020

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();
}

Hey @monbro, do these mocks exist for ORM?

@monbro
Copy link

monbro commented May 4, 2020

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();
}

Hey @monbro, do these mocks exist for ORM?

no Idea my friend, it has been 5 years this snippet...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment