mirror of
https://github.com/alextselegidis/easyappointments.git
synced 2024-12-27 00:52:56 +03:00
Removed old unit test files.
This commit is contained in:
parent
69586b5207
commit
ce89711af2
9 changed files with 0 additions and 4291 deletions
|
@ -1,72 +0,0 @@
|
|||
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Easy!Appointments - Open Source Web Scheduler
|
||||
*
|
||||
* @package EasyAppointments
|
||||
* @author A.Tselegidis <alextselegidis@gmail.com>
|
||||
* @copyright Copyright (c) 2013 - 2016, Alex Tselegidis
|
||||
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
|
||||
* @link http://easyappointments.org
|
||||
* @since v1.0.0
|
||||
* ---------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Test Controller
|
||||
*
|
||||
* NOTICE: This controller is outdated and must not be used.
|
||||
*
|
||||
* @deprecated v1.1.0
|
||||
*
|
||||
* @package Controllers
|
||||
*/
|
||||
class Test extends CI_Controller {
|
||||
/**
|
||||
* Class Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
$this->load->driver('Unit_tests');
|
||||
}
|
||||
|
||||
/**
|
||||
* Run all available unit tests.
|
||||
*
|
||||
* We only test models at the moment. In the future the unit test will be
|
||||
* improved.
|
||||
*/
|
||||
public function index() {
|
||||
// User must be logged in as an admin in order to run the tests.
|
||||
$this->load->library('session');
|
||||
$this->session->set_userdata('dest_url', site_url('test'));
|
||||
if ($this->session->userdata('role_slug') != DB_SLUG_ADMIN) {
|
||||
header('Location: ' . site_url('user/login'));
|
||||
return;
|
||||
}
|
||||
|
||||
if (ENVIRONMENT !== 'development') {
|
||||
$this->output->set_output('Tests are available only at development environment. '
|
||||
. 'Please check your "index.php" file settings.');
|
||||
return;
|
||||
}
|
||||
|
||||
$this->load->view('general/test');
|
||||
$this->unit_tests->run_all_tests();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test only the app models.
|
||||
*/
|
||||
public function models() {
|
||||
//$this->load->view('general/test');
|
||||
//$this->unit_tests->run_model_tests();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test only the app libraries.
|
||||
*/
|
||||
public function libraries() {
|
||||
//$this->load->view('general/test');
|
||||
//$this->unit_tests->run_library_tests();
|
||||
}
|
||||
}
|
|
@ -1,94 +0,0 @@
|
|||
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Easy!Appointments - Open Source Web Scheduler
|
||||
*
|
||||
* @package EasyAppointments
|
||||
* @author A.Tselegidis <alextselegidis@gmail.com>
|
||||
* @copyright Copyright (c) 2013 - 2016, Alex Tselegidis
|
||||
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
|
||||
* @link http://easyappointments.org
|
||||
* @since v1.0.0
|
||||
* ---------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Unit testing driver.
|
||||
*
|
||||
* This driver handles all the unit testing of the application.
|
||||
* Custom methods (test categories) can be created in order to
|
||||
* use different test groups on each testing
|
||||
*
|
||||
* @package Libraries
|
||||
* @subpackage Tests
|
||||
*/
|
||||
class Unit_tests extends CI_Driver_Library {
|
||||
public $CI;
|
||||
public $valid_drivers;
|
||||
|
||||
/**
|
||||
* Class Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->CI =& get_instance();
|
||||
// Add more subclasses to the following array to expand
|
||||
// the unit testing classes.
|
||||
$this->valid_drivers = array(
|
||||
'Unit_tests_appointments_model',
|
||||
'Unit_tests_customers_model',
|
||||
'Unit_tests_providers_model',
|
||||
'Unit_tests_services_model',
|
||||
'Unit_tests_settings_model',
|
||||
'Unit_tests_admins_model',
|
||||
'Unit_tests_secretaries_model'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run all the available tests of the system.
|
||||
*
|
||||
* If a new group of tests is added, it should be also added in
|
||||
* this method, in order to be executed when all the tests need to
|
||||
* be run.
|
||||
*/
|
||||
public function run_all_tests() {
|
||||
$this->run_model_tests(false);
|
||||
$this->run_library_tests(false);
|
||||
$this->CI->output->append_output($this->CI->unit->report());
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// UNIT TEST GROUPS
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
/**
|
||||
* Run all the models tests.
|
||||
*
|
||||
* @param bool $output_report Determines wether the test report
|
||||
* will be outputted.
|
||||
*/
|
||||
public function run_model_tests($output_report = true) {
|
||||
$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());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Run all the library tests.
|
||||
*
|
||||
* @param bool $output_report Determines wether the test
|
||||
* report will be outputted.
|
||||
*/
|
||||
public function run_library_tests($output_report = true) {
|
||||
// No tests for libraries at the moment.
|
||||
if ($output_report) {
|
||||
$this->CI->output->append_output($this->CI->unit->report());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,494 +0,0 @@
|
|||
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Easy!Appointments - Open Source Web Scheduler
|
||||
*
|
||||
* @package EasyAppointments
|
||||
* @author A.Tselegidis <alextselegidis@gmail.com>
|
||||
* @copyright Copyright (c) 2013 - 2016, Alex Tselegidis
|
||||
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
|
||||
* @link http://easyappointments.org
|
||||
* @since v1.0.0
|
||||
* ---------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Admins Model Unit Tests
|
||||
*
|
||||
* @package Libraries
|
||||
* @subpackage Tests
|
||||
*/
|
||||
class Unit_tests_admins_model extends CI_Driver {
|
||||
private $ci;
|
||||
private $admin_role_id;
|
||||
private $default_admin; // does not contain an 'id' value
|
||||
private $default_settings; // does not contain 'id_users' 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
|
||||
);
|
||||
|
||||
$this->default_settings = array(
|
||||
'username' => 'test_admin',
|
||||
'password' => 'test_pswd',
|
||||
'working_plan' => NULL,
|
||||
'notifications' => FALSE,
|
||||
'google_sync' => 0,
|
||||
'google_token' => NULL,
|
||||
'sync_past_days' => NULL,
|
||||
'sync_future_days' => NULL
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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['settings'] = $this->default_settings;
|
||||
|
||||
$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();
|
||||
|
||||
$db_record['settings'] = $this->ci->db->get_where('ea_user_settings',
|
||||
array('id_users' => $admin['id']))->row_array();
|
||||
unset($db_record['settings']['id_users'], $db_record['settings']['salt'],
|
||||
$admin['settings']['password'], $db_record['settings']['password']); // not needed
|
||||
$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['settings'] = $this->default_settings;
|
||||
$admin['settings']['id_users'] = $admin['id'];
|
||||
$this->ci->db->insert('ea_user_settings', $admin['settings']);
|
||||
unset($admin['settings']['id_users']);
|
||||
|
||||
$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();
|
||||
$db_record['settings'] = $this->ci->db->get_where('ea_user_settings', array('id_users' => $admin['id']))->row_array();
|
||||
unset($db_record['settings']['id_users'], $db_record['settings']['salt'],
|
||||
$admin['settings']['password'], $db_record['settings']['password']); // not needed
|
||||
|
||||
$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();
|
||||
|
||||
foreach($db_batch as &$admin) { // Get each admin settings
|
||||
$admin['settings'] = $this->ci->db->get_where('ea_user_settings',
|
||||
array('id_users' => $admin['id']))->row_array();
|
||||
unset($admin['settings']['id_users']);
|
||||
}
|
||||
|
||||
|
||||
$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();
|
||||
foreach($db_batch as &$admin) {
|
||||
$admin['settings'] = 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());
|
||||
$admin['settings'] = array();
|
||||
|
||||
$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;
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->admins_model->get_row($random_id);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if get_row() has thrown exception '
|
||||
. 'with record that does not exist.');
|
||||
}
|
||||
|
||||
// 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
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->admins_model->validate($admin);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if validate() has thrown an exception '
|
||||
. 'with 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']);
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->admins_model->validate($admin);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if validate() has thrown an exception '
|
||||
. 'with missing required field values.');
|
||||
}
|
||||
|
||||
private function test_validate_invalid_email() {
|
||||
$admin = $this->default_admin;
|
||||
$admin['email'] = 'This is invalid';
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->admins_model->validate($admin);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if validate() has thrown an exception '
|
||||
. 'with invalid email address.');
|
||||
}
|
||||
}
|
||||
|
||||
/* End of file Unit_tests_admins_model.php */
|
||||
/* Location: ./application/libraries/Unit_tests/drivers/Unit_tests_admins_model.php */
|
|
@ -1,725 +0,0 @@
|
|||
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Easy!Appointments - Open Source Web Scheduler
|
||||
*
|
||||
* @package EasyAppointments
|
||||
* @author A.Tselegidis <alextselegidis@gmail.com>
|
||||
* @copyright Copyright (c) 2013 - 2016, Alex Tselegidis
|
||||
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
|
||||
* @link http://easyappointments.org
|
||||
* @since v1.0.0
|
||||
* ---------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Appointments Model Unit Tests
|
||||
*
|
||||
* @package Libraries
|
||||
* @subpackage Tests
|
||||
*/
|
||||
class Unit_tests_appointments_model extends CI_Driver {
|
||||
private $ci;
|
||||
|
||||
private $provider_id;
|
||||
private $customer_id;
|
||||
private $service_id;
|
||||
|
||||
/**
|
||||
* Class Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->ci =& get_instance();
|
||||
|
||||
$this->ci->load->library('Unit_test');
|
||||
$this->ci->load->model('appointments_model');
|
||||
|
||||
// Add some sample data from the database (will be needed in the
|
||||
// testing methods).
|
||||
$provider = array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'email' => 'test@test.com',
|
||||
'mobile_number' => '000000',
|
||||
'phone_number' => '111111',
|
||||
'address' => 'Some Str',
|
||||
'city' => 'Some City',
|
||||
'state' => 'Some State',
|
||||
'zip_code' => '12345',
|
||||
'notes' => 'This is a test provider',
|
||||
'id_roles' => $this->ci->db->get_where('ea_roles',
|
||||
array('slug' => DB_SLUG_PROVIDER))->row()->id
|
||||
);
|
||||
$this->ci->db->insert('ea_users', $provider);
|
||||
$this->provider_id = $this->ci->db->insert_id();
|
||||
|
||||
$service = array(
|
||||
'name' => 'Test Service',
|
||||
'duration' => 30,
|
||||
'price' => '20.00',
|
||||
'currency' => 'Euro',
|
||||
'description' => 'Some description here ...',
|
||||
'id_service_categories' => NULL
|
||||
);
|
||||
$this->ci->db->insert('ea_services', $service);
|
||||
$this->service_id = $this->ci->db->insert_id();
|
||||
|
||||
$customer = array(
|
||||
'last_name' => 'Doe',
|
||||
'first_name' => 'John',
|
||||
'email' => 'john@doe.com',
|
||||
'phone_number' => '0123456789',
|
||||
'address' => 'Abbey Road 18',
|
||||
'city' => 'London',
|
||||
'zip_code' => '12345',
|
||||
'id_roles' => $this->ci->db->get_where('ea_roles',
|
||||
array('slug' => DB_SLUG_CUSTOMER))->row()->id
|
||||
);
|
||||
$this->ci->db->insert('ea_users', $customer);
|
||||
$this->customer_id = $this->ci->db->insert_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_appointments_model');
|
||||
foreach ($class_methods as $method_name) {
|
||||
if (substr($method_name, 0, 5) === 'test_') {
|
||||
call_user_func(array($this, $method_name));
|
||||
}
|
||||
}
|
||||
|
||||
$this->ci->db->delete('ea_users', array('id' => $this->customer_id));
|
||||
$this->ci->db->delete('ea_users', array('id' => $this->provider_id));
|
||||
$this->ci->db->delete('ea_services', array('id' => $this->service_id));
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// UNIT TESTS
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
/**
|
||||
* Test the appointment add method - insert new record.
|
||||
*/
|
||||
private function test_add_appointment_insert() {
|
||||
// Add - insert new appointment record to the database.
|
||||
$appointment = array(
|
||||
'start_datetime' => '2013-05-01 12:30:00',
|
||||
'end_datetime' => '2013-05-01 13:00:00',
|
||||
'notes' => 'Some notes right here...',
|
||||
'is_unavailable' => FALSE,
|
||||
'id_users_provider' => $this->provider_id,
|
||||
'id_users_customer' => $this->customer_id,
|
||||
'id_services' => $this->service_id
|
||||
);
|
||||
$appointment['id'] = $this->ci->appointments_model->add($appointment);
|
||||
$this->ci->unit->run($appointment['id'], 'is_int', 'Test if add() appointment (insert '
|
||||
. 'operation) returned the db row id.');
|
||||
|
||||
// Check if the record is the one that was inserted.
|
||||
$db_data = $this->ci->db->get_where('ea_appointments', array('id' => $appointment['id']))
|
||||
->row_array();
|
||||
|
||||
// These should not be included because they are generated when the
|
||||
// record is inserted.
|
||||
unset($db_data['hash']);
|
||||
unset($db_data['book_datetime']);
|
||||
unset($db_data['id_google_calendar']);
|
||||
|
||||
$this->ci->unit->run($appointment, $db_data, 'Test if add() appointment (insert '
|
||||
. 'operation) has successfully inserted a record.');
|
||||
|
||||
// Delete inserted record.
|
||||
$this->ci->db->delete('ea_appointments', array('id' => $appointment['id']));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the appointment add method - update existing record.
|
||||
*/
|
||||
private function test_add_appointment_update() {
|
||||
// Insert new appointment (this row will be updated later).
|
||||
$appointment = array(
|
||||
'start_datetime' => '2013-05-01 12:30:00',
|
||||
'end_datetime' => '2013-05-01 13:00:00',
|
||||
'notes' => 'Some notes right here...',
|
||||
'is_unavailable' => FALSE,
|
||||
'id_users_provider' => $this->provider_id,
|
||||
'id_users_customer' => $this->customer_id,
|
||||
'id_services' => $this->service_id
|
||||
);
|
||||
$this->ci->db->insert('ea_appointments', $appointment);
|
||||
$appointment['id'] = $this->ci->db->insert_id();
|
||||
|
||||
// Perform the update operation and check if the record is update.
|
||||
$changed_notes = 'Some CHANGED notes right here ...';
|
||||
$appointment['notes'] = $changed_notes;
|
||||
|
||||
$update_result = $this->ci->appointments_model->add($appointment);
|
||||
$this->ci->unit->run($update_result, 'is_int', 'Test if add() appointment (update '
|
||||
. 'operation) has returned the row id.');
|
||||
|
||||
$db_notes = $this->ci->db->get_where('ea_appointments', array('id' => $update_result))
|
||||
->row()->notes;
|
||||
$this->ci->unit->run($changed_notes, $db_notes, 'Test add() appointment (update '
|
||||
. 'operation) has successfully updated record.');
|
||||
|
||||
// Delete inserted record.
|
||||
$this->ci->db->delete('ea_appointments', array('id' => $appointment['id']));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test appointment data insertion with wrong date
|
||||
* format for the corresponding datetime db fields.
|
||||
*/
|
||||
private function test_add_appointment_wrong_date_format() {
|
||||
// Insert new appointment with no foreign keys.
|
||||
$appointment = array(
|
||||
'start_datetime' => '2013-0WRONG5-01 12:30WRONG:00',
|
||||
'end_datetime' => '2013-0WRONG5-01WRONG 13:00WRONG:00',
|
||||
'notes' => 'Some notes right here...',
|
||||
'is_unavailable' => FALSE,
|
||||
'id_users_provider' => $this->provider_id,
|
||||
'id_users_customer' => $this->customer_id,
|
||||
'id_services' => $this->service_id
|
||||
);
|
||||
|
||||
$has_thrown_exception = FALSE; // This method must throw a validation exception.
|
||||
try {
|
||||
$this->ci->appointments_model->add($appointment);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exception = TRUE;
|
||||
}
|
||||
|
||||
$this->ci->unit->run($has_thrown_exception, TRUE, 'Test add() appointment with wrong '
|
||||
. 'date format.', 'A validation exception must be thrown.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the exists() method.
|
||||
*
|
||||
* Insert a new appointment and test if it exists.
|
||||
*/
|
||||
private function test_exists() {
|
||||
// Insert new appointment (this row will be checked later).
|
||||
$appointment = array(
|
||||
'start_datetime' => '2013-05-01 12:30:00',
|
||||
'end_datetime' => '2013-05-01 13:00:00',
|
||||
'notes' => 'Some notes right here...',
|
||||
'is_unavailable' => FALSE,
|
||||
'id_users_provider' => $this->provider_id,
|
||||
'id_users_customer' => $this->customer_id,
|
||||
'id_services' => $this->service_id
|
||||
);
|
||||
$this->ci->db->insert('ea_appointments', $appointment);
|
||||
$appointment['id'] = $this->ci->db->insert_id();
|
||||
|
||||
// Test the exists() method
|
||||
$this->ci->unit->run($this->ci->appointments_model->exists($appointment), TRUE,
|
||||
'Test exists() method with an inserted record.');
|
||||
|
||||
// Delete inserted record.
|
||||
$this->ci->db->delete('ea_appointments', array('id' => $appointment['id']));
|
||||
}
|
||||
|
||||
private function test_exists_record_does_not_exist() {
|
||||
// Create random appointment data that doesn't exist in the database.
|
||||
$appointment = array(
|
||||
'start_datetime' => '2013-05-01 08:33:45',
|
||||
'end_datetime' => '2013-05-02 13:13:13',
|
||||
'notes' => 'This is totally random!',
|
||||
'is_unavailable' => FALSE,
|
||||
'id_users_provider' => '198678',
|
||||
'id_users_customer' => '194702',
|
||||
'id_services' => '8766293'
|
||||
);
|
||||
|
||||
$this->ci->unit->run($this->ci->appointments_model->exists($appointment), FALSE,
|
||||
'Test exists() method with an appointment that does not exist');
|
||||
}
|
||||
|
||||
private function test_exists_with_wrong_data() {
|
||||
// Create random appointment data that doesn't exist in the database.
|
||||
$appointment = array(
|
||||
'start_datetime' => '2WRONG013-05-01 0WRONG8:33:45',
|
||||
'end_datetime' => '2013-0WRONG5-02 13:13:WRONG13',
|
||||
'notes' => 'This is totally random!',
|
||||
'is_unavailable' => FALSE,
|
||||
'id_users_provider' => '1986WRONG78',
|
||||
'id_users_customer' => '1WRONG94702',
|
||||
'id_services' => '876WRONG6293'
|
||||
);
|
||||
|
||||
$this->ci->unit->run($this->ci->appointments_model->exists($appointment), FALSE,
|
||||
'Test exists() method with wrong appointment data.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the find record id method with a record that already
|
||||
* exists in the database.
|
||||
*/
|
||||
private function test_find_record_id() {
|
||||
// Create a new appointment record.
|
||||
$appointment = array(
|
||||
'start_datetime' => '2013-05-01 12:30:00',
|
||||
'end_datetime' => '2013-05-01 13:00:00',
|
||||
'notes' => 'Some notes right here...',
|
||||
'is_unavailable' => FALSE,
|
||||
'id_users_provider' => $this->provider_id,
|
||||
'id_users_customer' => $this->customer_id,
|
||||
'id_services' => $this->service_id
|
||||
);
|
||||
$this->ci->db->insert('ea_appointments', $appointment);
|
||||
$appointment['id'] = $this->ci->db->insert_id();
|
||||
|
||||
// Find record id of the new appointment record.
|
||||
$method_result_id = $this->ci->appointments_model->find_record_id($appointment);
|
||||
|
||||
$this->ci->unit->run($method_result_id, $appointment['id'], 'Test find_record_id() '
|
||||
. 'successfully returned the correct record id.');
|
||||
|
||||
// Delete appointment record.
|
||||
$this->ci->db->delete('ea_appointments', array('id' => $method_result_id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to find the record id of an appointment that doesn't
|
||||
* exist in the database.
|
||||
*
|
||||
* A database exception is expected to be raised.
|
||||
*/
|
||||
private function test_find_record_id_appointment_does_not_exist() {
|
||||
// Define appointment data. These data shouldn't exist in the database.
|
||||
$appointment = array(
|
||||
'start_datetime' => '2013-05-01 12:30:00',
|
||||
'end_datetime' => '2013-05-01 13:00:00',
|
||||
'notes' => 'Some notes right here...',
|
||||
'is_unavailable' => FALSE,
|
||||
'id_users_provider' => $this->provider_id,
|
||||
'id_users_customer' => $this->customer_id,
|
||||
'id_services' => $this->service_id
|
||||
);
|
||||
|
||||
// Load the appointments model and execute the find record id method.
|
||||
$has_thrown_exception = FALSE;
|
||||
|
||||
try {
|
||||
$this->ci->appointments_model->find_record_id($appointment);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exception = TRUE;
|
||||
}
|
||||
|
||||
$this->ci->unit->run($has_thrown_exception, TRUE, 'Test find_record_id() with appointment '
|
||||
. 'data that does not exist in the database.', 'A database exception is expected '
|
||||
. 'to be raised.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the find_record_id() method by providing wrong
|
||||
* appointment data.
|
||||
*
|
||||
* A database exception is expected to be raised.
|
||||
*/
|
||||
private function test_find_record_id_wrong_data() {
|
||||
// Define appointment data array with wrong values.
|
||||
$appointment = array(
|
||||
'start_datetime' => '2013WRONG-05-0WRONG1 12:WRONG30:00',
|
||||
'end_datetime' => '2013-05-01 13:00:00WRONG',
|
||||
'notes' => 'Some notes righWRONGt here...',
|
||||
'is_unavailable' => FALSE,
|
||||
'id_users_provider' => 'WRONG',
|
||||
'id_users_customer' => 'WRONG',
|
||||
'id_services' => 'WRONG'
|
||||
);
|
||||
|
||||
// Try to find the appointment's record id. A database
|
||||
// exception should be raised.
|
||||
$has_thrown_exception = FALSE;
|
||||
|
||||
try {
|
||||
$this->ci->appointments_model->find_record_id($appointment);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exception = TRUE;
|
||||
}
|
||||
|
||||
$this->ci->unit->run($has_thrown_exception, TRUE, 'Test find_record_id() with appointment '
|
||||
. 'data array with wrong values.', 'A database exception is expected to be raised.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the normal flow of deleting an appointment record.
|
||||
*/
|
||||
private function test_delete() {
|
||||
// Create a new appointment record.
|
||||
$appointment = array(
|
||||
'start_datetime' => '2013-05-01 12:30:00',
|
||||
'end_datetime' => '2013-05-01 13:00:00',
|
||||
'notes' => 'Some notes right here...',
|
||||
'is_unavailable' => FALSE,
|
||||
'id_users_provider' => $this->provider_id,
|
||||
'id_users_customer' => $this->customer_id,
|
||||
'id_services' => $this->service_id
|
||||
);
|
||||
$this->ci->db->insert('ea_appointments', $appointment);
|
||||
$appointment['id'] = $this->ci->db->insert_id();
|
||||
|
||||
// Delete new record
|
||||
$delete_result = $this->ci->appointments_model->delete($appointment['id']);
|
||||
$this->ci->unit->run($delete_result, TRUE, 'Test delete() method result (should be TRUE).');
|
||||
|
||||
// Check if the record has been successfully deleted.
|
||||
$num_rows = $this->ci->db->get_where('ea_appointments', array('id' => $appointment['id']))
|
||||
->num_rows();
|
||||
$this->ci->unit->run($num_rows, 0, 'Test if the record was successfully deleted.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the delete method with a record that doesn't exist
|
||||
* in the database.
|
||||
*/
|
||||
private function test_delete_record_does_not_exist() {
|
||||
$random_record_id = 1233265;
|
||||
$delete_result = $this->ci->appointments_model->delete($random_record_id);
|
||||
$this->ci->unit->run($delete_result, FALSE, 'Test delete() method with a record id'
|
||||
. ' that does not exist');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the delete method by providing a wrong argument
|
||||
* (string and not integer).
|
||||
*/
|
||||
private function test_delete_record_wrong_parameter_given() {
|
||||
$wrong_record_id = 'not_numeric';
|
||||
|
||||
$has_thrown_exception = FALSE;
|
||||
|
||||
try {
|
||||
$this->ci->appointments_model->delete($wrong_record_id);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exception = TRUE;
|
||||
}
|
||||
|
||||
$this->ci->unit->run($has_thrown_exception, TRUE, 'Test delete() method with argument '
|
||||
. 'that is not an numeric.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the get_batch() method of the appointments model.
|
||||
*/
|
||||
private function test_get_batch() {
|
||||
// Get all the appointment records (without using the model).
|
||||
$db_data = $this->ci->db->get('ea_appointments')->result_array();
|
||||
|
||||
// Get all the appointment records (by using the model).
|
||||
$model_data = $this->ci->appointments_model->get_batch();
|
||||
|
||||
// Check that the two arrays are the same.
|
||||
$this->ci->unit->run($model_data, $db_data, 'Test get_batch() method.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the get_batch() method of the appointments model
|
||||
* with a where clause.
|
||||
*/
|
||||
private function test_get_batch_where_clause() {
|
||||
// Insert new appointment.
|
||||
$appointment = array(
|
||||
'start_datetime' => '2013-05-01 12:30:00',
|
||||
'end_datetime' => '2013-05-01 13:00:00',
|
||||
'notes' => 'Some notes right here...',
|
||||
'is_unavailable' => FALSE,
|
||||
'id_users_provider' => $this->provider_id,
|
||||
'id_users_customer' => $this->customer_id,
|
||||
'id_services' => $this->service_id
|
||||
);
|
||||
$this->ci->db->insert('ea_appointments', $appointment);
|
||||
$appointment['id'] = $this->ci->db->insert_id();
|
||||
|
||||
// Get filtered appointment records without using the model.
|
||||
$db_data = $this->ci->db->get_where('ea_appointments', array('id' => $appointment['id']))
|
||||
->result_array();
|
||||
|
||||
// Get filtered appointment records by using the model.
|
||||
$model_data = $this->ci->appointments_model->get_batch(array('id' => $appointment['id']));
|
||||
|
||||
// Check that the two arrays are the same.
|
||||
$this->ci->unit->run($model_data, $db_data, 'Test get_batch() method.');
|
||||
|
||||
// Delete appointment record.
|
||||
$this->ci->db->delete('ea_appointments', array('id' => $appointment['id']));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the get_batch() method of the appointments model
|
||||
* with a wrong where clause.
|
||||
*
|
||||
* A database exception is expected to be raised.
|
||||
*
|
||||
* <strong>IMPORTANT!</strong> This test is unabled because code
|
||||
* igniter handles itself wrong queries.
|
||||
*/
|
||||
private function unabled_test_get_batch_wrong_where_clause() {
|
||||
$has_thrown_exception = FALSE;
|
||||
|
||||
try {
|
||||
$this->ci->appointments_model->get_batch('WRONG QUERY HERE');
|
||||
} catch(Exception $db_exc) {
|
||||
$has_thrown_exception = TRUE;
|
||||
}
|
||||
|
||||
$this->ci->unit->run($has_thrown_exception, TRUE, 'Test get_batch() with wrong where clause.',
|
||||
'A database exception is expected to be thrown.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_row() method.
|
||||
*/
|
||||
private function test_get_row() {
|
||||
// Insert new appointment record.
|
||||
$appointment = array(
|
||||
'start_datetime' => '2013-05-01 12:30:00',
|
||||
'end_datetime' => '2013-05-01 13:00:00',
|
||||
'notes' => 'Some notes right here...',
|
||||
'is_unavailable' => FALSE,
|
||||
'hash' => '91de2d31f5cbb6d26a5b1b3e710d38d1',
|
||||
'id_users_provider' => $this->provider_id,
|
||||
'id_users_customer' => $this->customer_id,
|
||||
'id_services' => $this->service_id
|
||||
);
|
||||
$this->ci->db->insert('ea_appointments', $appointment);
|
||||
$appointment['id'] = $this->ci->db->insert_id();
|
||||
|
||||
// Get the appointment row from the database.
|
||||
$db_data = $this->ci->appointments_model->get_row($appointment['id']);
|
||||
unset($db_data['book_datetime']);
|
||||
unset($db_data['id_google_calendar']);
|
||||
|
||||
// Check if this is the record we seek.
|
||||
$this->ci->unit->run($db_data, $appointment, 'Test get_row() method.');
|
||||
|
||||
// Delete appointment record.
|
||||
$this->ci->db->delete('ea_appointments', array('id' => $appointment['id']));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_row() with a record that doesn't exist in the db.
|
||||
*/
|
||||
private function test_get_row_that_does_not_exist() {
|
||||
$random_record_id = 789453486;
|
||||
|
||||
$row_data = $this->ci->appointments_model->get_row($random_record_id);
|
||||
|
||||
$this->ci->unit->run($row_data, NULL, 'Test get_row() with record id that does '
|
||||
. 'not exist in the database.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_row() method with wrong argument.
|
||||
*
|
||||
* A database exception is expected.
|
||||
*/
|
||||
private function test_get_row_with_invalid_argument() {
|
||||
$invalid_id = 'THIS IS NOT NUMERIC';
|
||||
|
||||
$has_thrown_exception = FALSE;
|
||||
try {
|
||||
$this->ci->appointments_model->get_row($invalid_id);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exception = TRUE;
|
||||
}
|
||||
|
||||
$this->ci->unit->run($has_thrown_exception, TRUE, 'Test get_row() with wrong argument.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the get field value method.
|
||||
*/
|
||||
private function test_get_value() {
|
||||
// Insert new appointment record.
|
||||
$appointment = array(
|
||||
'start_datetime' => '2013-05-01 12:30:00',
|
||||
'end_datetime' => '2013-05-01 13:00:00',
|
||||
'notes' => 'Some notes right here...',
|
||||
'is_unavailable' => FALSE,
|
||||
'id_users_provider' => $this->provider_id,
|
||||
'id_users_customer' => $this->customer_id,
|
||||
'id_services' => $this->service_id
|
||||
);
|
||||
$this->ci->db->insert('ea_appointments', $appointment);
|
||||
$appointment['id'] = $this->ci->db->insert_id();
|
||||
|
||||
// Get a specific value from the database.
|
||||
$db_value = $this->ci->appointments_model->get_value('start_datetime', $appointment['id']);
|
||||
|
||||
// Check if the value was correctly fetched from the database.
|
||||
$this->ci->unit->run($db_value, $appointment['start_datetime'], 'Test get_value() method.');
|
||||
|
||||
// Delete inserted appointment record.
|
||||
$this->ci->db->delete('ea_appointments', array('id' => $appointment['id']));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the get field value method with a record id that
|
||||
* doesn't exist in the db.
|
||||
*
|
||||
* A database exception is expected.
|
||||
*/
|
||||
private function test_get_value_record_does_not_exist() {
|
||||
$random_record_id = 843521368768;
|
||||
|
||||
$has_thrown_exception = FALSE;
|
||||
|
||||
try {
|
||||
$this->ci->appointments_model->get_value('start_datetime', $random_record_id);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exception = TRUE;
|
||||
}
|
||||
|
||||
$this->ci->unit->run($has_thrown_exception, TRUE, 'Test get_value() with record id that '
|
||||
. 'does not exist.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the get_value() method with a field that does
|
||||
* not exist in the db.
|
||||
*
|
||||
* A database exception is expected.
|
||||
*/
|
||||
private function test_get_value_field_does_not_exist() {
|
||||
// Insert new appointment record.
|
||||
$appointment = array(
|
||||
'start_datetime' => '2013-05-01 12:30:00',
|
||||
'end_datetime' => '2013-05-01 13:00:00',
|
||||
'notes' => 'Some notes right here...',
|
||||
'is_unavailable' => FALSE,
|
||||
'id_users_provider' => $this->provider_id,
|
||||
'id_users_customer' => $this->customer_id,
|
||||
'id_services' => $this->service_id
|
||||
);
|
||||
$this->ci->db->insert('ea_appointments', $appointment);
|
||||
$appointment['id'] = $this->ci->db->insert_id();
|
||||
|
||||
// Try to get record value with wrong field name.
|
||||
$wrong_field_name = 'THIS IS WRONG';
|
||||
$has_thrown_exception = FALSE;
|
||||
|
||||
try {
|
||||
$this->ci->appointments_model->get_value($wrong_field_name, $appointment['id']);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exception = TRUE;
|
||||
}
|
||||
|
||||
$this->ci->unit->run($has_thrown_exception, TRUE, 'Test get_value() with record id that '
|
||||
. 'does not exist.');
|
||||
|
||||
// Delete inserted record.
|
||||
$this->ci->db->delete('ea_appointments', array('id' => $appointment['id']));
|
||||
}
|
||||
|
||||
private function test_validate_data() {
|
||||
$appointment = array(
|
||||
'start_datetime' => '2013-05-01 12:30:00',
|
||||
'end_datetime' => '2013-05-01 13:00:00',
|
||||
'notes' => 'Some notes right here...',
|
||||
'is_unavailable' => FALSE,
|
||||
'id_users_provider' => $this->provider_id,
|
||||
'id_users_customer' => $this->customer_id,
|
||||
'id_services' => $this->service_id
|
||||
);
|
||||
$validation_result = $this->ci->appointments_model->validate($appointment);
|
||||
$this->ci->unit->run($validation_result, TRUE, 'Test validate() method.');
|
||||
}
|
||||
|
||||
private function test_validate_data_wrong_date_format() {
|
||||
$appointment = array(
|
||||
'start_datetime' => '20WRONG13-05-01 12WRONG:30:00',
|
||||
'end_datetime' => '2013-05WRONG-01 13:00WRONG:00',
|
||||
'notes' => 'Some notes right here...',
|
||||
'is_unavailable' => FALSE,
|
||||
'id_users_provider' => $this->provider_id,
|
||||
'id_users_customer' => $this->customer_id,
|
||||
'id_services' => $this->service_id
|
||||
);
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->appointments_model->validate($appointment);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
|
||||
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if validate() method with '
|
||||
. 'wrong date formats has thrown exception.');
|
||||
}
|
||||
|
||||
private function test_validate_data_invalid_provider_id() {
|
||||
$appointment = array(
|
||||
'start_datetime' => '2013-05-01 12:30:00',
|
||||
'end_datetime' => '2013-05-01 13:00:00',
|
||||
'notes' => 'Some notes right here...',
|
||||
'is_unavailable' => FALSE,
|
||||
'id_users_provider' => 'THIS IS WRONG',
|
||||
'id_users_customer' => $this->customer_id,
|
||||
'id_services' => $this->service_id
|
||||
);
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->appointments_model->validate($appointment);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if validate() method with '
|
||||
. 'invalid provider id has thrown exception.');
|
||||
}
|
||||
|
||||
private function test_validate_data_invalid_customer_id() {
|
||||
$appointment = array(
|
||||
'start_datetime' => '2013-05-01 12:30:00',
|
||||
'end_datetime' => '2013-05-01 13:00:00',
|
||||
'notes' => 'Some notes right here...',
|
||||
'is_unavailable' => FALSE,
|
||||
'id_users_provider' => $this->provider_id,
|
||||
'id_users_customer' => 'THIS IS WRONG',
|
||||
'id_services' => $this->service_id
|
||||
);
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->appointments_model->validate($appointment);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if validate() method with '
|
||||
. 'invalid customer id has thrown exception.');
|
||||
}
|
||||
|
||||
private function test_validate_data_invalid_service_id() {
|
||||
$appointment = array(
|
||||
'start_datetime' => '2013-05-01 12:30:00',
|
||||
'end_datetime' => '2013-05-01 13:00:00',
|
||||
'notes' => 'Some notes right here...',
|
||||
'is_unavailable' => FALSE,
|
||||
'id_users_provider' => $this->provider_id,
|
||||
'id_users_customer' => $this->customer_id,
|
||||
'id_services' => 'THIS IS WRONG'
|
||||
);
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->appointments_model->validate($appointment);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if validate() method with '
|
||||
. 'invalid service id has thrown exception.');
|
||||
}
|
||||
}
|
||||
|
||||
/* End of file Unit_tests_appointments_model.php */
|
||||
/* Location: ./application/libraries/Unit_tests/drivers/Unit_tests_appointments_model.php */
|
|
@ -1,592 +0,0 @@
|
|||
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Easy!Appointments - Open Source Web Scheduler
|
||||
*
|
||||
* @package EasyAppointments
|
||||
* @author A.Tselegidis <alextselegidis@gmail.com>
|
||||
* @copyright Copyright (c) 2013 - 2016, Alex Tselegidis
|
||||
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
|
||||
* @link http://easyappointments.org
|
||||
* @since v1.0.0
|
||||
* ---------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Customers Model Unit Tests
|
||||
*
|
||||
* @package Libraries
|
||||
* @subpackage Tests
|
||||
*/
|
||||
class Unit_tests_customers_model extends CI_Driver {
|
||||
private $CI;
|
||||
private $customer_role_id;
|
||||
|
||||
/**
|
||||
* Class Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
// CodeIgniter initialization.
|
||||
$this->CI =& get_instance();
|
||||
$this->CI->load->library('Unit_test');
|
||||
$this->CI->load->model('customers_model');
|
||||
|
||||
// Use this when creating test records.
|
||||
$this->customer_role_id = $this->CI->db->get_where('ea_roles',
|
||||
array('slug' => DB_SLUG_CUSTOMER))->row()->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_customers_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() CUSTOMER METHOD
|
||||
private function test_add_insert() {
|
||||
// Insert new customer record.
|
||||
$customer = array(
|
||||
'last_name' => 'Doe',
|
||||
'first_name' => 'John',
|
||||
'email' => 'test@test.com',
|
||||
'phone_number' => '0123456789',
|
||||
'address' => 'Abbey Road 18',
|
||||
'city' => 'London',
|
||||
'zip_code' => '12345',
|
||||
'id_roles' => $this->customer_role_id
|
||||
);
|
||||
$customer['id'] = $this->CI->customers_model->add($customer);
|
||||
$this->CI->unit->run($customer['id'], 'is_int', 'Test add() customer (insert operation) '
|
||||
. 'has returned the new row id.');
|
||||
|
||||
// Check if the record was successfully added to the database.
|
||||
$db_data = $this->CI->db->get_where('ea_users', array('id' => $customer['id']))->row_array();
|
||||
$are_the_same = TRUE;
|
||||
if ($customer['last_name'] != $db_data['last_name']
|
||||
|| $customer['first_name'] != $db_data['first_name']
|
||||
|| $customer['email'] != $db_data['email']
|
||||
|| $customer['phone_number'] != $db_data['phone_number']
|
||||
|| $customer['address'] != $db_data['address']
|
||||
|| $customer['city'] != $db_data['city']
|
||||
|| $customer['zip_code'] != $db_data['zip_code']
|
||||
|| $customer['id_roles'] != $db_data['id_roles']) {
|
||||
$are_the_same = FALSE;
|
||||
}
|
||||
$this->CI->unit->run($are_the_same, TRUE, 'Test add() customer (insert operation) has '
|
||||
. 'successfully been added to the datbase.');
|
||||
|
||||
// Delete inserted record.
|
||||
$this->CI->db->delete('ea_users', array('id' => $customer['id']));
|
||||
}
|
||||
|
||||
private function test_add_update() {
|
||||
// Insert new customer record (will be updated later).
|
||||
$customer = array(
|
||||
'last_name' => 'Doe',
|
||||
'first_name' => 'John',
|
||||
'email' => 'john@doe.com',
|
||||
'phone_number' => '0123456789',
|
||||
'address' => 'Abbey Road 18',
|
||||
'city' => 'London',
|
||||
'zip_code' => '12345',
|
||||
'id_roles' => $this->customer_role_id
|
||||
);
|
||||
$this->CI->db->insert('ea_users', $customer);
|
||||
$customer['id'] = intval($this->CI->db->insert_id());
|
||||
|
||||
// Update customer record.
|
||||
$new_phone_number = 'THE PHONE NUMBER IS UPDATED';
|
||||
$customer['phone_number'] = $new_phone_number;
|
||||
$update_result = $this->CI->customers_model->add($customer);
|
||||
$this->CI->unit->run($update_result, 'is_int', 'Test add() customer (update operation) '
|
||||
. 'has returned the row id.');
|
||||
|
||||
// Check if record was successfully updated.
|
||||
$db_phone_number = $this->CI->db->get_where('ea_users', array('id' => $customer['id']))
|
||||
->row()->phone_number;
|
||||
$this->CI->unit->run($customer['phone_number'], $db_phone_number, 'Test add() customer '
|
||||
. '(update operation) has successfully updated the phone number field.');
|
||||
|
||||
// Delete inserted record.
|
||||
$this->CI->db->delete('ea_users', array('id' => $customer['id']));
|
||||
}
|
||||
|
||||
private function test_add_invalid_email() {
|
||||
// Prepare customer's data (email address is invalid).
|
||||
$customer = array(
|
||||
'last_name' => 'Doe',
|
||||
'first_name' => 'John',
|
||||
'email' => 'THIS IS INVALID',
|
||||
'phone_number' => '0123456789',
|
||||
'address' => 'Abbey Road 18',
|
||||
'city' => 'London',
|
||||
'zip_code' => '12345',
|
||||
'id_roles' => $this->customer_role_id
|
||||
);
|
||||
|
||||
$has_thrown_exception = FALSE;
|
||||
try {
|
||||
$this->CI->customers_model->add($customer);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exception = TRUE;
|
||||
}
|
||||
|
||||
$this->CI->unit->run($has_thrown_exception, TRUE, 'Test add() customer with invalid '
|
||||
. 'email address.');
|
||||
}
|
||||
|
||||
private function test_add_missing_last_name() {
|
||||
// Prepare customer's data (last name field is missing).
|
||||
$customer = array(
|
||||
'first_name' => 'John',
|
||||
'email' => 'john@doe.com',
|
||||
'phone_number' => '0123456789',
|
||||
'address' => 'Abbey Road 18',
|
||||
'city' => 'London',
|
||||
'zip_code' => '12345',
|
||||
'id_roles' => $this->customer_role_id
|
||||
);
|
||||
|
||||
$has_thrown_exception = FALSE;
|
||||
try {
|
||||
$this->CI->customers_model->add($customer);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exception = TRUE;
|
||||
}
|
||||
|
||||
$this->CI->unit->run($has_thrown_exception, TRUE, 'Test add() customer with no last '
|
||||
. 'name value provided.');
|
||||
}
|
||||
|
||||
// TEST CUSTOMER EXISTS() METHOD
|
||||
private function test_exists() {
|
||||
// Insert new customer record (will be updated later).
|
||||
$customer = array(
|
||||
'last_name' => 'Doe',
|
||||
'first_name' => 'John',
|
||||
'email' => 'john@doe.com',
|
||||
'phone_number' => '0123456789',
|
||||
'address' => 'Abbey Road 18',
|
||||
'city' => 'London',
|
||||
'zip_code' => '12345',
|
||||
'id_roles' => $this->customer_role_id
|
||||
);
|
||||
$this->CI->db->insert('ea_users', $customer);
|
||||
$customer['id'] = intval($this->CI->db->insert_id());
|
||||
|
||||
// Test that exists returns true.
|
||||
$exists_result = $this->CI->customers_model->exists($customer);
|
||||
$this->CI->unit->run($exists_result, TRUE, 'Tests exists() with customer that exists.');
|
||||
|
||||
// Delete inserted record.
|
||||
$this->CI->db->delete('ea_users', array('id' => $customer['id']));
|
||||
}
|
||||
|
||||
private function test_exists_record_does_not_exist() {
|
||||
// Prepare customer's data with email that does not exist.
|
||||
$customer = array(
|
||||
'last_name' => 'Doe',
|
||||
'first_name' => 'John',
|
||||
'email' => 'THIS DOES NOT EXIST',
|
||||
'phone_number' => '0123456789',
|
||||
'address' => 'Abbey Road 18',
|
||||
'city' => 'London',
|
||||
'zip_code' => '12345',
|
||||
'id_roles' => $this->customer_role_id
|
||||
);
|
||||
|
||||
// Test that exists return false on email that doesn't exist.
|
||||
$exists_result = $this->CI->customers_model->exists($customer);
|
||||
$this->CI->unit->run($exists_result, FALSE, 'Test exists() method with customer data '
|
||||
. 'that does not exist in the database.');
|
||||
}
|
||||
|
||||
private function test_exists_no_email_provided() {
|
||||
// Prepare customer's data with no email value.
|
||||
$customer = array(
|
||||
'last_name' => 'Doe',
|
||||
'first_name' => 'John',
|
||||
'phone_number' => '0123456789',
|
||||
'address' => 'Abbey Road 18',
|
||||
'city' => 'London',
|
||||
'zip_code' => '12345',
|
||||
'id_roles' => $this->customer_role_id
|
||||
);
|
||||
|
||||
// Test that exists return false on email that doesn't exist.
|
||||
$has_thrown_exception = FALSE;
|
||||
try {
|
||||
$this->CI->customers_model->exists($customer);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exception = TRUE;
|
||||
}
|
||||
|
||||
$this->CI->unit->run($has_thrown_exception, TRUE, 'Test exists() method with email '
|
||||
. 'argument missing.');
|
||||
}
|
||||
|
||||
// TEST DELETE() CUSTOMER METHOD
|
||||
private function test_delete() {
|
||||
// Insert new customer record.
|
||||
$customer = array(
|
||||
'last_name' => 'Doe',
|
||||
'first_name' => 'John',
|
||||
'email' => 'john@doe.com',
|
||||
'phone_number' => '0123456789',
|
||||
'address' => 'Abbey Road 18',
|
||||
'city' => 'London',
|
||||
'zip_code' => '12345',
|
||||
'id_roles' => $this->customer_role_id
|
||||
);
|
||||
$this->CI->db->insert('ea_users', $customer);
|
||||
$customer['id'] = intval($this->CI->db->insert_id());
|
||||
|
||||
// Delete new customer record.
|
||||
$delete_result = $this->CI->customers_model->delete($customer['id']);
|
||||
$this->CI->unit->run($delete_result, TRUE, 'Test delete() method returned TRUE.');
|
||||
|
||||
// Check that the record is actually deleted (if not delete).
|
||||
$num_rows = $this->CI->db->get_where('ea_users', array('id' => $customer['id']))->num_rows();
|
||||
$this->CI->unit->run($num_rows, 0, 'Test delete() method has actually deleted the '
|
||||
. 'record from the db.');
|
||||
|
||||
if ($num_rows > 0) {
|
||||
$this->CI->db->delete('ea_users', array('id' => $customer['id']));
|
||||
}
|
||||
}
|
||||
|
||||
private function test_delete_record_that_does_not_exist() {
|
||||
$random_record_id = 879653245;
|
||||
|
||||
$delete_result = $this->CI->customers_model->delete($random_record_id);
|
||||
$this->CI->unit->run($delete_result, FALSE, 'Test delete() method with customer id '
|
||||
. 'that does not exist.');
|
||||
}
|
||||
|
||||
private function test_delete_record_with_invalid_argument() {
|
||||
$invalid_argument = 'THIS IS INVALID';
|
||||
$has_thrown_exception = FALSE;
|
||||
try {
|
||||
$this->CI->customers_model->delete($invalid_argument);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exception = TRUE;
|
||||
}
|
||||
$this->CI->unit->run($has_thrown_exception, TRUE, 'Test delete() method with invalid '
|
||||
. 'argument (not integer).');
|
||||
}
|
||||
|
||||
// TEST VALIDATE CUSTOMER DATA METHOD
|
||||
private function test_validate_data() {
|
||||
// Prepare customer's data.
|
||||
$customer = array(
|
||||
'last_name' => 'Doe',
|
||||
'first_name' => 'John',
|
||||
'email' => 'john@doe.com',
|
||||
'phone_number' => '0123456789',
|
||||
'address' => 'Abbey Road 18',
|
||||
'city' => 'London',
|
||||
'zip_code' => '12345',
|
||||
'id_roles' => $this->customer_role_id
|
||||
);
|
||||
|
||||
// Validate customers data.
|
||||
$validation_result = $this->CI->customers_model->validate($customer);
|
||||
$this->CI->unit->run($validation_result, TRUE, 'Test validate() method.');
|
||||
}
|
||||
|
||||
private function test_validate_data_no_last_name_provided() {
|
||||
// Prepare customer's data (no last_name value provided).
|
||||
$customer = array(
|
||||
'first_name' => 'John',
|
||||
'email' => 'john@doe.com',
|
||||
'phone_number' => '0123456789',
|
||||
'address' => 'Abbey Road 18',
|
||||
'city' => 'London',
|
||||
'zip_code' => '12345',
|
||||
'id_roles' => $this->customer_role_id
|
||||
);
|
||||
|
||||
// Validate customers data.
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->CI->customers_model->validate($customer);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
|
||||
$this->CI->unit->run($has_thrown_exc, TRUE, 'Test if validate() method without a '
|
||||
. 'last_name value has thrown exception.');
|
||||
}
|
||||
|
||||
private function test_validate_data_invalid_email_address() {
|
||||
// Prepare customer's data (invalid email address).
|
||||
$customer = array(
|
||||
'last_name' => 'Doe',
|
||||
'first_name' => 'John',
|
||||
'email' => 'THIS IS INVALID',
|
||||
'phone_number' => '0123456789',
|
||||
'address' => 'Abbey Road 18',
|
||||
'city' => 'London',
|
||||
'zip_code' => '12345',
|
||||
'id_roles' => $this->customer_role_id
|
||||
);
|
||||
|
||||
// Validate customers data.
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->CI->customers_model->validate($customer);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
|
||||
$this->CI->unit->run($has_thrown_exc, TRUE, 'Test if validate() method with invalid '
|
||||
. 'email address has thrown exception.');
|
||||
}
|
||||
|
||||
// TEST FIND RECORD ID METHOD
|
||||
private function test_find_record_id() {
|
||||
// Insert new customer to database.
|
||||
$customer = array(
|
||||
'last_name' => 'Doe',
|
||||
'first_name' => 'John',
|
||||
'email' => 'john@doe.com',
|
||||
'phone_number' => '0123456789',
|
||||
'address' => 'Abbey Road 18',
|
||||
'city' => 'London',
|
||||
'zip_code' => '12345',
|
||||
'id_roles' => $this->customer_role_id
|
||||
);
|
||||
$this->CI->db->insert('ea_users', $customer);
|
||||
$inserted_id = intval($this->CI->db->insert_id());
|
||||
|
||||
// Try to find the db id of the new customer record.
|
||||
$method_id = $this->CI->customers_model->find_record_id($customer);
|
||||
$this->CI->unit->run($inserted_id, $method_id, 'Test find_record_id() method.');
|
||||
|
||||
// Delete inserted customer record.
|
||||
$this->CI->db->delete('ea_users', array('id' => $inserted_id));
|
||||
}
|
||||
|
||||
private function test_find_record_id_without_email_address() {
|
||||
// Prepare customer's data without an email address.
|
||||
$customer = array(
|
||||
'last_name' => 'Doe',
|
||||
'first_name' => 'John',
|
||||
'phone_number' => '0123456789',
|
||||
'address' => 'Abbey Road 18',
|
||||
'city' => 'London',
|
||||
'zip_code' => '12345',
|
||||
'id_roles' => $this->customer_role_id
|
||||
);
|
||||
$has_thrown_exception = FALSE;
|
||||
try {
|
||||
$this->CI->customers_model->find_record_id($customer);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exception = TRUE;
|
||||
}
|
||||
|
||||
$this->CI->unit->run($has_thrown_exception, TRUE, 'Test find_record_id() without providing an email address.');
|
||||
}
|
||||
|
||||
private function test_find_record_id_record_does_not_exist() {
|
||||
// Prepare customer's data with an email address that does not exist in db.
|
||||
$customer = array(
|
||||
'last_name' => 'Doe',
|
||||
'first_name' => 'John',
|
||||
'email' => 'THIS EMAIL DOES NOT EXIST IN DB',
|
||||
'phone_number' => '0123456789',
|
||||
'address' => 'Abbey Road 18',
|
||||
'city' => 'London',
|
||||
'zip_code' => '12345',
|
||||
'id_roles' => $this->customer_role_id
|
||||
);
|
||||
$has_thrown_exception = FALSE;
|
||||
try {
|
||||
$this->CI->customers_model->find_record_id($customer);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exception = TRUE;
|
||||
}
|
||||
|
||||
$this->CI->unit->run($has_thrown_exception, TRUE, 'Test find_record_id() with email '
|
||||
. 'address that does not exist in db.');
|
||||
}
|
||||
|
||||
// TEST GET BATCH METHOD ---------------------------------------------------
|
||||
private function test_get_batch() {
|
||||
// Get all the customer rows without using the model.
|
||||
$db_data = $this->CI->db->get_where('ea_users',
|
||||
array('id_roles' => $this->customer_role_id))->result_array();
|
||||
// Get all the customer rows by using the model.
|
||||
$model_data = $this->CI->customers_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 customer record.
|
||||
$customer = array(
|
||||
'last_name' => 'Doe',
|
||||
'first_name' => 'John',
|
||||
'email' => 'john@doe.com',
|
||||
'phone_number' => '0123456789',
|
||||
'address' => 'Abbey Road 18',
|
||||
'city' => 'London',
|
||||
'zip_code' => '12345',
|
||||
'id_roles' => $this->customer_role_id
|
||||
);
|
||||
$this->CI->db->insert('ea_users', $customer);
|
||||
$customer['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' => $customer['id']))
|
||||
->result_array();
|
||||
|
||||
// Get data by using the model.
|
||||
$model_data = $this->CI->customers_model->get_batch(array('id' => $customer['id']));
|
||||
|
||||
// Check that the data arrays are the same.
|
||||
$this->CI->unit->run($no_model_data, $model_data, 'Test get_batch() with where clause.');
|
||||
|
||||
// Delete inserted record from database.
|
||||
$this->CI->db->delete('ea_users', array('id' => $customer['id']));
|
||||
}
|
||||
|
||||
private function unabled_test_get_batch_with_invalid_where_clause() {
|
||||
// CodeIgniter auto raises an exception if the where section is invalid.
|
||||
}
|
||||
|
||||
// TEST GET ROW METHOD
|
||||
private function test_get_row() {
|
||||
// Insert a new customer record.
|
||||
$customer = array(
|
||||
'last_name' => 'Doe',
|
||||
'first_name' => 'John',
|
||||
'email' => 'john@doe.com',
|
||||
'phone_number' => '0123456789',
|
||||
'address' => 'Abbey Road 18',
|
||||
'city' => 'London',
|
||||
'zip_code' => '12345',
|
||||
'id_roles' => $this->customer_role_id
|
||||
);
|
||||
$this->CI->db->insert('ea_users', $customer);
|
||||
$customer['id'] = intval($this->CI->db->insert_id());
|
||||
|
||||
// Get the new customer record from db.
|
||||
$no_model_data = $this->CI->db->get_where('ea_users', array('id' => $customer['id']))->row_array();
|
||||
$model_data = $this->CI->customers_model->get_row($customer['id']);
|
||||
|
||||
// Check that the row is the correct one.
|
||||
$this->CI->unit->run($no_model_data, $model_data, 'Test get_row() method');
|
||||
|
||||
// Delete inserted customer record.
|
||||
$this->CI->db->delete('ea_users', array('id' => $customer['id']));
|
||||
}
|
||||
|
||||
private function test_get_row_that_does_not_exist() {
|
||||
$random_record_id = 486868412;
|
||||
$row_data = $this->CI->customers_model->get_row($random_record_id);
|
||||
$this->CI->unit->run($row_data, NULL, 'Test get_row() with record id that does '
|
||||
. 'not exist in the database.');
|
||||
}
|
||||
|
||||
private function test_get_row_with_invalid_argument() {
|
||||
$invalid_id = 'THIS IS NOT AN INTEGER';
|
||||
|
||||
$has_thrown_exception = FALSE;
|
||||
try {
|
||||
$this->CI->customers_model->get_row($invalid_id);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exception = TRUE;
|
||||
}
|
||||
|
||||
$this->CI->unit->run($has_thrown_exception, TRUE, 'Test get_row() with wrong argument.');
|
||||
}
|
||||
|
||||
// TEST GET VALUE METHOD
|
||||
private function test_get_value() {
|
||||
// Insert new customer record.
|
||||
$customer = array(
|
||||
'last_name' => 'Doe',
|
||||
'first_name' => 'John',
|
||||
'email' => 'john@doe.com',
|
||||
'phone_number' => '0123456789',
|
||||
'address' => 'Abbey Road 18',
|
||||
'city' => 'London',
|
||||
'zip_code' => '12345',
|
||||
'id_roles' => $this->customer_role_id
|
||||
);
|
||||
$this->CI->db->insert('ea_users', $customer);
|
||||
$customer['id'] = intval($this->CI->db->insert_id());
|
||||
|
||||
// Get a specific value from the database.
|
||||
$model_value = $this->CI->customers_model->get_value('email', $customer['id']);
|
||||
|
||||
// Check if the value was correctly fetched from the database.
|
||||
$this->CI->unit->run($model_value, $customer['email'], 'Test get_value() method.');
|
||||
|
||||
// Delete inserted appointment record.
|
||||
$this->CI->db->delete('ea_users', array('id' => $customer['id']));
|
||||
}
|
||||
|
||||
private function test_get_value_record_does_not_exist() {
|
||||
$random_record_id = 843521368768;
|
||||
|
||||
$has_thrown_exception = FALSE;
|
||||
|
||||
try {
|
||||
$this->CI->customers_model->get_value('email', $random_record_id);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exception = TRUE;
|
||||
}
|
||||
|
||||
$this->CI->unit->run($has_thrown_exception, TRUE, 'Test get_value() with record id '
|
||||
. 'that does not exist.');
|
||||
}
|
||||
|
||||
private function test_get_value_field_does_not_exist() {
|
||||
// Insert new customer record.
|
||||
$customer = array(
|
||||
'last_name' => 'Doe',
|
||||
'first_name' => 'John',
|
||||
'email' => 'john@doe.com',
|
||||
'phone_number' => '0123456789',
|
||||
'address' => 'Abbey Road 18',
|
||||
'city' => 'London',
|
||||
'zip_code' => '12345',
|
||||
'id_roles' => $this->customer_role_id
|
||||
);
|
||||
$this->CI->db->insert('ea_users', $customer);
|
||||
$customer['id'] = intval($this->CI->db->insert_id());
|
||||
|
||||
// Try to get record value with wrong field name.
|
||||
$wrong_field_name = 'THIS IS WRONG';
|
||||
$has_thrown_exception = FALSE;
|
||||
|
||||
try {
|
||||
$this->CI->customers_model->get_value($wrong_field_name, $customer['id']);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exception = TRUE;
|
||||
}
|
||||
|
||||
$this->CI->unit->run($has_thrown_exception, TRUE, 'Test get_value() with record id '
|
||||
. 'that does not exist.');
|
||||
|
||||
// Delete inserted customer record.
|
||||
$this->CI->db->delete('ea_users', array('id' => $customer['id']));
|
||||
}
|
||||
}
|
||||
|
||||
/* End of file Unit_tests_customers_model.php */
|
||||
/* Location: ./application/libraries/Unit_tests/drivers/Unit_tests_customers_model.php */
|
|
@ -1,871 +0,0 @@
|
|||
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Easy!Appointments - Open Source Web Scheduler
|
||||
*
|
||||
* @package EasyAppointments
|
||||
* @author A.Tselegidis <alextselegidis@gmail.com>
|
||||
* @copyright Copyright (c) 2013 - 2016, Alex Tselegidis
|
||||
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
|
||||
* @link http://easyappointments.org
|
||||
* @since v1.0.0
|
||||
* ---------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Providers Model Unit Tests
|
||||
*
|
||||
* @package Libraries
|
||||
* @subpackage Tests
|
||||
*/
|
||||
class Unit_tests_providers_model extends CI_Driver {
|
||||
private $ci;
|
||||
private $provider_role_id;
|
||||
private $default_working_plan;
|
||||
|
||||
/**
|
||||
* Class Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->ci =& get_instance();
|
||||
$this->ci->load->library('Unit_test');
|
||||
$this->ci->load->model('providers_model');
|
||||
|
||||
$this->provider_role_id = $this->ci->db->get_where('ea_roles',
|
||||
array('slug' => DB_SLUG_PROVIDER))->row()->id;
|
||||
|
||||
$this->default_working_plan = $this->ci->db->get_where('ea_settings',
|
||||
array('name' => 'company_working_plan'))->row()->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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_providers_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() {
|
||||
$provider = array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'email' => 'test@test.com',
|
||||
'mobile_number' => '000000',
|
||||
'phone_number' => '111111',
|
||||
'address' => 'Some Str',
|
||||
'city' => 'Some City',
|
||||
'state' => 'Some State',
|
||||
'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',
|
||||
'working_plan' => $this->default_working_plan,
|
||||
'notifications' => TRUE,
|
||||
'google_sync' => 0,
|
||||
'google_token' => NULL,
|
||||
'google_calendar' => NULL,
|
||||
'sync_past_days' => '5',
|
||||
'sync_future_days' => '5'
|
||||
)
|
||||
);
|
||||
|
||||
// Insert new provider.
|
||||
$provider['id'] = $this->ci->providers_model->add($provider);
|
||||
$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['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'], $db_provider['settings']['salt'],
|
||||
$provider['settings']['password'], $db_provider['settings']['password']); // not needed
|
||||
|
||||
$this->ci->unit->run($provider, $db_provider, 'Check if add(insert) has successfully '
|
||||
. 'inserted a provider record.');
|
||||
|
||||
// Delete provider record (the relative ea_user_settings record will be deleted
|
||||
// automatically because of the cascade on delete setting.
|
||||
if ($this->ci->db->get_where('ea_users', array('id' => $provider['id']))->num_rows() > 0) {
|
||||
$this->ci->db->delete('ea_users', array('id' => $provider['id']));
|
||||
}
|
||||
}
|
||||
|
||||
private function test_add_update() {
|
||||
// Insert new provider.
|
||||
$provider = array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'email' => 'test@test.com',
|
||||
'mobile_number' => '000000',
|
||||
'phone_number' => '111111',
|
||||
'address' => 'Some Str',
|
||||
'city' => 'Some City',
|
||||
'state' => 'Some State',
|
||||
'zip_code' => '12345',
|
||||
'notes' => 'This is a test provider',
|
||||
'id_roles' => $this->provider_role_id
|
||||
);
|
||||
$this->ci->db->insert('ea_users', $provider);
|
||||
$provider['id'] = $this->ci->db->insert_id();
|
||||
|
||||
// Insert provider settings.
|
||||
$settings = array(
|
||||
'id_users' => $provider['id'],
|
||||
'username' => 'test_prov',
|
||||
'password' => 'test_prov',
|
||||
'working_plan' => $this->default_working_plan,
|
||||
'notifications' => TRUE,
|
||||
'google_sync' => 0,
|
||||
'google_token' => NULL,
|
||||
'google_calendar' => NULL,
|
||||
'sync_past_days' => '5',
|
||||
'sync_future_days' => '5'
|
||||
);
|
||||
$this->ci->db->insert('ea_user_settings', $settings);
|
||||
|
||||
// Update provider and check whether the operation completed successfully.
|
||||
$provider = array(
|
||||
'id' => $provider['id'], // Keep the old ID, everything else changes.
|
||||
'first_name' => 'CHANGED',
|
||||
'last_name' => 'CHANGED',
|
||||
'email' => 'changed@changed.com',
|
||||
'mobile_number' => 'CHANGED',
|
||||
'phone_number' => 'CHANGED',
|
||||
'address' => 'CHANGED',
|
||||
'city' => 'CHANGED',
|
||||
'state' => 'CHANGED',
|
||||
'zip_code' => 'CHANGED',
|
||||
'notes' => 'CHANGED',
|
||||
'id_roles' => $this->provider_role_id,
|
||||
'services' => array(),
|
||||
'settings' => array(
|
||||
'username' => 'CHANGED',
|
||||
'password' => 'CHANGED',
|
||||
'working_plan' => $this->default_working_plan,
|
||||
'notifications' => TRUE,
|
||||
'google_sync' => 0,
|
||||
'google_token' => NULL,
|
||||
'google_calendar' => NULL,
|
||||
'sync_past_days' => '9', // changed
|
||||
'sync_future_days' => '8' // changed
|
||||
)
|
||||
);
|
||||
|
||||
$update_result = $this->ci->providers_model->add($provider);
|
||||
$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['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'], $db_provider['settings']['salt'],
|
||||
$provider['settings']['password'], $db_provider['settings']['password']); // not needed
|
||||
|
||||
$this->ci->unit->run($provider, $db_provider, 'Check if add(update) has successfully '
|
||||
. 'updated a provider record.');
|
||||
|
||||
// Delete provider record (the relative ea_user_settings record will be deleted
|
||||
// automatically because of the cascade on delete setting.
|
||||
if ($this->ci->db->get_where('ea_users', array('id' => $provider['id']))->num_rows() > 0) {
|
||||
$this->ci->db->delete('ea_users', array('id' => $provider['id']));
|
||||
}
|
||||
}
|
||||
|
||||
private function test_add_with_invalid_data() {
|
||||
// Provider's data are missing required fields. That means that an excpetion
|
||||
// is expected.
|
||||
$provider = array(
|
||||
'first_name' => 'some name',
|
||||
'last_name' => 'some name',
|
||||
);
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->providers_model->add($provider);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if add() with invalid data has '
|
||||
. 'thrown an exception');
|
||||
}
|
||||
|
||||
// TEST EXISTS METHOD
|
||||
private function test_exists_with_record_that_exists() {
|
||||
// Insert new provider record.
|
||||
$provider = array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'email' => 'test@test.com',
|
||||
'mobile_number' => '000000',
|
||||
'phone_number' => '111111',
|
||||
'address' => 'Some Str',
|
||||
'city' => 'Some City',
|
||||
'state' => 'Some State',
|
||||
'zip_code' => '12345',
|
||||
'notes' => 'This is a test provider',
|
||||
'id_roles' => $this->provider_role_id
|
||||
);
|
||||
$this->ci->db->insert('ea_users', $provider);
|
||||
$provider['id'] = $this->ci->db->insert_id();
|
||||
|
||||
// Check whether exists() detects the record.
|
||||
$exists = $this->ci->providers_model->exists($provider);
|
||||
$this->ci->unit->run($exists, TRUE, 'Test exists() with record that exists.');
|
||||
|
||||
// Delete provider record.
|
||||
if ($this->ci->db->get_where('ea_users', array('id' => $provider['id']))->num_rows() > 0) {
|
||||
$this->ci->db->delete('ea_users', array('id' => $provider['id']));
|
||||
}
|
||||
}
|
||||
|
||||
private function test_exists_with_invalid_data() {
|
||||
// Insert new provider record.
|
||||
$provider = array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'email' => 'test@test.com',
|
||||
'mobile_number' => '000000',
|
||||
'phone_number' => '111111',
|
||||
'address' => 'Some Str',
|
||||
'city' => 'Some City',
|
||||
'state' => 'Some State',
|
||||
'zip_code' => '12345',
|
||||
'notes' => 'This is a test provider',
|
||||
'id_roles' => $this->provider_role_id
|
||||
);
|
||||
$this->ci->db->insert('ea_users', $provider);
|
||||
$provider['id'] = $this->ci->db->insert_id();
|
||||
|
||||
// Email is not provided, an exception is expected.
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
unset($provider['email']);
|
||||
$this->ci->providers_model->exists($provider);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test exists() with invalid record data.');
|
||||
|
||||
// Delete provider record.
|
||||
if ($this->ci->db->get_where('ea_users', array('id' => $provider['id']))->num_rows() > 0) {
|
||||
$this->ci->db->delete('ea_users', array('id' => $provider['id']));
|
||||
}
|
||||
}
|
||||
|
||||
private function test_exists_with_record_that_does_not_exist() {
|
||||
$provider = array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'email' => 'test@test.com',
|
||||
'mobile_number' => '000000',
|
||||
'phone_number' => '111111',
|
||||
'address' => 'Some Str',
|
||||
'city' => 'Some City',
|
||||
'state' => 'Some State',
|
||||
'zip_code' => '12345',
|
||||
'notes' => 'This is a test provider',
|
||||
'id_roles' => $this->provider_role_id
|
||||
);
|
||||
|
||||
$exists = $this->ci->providers_model->exists($provider);
|
||||
$this->ci->unit->run($exists, FALSE, 'Test exists() with record that does not exist.');
|
||||
}
|
||||
|
||||
// TEST FIND RECORD ID METHOD
|
||||
private function test_find_record_id() {
|
||||
// Insert new provider record.
|
||||
$provider = array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'email' => 'test@test.com',
|
||||
'mobile_number' => '000000',
|
||||
'phone_number' => '111111',
|
||||
'address' => 'Some Str',
|
||||
'city' => 'Some City',
|
||||
'state' => 'Some State',
|
||||
'zip_code' => '12345',
|
||||
'notes' => 'This is a test provider',
|
||||
'id_roles' => $this->provider_role_id
|
||||
);
|
||||
$this->ci->db->insert('ea_users', $provider);
|
||||
$insert_id = $this->ci->db->insert_id();
|
||||
|
||||
// Check if the record id will be retrieved.
|
||||
$provider['id'] = $this->ci->providers_model->find_record_id($provider);
|
||||
$this->ci->unit->run($provider['id'], $insert_id, 'Test if find_record_id() has '
|
||||
. 'successfully found the inserted record id.');
|
||||
|
||||
// Delete provider record.
|
||||
if ($this->ci->db->get_where('ea_users', array('id' => $insert_id))->num_rows() > 0) {
|
||||
$this->ci->db->delete('ea_users', array('id' => $insert_id));
|
||||
}
|
||||
}
|
||||
|
||||
private function test_find_record_id_with_invalid_data() {
|
||||
$provider = array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
//'email' => 'test@test.com', // Email is not provided
|
||||
'mobile_number' => '000000',
|
||||
'phone_number' => '111111',
|
||||
'address' => 'Some Str',
|
||||
'city' => 'Some City',
|
||||
'state' => 'Some State',
|
||||
'zip_code' => '12345',
|
||||
'notes' => 'This is a test provider',
|
||||
'id_roles' => $this->provider_role_id
|
||||
);
|
||||
|
||||
// An exception is expected.
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->providers_model->find_record_id($provider);
|
||||
} 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 data.');
|
||||
}
|
||||
|
||||
private function test_find_record_id_with_record_that_does_not_exist() {
|
||||
// The following provider record does not exist on database. Therefore an
|
||||
// exception is expected.
|
||||
$provider = array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'email' => 'test@test.com',
|
||||
'mobile_number' => '000000',
|
||||
'phone_number' => '111111',
|
||||
'address' => 'Some Str',
|
||||
'city' => 'Some City',
|
||||
'state' => 'Some State',
|
||||
'zip_code' => '12345',
|
||||
'notes' => 'This is a test provider',
|
||||
'id_roles' => $this->provider_role_id
|
||||
);
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->providers_model->find_record_id($provider);
|
||||
} 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.');
|
||||
}
|
||||
|
||||
// TEST VALIDATE RECORD METHOD
|
||||
private function test_validate() {
|
||||
$provider = array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'email' => 'test@test.com',
|
||||
'mobile_number' => '000000',
|
||||
'phone_number' => '111111',
|
||||
'address' => 'Some Str',
|
||||
'city' => 'Some City',
|
||||
'state' => 'Some State',
|
||||
'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',
|
||||
'working_plan' => $this->default_working_plan,
|
||||
'notifications' => TRUE,
|
||||
'google_sync' => FALSE,
|
||||
'google_token' => NULL,
|
||||
'sync_past_days' => '5',
|
||||
'sync_future_days' => '5'
|
||||
)
|
||||
);
|
||||
|
||||
$validation_result = $this->ci->providers_model->validate($provider);
|
||||
$this->ci->unit->run($validation_result, TRUE, 'Test if validate() returned TRUE '
|
||||
. 'with valid data');
|
||||
}
|
||||
|
||||
private function test_validate_with_record_that_does_not_exist() {
|
||||
$provider = array(
|
||||
'id' => '23209348092', // This id does not exists in db.
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'email' => 'test@test.com',
|
||||
'mobile_number' => '000000',
|
||||
'phone_number' => '111111',
|
||||
'address' => 'Some Str',
|
||||
'city' => 'Some City',
|
||||
'state' => 'Some State',
|
||||
'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',
|
||||
'working_plan' => $this->default_working_plan,
|
||||
'notifications' => TRUE,
|
||||
'google_sync' => FALSE,
|
||||
'google_token' => NULL,
|
||||
'sync_past_days' => '5',
|
||||
'sync_future_days' => '5'
|
||||
)
|
||||
);
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->providers_model->validate($provider);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if validate() has thrown an exception '
|
||||
. 'with record that does not exist.');
|
||||
}
|
||||
|
||||
private function test_validate_with_missing_required_fields() {
|
||||
$provider = array(
|
||||
'first_name' => 'John',
|
||||
//'last_name' => 'Doe',
|
||||
//'email' => 'test@test.com',
|
||||
'mobile_number' => '000000',
|
||||
//'phone_number' => '111111',
|
||||
'address' => 'Some Str',
|
||||
'city' => 'Some City',
|
||||
'state' => 'Some State',
|
||||
'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',
|
||||
'working_plan' => $this->default_working_plan,
|
||||
'notifications' => TRUE,
|
||||
'google_sync' => FALSE,
|
||||
'google_token' => NULL,
|
||||
'sync_past_days' => '5',
|
||||
'sync_future_days' => '5'
|
||||
)
|
||||
);
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->providers_model->validate($provider);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if validate() has thrown an exception '
|
||||
. 'with data that is missing required fields.');
|
||||
}
|
||||
|
||||
private function test_validate_with_invalid_email_address() {
|
||||
$provider = array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'email' => 'THIS IS INVALID',
|
||||
'mobile_number' => '000000',
|
||||
'phone_number' => '111111',
|
||||
'address' => 'Some Str',
|
||||
'city' => 'Some City',
|
||||
'state' => 'Some State',
|
||||
'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',
|
||||
'working_plan' => $this->default_working_plan,
|
||||
'notifications' => TRUE,
|
||||
'google_sync' => FALSE,
|
||||
'google_token' => NULL,
|
||||
'sync_past_days' => '5',
|
||||
'sync_future_days' => '5'
|
||||
)
|
||||
);
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->providers_model->validate($provider);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if validate() has thrown an exception '
|
||||
. 'with data that has invalid email.');
|
||||
}
|
||||
|
||||
private function test_validate_with_no_settings() {
|
||||
$provider = array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'email' => 'THIS IS INVALID',
|
||||
'mobile_number' => '000000',
|
||||
'phone_number' => '111111',
|
||||
'address' => 'Some Str',
|
||||
'city' => 'Some City',
|
||||
'state' => 'Some State',
|
||||
'zip_code' => '12345',
|
||||
'notes' => 'This is a test provider',
|
||||
'id_roles' => $this->provider_role_id
|
||||
);
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->providers_model->validate($provider);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if validate() has thrown an exception '
|
||||
. 'with data that do not contain the provider settings.');
|
||||
}
|
||||
|
||||
// TEST DELETE RECORD METHOD
|
||||
private function test_delete() {
|
||||
// Insert new provider record with settings.
|
||||
$provider = array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'email' => 'THIS IS INVALID',
|
||||
'mobile_number' => '000000',
|
||||
'phone_number' => '111111',
|
||||
'address' => 'Some Str',
|
||||
'city' => 'Some City',
|
||||
'state' => 'Some State',
|
||||
'zip_code' => '12345',
|
||||
'notes' => 'This is a test provider',
|
||||
'id_roles' => $this->provider_role_id
|
||||
);
|
||||
$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',
|
||||
'password' => 'test-prov',
|
||||
'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', $provider['settings']);
|
||||
|
||||
// Delete provider record from database.
|
||||
$result = $this->ci->providers_model->delete($provider['id']);
|
||||
$this->ci->unit->run($result, TRUE, 'Test if delete() returned TRUE as result');
|
||||
|
||||
// Check if the record has successfully been deleted.
|
||||
$provider_num_rows = $this->ci->db->get_where('ea_users',
|
||||
array('id' => $provider['id']))->num_rows();
|
||||
$this->ci->unit->run($provider_num_rows, 0, 'Test if delete() has successfully '
|
||||
. 'deleted provider record.');
|
||||
|
||||
$settings_num_rows = $this->ci->db->get_where('ea_user_settings',
|
||||
array('id_users' => $provider['id']))->num_rows();
|
||||
$this->ci->unit->run($settings_num_rows, 0, 'Test if delete() has successfully '
|
||||
. 'deleted provider settings record.');
|
||||
|
||||
// Delete records (if needed)
|
||||
if ($provider_num_rows > 0) {
|
||||
$this->ci->db->delete('ea_users', array('id' => $provider['id']));
|
||||
}
|
||||
|
||||
if ($settings_num_rows > 0) {
|
||||
$this->ci->db->delete('ea_user_settings', array('id_users' => $provider['id']));
|
||||
}
|
||||
}
|
||||
|
||||
private function test_delete_with_invalid_record_id() {
|
||||
$invalid_id = 'this is invalid';
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->providers_model->delete($invalid_id);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if delete() with invalid id argument '
|
||||
. 'has thrown an exception.');
|
||||
}
|
||||
|
||||
private function test_delete_with_record_that_does_not_exist() {
|
||||
$provider_id = '234082390'; // This record id does not exist in db.
|
||||
$result = $this->ci->providers_model->delete($provider_id);
|
||||
$this->ci->unit->run($result, FALSE, 'Test if delete() with record id that does '
|
||||
. 'not exist in database, has returned FALSE as result.');
|
||||
}
|
||||
|
||||
// TEST GET ROW METHOD
|
||||
private function test_get_row() {
|
||||
// Insert a new provider record with settings.
|
||||
$provider = array(
|
||||
'last_name' => 'Doe',
|
||||
'first_name' => 'John',
|
||||
'email' => 'john@doe.com',
|
||||
'phone_number' => '0123456789',
|
||||
'address' => 'Abbey Road 18',
|
||||
'city' => 'London',
|
||||
'zip_code' => '12345',
|
||||
'id_roles' => $this->provider_role_id
|
||||
);
|
||||
$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',
|
||||
'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', $provider['settings']);
|
||||
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['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']);
|
||||
|
||||
$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']));
|
||||
}
|
||||
|
||||
private function test_get_row_that_does_not_exist() {
|
||||
$random_record_id = 486868412;
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->providers_model->get_row($random_record_id);
|
||||
} 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.');
|
||||
}
|
||||
|
||||
private function test_get_row_with_invalid_argument() {
|
||||
$invalid_id = 'THIS IS NOT AN INTEGER';
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->providers_model->get_row($invalid_id);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test get_row() with wrong argument.');
|
||||
}
|
||||
|
||||
// TEST GET VALUE METHOD ---------------------------------------------
|
||||
private function test_get_value() {
|
||||
// Insert new provider record.
|
||||
$provider = array(
|
||||
'last_name' => 'Doe',
|
||||
'first_name' => 'John',
|
||||
'email' => 'john@doe.com',
|
||||
'phone_number' => '0123456789',
|
||||
'address' => 'Abbey Road 18',
|
||||
'city' => 'London',
|
||||
'zip_code' => '12345',
|
||||
'id_roles' => $this->provider_role_id
|
||||
);
|
||||
$this->ci->db->insert('ea_users', $provider);
|
||||
$provider['id'] = intval($this->ci->db->insert_id());
|
||||
|
||||
// Get a specific value from the database.
|
||||
$model_value = $this->ci->providers_model->get_value('email', $provider['id']);
|
||||
|
||||
// Check if the value was correctly fetched from the database.
|
||||
$this->ci->unit->run($model_value, $provider['email'], 'Test get_value() method.');
|
||||
|
||||
// Delete inserted appointment record.
|
||||
$this->ci->db->delete('ea_users', array('id' => $provider['id']));
|
||||
}
|
||||
|
||||
private function test_get_value_record_does_not_exist() {
|
||||
$random_record_id = 843521368768;
|
||||
|
||||
$has_thrown_exception = FALSE;
|
||||
|
||||
try {
|
||||
$this->ci->providers_model->get_value('email', $random_record_id);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exception = TRUE;
|
||||
}
|
||||
|
||||
$this->ci->unit->run($has_thrown_exception, TRUE, 'Test get_value() with record id that '
|
||||
. 'does not exist.');
|
||||
}
|
||||
|
||||
private function test_get_value_field_does_not_exist() {
|
||||
// Insert new provider record.
|
||||
$provider = array(
|
||||
'last_name' => 'Doe',
|
||||
'first_name' => 'John',
|
||||
'email' => 'john@doe.com',
|
||||
'phone_number' => '0123456789',
|
||||
'address' => 'Abbey Road 18',
|
||||
'city' => 'London',
|
||||
'zip_code' => '12345',
|
||||
'id_roles' => $this->provider_role_id
|
||||
);
|
||||
$this->ci->db->insert('ea_users', $provider);
|
||||
$provider['id'] = intval($this->ci->db->insert_id());
|
||||
|
||||
// Try to get record value with wrong field name.
|
||||
$wrong_field_name = 'THIS IS WRONG';
|
||||
$has_thrown_exception = FALSE;
|
||||
|
||||
try {
|
||||
$this->ci->providers_model->get_value($wrong_field_name, $provider['id']);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exception = TRUE;
|
||||
}
|
||||
|
||||
$this->ci->unit->run($has_thrown_exception, TRUE, 'Test get_value() with record id that '
|
||||
. 'does not exist.');
|
||||
|
||||
// Delete inserted appointment record.
|
||||
$this->ci->db->delete('ea_users', array('id' => $provider['id']));
|
||||
}
|
||||
|
||||
// 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();
|
||||
|
||||
foreach($db_data as &$db_provider) {
|
||||
$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 and try to fetch it.
|
||||
$provider = array(
|
||||
'last_name' => 'Doe',
|
||||
'first_name' => 'John',
|
||||
'email' => 'john@doe.com',
|
||||
'phone_number' => '0123456789',
|
||||
'address' => 'Abbey Road 18',
|
||||
'city' => 'London',
|
||||
'zip_code' => '12345',
|
||||
'id_roles' => $this->provider_role_id
|
||||
);
|
||||
$this->ci->db->insert('ea_users', $provider);
|
||||
$provider['id'] = intval($this->ci->db->insert_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_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_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_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 clause is invalid.
|
||||
}
|
||||
}
|
||||
|
||||
/* End of file Unit_tests_providers_model.php */
|
||||
/* Location: ./application/libraries/Unit_tests/drivers/Unit_tests_providers_model.php */
|
|
@ -1,554 +0,0 @@
|
|||
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Easy!Appointments - Open Source Web Scheduler
|
||||
*
|
||||
* @package EasyAppointments
|
||||
* @author A.Tselegidis <alextselegidis@gmail.com>
|
||||
* @copyright Copyright (c) 2013 - 2016, Alex Tselegidis
|
||||
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
|
||||
* @link http://easyappointments.org
|
||||
* @since v1.0.0
|
||||
* ---------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Secretaries Model Unit Tests
|
||||
*
|
||||
* @package Libraries
|
||||
* @subpackage Tests
|
||||
*/
|
||||
class Unit_tests_secretaries_model extends CI_Driver {
|
||||
private $ci;
|
||||
private $secretary_role_id;
|
||||
private $default_secretary; // does not contain an 'id' value
|
||||
private $default_settings;
|
||||
|
||||
/**
|
||||
* 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
|
||||
);
|
||||
|
||||
$this->default_settings = array(
|
||||
'username' => 'test_secretary',
|
||||
'password' => 'test_pswd',
|
||||
'working_plan' => NULL,
|
||||
'notifications' => FALSE,
|
||||
'google_sync' => 0,
|
||||
'google_token' => NULL,
|
||||
'sync_past_days' => NULL,
|
||||
'sync_future_days' => NULL
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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['settings'] = $this->default_settings;
|
||||
$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();
|
||||
|
||||
$db_secretary['providers'] = array();
|
||||
$db_secretary['settings'] = $this->ci->db->get_where('ea_user_settings',
|
||||
array('id_users' => $secretary['id']))->row_array();
|
||||
unset($db_secretary['settings']['id_users'], $db_secretary['settings']['salt'],
|
||||
$secretary['settings']['password'], $db_secretary['settings']['password']); // not needed
|
||||
|
||||
$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['settings'] = $this->default_settings;
|
||||
$secretary['settings']['id_users'] = $secretary['id'];
|
||||
$this->ci->db->insert('ea_user_settings', $secretary['settings']);
|
||||
unset($secretary['settings']['id_users']);
|
||||
|
||||
$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();
|
||||
$db_secretary['providers'] = array();
|
||||
$db_secretary['settings'] = $this->ci->db->get_where('ea_user_settings',
|
||||
array('id_users' => $secretary['id']))->row_array();
|
||||
unset($db_secretary['settings']['id_users'], $db_secretary['settings']['salt'],
|
||||
$secretary['settings']['password'], $db_secretary['settings']['password']); // not needed
|
||||
|
||||
$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 disabled_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());
|
||||
|
||||
$secretary['settings'] = $this->default_settings;
|
||||
$secretary['settings']['id_users'] = $secretary_id;
|
||||
$this->ci->db->insert('ea_user_settings', $secretary['settings']);
|
||||
unset($secretary['settings']['id_users']);
|
||||
|
||||
// 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 add() 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();
|
||||
$db_secretary['providers'] = array();
|
||||
unset($db_secretary['id']);
|
||||
|
||||
$db_secretary['settings'] = $this->ci->db->get_where('ea_user_settings',
|
||||
array('id_users' => $secretary_id))->row_array();
|
||||
unset($db_secretary['settings']['id_users'], $db_secretary['settings']['salt'],
|
||||
$secretary['settings']['password'], $db_secretary['settings']['password']); // not needed
|
||||
|
||||
$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.';
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->secretaries_model->validate($secretary);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if validate() has thrown an exception '
|
||||
. 'with invalid data (provided id does not exists in db).');
|
||||
}
|
||||
|
||||
private function test_validate_invalid_users_value_data_type() {
|
||||
$secretary = $this->default_secretary;
|
||||
$secretary['providers'] = 'This is not an array';
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->secretaries_model->validate($secretary);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if validate() has thrown an exception '
|
||||
. 'with 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']);
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->secretaries_model->validate($secretary);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if validate() has thrown an exception '
|
||||
. 'with invalid data (missing required field values).');
|
||||
}
|
||||
|
||||
private function test_validate_invalid_email_address() {
|
||||
$secretary = $this->default_secretary;
|
||||
$secretary['email'] = 'This is invalid.';
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->secretaries_model->validate($secretary);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if validate() has thrown an exception '
|
||||
. 'with 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());
|
||||
|
||||
$secretary['providers'] = array();
|
||||
|
||||
$secretary['settings'] = $this->default_settings;
|
||||
$secretary['settings']['id_users'] = $secretary['id'];
|
||||
$this->ci->db->insert('ea_user_settings', $secretary['settings']);
|
||||
|
||||
unset($secretary['settings']['id_users'], $secretary['settings']['password']);
|
||||
|
||||
$model_secretary = $this->ci->secretaries_model->get_row($secretary['id']);
|
||||
unset($model_secretary['settings']['password']);
|
||||
|
||||
$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.
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->secretaries_model->get_row($random_id);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if get_row() has thrown an exception '
|
||||
. 'when trying to get a record that does not exist in the database.');
|
||||
}
|
||||
|
||||
// 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();
|
||||
|
||||
$no_model_batch = $this->ci->db->get_where('ea_users',
|
||||
array('id_roles' => $this->secretary_role_id))->result_array();
|
||||
|
||||
foreach($no_model_batch as &$secretary) {
|
||||
$providers = $this->ci->db->get_where('ea_secretaries_providers',
|
||||
array('id_users_secretary' => $secretary['id']))->result_array();
|
||||
$secretary['providers'] = array();
|
||||
foreach($providers as $provider) {
|
||||
$secretary['providers'][] = $provider['id_users_provider'];
|
||||
}
|
||||
|
||||
$secretary['settings'] = $this->ci->db->get_where('ea_user_settings',
|
||||
array('id_users' => $secretary['id']))->row_array();
|
||||
unset($secretary['settings']['id_users']);
|
||||
}
|
||||
|
||||
$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());
|
||||
|
||||
$secretary['settings'] = $this->default_settings;
|
||||
$secretary['settings']['id_users'] = $secretary['id'];
|
||||
$this->ci->db->insert('ea_user_settings', $secretary['settings']);
|
||||
unset($secretary['settings']['id_users']);
|
||||
|
||||
$model_batch = $this->ci->secretaries_model->get_batch(array('id' => $secretary['id']));
|
||||
|
||||
$no_model_batch = $this->ci->db->get_where('ea_users', array('id' => $secretary['id']))->result_array();
|
||||
foreach($no_model_batch as &$secretary) {
|
||||
$providers = $this->ci->db->get_where('ea_secretaries_providers',
|
||||
array('id_users_secretary' => $secretary['id']))->result_array();
|
||||
$secretary['providers'] = array();
|
||||
foreach($providers as $provider) {
|
||||
$secretary['providers'][] = $provider['id_users_provider'];
|
||||
}
|
||||
|
||||
$secretary['settings'] = $this->ci->db->get_where('ea_user_settings',
|
||||
array('id_users' => $secretary['id']))->row_array();
|
||||
unset($secretary['settings']['id_users']);
|
||||
}
|
||||
|
||||
$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 */
|
|
@ -1,691 +0,0 @@
|
|||
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Easy!Appointments - Open Source Web Scheduler
|
||||
*
|
||||
* @package EasyAppointments
|
||||
* @author A.Tselegidis <alextselegidis@gmail.com>
|
||||
* @copyright Copyright (c) 2013 - 2016, Alex Tselegidis
|
||||
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
|
||||
* @link http://easyappointments.org
|
||||
* @since v1.0.0
|
||||
* ---------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Services Model Unit Tests
|
||||
*
|
||||
* @package Libraries
|
||||
* @subpackage Tests
|
||||
*/
|
||||
class Unit_tests_services_model extends CI_Driver {
|
||||
private $ci;
|
||||
|
||||
/**
|
||||
* Class Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->ci =& get_instance();
|
||||
$this->ci->load->library('Unit_test');
|
||||
$this->ci->load->model('services_model');
|
||||
}
|
||||
|
||||
/**
|
||||
* 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_services_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() {
|
||||
$service = array(
|
||||
'name' => 'Test Service',
|
||||
'duration' => 30,
|
||||
'price' => '20.00',
|
||||
'currency' => 'Euro',
|
||||
'description' => 'Some description here ...',
|
||||
'id_service_categories' => NULL
|
||||
);
|
||||
|
||||
$service['id'] = $this->ci->services_model->add($service);
|
||||
$this->ci->unit->run($service['id'], 'is_int', 'Test if add() - insert operation has '
|
||||
. 'returned a valid record id.');
|
||||
|
||||
$db_record = $this->ci->db->get_where('ea_services', array('id' => $service['id']))->row_array();
|
||||
$this->ci->unit->run($service, $db_record, 'Test if add() - insert operation, has '
|
||||
. 'successfully inserted a new record.');
|
||||
|
||||
$this->ci->db->delete('ea_services', array('id' => $service['id']));
|
||||
}
|
||||
|
||||
private function test_add_update() {
|
||||
$service = array(
|
||||
'name' => 'Test Service',
|
||||
'duration' => 30,
|
||||
'price' => '20.00',
|
||||
'currency' => 'Euro',
|
||||
'description' => 'Some description here ...',
|
||||
'id_service_categories' => NULL
|
||||
);
|
||||
|
||||
$this->ci->db->insert('ea_services', $service);
|
||||
$service['id'] = $this->ci->db->insert_id();
|
||||
|
||||
// Update record data
|
||||
$service['name'] = 'Updated Name';
|
||||
$service['duration'] = 60;
|
||||
$service['price'] = '60.45';
|
||||
$service['description'] = 'Updated description ...';
|
||||
|
||||
$update_result = $this->ci->services_model->add($service);
|
||||
$this->ci->unit->run($update_result, 'is_int', 'Test if add() - update operation, has '
|
||||
. 'returned the record id.');
|
||||
|
||||
$db_record = $this->ci->db->get_where('ea_services', array('id' => $service['id']))->row_array();
|
||||
$this->ci->unit->run($service, $db_record, 'Test if add() - update operation, has '
|
||||
. 'successfully updated a record.');
|
||||
|
||||
$this->ci->db->delete('ea_services', array('id' => $service['id']));
|
||||
}
|
||||
|
||||
private function test_add_invalid_data() {
|
||||
$service = array(
|
||||
'name' => '', // invalid
|
||||
'duration' => 'invalid',
|
||||
'price' => 'invalid',
|
||||
'currency' => 'Euro',
|
||||
'description' => 'Some description here ...',
|
||||
'id_service_categories' => NULL
|
||||
);
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->services_model->add($service);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Check if add() with invalid data has '
|
||||
. 'thrown exception.');
|
||||
}
|
||||
|
||||
// TEST EXISTS METHOD --------------------------------------------------
|
||||
private function test_exists_record_exists() {
|
||||
// insert record
|
||||
$service = array(
|
||||
'name' => 'Test Service',
|
||||
'duration' => 30,
|
||||
'price' => '20.00',
|
||||
'currency' => 'Euro',
|
||||
'description' => 'Some description here ...',
|
||||
'id_service_categories' => NULL
|
||||
);
|
||||
|
||||
$this->ci->db->insert('ea_services', $service);
|
||||
$service['id'] = $this->ci->db->insert_id();
|
||||
|
||||
// check exists
|
||||
$exists = $this->ci->services_model->exists($service);
|
||||
$this->ci->unit->run($exists, TRUE, 'Check if exists() returned true on existing record.');
|
||||
|
||||
// delete record
|
||||
$this->ci->db->delete('ea_services', array('id' => $service['id']));
|
||||
}
|
||||
|
||||
private function test_exists_record_does_not_exist() {
|
||||
$service = array(
|
||||
'name' => 'Random Name',
|
||||
'duration' => 'Random Duration',
|
||||
'price' => 'Random Price'
|
||||
);
|
||||
|
||||
$exists = $this->ci->services_model->exists($service);
|
||||
$this->ci->unit->run($exists, FALSE, 'Check if exists() returned false on non-existing record.');
|
||||
}
|
||||
|
||||
private function test_exists_invalid_arguments() {
|
||||
$invalid_arg = '';
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$exists = $this->ci->services_model->exists($invalid_arg);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Check if exists() with invalid argument has '
|
||||
. 'thrown an exception.');
|
||||
}
|
||||
|
||||
// TEST VALIDATE METHOD -------------------------------------------------
|
||||
private function test_validate() {
|
||||
$service = array(
|
||||
'name' => 'Test Service',
|
||||
'duration' => 30,
|
||||
'price' => '20.00',
|
||||
'currency' => 'Euro',
|
||||
'description' => 'Some description here ...',
|
||||
'id_service_categories' => NULL
|
||||
);
|
||||
|
||||
$is_valid = $this->ci->services_model->validate($service);
|
||||
$this->ci->unit->run($is_valid, TRUE, 'Test validate() method with valid data.');
|
||||
}
|
||||
|
||||
private function test_validate_invalid_record_id() {
|
||||
$service = array(
|
||||
'id' => 'THIS IS INVALID',
|
||||
'name' => 'Test Service',
|
||||
'duration' => 30,
|
||||
'price' => '20.00',
|
||||
'currency' => 'Euro',
|
||||
'description' => 'Some description here ...',
|
||||
'id_service_categories' => NULL
|
||||
);
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->services_model->validate($service);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if validate() has thrown an exception '
|
||||
. 'with invalid record id.');
|
||||
}
|
||||
|
||||
private function test_validate_invalid_service_category_id() {
|
||||
$service = array(
|
||||
'name' => 'Test Service',
|
||||
'duration' => 30,
|
||||
'price' => '20.00',
|
||||
'currency' => 'Euro',
|
||||
'description' => 'Some description here ...',
|
||||
'id_service_categories' => 'THIS IS INVALID'
|
||||
);
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->services_model->validate($service);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if validate() has thrown an exception '
|
||||
. 'with invalid service category id.');
|
||||
}
|
||||
|
||||
private function test_validate_invalid_service_name() {
|
||||
$service = array(
|
||||
'name' => '', // invalid
|
||||
'duration' => 30,
|
||||
'price' => '20.00',
|
||||
'currency' => 'Euro',
|
||||
'description' => 'Some description here ...',
|
||||
'id_service_categories' => 'THIS IS INVALID'
|
||||
);
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->services_model->validate($service);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if validate() has thrown an exception '
|
||||
. 'with invalid service service name.');
|
||||
}
|
||||
|
||||
// TEST FIND RECORD ID --------------------------------------------------
|
||||
private function test_find_record_id() {
|
||||
// insert record
|
||||
$service = array(
|
||||
'name' => 'Test Service',
|
||||
'duration' => 30,
|
||||
'price' => '20.00',
|
||||
'currency' => 'Euro',
|
||||
'description' => 'Some description here ...',
|
||||
'id_service_categories' => NULL
|
||||
);
|
||||
|
||||
$this->ci->db->insert('ea_services', $service);
|
||||
$service['id'] = intval($this->ci->db->insert_id());
|
||||
|
||||
// find record id
|
||||
$service_id = $this->ci->services_model->find_record_id($service);
|
||||
$this->ci->unit->run($service_id, $service['id'], 'Test find_record_id() with record '
|
||||
. 'that exist.');
|
||||
|
||||
// delete record
|
||||
$this->ci->db->delete('ea_services', array('id' => $service['id']));
|
||||
}
|
||||
|
||||
private function test_find_record_id_invalid_service_data() {
|
||||
$service = array(
|
||||
// name, duration and price are not provided
|
||||
'currency' => 'Euro',
|
||||
'description' => 'Some description here ...',
|
||||
'id_service_categories' => NULL
|
||||
);
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->services_model->find_record_id($service);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test find_record_id() with invalid service data.');
|
||||
}
|
||||
|
||||
private function test_find_record_id_record_does_not_exist() {
|
||||
$service = array(
|
||||
'name' => 'Does not exist',
|
||||
'duration' => 'Does not exist',
|
||||
'price' => 'Does not exist'
|
||||
);
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->services_model->find_record_id($service);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test find_record_id() with record that '
|
||||
. 'does not exist.');
|
||||
}
|
||||
|
||||
// TEST DELETE METHOD ---------------------------------------------------
|
||||
private function test_delete() {
|
||||
// insert record
|
||||
$service = array(
|
||||
'name' => 'Test Service',
|
||||
'duration' => 30,
|
||||
'price' => '20.00',
|
||||
'currency' => 'Euro',
|
||||
'description' => 'Some description here ...',
|
||||
'id_service_categories' => NULL
|
||||
);
|
||||
|
||||
$this->ci->db->insert('ea_services', $service);
|
||||
$service['id'] = intval($this->ci->db->insert_id());
|
||||
|
||||
// delete record
|
||||
$delete_result = $this->ci->services_model->delete($service['id']);
|
||||
$this->ci->unit->run($delete_result, TRUE, 'Test delete() method result.');
|
||||
|
||||
// check if record was actually deleted
|
||||
$num_rows = $this->ci->db->get_where('ea_services', array('name' => $service['name']))
|
||||
->num_rows();
|
||||
$this->ci->unit->run($num_rows, 0, 'Test if delete() method has actually deleted the record.');
|
||||
}
|
||||
|
||||
private function test_delete_invalid_argument() {
|
||||
$invalid_arg = 'THIS IS INVALID';
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->services_model->delete($invalid_arg);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if delete() method has thrown '
|
||||
. 'exception on invalid argument.');
|
||||
}
|
||||
|
||||
private function test_delete_record_does_not_exist() {
|
||||
$random_id = 1029800987;
|
||||
$has_thrown_exc = FALSE;
|
||||
$delete_result = $this->ci->services_model->delete($random_id);
|
||||
$this->ci->unit->run($delete_result, FALSE, 'Test if delete() method result on record '
|
||||
. 'that does not exist.');
|
||||
}
|
||||
|
||||
|
||||
// TEST GET BATCH METHOD -----------------------------------------
|
||||
private function test_get_batch() {
|
||||
// Get all the service rows without using the model.
|
||||
$db_data = $this->ci->db->get('ea_services')->result_array();
|
||||
// Get all the service rows by using the model.
|
||||
$model_data = $this->ci->services_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 service record.
|
||||
$service = array(
|
||||
'name' => 'General Examination',
|
||||
'duration' => 30,
|
||||
'price' => 50.00,
|
||||
'currency' => 'euro',
|
||||
'description' => 'This is some service description.'
|
||||
);
|
||||
|
||||
$this->ci->db->insert('ea_services', $service);
|
||||
$service['id'] = intval($this->ci->db->insert_id());
|
||||
|
||||
// Get data without using the model.
|
||||
$no_model_data = $this->ci->db->get_where('ea_services', array('id' => $service['id']))
|
||||
->result_array();
|
||||
|
||||
// Get data by using the model.
|
||||
$model_data = $this->ci->services_model->get_batch(array('id' => $service['id']));
|
||||
|
||||
// Check that the data arrays are the same.
|
||||
$this->ci->unit->run($no_model_data, $model_data, 'Test get_batch() with where clause.');
|
||||
|
||||
// Delete inserted record from database.
|
||||
$this->ci->db->delete('ea_services', array('id' => $service['id']));
|
||||
}
|
||||
|
||||
private function unabled_test_get_batch_with_invalid_where_clause() {
|
||||
// CodeIgniter auto raises an exception if the where section is invalid.
|
||||
}
|
||||
|
||||
// TEST GET ROW METHOD -----------------------------------------
|
||||
private function test_get_row() {
|
||||
// Insert a new service record.
|
||||
$service = array(
|
||||
'name' => 'General Examination',
|
||||
'duration' => 30,
|
||||
'price' => 50.00,
|
||||
'currency' => 'euro',
|
||||
'description' => 'This is some service description.'
|
||||
);
|
||||
|
||||
$this->ci->db->insert('ea_services', $service);
|
||||
$service['id'] = intval($this->ci->db->insert_id());
|
||||
|
||||
// Get the new service record from db.
|
||||
$no_model_data = $this->ci->db->get_where('ea_services', array('id' => $service['id']))
|
||||
->row_array();
|
||||
$model_data = $this->ci->services_model->get_row($service['id']);
|
||||
|
||||
// Check that the row is the correct one.
|
||||
$this->ci->unit->run($no_model_data, $model_data, 'Test get_row() method');
|
||||
|
||||
// Delete inserted service record.
|
||||
$this->ci->db->delete('ea_services', array('id' => $service['id']));
|
||||
}
|
||||
|
||||
private function test_get_row_that_does_not_exist() {
|
||||
$random_record_id = 486868412;
|
||||
$row_data = $this->ci->services_model->get_row($random_record_id);
|
||||
$this->ci->unit->run($row_data, NULL, 'Test get_row() with record id that does '
|
||||
. 'not exist in the database.');
|
||||
}
|
||||
|
||||
private function test_get_row_with_invalid_argument() {
|
||||
$invalid_id = 'THIS IS NOT AN INTEGER';
|
||||
|
||||
$has_thrown_exception = FALSE;
|
||||
try {
|
||||
$this->ci->services_model->get_row($invalid_id);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exception = TRUE;
|
||||
}
|
||||
|
||||
$this->ci->unit->run($has_thrown_exception, TRUE, 'Test get_row() with wrong argument.');
|
||||
}
|
||||
|
||||
// TEST GET VALUE METHOD -----------------------------------------
|
||||
private function test_get_value() {
|
||||
// Insert new service record.
|
||||
$service = array(
|
||||
'name' => 'General Examination',
|
||||
'duration' => 30,
|
||||
'price' => 50.00,
|
||||
'currency' => 'euro',
|
||||
'description' => 'This is some service description.'
|
||||
);
|
||||
$this->ci->db->insert('ea_services', $service);
|
||||
$service['id'] = intval($this->ci->db->insert_id());
|
||||
|
||||
// Get a specific value from the database.
|
||||
$model_value = $this->ci->services_model->get_value('name', $service['id']);
|
||||
|
||||
// Check if the value was correctly fetched from the database.
|
||||
$this->ci->unit->run($model_value, $service['name'], 'Test get_value() method.');
|
||||
|
||||
// Delete inserted appointment record.
|
||||
$this->ci->db->delete('ea_services', array('id' => $service['id']));
|
||||
}
|
||||
|
||||
private function test_get_value_record_does_not_exist() {
|
||||
$random_record_id = 843521368768;
|
||||
|
||||
$has_thrown_exception = FALSE;
|
||||
|
||||
try {
|
||||
$this->ci->services_model->get_value('name', $random_record_id);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exception = TRUE;
|
||||
}
|
||||
|
||||
$this->ci->unit->run($has_thrown_exception, TRUE, 'Test get_value() with record id that '
|
||||
. 'does not exist.');
|
||||
}
|
||||
|
||||
private function test_get_value_field_does_not_exist() {
|
||||
// Insert new service record.
|
||||
$service = array(
|
||||
'name' => 'General Examination',
|
||||
'duration' => 30,
|
||||
'price' => 50.00,
|
||||
'currency' => 'euro',
|
||||
'description' => 'This is some service description.'
|
||||
);
|
||||
$this->ci->db->insert('ea_services', $service);
|
||||
$service['id'] = intval($this->ci->db->insert_id());
|
||||
|
||||
// Try to get record value with wrong field name.
|
||||
$wrong_field_name = 'THIS IS WRONG';
|
||||
$has_thrown_exception = FALSE;
|
||||
|
||||
try {
|
||||
$this->ci->services_model->get_value($wrong_field_name, $service['id']);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exception = TRUE;
|
||||
}
|
||||
|
||||
$this->ci->unit->run($has_thrown_exception, TRUE, 'Test get_value() with record id that '
|
||||
. 'does not exist.');
|
||||
|
||||
// Delete inserted service record.
|
||||
$this->ci->db->delete('ea_services', array('id' => $service['id']));
|
||||
}
|
||||
|
||||
// TEST ADD SERVICE CATEGORY METHOD ---------------------------------------
|
||||
private function test_add_category_insert() {
|
||||
$category = array(
|
||||
'name' => 'Test Category',
|
||||
'description' => 'Test Description ...'
|
||||
);
|
||||
|
||||
$category['id'] = $this->ci->services_model->add_category($category);
|
||||
$this->ci->unit->run($category['id'], 'is_int', 'Test if add_category() - insert '
|
||||
. 'operation has returned a valid record id');
|
||||
|
||||
$db_record = $this->ci->db->get_where('ea_service_categories', array('id' => $category['id']))->row_array();
|
||||
$this->ci->unit->run($category, $db_record, 'Test if add_category() - insert '
|
||||
. 'operation has successfully inserted the record.');
|
||||
|
||||
$this->ci->db->delete('ea_service_categories', array('id' => $category['id']));
|
||||
}
|
||||
|
||||
private function test_add_category_update() {
|
||||
$category = array(
|
||||
'name' => 'Test Category',
|
||||
'description' => 'Test Description ...'
|
||||
);
|
||||
|
||||
$this->ci->db->insert('ea_service_categories', $category);
|
||||
$category['id'] = intval($this->ci->db->insert_id());
|
||||
|
||||
// update record
|
||||
$category['name'] = 'new name';
|
||||
$category['description'] = 'new description';
|
||||
|
||||
$update_result = $this->ci->services_model->add_category($category);
|
||||
$this->ci->unit->run($update_result, 'is_int', 'Check if add_category() - update '
|
||||
. 'operation has returned a valid record id.');
|
||||
|
||||
$db_record = $this->ci->db->get_where('ea_service_categories',
|
||||
array('id' => $category['id']))->row_array();
|
||||
$this->ci->unit->run($category, $db_record, 'Test if add_category() - update operation '
|
||||
. 'has successfully updated a record.');
|
||||
|
||||
$this->ci->db->delete('ea_service_categories', array('id' => $category['id']));
|
||||
}
|
||||
|
||||
private function test_add_category_invalid_data() {
|
||||
$category = array(
|
||||
'name' => '', // invalid
|
||||
'descrption' => ''
|
||||
);
|
||||
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->services_model->add_category($category);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if add_category() with invalid data '
|
||||
. 'has thrown an exception.');
|
||||
}
|
||||
|
||||
// TEST DELETE SERVICE CATEGORY METHOD -------------------------------------
|
||||
private function test_delete_category() {
|
||||
// insert record
|
||||
$category = array(
|
||||
'name' => 'Test Category',
|
||||
'description' => 'Test Description ...'
|
||||
);
|
||||
|
||||
$this->ci->db->insert('ea_service_categories', $category);
|
||||
$category['id'] = intval($this->ci->db->insert_id());
|
||||
|
||||
// delete record
|
||||
$delete_result = $this->ci->services_model->delete_category($category['id']);
|
||||
$this->ci->unit->run($delete_result, TRUE, 'Test if delete_category() method has '
|
||||
. 'returned a valid result.');
|
||||
|
||||
// check if record was actually deleted.
|
||||
$num_rows = $this->ci->db->get_where('ea_service_categories',
|
||||
array('id' => $category['id']))->num_rows();
|
||||
$this->ci->unit->run($num_rows, 0, 'Check if delete_category() has actually deleted '
|
||||
. 'the record.');
|
||||
|
||||
if ($num_rows > 0) {
|
||||
$this->ci->db->delete('ea_service_categories', array('id' => $category['id']));
|
||||
}
|
||||
}
|
||||
|
||||
private function test_delete_category_invalid_argument() {
|
||||
$invalid_arg = 'This is invalid';
|
||||
$has_thrown_exc = TRUE;
|
||||
try {
|
||||
$this->ci->services_model->delete_category($invalid_arg);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if delete_category() with invalid '
|
||||
. 'argument has thrown an exception.');
|
||||
}
|
||||
|
||||
private function test_delete_category_record_does_not_exist() {
|
||||
$random_id = 09182093;
|
||||
$has_thrown_exc = TRUE;
|
||||
try {
|
||||
$this->ci->services_model->delete_category($random_id);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if delete_category() with random id '
|
||||
. 'has thrown an exception.');
|
||||
}
|
||||
|
||||
// TEST GET SERVICE CATEGORY METHOD ---------------------------------------
|
||||
private function test_get_category() {
|
||||
// insert record
|
||||
$category = array(
|
||||
'name' => 'Test Category',
|
||||
'description' => 'Test Description ...'
|
||||
);
|
||||
|
||||
$this->ci->db->insert('ea_service_categories', $category);
|
||||
$category['id'] = intval($this->ci->db->insert_id());
|
||||
|
||||
// get record
|
||||
$db_record = $this->ci->services_model->get_category($category['id']);
|
||||
$this->ci->unit->run($db_record, $category, 'Test if get_category() has successfully '
|
||||
. 'returned the record.');
|
||||
|
||||
$this->ci->db->delete('ea_service_categories', array('id' => $category['id']));
|
||||
}
|
||||
|
||||
private function test_get_category_invalid_argument() {
|
||||
$invalid_arg = 'THIS IS INVALID';
|
||||
$has_thrown_exc = FALSE;
|
||||
try {
|
||||
$this->ci->services_model->get_category($invalid_arg);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if get_category() with invalid argument '
|
||||
. 'has thrown an exception.');
|
||||
}
|
||||
|
||||
private function test_get_category_record_does_not_exist() {
|
||||
$random_id = 123412343;
|
||||
$has_thrown_exc = TRUE;
|
||||
try {
|
||||
$this->ci->services_model->get_category($random_id);
|
||||
} catch(Exception $exc) {
|
||||
$has_thrown_exc = TRUE;
|
||||
}
|
||||
$this->ci->unit->run($has_thrown_exc, TRUE, 'Test if delete_category() with random id '
|
||||
. 'has thrown an exception.');
|
||||
}
|
||||
|
||||
// TEST GET ALL SERVICE CATEGORIES METHOD ----------------------------------
|
||||
private function test_get_all_categories() {
|
||||
$all_categories = $this->ci->services_model->get_all_categories();
|
||||
$db_categories = $this->ci->db->get('ea_service_categories')->result_array();
|
||||
$this->ci->unit->run($all_categories, $db_categories, 'Test if get_all_categories() method '
|
||||
. 'has successfully returned all the services categories.');
|
||||
}
|
||||
|
||||
// TEST VALIDATE SERVICE CATEGORY METHOD --------------------------------
|
||||
private function test_validate_category() {
|
||||
$category = array(
|
||||
'name' => 'Test Name',
|
||||
'description' => 'Test Description ...'
|
||||
);
|
||||
|
||||
$is_valid = $this->ci->services_model->validate_category($category);
|
||||
$this->ci->unit->run($is_valid, TRUE, 'Test if validate_category() has returned true '
|
||||
. 'with valid data.');
|
||||
}
|
||||
|
||||
private function test_validate_category_invalid_name() {
|
||||
$category = array(
|
||||
'name' => '', // invalid
|
||||
'description' => 'Test Description ...'
|
||||
);
|
||||
|
||||
$is_valid = $this->ci->services_model->validate_category($category);
|
||||
$this->ci->unit->run($is_valid, FALSE, 'Test if validate_category() has returned false '
|
||||
. 'with invalid data.');
|
||||
}
|
||||
}
|
||||
|
||||
/* End of file Unit_tests_services_model.php */
|
||||
/* Location: ./application/libraries/Unit_tests/drivers/Unit_tests_services_model.php */
|
|
@ -1,198 +0,0 @@
|
|||
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* Easy!Appointments - Open Source Web Scheduler
|
||||
*
|
||||
* @package EasyAppointments
|
||||
* @author A.Tselegidis <alextselegidis@gmail.com>
|
||||
* @copyright Copyright (c) 2013 - 2016, Alex Tselegidis
|
||||
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
|
||||
* @link http://easyappointments.org
|
||||
* @since v1.0.0
|
||||
* ---------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Settings Model Unit Tests
|
||||
*
|
||||
* @package Libraries
|
||||
* @subpackage Tests
|
||||
*/
|
||||
class Unit_tests_settings_model extends CI_Driver {
|
||||
private $CI;
|
||||
|
||||
/**
|
||||
* Class Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->CI =& get_instance();
|
||||
$this->CI->load->library('Unit_test');
|
||||
$this->CI->load->model('settings_model');
|
||||
}
|
||||
|
||||
/**
|
||||
* 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_settings_model');
|
||||
foreach ($class_methods as $method_name) {
|
||||
if (substr($method_name, 0, 5) === 'test_') {
|
||||
call_user_func(array($this, $method_name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// UNIT TESTS
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// TEST GET SETTING METHOD
|
||||
private function test_get_setting() {
|
||||
// Insert new setting to database.
|
||||
$setting = array(
|
||||
'name' => 'test_name',
|
||||
'value' => 'test_value'
|
||||
);
|
||||
$this->CI->db->insert('ea_settings', $setting);
|
||||
$setting['id'] = intval($this->CI->db->insert_id());
|
||||
|
||||
// Try to get the setting value by using the model.
|
||||
$model_value = $this->CI->settings_model->get_setting('test_name');
|
||||
$this->CI->unit->run($model_value, $setting['value'], 'Test get_setting() method.');
|
||||
|
||||
// Delete inserted setting.
|
||||
$this->CI->db->delete('ea_settings', array('id' => $setting['id']));
|
||||
}
|
||||
|
||||
private function test_get_setting_invalid_argument() {
|
||||
$invalid_argument = 564658765;
|
||||
$has_thrown_exception = FALSE;
|
||||
try {
|
||||
$this->CI->settings_model->get_setting($invalid_argument);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exception = TRUE;
|
||||
}
|
||||
$this->CI->unit->run($has_thrown_exception, TRUE, 'Test get_setting() with invalid '
|
||||
. 'method argument.');
|
||||
}
|
||||
|
||||
private function test_get_setting_that_does_not_exist() {
|
||||
$setting_name = 'THIS NAME DOES NOT EXIST IN DB';
|
||||
$has_thrown_exception = FALSE;
|
||||
try {
|
||||
$this->CI->settings_model->get_setting($setting_name);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exception = TRUE;
|
||||
}
|
||||
$this->CI->unit->run($has_thrown_exception, TRUE, 'Test get_setting() with a name that '
|
||||
. 'does not exist in database.');
|
||||
}
|
||||
|
||||
// TEST SET SETTING METHOD
|
||||
private function test_set_setting_insert() {
|
||||
$setting = array(
|
||||
'name' => 'test_setting',
|
||||
'value' => 'test_value'
|
||||
);
|
||||
|
||||
// Insert setting by using the model.
|
||||
$setting['id'] = $this->CI->settings_model->set_setting($setting['name'],
|
||||
$setting['value']);
|
||||
$this->CI->unit->run($setting['id'], 'is_int', 'Test that set_setting() method '
|
||||
. '(insert operation) has returned the setting database id.');
|
||||
|
||||
// Check that the setting has been successfully inserted.
|
||||
$db_data = $this->CI->db->get_where('ea_settings', array('id' => $setting['id']))
|
||||
->row_array();
|
||||
$this->CI->unit->run($setting, $db_data, 'Test set_setting() method has '
|
||||
. 'successfully inserted the setting into the database.');
|
||||
|
||||
// Delete inserted setting.
|
||||
$this->CI->db->delete('ea_settings', array('id' => $setting['id']));
|
||||
}
|
||||
|
||||
private function test_set_setting_update() {
|
||||
// Insert new setting into database.
|
||||
$setting = array(
|
||||
'name' => 'test_name',
|
||||
'value' => 'test_value'
|
||||
);
|
||||
$this->CI->db->insert('ea_settings', $setting);
|
||||
$setting['id'] = intval($this->CI->db->insert_id());
|
||||
|
||||
// Update the inserted setting.
|
||||
$new_setting_value = 'new_test_value';
|
||||
$set_setting_result = $this->CI->settings_model->set_setting($setting['name'],
|
||||
$new_setting_value);
|
||||
$this->CI->unit->run($set_setting_result, 'is_int', 'Test that set_setting() method '
|
||||
. '(update operation) has returned the setting database id.');
|
||||
|
||||
// Check if the update operation was completed successfully.
|
||||
$db_setting_value = $this->CI->db->get_where('ea_settings', array('id' => $setting['id']))
|
||||
->row()->value;
|
||||
$this->CI->unit->run($db_setting_value, $new_setting_value, 'Test set_setting() method '
|
||||
. 'has successfully updated a setting value.');
|
||||
|
||||
// Delete inserted record.
|
||||
$this->CI->db->delete('ea_settings', array('id' => $setting['id']));
|
||||
}
|
||||
|
||||
private function test_set_setting_invalid_setting_name() {
|
||||
$invalid_setting_name = 1219087912;
|
||||
$has_thrown_exception = FALSE;
|
||||
try {
|
||||
$this->CI->settings_model->set_setting($invalid_setting_name, 'test_value');
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exception = TRUE;
|
||||
}
|
||||
$this->CI->unit->run($has_thrown_exception, TRUE, 'Test set_setting() method with '
|
||||
. 'invalid argument.');
|
||||
}
|
||||
|
||||
// TEST REMOVE SETTING METHOD
|
||||
private function test_remove_setting() {
|
||||
// Insert new setting
|
||||
$setting = array(
|
||||
'name' => 'test_name',
|
||||
'value' => 'test_value'
|
||||
);
|
||||
$this->CI->db->insert('ea_settings', $setting);
|
||||
$setting['id'] = intval($this->CI->db->insert_id());
|
||||
|
||||
// Remove setting
|
||||
$remove_setting_result = $this->CI->settings_model->remove_setting($setting['name']);
|
||||
$this->CI->unit->run($remove_setting_result, TRUE, 'Test remove_setting() return value.');
|
||||
|
||||
// Check if setting is removed
|
||||
$num_rows = $this->CI->db->get_where('ea_settings', array('id' => $setting['id']))->num_rows();
|
||||
$this->CI->unit->run($num_rows, 0, 'Test if remove_setting() method has successfully '
|
||||
. 'removed the setting from the database.');
|
||||
if ($num_rows > 0) {
|
||||
$this->CI->db->delete('ea_settings', array('id' => $setting['id']));
|
||||
}
|
||||
}
|
||||
|
||||
private function test_remove_setting_record_does_not_exist() {
|
||||
$random_setting_name = 'THIS IS TOTALLY RANDOM';
|
||||
$remove_setting_result = $this->CI->settings_model->remove_setting($random_setting_name);
|
||||
$this->CI->unit->run($remove_setting_result, FALSE, 'Test remove_setting() with record '
|
||||
. 'that does not exist.');
|
||||
}
|
||||
|
||||
private function test_remove_setting_invalid_setting_name() {
|
||||
$invalid_setting_name = 12092130968;
|
||||
$has_thrown_exception = FALSE;
|
||||
try {
|
||||
$this->CI->settings_model->remove_setting($invalid_setting_name);
|
||||
} catch (Exception $exc) {
|
||||
$has_thrown_exception = TRUE;
|
||||
}
|
||||
$this->CI->unit->run($has_thrown_exception, TRUE, 'Test remove_setting() with invalid '
|
||||
. 'setting name.');
|
||||
}
|
||||
}
|
||||
|
||||
/* End of file Unit_tests_settings_model.php */
|
||||
/* Location: ./application/libraries/Unit_tests/drivers/Unit_tests_settings_model.php */
|
Loading…
Reference in a new issue