I have this code in my PHPUnit test and it's trying to mock a table entity. But when i try to run it i get this error.
Expectation failed for method name is equal to when invoked 1 time(s). Method was expected to be called 1 times, actually called 0 times.
public function testFetchSingleById()
{
$mockJobTable = $this->getMockBuilder('Job\Table\JobTable')
->disableOriginalConstructor()
->setMethods(['fetchSingleById'])
->getMock();
$firstDateRun = new \DateTime('2016-06-03');
$firstDateRun = $firstDateRun->format('Y-m-d H:i:s');
$job = new JobEntity();
$job->exchangeArray([
'id' => 1,
'name' => 'parse',
'params' => '--dry-run',
'setFirstDateRun' => $firstDateRun,
'period' => '* * * * *'
]);
$mockJobTable->expects($this->once())
->method('fetchSingleById')
->with(1)
->will($this->returnValue($job));
}
method that i am testing:
class JobTable extends TableGateway implements JobTableInterface
{
public function __construct(Adapter $adapter, JobEntity $entity)
{
$resultSet = new ResultSet();
$resultSet->setArrayObjectPrototype($entity);
parent::__construct('job', $adapter, null, $resultSet);
}
public function fetchSingleById($id)
{
$select = $this->getSql()->select();
$select->where->equalTo('id', $id);
return $this->selectWith($select)->current();
}
}
I am using ZF3.
I'm missing something but I don't know what.
The code sets up an expectation and then does nothing with it. There has to be something that actually invokes fetchSingleById
on the mock object. Also, from the name of the test it looks like it's mocking the very method it's supposed to test. There's no need to test PHPUnit in a test case that's not part of PHPUnit itself.