Disclaimer: I'm new to unit testing and just getting my feet wet. That being said, I understand 100% is not always possible, but I'm curious if there is a solution to code coverage with if/else statements such as this or if they are just untestable.
The function:
public function getAll()
{
// Query table for all rows sorted by name
$select = $this->select();
$select->order('name ASC');
$rowset = $this->fetchAll($select);
// Validate and return row
if($rowset->current())
{
// Return rowset
return $rowset;
}
else { return false; }
}
The Unit Test
public function testCanGetAll()
{
// Try to get all states
$result = $this->model->getAll();
$this->assertNotNull($result);
$this->assertNotEquals(false,$result);
}
The Result
As you can see, I can't cover the return false;
line. The only way I can think of to test it is to rename my database table or something drastic like that.
Is there another way to write functions with if/else statements (which are a fairly common practice) which allows for easier code coverage?
[EDIT] Perhaps it's better to not check results in the function, and just return it? Whatever calls this function has to do it's own checking?
public function getAll()
{
// Query table for all rows sorted by name
$select = $this->select();
$select->order('name ASC');
$rowset = $this->fetchAll($select);
// Return rowset whether valid or not
return $rowset;
}