lundi 5 janvier 2015

Mocking the database

I want to mock two entities, each should contain two result sets (aka rows in a database). How can I do this? Right now I have mocked just one repository, and it doesn't work.



$entityManager->getRepository('AcmeBundle:User')->getLogin();


displays that the getRepository method wasn't found.


Also, how can I add another entity object to the same mocked entityManager?


Right now it looks like this:



$repository = $this->getMockBuilder('\Doctrine\ORM\EntityRepository')
->disableOriginalConstructor()
->getMock();

$userObj = $this->getMock('AcmeBundle\Entity\User');

$userObj->expects($this->any())
->method('getLogin')
->will($this->returnValue('aaa'));
$userObj->expects($this->any())
->method('getPassword')
->will($this->returnValue('pass1'));

$repository->expects($this->any())
->method('find')
->will($this->returnValue($userObj));

unset($userObj);

$userObj = $this->getMock('AcmeBundle\Entity\User');

$userObj->expects($this->any())
->method('getLogin')
->will($this->returnValue('bbb'));
$userObj->expects($this->any())
->method('getPassword')
->will($this->returnValue('pass2'));

$repository->expects($this->any())
->method('find')
->will($this->returnValue($userObj));

$entityManager = $this->getMockBuilder('\Doctrine\Common\Persistence\ObjectManager')
->disableOriginalConstructor()
->getMock();

$entityManager->expects($this->any())
->method('getRepository')
->will($this->returnValue($repository));

Aucun commentaire:

Enregistrer un commentaire