* Completed admins_model.php and secretaries_model.php
* Completed unit testing for admins_model.php and secretaries_model.php
This commit is contained in:
parent
6b88842d4f
commit
b5e771339c
7 changed files with 1246 additions and 48 deletions
|
@ -23,7 +23,9 @@ class Unit_tests extends CI_Driver_Library {
|
|||
'Unit_tests_customers_model',
|
||||
'Unit_tests_providers_model',
|
||||
'Unit_tests_services_model',
|
||||
'Unit_tests_settings_model'
|
||||
'Unit_tests_settings_model',
|
||||
'Unit_tests_admins_model',
|
||||
'Unit_tests_secretaries_model'
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -53,11 +55,13 @@ class Unit_tests extends CI_Driver_Library {
|
|||
*/
|
||||
public function run_model_tests($output_report = true) {
|
||||
// @task Reenable all model tests.
|
||||
//$this->appointments_model->run_all();
|
||||
//$this->customers_model->run_all();
|
||||
//$this->settings_model->run_all();
|
||||
$this->providers_model->run_all();
|
||||
//$this->services_model->run_all();
|
||||
// $this->appointments_model->run_all();
|
||||
// $this->customers_model->run_all();
|
||||
// $this->settings_model->run_all();
|
||||
// $this->providers_model->run_all();
|
||||
// $this->services_model->run_all();
|
||||
// $this->admins_model->run_all();
|
||||
$this->secretaries_model->run_all();
|
||||
|
||||
if ($output_report) {
|
||||
$this->CI->output->append_output($this->CI->unit->report());
|
||||
|
|
|
@ -0,0 +1,416 @@
|
|||
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
||||
|
||||
class Unit_tests_admins_model extends CI_Driver {
|
||||
private $ci;
|
||||
private $admin_role_id;
|
||||
private $default_admin; // does not contain an 'id' value
|
||||
|
||||
/**
|
||||
* Class Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->ci =& get_instance();
|
||||
$this->ci->load->library('Unit_test');
|
||||
$this->ci->load->model('admins_model');
|
||||
|
||||
$this->admin_role_id = $this->ci->db->get_where('ea_roles',
|
||||
array('slug' => DB_SLUG_ADMIN))->row()->id;
|
||||
|
||||
$this->default_admin = array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'email' => 'john@doe.com',
|
||||
'mobile_number' => '2340982039',
|
||||
'phone_number' => '9098091234',
|
||||
'address' => 'Some Street 80',
|
||||
'city' => 'Some City',
|
||||
'state' => 'Some State',
|
||||
'zip_code' => '12345',
|
||||
'notes' => 'This is a test admin user.',
|
||||
'id_roles' => $this->admin_role_id
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run all the available tests
|
||||
*/
|
||||
public function run_all() {
|
||||
// All the methods whose names start with "test" are going to be
|
||||
// executed. If you want a method to not be executed remove the
|
||||
// "test" keyword from the beginning.
|
||||
$class_methods = get_class_methods('Unit_tests_admins_model');
|
||||
foreach ($class_methods as $method_name) {
|
||||
if (substr($method_name, 0, 5) === 'test_') {
|
||||
call_user_func(array($this, $method_name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// UNIT TESTS
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// TEST ADD METHOD ------------------------------------------------------
|
||||
private function test_add_insert() {
|
||||
$admin = $this->default_admin;
|
||||
|
||||
$admin['id'] = $this->ci->admins_model->add($admin);
|
||||
$this->ci->unit->run($admin['id'], 'is_int', 'Test if add() - insert operation - has '
|
||||
. 'has returned an integer value.');
|
||||
|
||||
$db_record = $this->ci->db->get_where('ea_users', array('id' => $admin['id']))->row_array();
|
||||
$this->ci->unit->run($admin, $db_record, 'Test if add() - insert operation - has '
|
||||
. 'successfully inserted a new admin record.');
|
||||
|
||||
$this->ci->db->delete('ea_users', array('id' => $admin['id']));
|
||||
}
|
||||
|
||||
private function test_add_update() {
|
||||
// Insert an admin record first and then update it.
|
||||
$admin = $this->default_admin;
|
||||
$this->ci->db->insert('ea_users', $admin);
|
||||
$admin['id'] = intval($this->ci->db->insert_id());
|
||||
|
||||
$admin['first_name'] = 'First Name Changed';
|
||||
$admin['last_name'] = 'Last Name Changed';
|
||||
$admin['email'] = 'email@changed.com';
|
||||
$admin['mobile_number'] = 'Mobile Number Changed';
|
||||
$admin['phone_number'] = 'Phone Number Changed';
|
||||
$admin['address'] = 'Address Changed';
|
||||
$admin['city'] = 'City Changed';
|
||||
$admin['zip_code'] = 'Zip Code Changed';
|
||||
$admin['notes'] = 'Notes Changed';
|
||||
|
||||
$update_result = $this->ci->admins_model->add($admin); // updates record
|
||||
$this->ci->unit->run($update_result, 'is_int', 'Test if add() - update operation - has '
|
||||
. 'returned a valid integer value.');
|
||||
|
||||
$db_record = $this->ci->db->get_where('ea_users', array('id' => $admin['id']))->row_array();
|
||||
$this->ci->unit->run($admin, $db_record, 'Test if add() - update operation - has '
|
||||
. 'successfully updated an existing admin record.');
|
||||
|
||||
$this->ci->db->delete('ea_users', array('id' => $admin['id']));
|
||||
}
|
||||
|
||||
private function test_add_with_invalid_data() {
|
||||
$admin = $this->default_admin;
|
||||
$admin['email'] = 'Invalid Email Value';
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->admins_model->add($admin);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if add() has thrown an exception with '
|
||||
. 'invalid data.');
|
||||
}
|
||||
|
||||
|
||||
// TEST EXITS METHOD ----------------------------------------------------
|
||||
private function test_exists_with_record_that_exists() {
|
||||
$admin = $this->default_admin;
|
||||
$this->ci->db->insert('ea_users', $admin);
|
||||
$admin['id'] = intval($this->ci->db->insert_id());
|
||||
|
||||
$exists = $this->ci->admins_model->exists($admin);
|
||||
$this->ci->unit->run($exists, TRUE, 'Test if exists() has returned TRUE on record '
|
||||
. 'that exists.');
|
||||
|
||||
$this->ci->db->delete('ea_users', array('id' => $admin['id']));
|
||||
}
|
||||
|
||||
private function test_exists_with_invalid_data() {
|
||||
$admin = $this->default_admin;
|
||||
unset($admin['email']); // Email is mandatory for the function
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->admins_model->exists($admin);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if exists() has thrown an exception '
|
||||
. 'on invalid admin data.');
|
||||
}
|
||||
|
||||
private function test_exists_with_record_that_does_not_exist() {
|
||||
$admin = $this->default_admin;
|
||||
$exists = $this->ci->admins_model->exists($admin);
|
||||
$this->ci->unit->run($exists, FALSE, 'Test if exists() returned FALSE with record '
|
||||
. 'that does not exist.');
|
||||
}
|
||||
|
||||
// TEST DELETE METHOD ---------------------------------------------------
|
||||
private function test_delete() {
|
||||
$admin = $this->default_admin;
|
||||
$this->ci->db->insert('ea_users', $admin);
|
||||
$admin['id'] = intval($this->ci->db->insert_id());
|
||||
|
||||
$delete_result = $this->ci->admins_model->delete($admin['id']);
|
||||
$this->ci->unit->run($delete_result, TRUE, 'Test if delete() has returned TRUE on '
|
||||
. 'successfull deletion.');
|
||||
|
||||
$num_rows = $this->ci->db->get_where('ea_users', array('id' => $admin['id']))->num_rows();
|
||||
$this->ci->unit->run($num_rows, 0, 'Test if delete() has successfully removed the '
|
||||
. 'admin record from the database.');
|
||||
|
||||
if ($num_rows > 0) {
|
||||
$this->ci->db->delete('ea_users', array('id' => $admin['id']));
|
||||
}
|
||||
}
|
||||
|
||||
private function test_delete_with_record_that_does_not_exist() {
|
||||
$random_id = 2340923234;
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->admins_model->delete($random_id);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if delete() has thrown exception '
|
||||
. 'on record that does not exist.');
|
||||
}
|
||||
|
||||
private function test_delete_with_invalid_id_argument() {
|
||||
$invalid_id = 'Not Numeric Value';
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->admins_model->delete($invalid_id);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if delete() has thrown exception '
|
||||
. 'on invalid id argument.');
|
||||
}
|
||||
|
||||
private function test_delete_when_only_one_admin_user_left() {
|
||||
// E!A must always have at least one admin user in the system
|
||||
// This unit test requires only one admin record present in the database.
|
||||
$admin_id = $this->ci->db->get_where('ea_users', array('id_roles' => $this->admin_role_id))
|
||||
->row()->id;
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->admins_model->delete($admin_id);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if delete() has thrown exception '
|
||||
. 'when trying to delete the only one left admin record.');
|
||||
}
|
||||
|
||||
// TEST FIND RECORD ID METHOD -------------------------------------------
|
||||
private function test_find_record_id() {
|
||||
$admin = $this->default_admin;
|
||||
$this->ci->db->insert('ea_users', $admin);
|
||||
$admin['id'] = intval($this->ci->db->insert_id());
|
||||
|
||||
$result = $this->ci->admins_model->find_record_id($admin);
|
||||
$this->ci->unit->run($result, $admin['id'], 'Test if find_record_id() has successfully '
|
||||
. 'found the correct record id.');
|
||||
|
||||
$this->ci->db->delete('ea_users', array('id' => $admin['id']));
|
||||
}
|
||||
|
||||
private function test_find_record_id_with_no_email_provided() {
|
||||
$admin = $this->default_admin;
|
||||
unset($admin['email']);
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->admins_model->find_record_id($admin);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if find_record_id() has thrown '
|
||||
. 'an exception when trying to find a record id without email provided.');
|
||||
}
|
||||
|
||||
private function test_find_record_id_with_record_that_does_not_exist() {
|
||||
$admin = $this->default_admin;
|
||||
$has_thrown_ecx = FALSE;
|
||||
try {
|
||||
$this->ci->admins_model->find_record_id($admin);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_ecx = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_ecx, TRUE, 'Test if find_record_id() has thrown '
|
||||
. 'exception when trying to find record that does not exist.');
|
||||
}
|
||||
|
||||
|
||||
// TEST GET BATCH METHOD ------------------------------------------------
|
||||
private function test_get_batch() {
|
||||
$model_batch = $this->ci->admins_model->get_batch();
|
||||
$db_batch = $this->ci->db->get_where('ea_users', array('id_roles' => $this->admin_role_id))
|
||||
->result_array();
|
||||
$this->ci->unit->run($model_batch, $db_batch, 'Test if get_batch() has successfully '
|
||||
. 'returned an array of admin users.');
|
||||
}
|
||||
|
||||
private function test_get_batch_with_where_clause() {
|
||||
$admin = $this->default_admin;
|
||||
$this->ci->db->insert('ea_users', $admin);
|
||||
$admin['id'] = intval($this->ci->db->insert_id());
|
||||
|
||||
$model_batch = $this->ci->admins_model->get_batch(array('id' => $admin['id']));
|
||||
$db_batch = $this->ci->db->get_where('ea_users', array('id' => $admin['id']))->result_array();
|
||||
|
||||
$this->ci->unit->run($model_batch, $db_batch, 'Test if get_batch() with where clause '
|
||||
. 'has successfully returned the correct results.');
|
||||
|
||||
$this->ci->db->delete('ea_users', array('id' => $admin['id']));
|
||||
}
|
||||
|
||||
private function test_get_batch_with_invalid_where_clause() {
|
||||
// CI raises by itself an exception so this test case can be ignored.
|
||||
}
|
||||
|
||||
// TEST GET ROW METHOD --------------------------------------------------
|
||||
private function test_get_row() {
|
||||
$admin = $this->default_admin;
|
||||
$this->ci->db->insert('ea_users', $admin);
|
||||
$admin['id'] = intval($this->ci->db->insert_id());
|
||||
|
||||
$model_admin = $this->ci->admins_model->get_row($admin['id']);
|
||||
$this->ci->unit->run($model_admin, $admin, 'Test if get_row() has successfully '
|
||||
. 'returned the data of the selected row.');
|
||||
|
||||
$this->ci->db->delete('ea_users', array('id' => $admin['id']));
|
||||
}
|
||||
|
||||
private function test_get_row_invalid_argument() {
|
||||
$invalid_id = 'This is not numeric.';
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->admins_model->get_row($invalid_id);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if get_row() has thrown exception with '
|
||||
. 'invalid argument.');
|
||||
}
|
||||
|
||||
private function test_get_row_record_does_not_exist() {
|
||||
$random_id = 2390482039;
|
||||
$res = $this->ci->admins_model->get_row($random_id);
|
||||
$this->ci->unit->run($res, array(), 'Test if get_row() with record that does not exist '
|
||||
. 'has returned an empy array.');
|
||||
}
|
||||
|
||||
// TEST GET VALUE METHOD ------------------------------------------------
|
||||
private function test_get_value() {
|
||||
$admin = $this->default_admin;
|
||||
$this->ci->db->insert('ea_users', $admin);
|
||||
$admin['id'] = intval($this->ci->db->insert_id());
|
||||
|
||||
$last_name = $this->ci->admins_model->get_value('last_name', $admin['id']);
|
||||
$this->ci->unit->run($last_name, $admin['last_name'], 'Test if get_value() has successfully '
|
||||
. 'returned the correct value.');
|
||||
|
||||
$this->ci->db->delete('ea_users', array('id' => $admin['id']));
|
||||
}
|
||||
|
||||
private function test_get_value_invalid_field_name() {
|
||||
$field_name = 230982039; // invalid: not string
|
||||
$admin_id = 23; // random pick
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->admins_model->get_value($field_name, $admin_id);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if get_value() has thrown an exception '
|
||||
. 'with invalid field name argument.');
|
||||
}
|
||||
|
||||
private function test_get_value_invalid_admin_id() {
|
||||
$field_name = 'last_name'; // random pick
|
||||
$admin_id = 'This is invalid'; // not numerical
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->admins_model->get_value($field_name, $admin_id);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if get_value() has thrown an exception '
|
||||
. 'with invalid admin id argument.');
|
||||
}
|
||||
|
||||
private function test_get_value_record_does_not_exist() {
|
||||
$field_name = 'last_name'; // random pick
|
||||
$admin_id = 239409283092; // this id does not exist
|
||||
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->admins_model->get_value($field_name, $admin_id);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if get_value() has thrown an exception '
|
||||
. 'with record that does not exist.');
|
||||
}
|
||||
|
||||
private function tes_get_value_field_name_does_not_exist() {
|
||||
$field_name = 'this does not exist';
|
||||
$admin_id = 23; // random pick
|
||||
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->admins_model->get_value($field_name, $admin_id);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if get_value() has thrown an exception '
|
||||
. 'with field name that does not exist.');
|
||||
}
|
||||
|
||||
// TEST VALIDATE METHOD -------------------------------------------------
|
||||
private function test_validate() {
|
||||
$admin = $this->default_admin;
|
||||
$validation_result = $this->ci->admins_model->validate($admin);
|
||||
$this->ci->unit->run($validation_result, TRUE, 'Test if validate() has returned TRUE on '
|
||||
. 'valid admin data.');
|
||||
}
|
||||
|
||||
private function test_validate_record_does_not_exist() {
|
||||
$admin = $this->default_admin;
|
||||
$admin['id'] = 234092830; // record does not exist
|
||||
$validation_result = $this->ci->admins_model->validate($admin);
|
||||
$this->ci->unit->run($validation_result, FALSE, 'Test if validate() has returned FALSE on '
|
||||
. 'record that does not exist.');
|
||||
}
|
||||
|
||||
private function test_validate_missing_required_fields() {
|
||||
// required fields: last_name, email, phone_number
|
||||
|
||||
$admin = $this->default_admin;
|
||||
unset($admin['last_name']);
|
||||
unset($admin['email']);
|
||||
unset($admin['phone_number']);
|
||||
$validation_result = $this->ci->admins_model->validate($admin);
|
||||
$this->ci->unit->run($validation_result, FALSE, 'Test if validate() has returned FALSE on '
|
||||
. 'missing required field values.');
|
||||
}
|
||||
|
||||
private function test_validate_invalid_email() {
|
||||
$admin = $this->default_admin;
|
||||
$admin['email'] = 'This is invalid';
|
||||
$validation_result = $this->ci->admins_model->validate($admin);
|
||||
$this->ci->unit->run($validation_result, FALSE, 'Test if validate() has returned FALSE on '
|
||||
. 'invalid email address.');
|
||||
}
|
||||
}
|
||||
|
||||
/* End of file Unit_tests_admins_model.php */
|
||||
/* Location: ./application/libraries/Unit_tests/drivers/Unit_tests_admins_model.php */
|
|
@ -52,6 +52,7 @@ class Unit_tests_providers_model extends CI_Driver {
|
|||
'zip_code' => '12345',
|
||||
'notes' => 'This is a test provider',
|
||||
'id_roles' => $this->provider_role_id,
|
||||
'services' => array(),
|
||||
'settings' => array(
|
||||
'username' => 'test_prov',
|
||||
'password' => 'test_prov',
|
||||
|
@ -69,10 +70,15 @@ class Unit_tests_providers_model extends CI_Driver {
|
|||
$this->ci->unit->run($provider['id'], 'is_int', 'Check if add (insert) result is integer.');
|
||||
|
||||
// Check if record was inserted successfully.
|
||||
$db_provider = $this->ci->db->get_where('ea_users', array('id' => $provider['id']))->row_array();
|
||||
$db_provider['settings'] = $this->ci->db->get_where('ea_user_settings',
|
||||
array('id_users' => $provider['id']))->row_array();
|
||||
$db_provider = $this->ci->db
|
||||
->get_where('ea_users', array('id' => $provider['id']))
|
||||
->row_array();
|
||||
$db_provider['services'] = array();
|
||||
$db_provider['settings'] = $this->ci->db
|
||||
->get_where('ea_user_settings', array('id_users' => $provider['id']))
|
||||
->row_array();
|
||||
unset($db_provider['settings']['id_users']); // not needed
|
||||
|
||||
$this->ci->unit->run($provider, $db_provider, 'Check if add(insert) has successfully '
|
||||
. 'inserted a provider record.');
|
||||
|
||||
|
@ -129,6 +135,7 @@ class Unit_tests_providers_model extends CI_Driver {
|
|||
'zip_code' => 'CHANGED',
|
||||
'notes' => 'CHANGED',
|
||||
'id_roles' => $this->provider_role_id,
|
||||
'services' => array(),
|
||||
'settings' => array(
|
||||
'username' => 'CHANGED',
|
||||
'password' => 'CHANGED',
|
||||
|
@ -145,10 +152,15 @@ class Unit_tests_providers_model extends CI_Driver {
|
|||
$this->ci->unit->run($update_result, 'is_int', 'Check if add (update) result is integer.');
|
||||
|
||||
// Check if record was updated successfully
|
||||
$db_provider = $this->ci->db->get_where('ea_users', array('id' => $provider['id']))->row_array();
|
||||
$db_provider['settings'] = $this->ci->db->get_where('ea_user_settings',
|
||||
array('id_users' => $provider['id']))->row_array();
|
||||
$db_provider = $this->ci->db
|
||||
->get_where('ea_users', array('id' => $provider['id']))
|
||||
->row_array();
|
||||
$db_provider['services'] = array();
|
||||
$db_provider['settings'] = $this->ci->db
|
||||
->get_where('ea_user_settings', array('id_users' => $provider['id']))
|
||||
->row_array();
|
||||
unset($db_provider['settings']['id_users']);
|
||||
|
||||
$this->ci->unit->run($provider, $db_provider, 'Check if add(update) has successfully '
|
||||
. 'updated a provider record.');
|
||||
|
||||
|
@ -358,6 +370,7 @@ class Unit_tests_providers_model extends CI_Driver {
|
|||
'zip_code' => '12345',
|
||||
'notes' => 'This is a test provider',
|
||||
'id_roles' => $this->provider_role_id,
|
||||
'services' => array(),
|
||||
'settings' => array(
|
||||
'username' => 'test_prov',
|
||||
'password' => 'test_prov',
|
||||
|
@ -389,6 +402,7 @@ class Unit_tests_providers_model extends CI_Driver {
|
|||
'zip_code' => '12345',
|
||||
'notes' => 'This is a test provider',
|
||||
'id_roles' => $this->provider_role_id,
|
||||
'services' => array(),
|
||||
'settings' => array(
|
||||
'username' => 'test_prov',
|
||||
'password' => 'test_prov',
|
||||
|
@ -419,6 +433,7 @@ class Unit_tests_providers_model extends CI_Driver {
|
|||
'zip_code' => '12345',
|
||||
'notes' => 'This is a test provider',
|
||||
'id_roles' => $this->provider_role_id,
|
||||
'services' => array(),
|
||||
'settings' => array(
|
||||
'username' => 'test_prov',
|
||||
'password' => 'test_prov',
|
||||
|
@ -449,6 +464,7 @@ class Unit_tests_providers_model extends CI_Driver {
|
|||
'zip_code' => '12345',
|
||||
'notes' => 'This is a test provider',
|
||||
'id_roles' => $this->provider_role_id,
|
||||
'services' => array(),
|
||||
'settings' => array(
|
||||
'username' => 'test_prov',
|
||||
'password' => 'test_prov',
|
||||
|
@ -505,6 +521,8 @@ class Unit_tests_providers_model extends CI_Driver {
|
|||
$this->ci->db->insert('ea_users', $provider);
|
||||
$provider['id'] = intval($this->ci->db->insert_id());
|
||||
|
||||
$provider['services'] = array();
|
||||
|
||||
$provider['settings'] = array(
|
||||
'id_users' => $provider['id'],
|
||||
'username' => 'test-prov',
|
||||
|
@ -579,6 +597,8 @@ class Unit_tests_providers_model extends CI_Driver {
|
|||
$this->ci->db->insert('ea_users', $provider);
|
||||
$provider['id'] = $this->ci->db->insert_id();
|
||||
|
||||
$provider['services'] = array();
|
||||
|
||||
$provider['settings'] = array(
|
||||
'id_users' => $provider['id'],
|
||||
'username' => 'testprov',
|
||||
|
@ -594,9 +614,13 @@ class Unit_tests_providers_model extends CI_Driver {
|
|||
unset($provider['settings']['id_users']);
|
||||
|
||||
// Check if get_row() will successfully return the record.
|
||||
$no_model_provider = $this->ci->db->get_where('ea_users', array('id' => $provider['id']))->row_array();
|
||||
$no_model_provider['settings'] = $this->ci->db->get_where('ea_user_settings',
|
||||
array('id_users' => $provider['id']))->row_array();
|
||||
$no_model_provider = $this->ci->db
|
||||
->get_where('ea_users', array('id' => $provider['id']))
|
||||
->row_array();
|
||||
$no_model_provider['services'] = array();
|
||||
$no_model_provider['settings'] = $this->ci->db
|
||||
->get_where('ea_user_settings', array('id_users' => $provider['id']))
|
||||
->row_array();
|
||||
unset($no_model_provider['settings']['id_users']);
|
||||
|
||||
$model_provider = $this->ci->providers_model->get_row($provider['id']);
|
||||
|
@ -604,8 +628,6 @@ class Unit_tests_providers_model extends CI_Driver {
|
|||
$this->ci->unit->run($no_model_provider, $model_provider, 'Test get_row() method successfully '
|
||||
. 'returned provider record.');
|
||||
|
||||
|
||||
|
||||
// Delete inserted provider record.
|
||||
$this->ci->db->delete('ea_users', array('id' => $provider['id']));
|
||||
}
|
||||
|
@ -619,8 +641,6 @@ class Unit_tests_providers_model extends CI_Driver {
|
|||
} catch (Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
|
||||
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if get_row() with record id that does '
|
||||
. 'not exist in the database has thrown an exception.');
|
||||
}
|
||||
|
@ -711,25 +731,38 @@ class Unit_tests_providers_model extends CI_Driver {
|
|||
$this->ci->db->delete('ea_users', array('id' => $provider['id']));
|
||||
}
|
||||
|
||||
// TEST GET BATCH METHOD ---------------------------------------------
|
||||
// TEST GET BATCH METHOD
|
||||
private function test_get_batch() {
|
||||
// Get all the provider rows without using the model.
|
||||
$db_data = $this->ci->db->get_where('ea_users',
|
||||
array('id_roles' => $this->provider_role_id))->result_array();
|
||||
$db_data = $this->ci->db
|
||||
->get_where('ea_users', array('id_roles' => $this->provider_role_id))
|
||||
->result_array();
|
||||
|
||||
foreach($db_data as &$db_provider) {
|
||||
$db_provider['settings'] = $this->ci->db->get_where('ea_user_settings',
|
||||
array('id_users' => $db_provider['id']))->row_array();
|
||||
$services = $this->ci->db
|
||||
->get_where('ea_services_providers', array('id_users' => $db_provider['id']))
|
||||
->result_array();
|
||||
$db_provider['services'] = array();
|
||||
foreach($services as $service) {
|
||||
$db_provider['services'][] = $service['id_services'];
|
||||
}
|
||||
|
||||
|
||||
$db_provider['settings'] = $this->ci->db
|
||||
->get_where('ea_user_settings', array('id_users' => $db_provider['id']))
|
||||
->row_array();
|
||||
unset($db_provider['settings']['id_users']);
|
||||
}
|
||||
|
||||
// Get all the provider rows by using the model.
|
||||
$model_data = $this->ci->providers_model->get_batch();
|
||||
|
||||
// Check that the two arrays are the same.
|
||||
$this->ci->unit->run($db_data, $model_data, 'Test get_batch() method.');
|
||||
}
|
||||
|
||||
private function test_get_batch_with_where_clause() {
|
||||
// Insert new provider record.
|
||||
// Insert new provider record and try to fetch it.
|
||||
$provider = array(
|
||||
'last_name' => 'Doe',
|
||||
'first_name' => 'John',
|
||||
|
@ -743,27 +776,52 @@ class Unit_tests_providers_model extends CI_Driver {
|
|||
$this->ci->db->insert('ea_users', $provider);
|
||||
$provider['id'] = intval($this->ci->db->insert_id());
|
||||
|
||||
// Get data without using the model.
|
||||
$no_model_data = $this->ci->db->get_where('ea_users', array('id' => $provider['id']))
|
||||
$settings = array(
|
||||
'id_users' => $provider['id'],
|
||||
'username' => 'testprov',
|
||||
'password' => 'testprov',
|
||||
'notifications' => FALSE,
|
||||
'working_plan' => $this->default_working_plan,
|
||||
'google_sync' => FALSE,
|
||||
'google_token' => NULL,
|
||||
'sync_past_days' => '5',
|
||||
'sync_future_days' => '5'
|
||||
);
|
||||
$this->ci->db->insert('ea_user_settings', $settings);
|
||||
|
||||
// Get provider records without using the model.
|
||||
$no_model_batch = $this->ci->db
|
||||
->get_where('ea_users', array('id' => $provider['id']))
|
||||
->result_array();
|
||||
foreach($no_model_data as &$no_model_provider) {
|
||||
$no_model_provider['settings'] = $this->ci->db->get_where('ea_user_settings',
|
||||
array('id_users' => $no_model_provider['id']))->row_array();
|
||||
|
||||
foreach($no_model_batch as &$no_model_provider) {
|
||||
$services = $this->ci->db
|
||||
->get_where('ea_services_providers', array('id_users' => $provider['id']))
|
||||
->result_array();
|
||||
$no_model_provider['services'] = array();
|
||||
foreach($services as $service) {
|
||||
$no_model_provider['services'][] = $service['id_services'];
|
||||
}
|
||||
|
||||
|
||||
$no_model_provider['settings'] = $this->ci->db
|
||||
->get_where('ea_user_settings', array('id_users' => $no_model_provider['id']))
|
||||
->row_array();
|
||||
unset($no_model_provider['settings']['id_users']);
|
||||
}
|
||||
|
||||
// Get data by using the model.
|
||||
$model_data = $this->ci->providers_model->get_batch(array('id' => $provider['id']));
|
||||
$model_batch = $this->ci->providers_model->get_batch(array('id' => $provider['id']));
|
||||
|
||||
// Check that the data arrays are the same.
|
||||
$this->ci->unit->run($no_model_data, $model_data, 'Test get_batch() with where clause.');
|
||||
$this->ci->unit->run($no_model_batch, $model_batch, 'Test get_batch() with where clause.');
|
||||
|
||||
// Delete inserted record from database.
|
||||
$this->ci->db->delete('ea_users', array('id' => $provider['id']));
|
||||
}
|
||||
|
||||
private function unabled_test_get_batch_with_invalid_where_clause() {
|
||||
// CodeIgniter auto raises an exception if the where section is invalid.
|
||||
// CodeIgniter auto raises an exception if the where clause is invalid.
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,441 @@
|
|||
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
||||
|
||||
class Unit_tests_secretaries_model extends CI_Driver {
|
||||
private $ci;
|
||||
private $secretary_role_id;
|
||||
private $default_secretary; // does not contain an 'id' value
|
||||
|
||||
/**
|
||||
* Class Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->ci =& get_instance();
|
||||
$this->ci->load->library('Unit_test');
|
||||
$this->ci->load->model('secretaries_model');
|
||||
|
||||
$this->secretary_role_id = $this->ci->db->get_where('ea_roles',
|
||||
array('slug' => DB_SLUG_SECRETARY))->row()->id;
|
||||
|
||||
$this->default_secretary = array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'email' => 'john@doe.com',
|
||||
'mobile_number' => '2340982039',
|
||||
'phone_number' => '9098091234',
|
||||
'address' => 'Some Street 80',
|
||||
'city' => 'Some City',
|
||||
'state' => 'Some State',
|
||||
'zip_code' => '12345',
|
||||
'notes' => 'This is a test secretary user.',
|
||||
'id_roles' => $this->secretary_role_id
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run all the available tests
|
||||
*/
|
||||
public function run_all() {
|
||||
// All the methods whose names start with "test" are going to be
|
||||
// executed. If you want a method to not be executed remove the
|
||||
// "test" keyword from the beginning.
|
||||
$class_methods = get_class_methods('Unit_tests_secretaries_model');
|
||||
foreach ($class_methods as $method_name) {
|
||||
if (substr($method_name, 0, 5) === 'test_') {
|
||||
call_user_func(array($this, $method_name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// UNIT TESTS
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// TEST ADD METHOD ------------------------------------------------------
|
||||
private function test_add_insert() {
|
||||
$secretary = $this->default_secretary;
|
||||
$secretary['providers'] = array();
|
||||
$secretary['id'] = $this->ci->secretaries_model->add($secretary);
|
||||
|
||||
$this->ci->unit->run($secretary['id'], 'is_int', 'Test if add() - insert operation - '
|
||||
. 'has returned and integer value.');
|
||||
|
||||
$db_secretary = $this->ci->db->get_where('ea_users', array('id' => $secretary['id']))->row_array();
|
||||
$this->ci->unit->run($secretary, $db_secretary, 'Test if add() - insert operation - '
|
||||
. 'has successfully inserted a new record.');
|
||||
|
||||
$this->ci->db->delete('ea_users', array('id' => $secretary['id']));
|
||||
}
|
||||
|
||||
private function test_add_update() {
|
||||
$secretary = $this->default_secretary;
|
||||
$this->ci->db->insert('ea_users', $secretary);
|
||||
$secretary['id'] = intval($this->ci->db->insert_id());
|
||||
|
||||
$secretary['first_name'] = 'value changed';
|
||||
$secretary['last_name'] = 'value changed';
|
||||
$secretary['email'] = 'value@changed.com';
|
||||
$secretary['mobile_number'] = 'value changed';
|
||||
$secretary['phone_number'] = 'value changed';
|
||||
$secretary['address'] = 'value changed';
|
||||
$secretary['city'] = 'value changed';
|
||||
$secretary['state'] = 'value changed';
|
||||
$secretary['zip_code'] = 'value changed';
|
||||
$secretary['notes'] = 'value changed';
|
||||
|
||||
$secretary['providers'] = array();
|
||||
|
||||
$update_result = $this->ci->secretaries_model->add($secretary);
|
||||
$this->ci->unit->run($update_result, 'is_int', 'Test if add() - update operation - has '
|
||||
. 'returned an integer value.');
|
||||
|
||||
$db_secretary = $this->ci->db->get_where('ea_users', array('id' => $secretary['id']))->row_array();
|
||||
$this->ci->unit->run($secretary, $db_secretary, 'Test if add() - update operation - has '
|
||||
. 'successfully updated the secretary record.');
|
||||
|
||||
$this->ci->db->delete('ea_users', array('id' => $secretary['id']));
|
||||
}
|
||||
|
||||
private function test_add_invalid_data() {
|
||||
$secretary = $this->default_secretary;
|
||||
$secretary['email'] = 'this value is invalid';
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->secretaries_model->add($secretary);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if add() has thrown exception '
|
||||
. 'on invalid data.');
|
||||
}
|
||||
|
||||
private function test_add_using_find_record_id() {
|
||||
$secretary = $this->default_secretary;
|
||||
$this->ci->db->insert('ea_users', $secretary);
|
||||
$secretary_id = intval($this->ci->db->insert_id());
|
||||
|
||||
// since $secretary array does not contain an 'id'value but
|
||||
// exists in the database, the find_record_id() method is going
|
||||
// to be used inside the method to find the secretary id.
|
||||
|
||||
$secretary['last_name'] = 'updated value';
|
||||
$secretary['providers'] = array();
|
||||
$update_result = $this->ci->secretaries_model->add($secretary);
|
||||
$this->ci->unit->run($update_result, 'is_int', 'Test if add() - update operation - has '
|
||||
. 'returned and integer value.');
|
||||
|
||||
$db_secretary = $this->ci->db->get_where('ea_users', array('id' => $secretary_id))->row_array();
|
||||
unset($db_secretary['id']);
|
||||
$this->ci->unit->run($secretary, $db_secretary, 'Test if add() - update operation - has '
|
||||
. 'successfully updated the secretary record using find_record_id() method '
|
||||
. 'internally.');
|
||||
|
||||
$this->ci->db->delete('ea_users', array('id' => $secretary_id));
|
||||
}
|
||||
|
||||
// TEST EXISTS METHOD -----------------------------------------------------
|
||||
private function test_exists_record_exists() {
|
||||
$secretary = $this->default_secretary;
|
||||
$this->ci->db->insert('ea_users', $secretary);
|
||||
$secretary['id'] = intval($this->ci->db->insert_id());
|
||||
|
||||
$exists = $this->ci->secretaries_model->exists($secretary);
|
||||
$this->ci->unit->run($exists, TRUE, 'Test if exists() has returned TRUE with record '
|
||||
. 'that exists.');
|
||||
|
||||
$this->ci->db->delete('ea_users', array('id' => $secretary['id']));
|
||||
}
|
||||
|
||||
private function test_exists_record_does_not_exist() {
|
||||
$secretary = $this->default_secretary;
|
||||
$exists = $this->ci->secretaries_model->exists($secretary);
|
||||
$this->ci->unit->run($exists, FALSE, 'Test if exists() has returned FALSE with record '
|
||||
. 'that does not exists.');
|
||||
}
|
||||
|
||||
private function test_exists_invalid_argument() {
|
||||
$secretary = $this->default_secretary;
|
||||
unset($secretary['email']);
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->secretaries_model->exists($secretary);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if exists() has thrown exception with '
|
||||
. 'invalid argument (missing email).');
|
||||
}
|
||||
|
||||
// TEST FIND RECORD ID METHOD ---------------------------------------------
|
||||
private function test_find_record_id_record_exists() {
|
||||
$secretary = $this->default_secretary;
|
||||
$this->ci->db->insert('ea_users', $secretary);
|
||||
$secretary['id'] = intval($this->ci->db->insert_id());
|
||||
|
||||
$model_id = $this->ci->secretaries_model->find_record_id($secretary);
|
||||
|
||||
$this->ci->unit->run($model_id, 'is_int', 'Test if find_record_id() has returned '
|
||||
. 'an integer valuel.');
|
||||
|
||||
$this->ci->unit->run($secretary['id'], $model_id, 'Test if find_record_id() has '
|
||||
. 'successfully found the selected record id');
|
||||
|
||||
$this->ci->db->delete('ea_users', array('id' => $model_id));
|
||||
}
|
||||
|
||||
private function test_find_record_id_record_does_not_exist() {
|
||||
$secretary = $this->default_secretary;
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->secretaries_model->find_record_id($secretary);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if find_record_id() has thrown '
|
||||
. 'exception on record that does not exist.');
|
||||
}
|
||||
|
||||
private function test_find_record_id_invalid_argument() {
|
||||
$secretary = $this->default_secretary;
|
||||
unset($secretary['email']);
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->secretaries_model->find_record_id($secretary);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if find_record_id() has thrown '
|
||||
. 'exception on invalid argument given (missing email value).');
|
||||
}
|
||||
|
||||
// TEST VALIDATE METHOD ---------------------------------------------------
|
||||
private function test_validate() {
|
||||
$secretary = $this->default_secretary;
|
||||
$validation_result = $this->ci->secretaries_model->validate($secretary);
|
||||
$this->ci->unit->run($validation_result, TRUE, 'Test if validate() has returned TRUE '
|
||||
. 'on valid secretary data.');
|
||||
|
||||
}
|
||||
|
||||
private function test_validate_provided_id_does_not_exist() {
|
||||
$secretary = $this->default_secretary;
|
||||
$secretary['id'] = 'This id does not exist in database.';
|
||||
|
||||
$validation_result = $this->ci->secretaries_model->validate($secretary);
|
||||
$this->ci->unit->run($validation_result, FALSE, 'Test if validate() has '
|
||||
. 'returned FALSE on invalid data (provided id not exists in db).');
|
||||
}
|
||||
|
||||
private function test_validate_invalid_users_value_data_type() {
|
||||
$secretary = $this->default_secretary;
|
||||
$secretary['providers'] = 'This is not an array';
|
||||
|
||||
$validation_result = $this->ci->secretaries_model->validate($secretary);
|
||||
$this->ci->unit->run($validation_result, FALSE, 'Test if validate() has '
|
||||
. 'returned FALSE on invalid data (users value is not an array).');
|
||||
}
|
||||
|
||||
private function test_validate_missing_required_field_values() {
|
||||
$secretary = $this->default_secretary;
|
||||
unset($secretary['last_name']);
|
||||
unset($secretary['email']);
|
||||
unset($secretary['phone_number']);
|
||||
|
||||
$validation_result = $this->ci->secretaries_model->validate($secretary);
|
||||
$this->ci->unit->run($validation_result, FALSE, 'Test if validate() has '
|
||||
. 'returned FALSE on invalid data (missing required field values).');
|
||||
}
|
||||
|
||||
private function test_validate_invalid_email_address() {
|
||||
$secretary = $this->default_secretary;
|
||||
$secretary['email'] = 'This is invalid.';
|
||||
|
||||
$validation_result = $this->ci->secretaries_model->validate($secretary);
|
||||
$this->ci->unit->run($validation_result, FALSE, 'Test if validate() has '
|
||||
. 'returned FALSE on invalid data (invalid email address).');
|
||||
}
|
||||
|
||||
// TEST DELETE METHOD -----------------------------------------------------
|
||||
private function test_delete() {
|
||||
$secretary = $this->default_secretary;
|
||||
$this->ci->db->insert('ea_users', $secretary);
|
||||
$secretary['id'] = intval($this->ci->db->insert_id());
|
||||
|
||||
$delete_result = $this->ci->secretaries_model->delete($secretary['id']);
|
||||
$this->ci->unit->run($delete_result, TRUE, 'Test if delete() method has returned TRUE '
|
||||
. 'successfull deletion.');
|
||||
|
||||
$num_rows = $this->ci->db->get_where('ea_users', array('id' => $secretary['id']))->num_rows();
|
||||
$this->ci->unit->run($num_rows, 0, 'Test if delete() method has successfully deleted '
|
||||
. 'the secretary record.');
|
||||
|
||||
if ($num_rows > 0) {
|
||||
$this->ci->db->delete('ea_users', array('id' => $secretary['id']));
|
||||
}
|
||||
}
|
||||
|
||||
private function test_delete_invalid_argument() {
|
||||
$invalid_id = 'This is invalid';
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->secretaries_model->delete($invalid_id);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if delete() has thrown exception on '
|
||||
. 'invalid argument given.');
|
||||
}
|
||||
|
||||
private function test_delete_record_does_not_exist() {
|
||||
$random_id = 23420930923; // no record exists with this id
|
||||
$delete_result = $this->ci->secretaries_model->delete($random_id);
|
||||
$this->ci->unit->run($delete_result, FALSE, 'Test if delete() method has returned FALSE '
|
||||
. 'when trying to delete a record that does not exist.');
|
||||
}
|
||||
|
||||
// TEST GET ROW METHOD ----------------------------------------------------
|
||||
private function test_get_row() {
|
||||
$secretary = $this->default_secretary;
|
||||
$this->ci->db->insert('ea_users', $secretary);
|
||||
$secretary['id'] = intval($this->ci->db->insert_id());
|
||||
|
||||
$model_secretary = $this->ci->secretaries_model->get_row($secretary['id']);
|
||||
unset($model_secretary['providers']);
|
||||
|
||||
$this->ci->unit->run($secretary, $model_secretary, 'Test if get_row() has successfully '
|
||||
. 'returned the secretary record.');
|
||||
|
||||
$this->ci->db->delete('ea_users', array('id' => $secretary['id']));
|
||||
}
|
||||
|
||||
private function test_get_row_invalid_argument() {
|
||||
$invalid_id = 'this is invalid';
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->secretaries_model->get_row($invalid_id);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if get_row() has thrown exception '
|
||||
. 'on invalid argument given.');
|
||||
}
|
||||
|
||||
private function test_get_row_record_does_not_exist() {
|
||||
$random_id = 2309203923; // no record exists with this id.
|
||||
$model_secretary = $this->ci->secretaries_model->get_row($random_id);
|
||||
$this->ci->unit->run($model_secretary, array(), 'Test if get_row() has returned an empty '
|
||||
. 'array on record that does not exist.');
|
||||
}
|
||||
|
||||
// TEST GET VALUE METHOD --------------------------------------------------
|
||||
private function test_get_value() {
|
||||
$secretary = $this->default_secretary;
|
||||
$this->ci->db->insert('ea_users', $secretary);
|
||||
$secretary['id'] = intval($this->ci->db->insert_id());
|
||||
|
||||
$field_name = 'last_name';
|
||||
|
||||
$last_name = $this->ci->secretaries_model->get_value($field_name, $secretary['id']);
|
||||
$this->ci->unit->run($secretary['last_name'], $last_name, 'Test if get_value() has '
|
||||
. 'successfully returned the correct value.');
|
||||
|
||||
$this->ci->db->delete('ea_users', array('id' => $secretary['id']));
|
||||
}
|
||||
|
||||
private function test_get_value_invalid_field_name() {
|
||||
$field_name = 23423452342; // this is invalid
|
||||
$secretary_id = 1; // random pick
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->secretaries_model->get_value($field_name, $secretary_id);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if get_value() has thrown exception '
|
||||
. 'on invalid field name.');
|
||||
}
|
||||
|
||||
private function test_get_value_invalid_record_id() {
|
||||
$field_name = 'last_name'; // random pick
|
||||
$secretary_id = 'this is invalid';
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->secretaries_model->get_value($field_name, $secretary_id);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if get_value() has thrown exception '
|
||||
. 'on invalid record id.');
|
||||
}
|
||||
|
||||
private function test_get_value_record_does_not_exist() {
|
||||
$field_name = 'last_name';
|
||||
$secretary_id = 23092093233; // this record does not exist
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->secretaries_model->get_value($field_name, $secretary_id);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if get_value() has thrown exception '
|
||||
. 'on record does not exist.');
|
||||
}
|
||||
|
||||
private function test_get_value_field_does_not_exist() {
|
||||
$field_name = 'this field name does not exist';
|
||||
$secretary_id = 23; // random pick
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->secretaries_model->get_value($field_name, $secretary_id);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if get_value() has thrown exception '
|
||||
. 'on field name does not exist.');
|
||||
}
|
||||
|
||||
// TEST GET BATCH METHOD --------------------------------------------------
|
||||
private function test_get_batch() {
|
||||
$model_batch = $this->ci->secretaries_model->get_batch();
|
||||
foreach ($model_batch as &$secretary) {
|
||||
if (isset($secretary['providers'])) {
|
||||
unset($secretary['providers']); // will not be included in the test
|
||||
}
|
||||
}
|
||||
|
||||
$no_model_batch = $this->ci->db->get_where('ea_users',
|
||||
array('id_roles' => $this->secretary_role_id))->result_array();
|
||||
|
||||
$this->ci->unit->run($model_batch, $no_model_batch, 'Test if get_batch() has '
|
||||
. 'returned the correct results.');
|
||||
}
|
||||
|
||||
private function test_get_batch_where_clause() {
|
||||
$secretary = $this->default_secretary;
|
||||
$this->ci->db->insert('ea_users', $secretary);
|
||||
$secretary['id'] = intval($this->ci->db->insert_id());
|
||||
|
||||
$model_batch = $this->ci->secretaries_model->get_batch(array('id' => $secretary['id']));
|
||||
foreach ($model_batch as &$tmp_secretary) {
|
||||
if (isset($tmp_secretary['providers'])) {
|
||||
unset($tmp_secretary['providers']); // will not be included in the test
|
||||
}
|
||||
}
|
||||
|
||||
$no_model_batch = $this->ci->db->get_where('ea_users', array('id' => $secretary['id']))->result_array();
|
||||
|
||||
$this->ci->unit->run($model_batch, $no_model_batch, 'Test if get_batch() with where clause '
|
||||
. 'has returned the correct results.');
|
||||
|
||||
$this->ci->db->delete('ea_users', array('id' => $secretary['id']));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* End of file Unit_tests_secretaries_model.php */
|
||||
/* Location: ./application/libraries/Unit_tests/drivers/Unit_tests_secretaries_model.php */
|
|
@ -27,7 +27,7 @@ class Admins_Model extends CI_Model {
|
|||
}
|
||||
|
||||
/**
|
||||
* Add (insert or update) a admin user record into database.
|
||||
* Add (insert or update) an admin user record into database.
|
||||
*
|
||||
* @param array $admin Contains the admin user data.
|
||||
* @return int Returns the record id.
|
||||
|
@ -48,7 +48,7 @@ class Admins_Model extends CI_Model {
|
|||
$admin['id'] = $this->update($admin);
|
||||
}
|
||||
|
||||
return intval($admin);
|
||||
return intval($admin['id']);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -69,7 +69,7 @@ class Admins_Model extends CI_Model {
|
|||
->select('*')
|
||||
->from('ea_users')
|
||||
->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner')
|
||||
->where('ea_users.email', $provider['email'])
|
||||
->where('ea_users.email', $admin['email'])
|
||||
->where('ea_roles.slug', DB_SLUG_ADMIN)
|
||||
->get()->num_rows();
|
||||
|
||||
|
@ -110,9 +110,9 @@ class Admins_Model extends CI_Model {
|
|||
}
|
||||
|
||||
/**
|
||||
* Find the database record id of a provider.
|
||||
* Find the database record id of an admin user.
|
||||
*
|
||||
* @param array $admin Contains the admin data. The 'email value is required in order to
|
||||
* @param array $admin Contains the admin data. The 'email' value is required in order to
|
||||
* find the record id.
|
||||
* @return int Returns the record id
|
||||
* @throws Exception When the 'email' value is not present on the $admin array.
|
||||
|
@ -191,7 +191,7 @@ class Admins_Model extends CI_Model {
|
|||
// There must be always at least one admin user. If this is the only admin
|
||||
// the system, it cannot be deleted.
|
||||
$admin_count = $this->db->get_where('ea_users',
|
||||
array('id_roles' => $this->get_admin_roles_id()))->num_rows();
|
||||
array('id_roles' => $this->get_admin_role_id()))->num_rows();
|
||||
if ($admin_count == 1) {
|
||||
throw new Exception('Record could not be deleted. The system requires at least '
|
||||
. 'one admin user.');
|
||||
|
@ -266,13 +266,15 @@ class Admins_Model extends CI_Model {
|
|||
* @return array Returns an array with admin records.
|
||||
*/
|
||||
public function get_batch($where_clause = '') {
|
||||
$role_id = $this->get_admin_role_id();
|
||||
|
||||
if ($where_clause != '') {
|
||||
$this->db->where($where_clause);
|
||||
}
|
||||
|
||||
$this->db->where('id_roles', $this->get_admin_role_id());
|
||||
$this->db->where('id_roles', $role_id);
|
||||
$batch = $this->db->get('ea_users')->result_array();
|
||||
|
||||
$batch = $this->get('ea_users')->result_array();
|
||||
return $batch;
|
||||
}
|
||||
|
||||
|
|
|
@ -329,19 +329,23 @@ class Providers_Model extends CI_Model {
|
|||
*
|
||||
* @example $this->Model->get_batch('id = ' . $recordId);
|
||||
*
|
||||
* @param string $where_clause (OPTIONAL) The WHERE clause of the query to be executed.
|
||||
* @param mixed $where_clause (OPTIONAL) The WHERE clause of the query to be executed.
|
||||
*
|
||||
* NOTICE: DO NOT INCLUDE 'WHERE' KEYWORD.
|
||||
*
|
||||
* @return array Returns the rows from the database.
|
||||
*/
|
||||
public function get_batch($where_clause = '') {
|
||||
// CI db class may confuse two where clauses made in the same time, so
|
||||
// get the role id first and then apply the get_batch() where clause.
|
||||
$role_id = $this->get_providers_role_id();
|
||||
|
||||
if ($where_clause != '') {
|
||||
$this->db->where($where_clause);
|
||||
}
|
||||
|
||||
$batch = $this->db->get_where('ea_users',
|
||||
array('id_roles' => $this->get_providers_role_id()))->result_array();
|
||||
array('id_roles' => $role_id))->result_array();
|
||||
|
||||
// Include each provider sevices and settings.
|
||||
foreach($batch as &$provider) {
|
||||
|
@ -408,8 +412,8 @@ class Providers_Model extends CI_Model {
|
|||
*
|
||||
* @return int Returns the role id for the provider records.
|
||||
*/
|
||||
public function get_providers_role_id() {
|
||||
return $this->db->get_where('ea_roles', array('slug' => DB_SLUG_PROVIDER))->row()->id;
|
||||
public function get_providers_role_id() {
|
||||
return $this->db->get_where('ea_roles', array('slug' => DB_SLUG_PROVIDER))->row()->id;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -485,7 +489,7 @@ class Providers_Model extends CI_Model {
|
|||
}
|
||||
|
||||
// Save provider services in the database (delete old records and add new).
|
||||
$this->db->delete('ea_services_records', array('id_users' => $provider_id));
|
||||
$this->db->delete('ea_services_providers', array('id_users' => $provider_id));
|
||||
foreach($services as $service_id) {
|
||||
$service_provider = array(
|
||||
'id_users' => $provider_id,
|
||||
|
|
|
@ -1,6 +1,25 @@
|
|||
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed.');
|
||||
|
||||
class Admins_Model extends CI_Model {
|
||||
/**
|
||||
* Secretaries Model
|
||||
*
|
||||
* Handles the db actions that have to do with secretaries.
|
||||
*
|
||||
* Data Structure
|
||||
* 'first_name'
|
||||
* 'last_name'
|
||||
* 'email'
|
||||
* 'mobile_number'
|
||||
* 'phone_number'
|
||||
* 'address'
|
||||
* 'city'
|
||||
* 'state'
|
||||
* 'zip_code'
|
||||
* 'notes'
|
||||
* 'id_roles'
|
||||
* 'provders' >> array with provider ids that the secretary handles
|
||||
*/
|
||||
class Secretaries_Model extends CI_Model {
|
||||
/**
|
||||
* Class Constructor
|
||||
*/
|
||||
|
@ -8,47 +27,301 @@ class Admins_Model extends CI_Model {
|
|||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add (insert or update) a secretary user record into database.
|
||||
*
|
||||
* @param array $secretary Contains the secretary user data.
|
||||
* @return int Returns the record id.
|
||||
* @throws Exception When the secretary data are invalid (see validate() method).
|
||||
*/
|
||||
public function add($secretary) {
|
||||
if (!$this->validate($secretary)) {
|
||||
throw new Exception('Secretary data are invalid: ' . print_r($secretary, TRUE));
|
||||
}
|
||||
|
||||
if ($this->exists($secretary) && !isset($secretary['id'])) {
|
||||
$secretary['id'] = $this->find_record_id($secretary);
|
||||
}
|
||||
|
||||
if (!isset($secretary['id'])) {
|
||||
$secretary['id'] = $this->insert($secretary);
|
||||
} else {
|
||||
$secretary['id'] = $this->update($secretary);
|
||||
}
|
||||
|
||||
return intval($secretary['id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a particular secretary record exists in the database.
|
||||
*
|
||||
* @param array $secretary Contains the secretary data. The 'email' value is required to
|
||||
* be present at the moment.
|
||||
* @return bool Returns whether the record exists or not.
|
||||
* @throws Exception When the 'email' value is not present on the $secretary argument.
|
||||
*/
|
||||
public function exists($secretary) {
|
||||
if (!isset($secretary['email'])) {
|
||||
throw new Exception('Secretary email is not provided: ' . print_r($secretary, TRUE));
|
||||
}
|
||||
|
||||
// This method shouldn't depend on another method of this class.
|
||||
$num_rows = $this->db
|
||||
->select('*')
|
||||
->from('ea_users')
|
||||
->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner')
|
||||
->where('ea_users.email', $secretary['email'])
|
||||
->where('ea_roles.slug', DB_SLUG_SECRETARY)
|
||||
->get()->num_rows();
|
||||
|
||||
return ($num_rows > 0) ? TRUE : FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a new sercretary record into the database.
|
||||
*
|
||||
* @param array $secretary Contains the secretary data.
|
||||
* @return int Returns the new record id.
|
||||
* @throws Exception When the insert operation fails.
|
||||
*/
|
||||
public function insert($secretary) {
|
||||
$providers = $secretary['providers'];
|
||||
unset($secretary['providers']);
|
||||
|
||||
$secretary['id_roles'] = $this->get_secretary_role_id();
|
||||
|
||||
if (!$this->db->insert('ea_users', $secretary)) {
|
||||
throw new Exception('Could not insert secretary into the database.');
|
||||
}
|
||||
|
||||
$secretary['id'] = intval($this->db->insert_id());
|
||||
|
||||
$this->save_providers($providers,$secretary['id']);
|
||||
|
||||
return $secretary['id'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an existing secretary record in the database.
|
||||
*
|
||||
* @param array $secretary Contains the secretary record data.
|
||||
* @return int Retuns the record id.
|
||||
* @throws Exception When the update operation fails.
|
||||
*/
|
||||
public function update($secretary) {
|
||||
$providers = $secretary['providers'];
|
||||
unset($secretary['providers']);
|
||||
|
||||
$this->db->where('id', $secretary['id']);
|
||||
if (!$this->db->update('ea_users', $secretary)){
|
||||
throw new Exception('Could not update secretary record.');
|
||||
}
|
||||
|
||||
$this->save_providers($providers, $secretary['id']);
|
||||
|
||||
return intval($secretary['id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the database record id of a secretary.
|
||||
*
|
||||
* @param array $secretary Contains the secretary data. The 'email' value is required
|
||||
* in order to find the record id.
|
||||
* @return int Returns the record id
|
||||
* @throws Exception When the 'email' value is not present on the $secretary array.
|
||||
*/
|
||||
public function find_record_id($secretary) {
|
||||
if (!isset($secretary['email'])) {
|
||||
throw new Exception('Secretary email was not provided: ' . print_r($secretary, TRUE));
|
||||
}
|
||||
|
||||
$result = $this->db
|
||||
->select('ea_users.id')
|
||||
->from('ea_users')
|
||||
->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner')
|
||||
->where('ea_users.email', $secretary['email'])
|
||||
->where('ea_roles.slug', DB_SLUG_SECRETARY)
|
||||
->get();
|
||||
|
||||
if ($result->num_rows() == 0) {
|
||||
throw new Exception('Could not find secretary record id.');
|
||||
}
|
||||
|
||||
return intval($result->row()->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate secretary user data before add() operation is executed.
|
||||
*
|
||||
* @param array $secretary Contains the secretary user data.
|
||||
* @return bool Returns the validation result.
|
||||
*/
|
||||
public function validate($secretary) {
|
||||
$this->load->helper('data_validation');
|
||||
|
||||
try {
|
||||
// If a record id is provided then check whether the record exists in the database.
|
||||
if (isset($secretary['id'])) {
|
||||
$num_rows = $this->db->get_where('ea_users', array('id' => $secretary['id']))
|
||||
->num_rows();
|
||||
if ($num_rows == 0) {
|
||||
throw new Exception('Given secretary id does not exist in database: ' . $secretary['id']);
|
||||
}
|
||||
}
|
||||
|
||||
// Validate 'providers' value datatype (must be array)
|
||||
if (isset($secretary['provders']) && !is_array($secretary['providers'])) {
|
||||
throw new Exception('Secretary providers value is not an array.');
|
||||
}
|
||||
|
||||
// Validate required fields integrity.
|
||||
if (!isset($secretary['last_name'])
|
||||
|| !isset($secretary['email'])
|
||||
|| !isset($secretary['phone_number'])) {
|
||||
throw new Exception('Not all required fields are provided : ' . print_r($secretary, TRUE));
|
||||
}
|
||||
|
||||
// Validate secretary email address.
|
||||
if (!filter_var($secretary['email'], FILTER_VALIDATE_EMAIL)) {
|
||||
throw new Exception('Invalid email address provided : ' . $secretary['email']);
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
} catch (Exception $exc) {
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
public function delete($secretary) {
|
||||
/**
|
||||
* Delete an existing secretary record from the database.
|
||||
*
|
||||
* @param numeric $secretary_id The secretary record id to be deleted.
|
||||
* @return bool Returns the delete operation result.
|
||||
* @throws Exception When the $secretary_id is not a valid numeric value.
|
||||
*/
|
||||
public function delete($secretary_id) {
|
||||
if (!is_numeric($secretary_id)) {
|
||||
throw new Exception('Invalid argument type $secretary_id : ' . $secretary_id);
|
||||
}
|
||||
|
||||
$num_rows = $this->db->get_where('ea_users', array('id' => $secretary_id))->num_rows();
|
||||
if ($num_rows == 0) {
|
||||
return FALSE; // Record does not exist in database.
|
||||
}
|
||||
|
||||
return $this->db->delete('ea_users', array('id' => $secretary_id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a specific secretary record from the database.
|
||||
*
|
||||
* @param numeric $secretary_id The id of the record to be returned.
|
||||
* @return array Returns an array with the secretary user data.
|
||||
* @throws Exception When the $secretary_id is not a valid numeric value.
|
||||
*/
|
||||
public function get_row($secretary_id) {
|
||||
if (!is_numeric($secretary_id)) {
|
||||
throw new Exception('$secretary_id argument is not a valid numeric value: ' . $secretary_id);
|
||||
}
|
||||
|
||||
$secretary = $this->db->get_where('ea_users', array('id' => $secretary_id))->row_array();
|
||||
$secretary['providers'] = $this->db->get_where('ea_secretaries_providers',
|
||||
array('id_users_secretary' => $secretary_id))->result_array();
|
||||
|
||||
return $secretary;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a specific field value from the database.
|
||||
*
|
||||
* @param string $field_name The field name of the value to be returned.
|
||||
* @param numeric $secretary_id Record id of the value to be returned.
|
||||
* @return string Returns the selected record value from the database.
|
||||
* @throws Exception When the $field_name argument is not a valid string.
|
||||
* @throws Exception When the $secretary_id is not a valid numeric.
|
||||
* @throws Exception When the secretary record does not exist in the database.
|
||||
* @throws Exception When the selected field value is not present on database.
|
||||
*/
|
||||
public function get_value($field_name, $secretary_id) {
|
||||
if (!is_string($field_name)) {
|
||||
throw new Exception('$field_name argument is not a string : ' . $field_name);
|
||||
}
|
||||
|
||||
if (!is_numeric($secretary_id)) {
|
||||
throw new Exception('$secretary_id argument is not a valid numeric value: ' . $secretary_id);
|
||||
}
|
||||
|
||||
// Check whether the secretary record exists.
|
||||
$result = $this->db->get_where('ea_users', array('id' => $secretary_id));
|
||||
if ($result->num_rows() == 0) {
|
||||
throw new Exception('The record with the given id does not exist in the '
|
||||
. 'database : ' . $secretary_id);
|
||||
}
|
||||
|
||||
// Check if the required field name exist in database.
|
||||
$provider = $result->row_array();
|
||||
if (!isset($provider[$field_name])) {
|
||||
throw new Exception('The given $field_name argument does not exist in the '
|
||||
. 'database: ' . $field_name);
|
||||
}
|
||||
|
||||
return $provider[$field_name];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all, or specific secretary records from database.
|
||||
*
|
||||
* @param string|array $where_clause (OPTIONAL) The WHERE clause of the query to be executed.
|
||||
* Use this to get specific secretary records.
|
||||
* @return array Returns an array with secretary records.
|
||||
*/
|
||||
public function get_batch($where_clause = '') {
|
||||
$role_id = $this->get_secretary_role_id();
|
||||
|
||||
if ($where_clause != '') {
|
||||
$this->db->where($where_clause);
|
||||
}
|
||||
|
||||
$this->db->where('id_roles', $role_id);
|
||||
$batch = $this->db->get('ea_users')->result_array();
|
||||
|
||||
// Include every secretary handling users.
|
||||
foreach ($batch as &$secretary) {
|
||||
$secretary['providers'] = $this->db->get_where('ea_secretaries_providers',
|
||||
array('id_users_secretary' => $secretary['id']))->result_array();
|
||||
}
|
||||
|
||||
return $batch;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the secretary users role id.
|
||||
*
|
||||
* @return int Returns the role record id.
|
||||
*/
|
||||
public function get_secretary_role_id() {
|
||||
return intval($this->db->get_where('ea_roles', array('slug' => DB_SLUG_SECRETARY))->row()->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save a secretary hasndling users.
|
||||
* @param array $providers Contains the provider ids that are handled by the secretary.
|
||||
* @param numeric $secretary_id The selected secretary record.
|
||||
*/
|
||||
private function save_providers($providers, $secretary_id) {
|
||||
if (!is_array($providers)) {
|
||||
throw new Exception('Invalid argument given $providers: ' . print_r($providers, TRUE));
|
||||
}
|
||||
|
||||
if (count($providers) > 0) {
|
||||
foreach ($providers as $provider_id) {
|
||||
$this->db->insert('ea_secretaries_providers', array(
|
||||
'id_users_secretary' => $secretary_id,
|
||||
'id_users_provider' => $provider_id
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* End of file secretaries_model.php */
|
||||
|
|
Loading…
Reference in a new issue