Recently I started to write PHPUnit test. And this is my model code.(I used CodeIgniter 3).
Account_model.php
class Account_model extends CI_Model
{
...
public function select_by_seq($seq = '', $select_columns = [])
{
try {
$bind = [':a_seq' => $seq];
// check $select_colums is exist in table
if ($this->check_column($select_columns) === false)
{
throw new Exception('columns illegal', 201);
}
...
$sql = "select ....
from {$this->db->dbprefix('account')}
where a_seq = :a_seq";
$query = $this->db->query($sql, $bind);
// ===== this always not runing. =====
if ($query === false)
{
// ===== this always not runing. =====
throw new Exception('sql errors', 301);
}
else
{
return $query->result_array();
}
}
catch (Exception $error)
{
// set error log
$this->set_error_log($error->getCode() . $error->getMessage());
}
return false;
}
}
This is my test Account_model_test.php
class Account_model_test extends TestCase
{
public static function setUpBeforeClass()
{
parent::setUpBeforeClass();
$CI =& get_instance();
}
public function setUp()
{
$this->resetInstance();
loader('model', 'account_model');
$this->obj = $this->CI->account_model;
}
public function test_select_by_seq()
{
$result = $this->obj->select_by_seq(
$seq = '20160830'
);
$this->assertCount(1, $result);
}
public function test_select_by_seq_with_illegal_column()
{
$result = $this->obj->select_by_seq(
$seq = '20160830',
$select_columns = ['illegal']
);
$this->assertFalse($result);
}
...
}
Because I write SQL by myself. I founded my PHPUnit test can't cover this if ($query === false)
. And then my code coverage didn't achieve 100%. This problem let me think the 100% is very important for the unit test? Or I had to modify my model code? Thanks your help.