Reputation: 3498
I have a function in my HandlerClass that works fine:
/**
* @param $entity
* @return null|object
*/
public function findEntityById($id)
{
if($id)
{
$entity = $this->repository->find($id);
if (empty($entity)) {
return false;
}
return $entity;
}
return false;
}
I wrote a test for a function:getCheckedList(), which uses this function:findEntityById(). I mocked the function:findEntityById(), so that it should return false.
$reviewHandler = $this->getMock(
'My\Bundle\Handler\RevHandler',
array('saveEntity'),
array(
$this->entityManager,
'My\Bundle\Entity\Rev',
$this->guzzleClient,
$this->serializer,
$this->apiSettings
)
);
$reviewHandler->expects($this->once())
->method('findEntityById')
->will($this->returnValue(false));
$result = $reviewHandler->getCheckedList(22);
$this->assertArrayHasKey(0,$result);
$this->assertEquals(22,$result[0]);
But then I get an error after my test:report:phpunit
PHP Fatal error: Call to a member function find() on a non-object on line 152
And this is this line in my function:findEntityById() which is mocked and which throws an error in my test:
$entity = $this->repository->find($id);
In the __construct form my RevHandler I call the parent::__construct:
public function __construct($entityManager, $entityClass, $guzzleClient, $serializer, $apiSettings)
{
parent::__construct($entityManager, $entityClass, $guzzleClient, $serializer, $apiSettings);
}
In my parent::__construct it looks like this:
public function __construct(EntityManager $entityManager, $entityClass, $guzzleClient, $serializer, $apiSettings)
{
$this->entityManager = $entityManager;
$this->entityClass = $entityClass;
$this->repository = $this->entityManager->getRepository($this->entityClass);
$this->guzzleClient = $guzzleClient;
$this->serializer = $serializer;
$this->apiSettings = $apiSettings;
}
The code without testing works fine, but I don't know what can be wrong when I'm testing. Any idea? THANKS!!!
Upvotes: 0
Views: 4981
Reputation: 3498
Have it, it was too simple and a really stupid failure from me:
WRONG:
$reviewHandler = $this->getMock(
'My\Bundle\Handler\RevHandler',
array('saveEntity'),
array(
$this->entityManager,
'My\Bundle\Entity\Rev',
$this->guzzleClient,
$this->serializer,
$this->apiSettings
)
);
RIGHT:
$reviewHandler = $this->getMock(
'My\Bundle\Handler\RevHandler',
array('findEntityById'),
array(
$this->entityManager,
'My\Bundle\Entity\Rev',
$this->guzzleClient,
$this->serializer,
$this->apiSettings
)
);
I gave the wrong function to my mocked object! Wrong: saveEntity Right: findEntityById
Upvotes: 2