Codeigniter Unit Testing Models

I'm new to unit testing, so maybe this is a bit of a dumb question. Imagine we have a simple model method.

public function get_all_users($uid = false, $params = array()){ $users = array(); if(empty($uid) && empty($params)){return $users;} $this->db->from('users u'); if($uid){ $this->db->where('u.id',(int)$id); } if(!empty($params)){ if(isset($params['is_active']){ $this->db->where('u.status ', 'active'); } if(isset($params['something_else']){ // some more filter actions} } $q = $this->db->get(); if($q->num_rows()){ foreach($q->result_array() as $user){ $users[$user['id']] = $user; } } $q->free_result(); return $users; } 

The question is, how will _good test be written for it? UPD: I think the best library for testing modules for CI is Toast, so the example I'm looking for is preferable to write with it. Thanks.

+6
php unit-testing codeigniter models
source share
1 answer

I also use toast, and I mainly use it to test model methods. To do this, first truncate all the table values, insert the predefined value, and then get it. This is an example of a test that I used in my application:

 class Jobads_tests extends Toast { function Jobads_tests() { parent::Toast(__FILE__); // Load any models, libraries etc. you need here $this->load->model('jobads_draft_model'); $this->load->model('jobads_model'); } /** * OPTIONAL; Anything in this function will be run before each test * Good for doing cleanup: resetting sessions, renewing objects, etc. */ function _pre() { $this->adodb->Execute("TRUNCATE TABLE `jobads_draft`"); } /** * OPTIONAL; Anything in this function will be run after each test * I use it for setting $this->message = $this->My_model->getError(); */ function _post() { $this->message = $this->jobads_draft_model->display_errors(' ', '<br/>'); $this->message .= $this->jobads_model->display_errors(' ', '<br/>'); } /* TESTS BELOW */ function test_insert_to_draft() { //default data $user_id = 1; //test insert $data = array( 'user_id' => $user_id, 'country' => 'ID', 'contract_start_date' => strtotime("+1 day"), 'contract_end_date' => strtotime("+1 week"), 'last_update' => time() ); $jobads_draft_id = $this->jobads_draft_model->insert_data($data); $this->_assert_equals($jobads_draft_id, 1); //test update $data = array( 'jobs_detail' => 'jobs_detail', 'last_update' => time() ); $update_result = $this->jobads_draft_model->update_data($jobads_draft_id, $data); $this->_assert_true($update_result); //test insert_from_draft $payment_data = array( 'activation_date' => date('Ym-d', strtotime("+1 day")), 'duration_amount' => '3', 'duration_unit' => 'weeks', 'payment_status' => 'paid', 'total_charge' => 123.45 ); $insert_result = $this->jobads_model->insert_from_draft($jobads_draft_id, $payment_data); $this->_assert_true($insert_result); //draft now must be empty $this->_assert_false($this->jobads_draft_model->get_current_jobads_draft($user_id)); } } 

I use AdoDB in my application, but do not get confused with this. You can execute $this->db inside the test controller after loading the database library. You can put it in autoload so that it automatically loads.

See that in my code the table is truncated before running the test. After starting, I will get any error that may occur. I claim for a predefined insert and update. Using Toast to test the model allows you to make sure that the model method performs exactly the task that you want to do. Do the test you need and make sure that you cover all the possibilities of input and output of values.

+10
source share

All Articles