From 7e2baf30f912b537fb9cafcffcc5ffc7345bd99e Mon Sep 17 00:00:00 2001 From: "alextselegidis@gmail.com" Date: Wed, 15 May 2013 15:03:47 +0000 Subject: [PATCH] =?UTF-8?q?-=20=CE=94=CE=B9=CE=BF=CF=81=CE=B8=CF=8E=CF=83?= =?UTF-8?q?=CE=B5=CE=B9=CF=82=20=CF=83=CF=84=CE=B1=20model=20=CF=84=CE=B7?= =?UTF-8?q?=CF=82=20=CE=B5=CF=86=CE=B1=CF=81=CE=BC=CE=BF=CE=B3=CE=AE=CF=82?= =?UTF-8?q?=20-=20=CE=9F=CE=BB=CE=BF=CE=BA=CE=BB=CE=AE=CF=81=CF=89=CF=83?= =?UTF-8?q?=CE=B7=20=CF=84=CF=89=CE=BD=20unit=20tests=20=CF=84=CF=89=CE=BD?= =?UTF-8?q?=20models=20=CF=80=CE=BF=CF=85=20=CF=87=CF=81=CE=B7=CF=83=CE=B9?= =?UTF-8?q?=CE=BC=CE=BF=CF=80=CE=BF=CE=B9=CE=BF=CF=8D=CE=BD=CF=84=CE=B1?= =?UTF-8?q?=CE=B9=20=CF=83=CF=84=CE=B7=20=CF=80=CE=B5=CF=81=CE=AF=CF=80?= =?UTF-8?q?=CF=84=CF=89=CF=83=CE=B7=20=CF=87=CF=81=CE=AE=CF=83=CE=B7=CF=82?= =?UTF-8?q?=20'=CE=9A=CF=81=CE=AC=CF=84=CE=B7=CF=83=CE=B7=20=CE=A1=CE=B1?= =?UTF-8?q?=CE=BD=CF=84=CE=B5=CE=B2=CE=BF=CF=8D'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../libraries/Unit_tests/Unit_tests.php | 8 +- .../drivers/Unit_tests_appointments_model.php | 80 +-- .../drivers/Unit_tests_customers_model.php | 508 +++++++++++++++++- src/application/models/appointments_model.php | 46 +- src/application/models/customers_model.php | 195 +++++-- src/application/models/providers_model.php | 20 + src/application/models/services_model.php | 20 + src/application/models/settings_model.php | 59 +- 8 files changed, 830 insertions(+), 106 deletions(-) diff --git a/src/application/libraries/Unit_tests/Unit_tests.php b/src/application/libraries/Unit_tests/Unit_tests.php index e65180cd..bad102c3 100644 --- a/src/application/libraries/Unit_tests/Unit_tests.php +++ b/src/application/libraries/Unit_tests/Unit_tests.php @@ -20,7 +20,10 @@ class Unit_tests extends CI_Driver_Library { // the unit testing classes. $this->valid_drivers = array( 'Unit_tests_appointments_model', - 'Unit_tests_customers_model' + 'Unit_tests_customers_model', + 'Unit_tests_providers_model', + 'Unit_tests_services_model', + 'Unit_tests_settings_model' ); } @@ -49,6 +52,9 @@ class Unit_tests extends CI_Driver_Library { 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(); if ($output_report) { $this->CI->output->append_output($this->CI->unit->report()); diff --git a/src/application/libraries/Unit_tests/drivers/Unit_tests_appointments_model.php b/src/application/libraries/Unit_tests/drivers/Unit_tests_appointments_model.php index 0ba9a5a5..8a77abdc 100644 --- a/src/application/libraries/Unit_tests/drivers/Unit_tests_appointments_model.php +++ b/src/application/libraries/Unit_tests/drivers/Unit_tests_appointments_model.php @@ -125,14 +125,14 @@ class Unit_tests_appointments_model extends CI_Driver { 'id_services' => $this->service_id ); - $hasThrownException = FALSE; // This method must throw a validation exception. + $has_thrown_exception = FALSE; // This method must throw a validation exception. try { $this->CI->Appointments_Model->add($appointment_data); } catch(ValidationException $valExc) { - $hasThrownException = TRUE; + $has_thrown_exception = TRUE; } - $this->CI->unit->run($hasThrownException, TRUE, 'Test add() appointment with wrong date format.', 'A validation exception must be thrown.'); + $this->CI->unit->run($has_thrown_exception, TRUE, 'Test add() appointment with wrong date format.', 'A validation exception must be thrown.'); } /** @@ -140,7 +140,7 @@ class Unit_tests_appointments_model extends CI_Driver { * * Insert a new appointment and test if it exists. */ - private function test_appointment_exists() { + private function test_exists() { // Insert new appointment (this row will be checked later). $appointment_data = array( 'start_datetime' => '2013-05-01 12:30:00', @@ -160,7 +160,7 @@ class Unit_tests_appointments_model extends CI_Driver { $this->CI->db->delete('ea_appointments', array('id' => $appointment_data['id'])); } - private function test_appointment_does_not_exist() { + private function test_exists_record_does_not_exist() { // Create random appointmnet data that doesn't exist in the database. $appointment_data = array( 'start_datetime' => '2013-05-01 08:33:45', @@ -174,7 +174,7 @@ class Unit_tests_appointments_model extends CI_Driver { $this->CI->unit->run($this->CI->Appointments_Model->exists($appointment_data), FALSE, 'Test exists() method with an appointment that does not exist'); } - private function test_appointment_exists_wrong_data() { + private function test_exists_with_wrong_data() { // Create random appointmnet data that doesn't exist in the database. $appointment_data = array( 'start_datetime' => '2WRONG013-05-01 0WRONG8:33:45', @@ -232,15 +232,15 @@ class Unit_tests_appointments_model extends CI_Driver { ); // Load the appointments model and execute the find record id method. - $hasThrownException = FALSE; + $has_thrown_exception = FALSE; try { $this->CI->Appointments_Model->find_record_id($appointment_data); - } catch(DatabaseException $dbExc) { - $hasThrownException = TRUE; + } catch(DatabaseException $db_exc) { + $has_thrown_exception = TRUE; } - $this->CI->unit->run($hasThrownException, TRUE, 'Test find_record_id() with appointment ' + $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.'); } @@ -264,15 +264,15 @@ class Unit_tests_appointments_model extends CI_Driver { // Try to find the appointmet's record id. A database // exception should be raised. - $hasThrownException = FALSE; + $has_thrown_exception = FALSE; try { $this->CI->Appointments_Model->find_record_id($appointment_data); - } catch(DatabaseException $dbExc) { - $hasThrownException = TRUE; + } catch(DatabaseException $db_exc) { + $has_thrown_exception = TRUE; } - $this->CI->unit->run($hasThrownException, TRUE, 'Test find_record_id() with appointment ' + $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.'); } @@ -321,10 +321,15 @@ class Unit_tests_appointments_model extends CI_Driver { private function test_delete_record_wrong_parameter_given() { $wrong_record_id = 'not_an_integer'; - $delete_result = $this->CI->Appointments_Model->delete($wrong_record_id); - echo $delete_result; - $this->CI->unit->run($delete_result, FALSE, 'Test delete() method with a record id' - . ' that is not an integer'); + $has_thrown_exception = FALSE; + + try { + $this->CI->Appointments_Model->delete($wrong_record_id); + } catch (InvalidArgumentException $ia_exc) { + $has_thrown_exception = TRUE; + } + + $this->CI->unit->run($has_thrown_exception, TRUE, 'Test delete() method with argument that is not an integer.'); } /** @@ -381,15 +386,15 @@ class Unit_tests_appointments_model extends CI_Driver { * igniter handles itself wrong queries. */ private function unabled_test_get_batch_wrong_where_clause() { - $hasThrownException = FALSE; + $has_thrown_exception = FALSE; try { $this->CI->Appointments_Model->get_batch('WRONG QUERY HERE'); - } catch(DatabaseException $dbExc) { - $hasThrownException = TRUE; + } catch(DatabaseException $db_exc) { + $has_thrown_exception = TRUE; } - $this->CI->unit->run($hasThrownException, TRUE, 'Test get_batch() with wrong where clause.', + $this->CI->unit->run($has_thrown_exception, TRUE, 'Test get_batch() with wrong where clause.', 'A database excpetion is expected to be thrown.'); } @@ -436,17 +441,17 @@ class Unit_tests_appointments_model extends CI_Driver { * * A database exception is expected. */ - private function test_get_row_wrong_argument_given() { - $wrong_arguement_id = 'THIS IS NOT AN INTEGER'; + private function test_get_row_with_invalid_argument() { + $invalid_id = 'THIS IS NOT AN INTEGER'; - $hasThrownException = FALSE; + $has_thrown_exception = FALSE; try { - $this->CI->Appointments_Model->get_row($wrong_arguement_id); - } catch (DatabaseException $dbExc) { - $hasThrownException = TRUE; + $this->CI->Appointments_Model->get_row($invalid_id); + } catch (InvalidArgumentException $db_exc) { + $has_thrown_exception = TRUE; } - $this->CI->unit->run($hasThrownException, TRUE, 'Test get_row() with wrong arguement.'); + $this->CI->unit->run($has_thrown_exception, TRUE, 'Test get_row() with wrong argument.'); } /** @@ -466,7 +471,6 @@ class Unit_tests_appointments_model extends CI_Driver { $appointment_data['id'] = $this->CI->db->insert_id(); // Get a specific value from the database. - $db_value = $this->CI->Appointments_Model->get_value('start_datetime', $appointment_data['id']); // Check if the value was correctly fetched from the database. @@ -485,15 +489,15 @@ class Unit_tests_appointments_model extends CI_Driver { private function test_get_value_record_does_not_exist() { $random_record_id = 843521368768; - $hasThrownException = FALSE; + $has_thrown_exception = FALSE; try { $this->CI->Appointments_Model->get_value('start_datetime', $random_record_id); - } catch (DatabaseException $dbExc) { - $hasThrownException = TRUE; + } catch (InvalidArgumentException $db_exc) { + $has_thrown_exception = TRUE; } - $this->CI->unit->run($hasThrownException, TRUE, 'Test get_value() with record id that does not exist.'); + $this->CI->unit->run($has_thrown_exception, TRUE, 'Test get_value() with record id that does not exist.'); } /** @@ -517,15 +521,15 @@ class Unit_tests_appointments_model extends CI_Driver { // Try to get record value with wrong field name. $wrong_field_name = 'THIS IS WRONG'; - $hasThrownException = FALSE; + $has_thrown_exception = FALSE; try { $this->CI->Appointments_Model->get_value($wrong_field_name, $appointment_data['id']); - } catch (DatabaseException $dbExc) { - $hasThrownException = TRUE; + } catch (InvalidArgumentException $db_exc) { + $has_thrown_exception = TRUE; } - $this->CI->unit->run($hasThrownException, TRUE, 'Test get_value() with record id that does not exist.'); + $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_data['id'])); diff --git a/src/application/libraries/Unit_tests/drivers/Unit_tests_customers_model.php b/src/application/libraries/Unit_tests/drivers/Unit_tests_customers_model.php index dcb7a1d5..9019ea08 100644 --- a/src/application/libraries/Unit_tests/drivers/Unit_tests_customers_model.php +++ b/src/application/libraries/Unit_tests/drivers/Unit_tests_customers_model.php @@ -2,11 +2,19 @@ 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; } /** @@ -27,12 +35,508 @@ class Unit_tests_customers_model extends CI_Driver { ///////////////////////////////////////////////////////////////////////// // UNIT TESTS ///////////////////////////////////////////////////////////////////////// - private function test_true() { - $this->CI->unit->run(TRUE, TRUE, 'True!'); + + // TEST ADD() CUSTOMER METHOD + private function test_add_insert() { + // Insert new customer record. + $customer_data = array( + 'last_name' => 'Doe', + 'first_name' => 'John', + 'email' => 'alextselegidis@gmail.com', + 'phone_number' => '0123456789', + 'address' => 'Abbey Road 18', + 'city' => 'London', + 'zip_code' => '12345', + 'id_roles' => $this->customer_role_id + ); + $customer_data['id'] = $this->CI->Customers_Model->add($customer_data); + $this->CI->unit->run($customer_data['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_data['id']))->row_array(); + $are_the_same = TRUE; + if ($customer_data['last_name'] != $db_data['last_name'] + || $customer_data['first_name'] != $db_data['first_name'] + || $customer_data['email'] != $db_data['email'] + || $customer_data['phone_number'] != $db_data['phone_number'] + || $customer_data['address'] != $db_data['address'] + || $customer_data['city'] != $db_data['city'] + || $customer_data['zip_code'] != $db_data['zip_code'] + || $customer_data['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_data['id'])); } + private function test_add_update() { + // Insert new customer record (will be updated later). + $customer_data = array( + 'last_name' => 'Doe', + 'first_name' => 'John', + 'email' => 'alextselegidis@gmail.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_data); + $customer_data['id'] = intval($this->CI->db->insert_id()); + + // Update customer record. + $new_phone_number = 'THE PHONE NUMBER IS UPDATED'; + $customer_data['phone_number'] = $new_phone_number; + $update_result = $this->CI->Customers_Model->add($customer_data); + $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_data['id']))->row()->phone_number; + $this->CI->unit->run($customer_data['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_data['id'])); + } + private function test_add_invalid_email() { + // Prepare customer's data (email address is invalid). + $customer_data = 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_data); + } catch(ValidationException $valExc) { + $has_thrown_exception = TRUE; + } + + $this->CI->unit->run($has_thrown_exception, TRUE, 'Test add() customer with invalid email address'); + } + private function test_add_missing_no_last_name() { + // Prepare customer's data (last name field is missing). + $customer_data = array( + 'first_name' => 'John', + 'email' => 'alextselegidis@gmail.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_data); + } catch(ValidationException $valExc) { + $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_data = array( + 'last_name' => 'Doe', + 'first_name' => 'John', + 'email' => 'alextselegidis@gmail.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_data); + $customer_data['id'] = intval($this->CI->db->insert_id()); + + // Test that exists returns true. + $exists_result = $this->CI->Customers_Model->exists($customer_data); + $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_data['id'])); + } + + private function test_exists_record_does_not_exist() { + // Prepare customer's data with email that does not exist. + $customer_data = 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_data); + $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_data = 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_data); + } catch (InvalidArgumentException $ia_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_data = array( + 'last_name' => 'Doe', + 'first_name' => 'John', + 'email' => 'alextselegidis@gmail.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_data); + $customer_data['id'] = intval($this->CI->db->insert_id()); + + // Delete new customer record. + $delete_result = $this->CI->Customers_Model->delete($customer_data['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_data['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_data['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 (InvalidArgumentException $ia_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_data = array( + 'last_name' => 'Doe', + 'first_name' => 'John', + 'email' => 'alextselegidis@gmail.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_data($customer_data); + $this->CI->unit->run($validation_result, TRUE, 'Test validate_data() method.'); + } + + private function test_validate_data_no_last_name_provided() { + // Prepare customer's data (no last_name value provided). + $customer_data = array( + 'first_name' => 'John', + 'email' => 'alextselegidis@gmail.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_data($customer_data); + $this->CI->unit->run($validation_result, FALSE, 'Test validate_data() method without a last_name value.'); + } + + private function test_validate_data_invalid_email_address() { + // Prepare customer's data (invalid email address). + $customer_data = 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. + $validation_result = $this->CI->Customers_Model->validate_data($customer_data); + $this->CI->unit->run($validation_result, FALSE, 'Test validate_data() method with invalid email address.'); + } + + // TEST FIND RECORD ID METHOD + private function test_find_record_id() { + // Insert new customer to database. + $customer_data = array( + 'last_name' => 'Doe', + 'first_name' => 'John', + 'email' => 'alextselegidis@gmail.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_data); + $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_data); + $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_data = 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_data); + } catch (InvalidArgumentException $ia_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_data = 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_data); + } catch (DatabaseException $db_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_data = array( + 'last_name' => 'Doe', + 'first_name' => 'John', + 'email' => 'alextselegidis@gmail.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_data); + $customer_data['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_data['id']))->result_array(); + + // Get data by using the model. + $model_data = $this->CI->Customers_Model->get_batch(array('id' => $customer_data['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_data['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_data = array( + 'last_name' => 'Doe', + 'first_name' => 'John', + 'email' => 'alextselegidis@gmail.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_data); + $customer_data['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_data['id']))->row_array(); + $model_data = $this->CI->Customers_Model->get_row($customer_data['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_data['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 (InvalidArgumentException $ia_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_data = array( + 'last_name' => 'Doe', + 'first_name' => 'John', + 'email' => 'alextselegidis@gmail.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_data); + $customer_data['id'] = intval($this->CI->db->insert_id()); + + // Get a specific value from the database. + $model_value = $this->CI->Customers_Model->get_value('email', $customer_data['id']); + + // Check if the value was correctly fetched from the database. + $this->CI->unit->run($model_value, $customer_data['email'], 'Test get_value() method.'); + + // Delete inserted appointment record. + $this->CI->db->delete('ea_users', array('id' => $customer_data['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 (InvalidArgumentException $db_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_data = array( + 'last_name' => 'Doe', + 'first_name' => 'John', + 'email' => 'alextselegidis@gmail.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_data); + $customer_data['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_data['id']); + } catch (InvalidArgumentException $db_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_data['id'])); + } } /* End of file Unit_tests_customers_model.php */ diff --git a/src/application/models/appointments_model.php b/src/application/models/appointments_model.php index 988e8123..185cc5e7 100644 --- a/src/application/models/appointments_model.php +++ b/src/application/models/appointments_model.php @@ -41,23 +41,36 @@ class Appointments_Model extends CI_Model { /** * Check if a particular appointment record already exists. * - * This method checks wether the given appointment already exists in - * the database. This method does not search with the id, but with a - * combination of the appointments field values. + * This method checks wether the given appointment already exists + * in the database. It doesn't search with the id, but by using the + * following fields: "start_datetime", "end_datetime", "id_users_provider", + * "id_users_customer", "id_services". * - * @uses find_record_id() + * @expectedException InvalidArgumentException When the $appointment_data + * array does not contain the necessary field. * * @param array $appointment_data Associative array with the appointment's * data. Each key has the same name with the database fields. * @return bool Returns wether the record exists or not. */ public function exists($appointment_data) { - try { - $this->find_record_id($appointment_data); - return TRUE; - } catch(DatabaseException $dbExc) { - return FALSE; + if (!isset($appointment_data['start_datetime']) + || !isset($appointment_data['end_datetime']) + || !isset($appointment_data['id_users_provider']) + || !isset($appointment_data['id_users_customer']) + || !isset($appointment_data['id_services'])) { + throw new InvalidArgumentException('Not all appointment field values are provided : ' . print_r($appointment_data, TRUE)); } + + $num_rows = $this->db->get_where('ea_appointments', array( + 'start_datetime' => $appointment_data['start_datetime'], + 'end_datetime' => $appointment_data['end_datetime'], + 'id_users_provider' => $appointment_data['id_users_provider'], + 'id_users_customer' => $appointment_data['id_users_customer'], + 'id_services' => $appointment_data['id_services'], + ))->num_rows(); + + return ($num_rows > 0) ? TRUE : FALSE; } /** @@ -192,12 +205,15 @@ class Appointments_Model extends CI_Model { /** * Delete an existing appointment record from the database. * + * @expectedException InvalidArgumentException Raises when the $appointment_id + * is not an integer. + * * @param int $appointment_id The record id to be deleted. * @return bool Returns the delete operation result. */ public function delete($appointment_id) { if (!is_int($appointment_id)) { - return FALSE; // Invalid parameter given. + throw new InvalidArgumentException('Invalid argument type $appointment_id : ' . $appointment_id); } $num_rows = $this->db->get_where('ea_appointments', array('id' => $appointment_id))->num_rows(); @@ -220,7 +236,7 @@ class Appointments_Model extends CI_Model { */ public function get_row($appointment_id) { if (!is_int($appointment_id)) { - throw new DatabaseException('Invalid argument given. Expected integer for the $appointment_id.'); + throw new InvalidArgumentException('Invalid argument given. Expected integer for the $appointment_id : ' . $appointment_id); } return $this->db->get_where('ea_appointments', array('id' => $appointment_id))->row_array(); } @@ -235,21 +251,21 @@ class Appointments_Model extends CI_Model { */ public function get_value($field_name, $appointment_id) { if (!is_int($appointment_id)) { - throw new DatabaseException('Invalid argument given, expected integer for the $appointment_id.'); + throw new InvalidArgumentException('Invalid argument given, expected integer for the $appointment_id : ' . $appointment_id); } if (!is_string($field_name)) { - throw new DatabaseException('Invalid argument given, expected string for the $field_name.'); + throw new InvalidArgumentException('Invalid argument given, expected string for the $field_name : ' . $field_name); } if ($this->db->get_where('ea_appointments', array('id' => $appointment_id))->num_rows() == 0) { - throw new DatabaseException('The record with the provided id does not exist in the datbase.'); + throw new InvalidArgumentException('The record with the provided id does not exist in the database : ' . $appointment_id); } $row_data = $this->db->get_where('ea_appointments', array('id' => $appointment_id))->row_array(); if (!isset($row_data[$field_name])) { - throw new DatabaseException('The given field name does not exist in the database.'); + throw new InvalidArgumentException('The given field name does not exist in the database : ' . $field_name); } return $row_data[$field_name]; diff --git a/src/application/models/customers_model.php b/src/application/models/customers_model.php index 88de6d03..d89c0be0 100644 --- a/src/application/models/customers_model.php +++ b/src/application/models/customers_model.php @@ -15,47 +15,57 @@ class Customers_Model extends CI_Model { * doesn't exists it is going to be inserted, otherwise the * record is going to be updated. * + * @expectedException ValidationException When customer's data are invalid. + * @expectedException DatabaseException When update or insert method fails. + * * @param array $customer_data Associative array with the customer's * data. Each key has the same name with the database fields. * @return int Returns the customer id. */ public function add($customer_data) { - try { - $customer_id = $this->exists($customer_data['email']); - - if (!$customer_id) { - $customer_id = $this->insert($customer_data); - } else { - $customer_data['id'] = $customer_id; - $this->update($customer_data); - } - - return $customer_id; - - } catch(Exception $exc) { - // Send some info for the exception back to the browser. - echo $exc->getTraceAsString(); + // Validate the appointment data before doing anything. + if (!$this->validate_data($customer_data)) { + throw new ValidationException('Customer data are not valid.'); } + + if (!$this->exists($customer_data)) { + $customer_data['id'] = $this->insert($customer_data); + } else { + $customer_data['id'] = $this->update($customer_data); + } + + return $customer_data['id']; } /** * Check if a particular customer record already exists. * * This method checks wether the given customer already exists in - * the database. This method does not search with the id, but witha - * the email value of the customer. + * the database. It doesn't search with the id, but with the following + * fields: "email" * - * @param string $customer_email The email field value is used to - * distinguish customer records. - * @return int|bool Returns the record id or FALSE if it doesn't exist. + * @expectedException InvalidArgumentException When the $customer_data + * array does not contain all the necessary fields. + * + * @param array $customer_data Associative array with the customer's + * data. Each key has the same name with the database fields. + * @return bool Returns wether the record exists or not. */ - public function exists($customer_email) { - $this->db->where(array( - 'email' => $customer_email, - 'id_roles' => $this->get_customers_role_id() - )); - $result = $this->db->get('ea_users'); - return ($result->num_rows() > 0) ? $result->row()->id : FALSE; + public function exists($customer_data) { + if (!isset($customer_data['email'])) { + throw new InvalidArgumentException('Customer\'s email is not provided.'); + } + + // This method shouldn't depend on another method of this class. + $num_rows = $this->db + ->select('*') + ->from('ea_users') + ->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner') + ->where('ea_users.email', $customer_data['email']) + ->where('ea_roles.slug', DB_SLUG_CUSTOMER) + ->get()->num_rows(); + + return ($num_rows > 0) ? TRUE : FALSE; } /** @@ -68,22 +78,19 @@ class Customers_Model extends CI_Model { private function insert($customer_data) { // Before inserting the customer we need to get the customer's role id // from the database and assign it to the new record as a foreign key. - $this->db - ->select('id') - ->from('ea_roles') - ->where('slug', DB_SLUG_CUSTOMER); + $customer_role_id = $this->db + ->select('id') + ->from('ea_roles') + ->where('slug', DB_SLUG_CUSTOMER) + ->get()->row()->id; - $customer_role_id = $this->db->get()->row()->id; - - if ($customer_role_id !== NULL) { - $customer_data['id_roles'] = $customer_role_id; - } + $customer_data['id_roles'] = $customer_role_id; if (!$this->db->insert('ea_users', $customer_data)) { - throw new Exception('Could not insert customer to the database.'); + throw new DatabaseException('Could not insert customer to the database.'); } - return $this->db->insert_id(); + return intval($this->db->insert_id()); } /** @@ -97,20 +104,104 @@ class Customers_Model extends CI_Model { * @return int Returns the updated record id. */ private function update($customer_data) { + if (!isset($customer_data['id'])) { + $customer_data['id'] = $this->find_record_id($customer_data); + } + $this->db->where('id', $customer_data['id']); if (!$this->db->update('ea_users', $customer_data)) { - throw new Exception('Could not update customer to the database.'); + throw new DatabaseException('Could not update customer to the database.'); + } + + return intval($customer_data['id']); + } + + /** + * Find the database id of a customer record. + * + * The customer data should include the following fields in order to + * get the unique id from the database: "email" + * + * IMPORTANT! The record must already exists in the + * database, otherwise an exception is raised. + * + * @expectedException DatabaseException When the record is not found. + * + * @param array $customer_data Array with the customer data. The + * keys of the array should have the same names as the db fields. + * @return int Returns the id. + */ + public function find_record_id($customer_data) { + if (!isset($customer_data['email'])) { + throw new InvalidArgumentException('Customer\'s email was not provided : ' + . print_r($customer_data, TRUE)); + } + + // Get customer's role id + $result = $this->db + ->select('ea_users.id') + ->from('ea_users') + ->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner') + ->where('ea_users.email', $customer_data['email']) + ->where('ea_roles.slug', DB_SLUG_CUSTOMER) + ->get(); + + if ($result->num_rows() == 0) { + throw new DatabaseException('Could not find appointment record id.'); + } + + return $result->row()->id; + } + + /** + * Validate customer data before the insert or + * update operation is executed. + * + * @param array $customer_data Contains the customer data. + * @return bool Returns the validation result. + */ + public function validate_data($customer_data) { + $this->load->helper('data_validation'); + + try { + // Validate required fields + if (!isset($customer_data['last_name']) + || !isset($customer_data['email']) + || !isset($customer_data['phone_number'])) { + throw new Exception('Not all required fields are provided : ' + . print_r($customer_data, TRUE)); + } + + // Validate email address + if (!filter_var($customer_data['email'], FILTER_VALIDATE_EMAIL)) { + throw new Exception('Invalid email address provided : ' . $customer_data['email']); + } + + return TRUE; + } catch (Exception $exc) { + return FALSE; } - return $this->db->get_where('ea_users', array('email' => $customer_data['email']))->row()->id; } /** * Delete an existing customer record from the database. * + * @expectedException InvalidArgumentException Raises when + * the $customer_id is not an integer. + * * @param int $customer_id The record id to be deleted. * @return bool Returns the delete operation result. */ public function delete($customer_id) { + if (!is_int($customer_id)) { + throw new InvalidArgumentException('Invalid argument type $customer_id : ' . $customer_id); + } + + $num_rows = $this->db->get_where('ea_users', array('id' => $customer_id))->num_rows(); + if ($num_rows == 0) { + return FALSE; + } + $this->db->where('id', $customer_id); return $this->db->delete('ea_users'); } @@ -124,6 +215,9 @@ class Customers_Model extends CI_Model { * field names. */ public function get_row($customer_id) { + if (!is_int($customer_id)) { + throw new InvalidArgumentException('Invalid argument provided as $customer_id : ' . $customer_id); + } return $this->db->get_where('ea_users', array('id' => $customer_id))->row_array(); } @@ -136,6 +230,23 @@ class Customers_Model extends CI_Model { * @return string Returns the records value from the database. */ public function get_value($field_name, $customer_id) { + if (!is_int($customer_id)) { + throw new InvalidArgumentException('Invalid argument provided as $customer_id : ' . $customer_id); + } + + if (!is_string($field_name)) { + throw new InvalidArgumentException('$field_name argument is not a string : ' . $field_name); + } + + if ($this->db->get_where('ea_users', array('id' => $customer_id))->num_rows() == 0) { + throw new InvalidArgumentException('The record with the $customer_id argument does not exist in the database : ' . $customer_id); + } + + $row_data = $this->db->get_where('ea_users', array('id' => $customer_id))->row_array(); + if (!isset($row_data[$field_name])) { + throw new InvalidArgumentException('The given $field_name argument does not exist in the database : ' . $field_name); + } + return $this->db->get_where('ea_users', array('id' => $customer_id))->row_array()[$field_name]; } @@ -148,10 +259,10 @@ class Customers_Model extends CI_Model { * the query to be executed. DO NOT INCLUDE 'WHERE' KEYWORD. * @return array Returns the rows from the database. */ - public function get_batch($where_clause = NULL) { + public function get_batch($where_clause = '') { $customers_role_id = $this->get_customers_role_id(); - if ($where_clause != NULL) { + if ($where_clause != '') { $this->db->where($where_clause); } diff --git a/src/application/models/providers_model.php b/src/application/models/providers_model.php index 7b44f7e3..06d2f0bf 100644 --- a/src/application/models/providers_model.php +++ b/src/application/models/providers_model.php @@ -17,6 +17,9 @@ class Providers_Model extends CI_Model { * field names. */ public function get_row($provider_id) { + if (!is_int($provider_id)) { + throw new InvalidArgumentException('$provider_id argument is not an integer : ' . $provider_id); + } return $this->db->get_where('ea_users', array('id' => $provider_id))->row_array(); } @@ -29,6 +32,23 @@ class Providers_Model extends CI_Model { * @return string Returns the records value from the database. */ public function get_value($field_name, $provider_id) { + if (!is_int($provider_id)) { + throw new InvalidArgumentException('Invalid argument provided as $customer_id : ' . $provider_id); + } + + if (!is_string($field_name)) { + throw new InvalidArgumentException('$field_name argument is not a string : ' . $field_name); + } + + if ($this->db->get_where('ea_users', array('id' => $provider_id))->num_rows() == 0) { + throw new InvalidArgumentException('The record with the $provider_id argument does not exist in the database : ' . $provider_id); + } + + $row_data = $this->db->get_where('ea_users', array('id' => $provider_id))->row_array(); + if (!isset($row_data[$field_name])) { + throw new InvalidArgumentException('The given $field_name argument does not exist in the database : ' . $field_name); + } + return $this->db->get_where('ea_users', array('id' => $provider_id))->row_array()[$field_name]; } diff --git a/src/application/models/services_model.php b/src/application/models/services_model.php index 93cc9744..51786b91 100644 --- a/src/application/models/services_model.php +++ b/src/application/models/services_model.php @@ -16,6 +16,9 @@ class Services_Model extends CI_Model { * field names. */ public function get_row($service_id) { + if (!is_int($service_id)) { + throw new InvalidArgumentException('$service_id argument is not an integer : ' . $service_id); + } return $this->db->get_where('ea_services', array('id' => $service_id))->row_array(); } @@ -28,6 +31,23 @@ class Services_Model extends CI_Model { * @return string Returns the records value from the database. */ public function get_value($field_name, $service_id) { + if (!is_int($service_id)) { + throw new InvalidArgumentException('Invalid argument provided as $service_id : ' . $service_id); + } + + if (!is_string($field_name)) { + throw new InvalidArgumentException('$field_name argument is not a string : ' . $field_name); + } + + if ($this->db->get_where('ea_services', array('id' => $service_id))->num_rows() == 0) { + throw new InvalidArgumentException('The record with the $service_id argument does not exist in the database : ' . $service_id); + } + + $row_data = $this->db->get_where('ea_services', array('id' => $service_id))->row_array(); + if (!isset($row_data[$field_name])) { + throw new InvalidArgumentException('The given $field_name argument does not exist in the database : ' . $field_name); + } + return $this->db->get_where('ea_services', array('id' => $service_id))->row_array()[$field_name]; } diff --git a/src/application/models/settings_model.php b/src/application/models/settings_model.php index 79de47ec..c6706bb5 100644 --- a/src/application/models/settings_model.php +++ b/src/application/models/settings_model.php @@ -13,11 +13,22 @@ class Settings_Model extends CI_Model { * This method returns a system setting from the * database. * + * @expectedException InvalidArgumentException Raises whenever the + * $name argument is not a string, or does not exist in the database. + * * @param string $name The database setting name. * @return string Returns the database value for * the selected setting. */ function get_setting($name) { + if (!is_string($name)) { // Check argument type. + throw new InvalidArgumentException('$name argument is not a string : ' . $name); + } + + if ($this->db->get_where('ea_settings', array('name' => $name))->num_rows() == 0) { // Check if setting exists in db. + throw new InvalidArgumentException('$name setting does not exist in database : ' . $name); + } + $query = $this->db->get_where('ea_settings', array('name' => $name)); $setting = ($query->num_rows() > 0) ? $query->row() : ''; return $setting->value; @@ -28,31 +39,63 @@ class Settings_Model extends CI_Model { * on the database. If the setting doesn't exist, it * is going to be created, otherwise updated. * + * @expectedException DatabaseException Raises whenever an error + * occures during the insert or the update operation. + * @expectedException InvalidArgumentException Raises whenever + * the $name argument is not a string. + * * @param string $name The setting name. * @param type $value The setting value. - * @return bool Returns the operation success - failure - * result. + * @return int Returns the setting database id. */ function set_setting($name, $value) { + if (!is_string($name)) { + throw new InvalidArgumentException('$name argument is not a string : ' . $name); + } + $query = $this->db->get_where('ea_settings', array('name' => $name)); if ($query->num_rows() > 0) { // Update setting - $update_data = array('value' => $value); - $this->db->where('name', $name); - $result = $this->db->update('ea_settings', $update_data); + if (!$this->db->update('ea_settings', array('value' => $value), array('name' => $name))) { + throw new DatabaseException('Could not update database setting.'); + } + $setting_id = intval($this->db->get_where('ea_settings', array('name' => $name))->row()->id); } else { // Insert setting $insert_data = array( 'name' => $name, 'value' => $value ); - $result = $this->db->insert('ea_settings', $insert_data); + if (!$this->db->insert('ea_settings', $insert_data)) { + throw new DatabaseException('Could not insert database setting'); + } + $setting_id = intval($this->db->insert_id()); } - return $result; + return $setting_id; + } + + /** + * Remove a setting from the database. + * + * @expectedException InvalidArgumentException Raises whenever + * the $name parameter is not a string. + * + * @param string $name The setting name to be removed. + * @return bool Returns the delete operation result. + */ + function remove_setting($name) { + if (!is_string($name)) { + throw new InvalidArgumentException('$name is not a string : ' . $name); + } + + if ($this->db->get_where('ea_settings', array('name' => $name))->num_rows() == 0) { + return FALSE; // There is no such setting. + } + + return $this->db->delete('ea_settings', array('name' => $name)); } } - /* End of file settings_model.php */ /* Location: ./application/models/settings_model.php */ \ No newline at end of file