Reputation: 1010
I'm building a ZF2 application with Doctrine 2, and am trying to test it by using mock objects. The original action I'm trying to test is as follows:
public function indexAction()
{
$title = 'File Types';
$this->layout()->title = $title;
$em = $this->serviceLocator->get('entity_manager');
$fileTypes = $em->getRepository('Resource\Entity\FileType')
->findBy(array(), array('type' => 'ASC'));
return array(
'title' => $title,
'fileTypes' => $fileTypes
);
}
In my test, I use the following method to create mocks of the entity manager and the FileTypes entity repository:
public function mockFiletypeResult($output)
{
$emMock = $this->getMockBuilder('Doctrine\ORM\EntityManager')
->disableOriginalConstructor()
->getMock();
$repositoryMock = $this->getMock('Resource\Entity\FileType');
$repositoryMock->expects($this->any())
->method('findBy')
->will($this->returnValue($output));
$emMock->expects($this->any())
->method('getRepository')
->will($this->returnValue($repositoryMock));
$this->getApplicationServiceLocator()->setAllowOverride(true);
$this->getApplicationServiceLocator()->setService('Resource\Entity\FileType', $repositoryMock);
$this->getApplicationServiceLocator()->setService('Doctrine\ORM\EntityManager', $emMock);
}
The above code is based on what I read in this post on stackoverflow.
The problem is, when I run the test, I get the following error: Fatal error: Call to undefined method Mock_FileType_39345bde::findBy() in /path/to/test
. What am I doing wrong? I've looked around a bunch but can't seem to figure out the problem.
EDIT: I had originally written that the error message was complaining about the unidentified findAll() method, when it was actually findBy().
EDIT 2: I have since tried adding a new method to my entity repository, as follows:
public function getFileTypes()
{
$query = $this->_em->createQuery('SELECT t
FROM Resource\Entity\FileType t
ORDER BY t.type ASC, t.extension ASC');
return $query->getResult();
}
I then tried replacing the findBy method with getFileTypes in my controller, and stubbing out the getFileTypes in the mock. Same problem: it says it can't find the method.
One more thing: not sure if it matters or not, but I'm using PHPUnit version 3.7. For some reason I thought the 4.x versions didn't work properly with ZF2. Should I upgrade?
Upvotes: 2
Views: 3801
Reputation: 8276
If you don't use getMockBuilder()
on your repository, it will expect you to stub any methods that are called. If you use getMockBuilder()
, it will automatically replace all functions with a dummy implementation that returns null
.
So you can either use the mock builder
$repositoryMock =
$this->getMockBuilder('\Doctrine\ORM\EntityRepository')->getMock();
or stub out the findBy()
function that is being called elsewhere
$repositoryMock->expects($this->any())
->method('findBy')
->will($this->returnValue(null));
See more: https://phpunit.de/manual/current/en/test-doubles.html
EDIT:
I just noticed that you are mocking your entity, but you need to be mocking either your repository or Doctrine's. See my edited comment above. If you keep the findBy()
method you can simply mock the EntityRepository class. If you had your own (such as Resources\Repository\FileTypeRepository), you could mock that instead.
Also, you might need to put the \
at the beginning of your MockBuilder calls so they are properly namespaced.
Upvotes: 8