I’m trying to write a unit test with phpunit for a model that uses doctrine 2. I want to mock the doctrine entities but I really don’t have a clue of how to do this. Can anyone explain to me how I need to do this? I’m using Zend Framework.
The model that needs to be tested
class Country extends App_Model
{
public function findById($id)
{
try {
return $this->_em->find('Entities\Country', $id);
} catch (\Doctrine\ORM\ORMException $e) {
return NULL;
}
}
public function findByIso($iso)
{
try {
return $this->_em->getRepository('Entities\Country')->findOneByIso($iso);
} catch (\Doctrine\ORM\ORMException $e) {
return NULL;
}
}
}
Bootstrap.php
protected function _initDoctrine()
{
Some configuration of doctrine
...
// Create EntityManager
$em = EntityManager::create($connectionOptions, $dcConf);
Zend_Registry::set('EntityManager', $em);
}
Extended model
class App_Model
{
// Doctrine 2.0 entity manager
protected $_em;
public function __construct()
{
$this->_em = Zend_Registry::get('EntityManager');
}
}
Doctrine 2 Entities should be treated like any old class. You can mock them just like any other object in PHPUnit.
As of PHPUnit 5.4, the method getMock() has been depricated. Use createMock() or getMockbuilder() instead.
As @beberlei noted, you are using the EntityManager inside the Entity class itself, which creates a number of sticky problems, and defeats one of the major purposes of Doctrine 2, which is that Entity’s aren’t concerned with their own persistance. Those ‘find’ methods really belong in a repository class.