Reputation: 620
I have a problem with making a test.
I have made a function in my class that counts all the rows in a database table. To access to the database i use Zend Frameworks TableGateway class. My problem is that i don't know how to write a test for the function.
One way to look on it is that the function is so simple that it needs no tests but it would be nice to know how i can make it work.
There is no function on the AbstractTableGateway that allow me to set the internal variable $adapter. How can i set the protected variable $adapter?
Function
public function count()
{
$sql = "SELECT COUNT(*) AS Count FROM ". $this->tableGateway->getTable();
$statement = $this->tableGateway->adapter->query($sql);
$result = $statement->execute()->current();
return $result['Count'];
}
Test for function
public function testCount()
{
$sql = "SELECT COUNT(*) AS Count FROM DbTable";
$result = $this->prophesize(Result::class);
$result->current()->willReturn(["Count" => 10]);
$statement = $this->prophesize(Statement::class);
$statement->execute()->willReturn($result);
$adapter = $this->prophesize(Adapter::class);
$adapter->query($sql)->willReturn($statement);
$this->tableGateway->adapter = $adapter;
$this->assertSame(10, $this->DbTable->count());
}
Upvotes: 0
Views: 263
Reputation: 407
TableGateway
gets its adapter from constructor. Whatever factory creates TableGateway
instance at your $this->tableGateway
property, should pass your mock adapter (or real adapter if this is aimed as integration test) to its constructor.
Upvotes: 1