1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2
3 class Unit_tests_customers_model extends CI_Driver {
4 private $CI;
5 private $customer_role_id;
6
7 8 9
10 public function __construct() {
11
12 $this->CI =& get_instance();
13 $this->CI->load->library('Unit_test');
14 $this->CI->load->model('customers_model');
15
16
17 $this->customer_role_id = $this->CI->db->get_where('ea_roles',
18 array('slug' => DB_SLUG_CUSTOMER))->row()->id;
19 }
20
21 22 23
24 public function run_all() {
25
26
27
28 $class_methods = get_class_methods('Unit_tests_customers_model');
29 foreach ($class_methods as $method_name) {
30 if (substr($method_name, 0, 5) === 'test_') {
31 call_user_func(array($this, $method_name));
32 }
33 }
34 }
35
36
37
38
39
40
41 private function test_add_insert() {
42
43 $customer = array(
44 'last_name' => 'Doe',
45 'first_name' => 'John',
46 'email' => 'test@test.com',
47 'phone_number' => '0123456789',
48 'address' => 'Abbey Road 18',
49 'city' => 'London',
50 'zip_code' => '12345',
51 'id_roles' => $this->customer_role_id
52 );
53 $customer['id'] = $this->CI->customers_model->add($customer);
54 $this->CI->unit->run($customer['id'], 'is_int', 'Test add() customer (insert operation) '
55 . 'has returned the new row id.');
56
57
58 $db_data = $this->CI->db->get_where('ea_users', array('id' => $customer['id']))->row_array();
59 $are_the_same = TRUE;
60 if ($customer['last_name'] != $db_data['last_name']
61 || $customer['first_name'] != $db_data['first_name']
62 || $customer['email'] != $db_data['email']
63 || $customer['phone_number'] != $db_data['phone_number']
64 || $customer['address'] != $db_data['address']
65 || $customer['city'] != $db_data['city']
66 || $customer['zip_code'] != $db_data['zip_code']
67 || $customer['id_roles'] != $db_data['id_roles']) {
68 $are_the_same = FALSE;
69 }
70 $this->CI->unit->run($are_the_same, TRUE, 'Test add() customer (insert operation) has '
71 . 'successfully been added to the datbase.');
72
73
74 $this->CI->db->delete('ea_users', array('id' => $customer['id']));
75 }
76
77 private function test_add_update() {
78
79 $customer = array(
80 'last_name' => 'Doe',
81 'first_name' => 'John',
82 'email' => 'alextselegidis@gmail.com',
83 'phone_number' => '0123456789',
84 'address' => 'Abbey Road 18',
85 'city' => 'London',
86 'zip_code' => '12345',
87 'id_roles' => $this->customer_role_id
88 );
89 $this->CI->db->insert('ea_users', $customer);
90 $customer['id'] = intval($this->CI->db->insert_id());
91
92
93 $new_phone_number = 'THE PHONE NUMBER IS UPDATED';
94 $customer['phone_number'] = $new_phone_number;
95 $update_result = $this->CI->customers_model->add($customer);
96 $this->CI->unit->run($update_result, 'is_int', 'Test add() customer (update operation) '
97 . 'has returned the row id.');
98
99
100 $db_phone_number = $this->CI->db->get_where('ea_users', array('id' => $customer['id']))
101 ->row()->phone_number;
102 $this->CI->unit->run($customer['phone_number'], $db_phone_number, 'Test add() customer '
103 . '(update operation) has successfully updated the phone number field.');
104
105
106 $this->CI->db->delete('ea_users', array('id' => $customer['id']));
107 }
108
109 private function test_add_invalid_email() {
110
111 $customer = array(
112 'last_name' => 'Doe',
113 'first_name' => 'John',
114 'email' => 'THIS IS INVALID',
115 'phone_number' => '0123456789',
116 'address' => 'Abbey Road 18',
117 'city' => 'London',
118 'zip_code' => '12345',
119 'id_roles' => $this->customer_role_id
120 );
121
122 $has_thrown_exception = FALSE;
123 try {
124 $this->CI->customers_model->add($customer);
125 } catch(Exception $exc) {
126 $has_thrown_exception = TRUE;
127 }
128
129 $this->CI->unit->run($has_thrown_exception, TRUE, 'Test add() customer with invalid '
130 . 'email address.');
131 }
132
133 private function test_add_missing_last_name() {
134
135 $customer = array(
136 'first_name' => 'John',
137 'email' => 'alextselegidis@gmail.com',
138 'phone_number' => '0123456789',
139 'address' => 'Abbey Road 18',
140 'city' => 'London',
141 'zip_code' => '12345',
142 'id_roles' => $this->customer_role_id
143 );
144
145 $has_thrown_exception = FALSE;
146 try {
147 $this->CI->customers_model->add($customer);
148 } catch(Exception $exc) {
149 $has_thrown_exception = TRUE;
150 }
151
152 $this->CI->unit->run($has_thrown_exception, TRUE, 'Test add() customer with no last '
153 . 'name value provided.');
154 }
155
156
157 private function test_exists() {
158
159 $customer = array(
160 'last_name' => 'Doe',
161 'first_name' => 'John',
162 'email' => 'alextselegidis@gmail.com',
163 'phone_number' => '0123456789',
164 'address' => 'Abbey Road 18',
165 'city' => 'London',
166 'zip_code' => '12345',
167 'id_roles' => $this->customer_role_id
168 );
169 $this->CI->db->insert('ea_users', $customer);
170 $customer['id'] = intval($this->CI->db->insert_id());
171
172
173 $exists_result = $this->CI->customers_model->exists($customer);
174 $this->CI->unit->run($exists_result, TRUE, 'Tests exists() with customer that exists.');
175
176
177 $this->CI->db->delete('ea_users', array('id' => $customer['id']));
178 }
179
180 private function test_exists_record_does_not_exist() {
181
182 $customer = array(
183 'last_name' => 'Doe',
184 'first_name' => 'John',
185 'email' => 'THIS DOES NOT EXIST',
186 'phone_number' => '0123456789',
187 'address' => 'Abbey Road 18',
188 'city' => 'London',
189 'zip_code' => '12345',
190 'id_roles' => $this->customer_role_id
191 );
192
193
194 $exists_result = $this->CI->customers_model->exists($customer);
195 $this->CI->unit->run($exists_result, FALSE, 'Test exists() method with customer data '
196 . 'that does not exist in the database.');
197 }
198
199 private function test_exists_no_email_provided() {
200
201 $customer = array(
202 'last_name' => 'Doe',
203 'first_name' => 'John',
204 'phone_number' => '0123456789',
205 'address' => 'Abbey Road 18',
206 'city' => 'London',
207 'zip_code' => '12345',
208 'id_roles' => $this->customer_role_id
209 );
210
211
212 $has_thrown_exception = FALSE;
213 try {
214 $this->CI->customers_model->exists($customer);
215 } catch(Exception $exc) {
216 $has_thrown_exception = TRUE;
217 }
218
219 $this->CI->unit->run($has_thrown_exception, TRUE, 'Test exists() method with email '
220 . 'argument missing.');
221 }
222
223
224 private function test_delete() {
225
226 $customer = array(
227 'last_name' => 'Doe',
228 'first_name' => 'John',
229 'email' => 'alextselegidis@gmail.com',
230 'phone_number' => '0123456789',
231 'address' => 'Abbey Road 18',
232 'city' => 'London',
233 'zip_code' => '12345',
234 'id_roles' => $this->customer_role_id
235 );
236 $this->CI->db->insert('ea_users', $customer);
237 $customer['id'] = intval($this->CI->db->insert_id());
238
239
240 $delete_result = $this->CI->customers_model->delete($customer['id']);
241 $this->CI->unit->run($delete_result, TRUE, 'Test delete() method returned TRUE.');
242
243
244 $num_rows = $this->CI->db->get_where('ea_users', array('id' => $customer['id']))->num_rows();
245 $this->CI->unit->run($num_rows, 0, 'Test delete() method has actually deleted the '
246 . 'record from the db.');
247
248 if ($num_rows > 0) {
249 $this->CI->db->delete('ea_users', array('id' => $customer['id']));
250 }
251 }
252
253 private function test_delete_record_that_does_not_exist() {
254 $random_record_id = 879653245;
255
256 $delete_result = $this->CI->customers_model->delete($random_record_id);
257 $this->CI->unit->run($delete_result, FALSE, 'Test delete() method with customer id '
258 . 'that does not exist.');
259 }
260
261 private function test_delete_record_with_invalid_argument() {
262 $invalid_argument = 'THIS IS INVALID';
263 $has_thrown_exception = FALSE;
264 try {
265 $this->CI->customers_model->delete($invalid_argument);
266 } catch(Exception $exc) {
267 $has_thrown_exception = TRUE;
268 }
269 $this->CI->unit->run($has_thrown_exception, TRUE, 'Test delete() method with invalid '
270 . 'argument (not integer).');
271 }
272
273
274 private function test_validate_data() {
275
276 $customer = array(
277 'last_name' => 'Doe',
278 'first_name' => 'John',
279 'email' => 'alextselegidis@gmail.com',
280 'phone_number' => '0123456789',
281 'address' => 'Abbey Road 18',
282 'city' => 'London',
283 'zip_code' => '12345',
284 'id_roles' => $this->customer_role_id
285 );
286
287
288 $validation_result = $this->CI->customers_model->validate($customer);
289 $this->CI->unit->run($validation_result, TRUE, 'Test validate() method.');
290 }
291
292 private function test_validate_data_no_last_name_provided() {
293
294 $customer = array(
295 'first_name' => 'John',
296 'email' => 'alextselegidis@gmail.com',
297 'phone_number' => '0123456789',
298 'address' => 'Abbey Road 18',
299 'city' => 'London',
300 'zip_code' => '12345',
301 'id_roles' => $this->customer_role_id
302 );
303
304
305 $has_thrown_exc = FALSE;
306 try {
307 $this->CI->customers_model->validate($customer);
308 } catch (Exception $exc) {
309 $has_thrown_exc = TRUE;
310 }
311
312 $this->CI->unit->run($has_thrown_exc, TRUE, 'Test if validate() method without a '
313 . 'last_name value has thrown exception.');
314 }
315
316 private function test_validate_data_invalid_email_address() {
317
318 $customer = array(
319 'last_name' => 'Doe',
320 'first_name' => 'John',
321 'email' => 'THIS IS INVALID',
322 'phone_number' => '0123456789',
323 'address' => 'Abbey Road 18',
324 'city' => 'London',
325 'zip_code' => '12345',
326 'id_roles' => $this->customer_role_id
327 );
328
329
330 $has_thrown_exc = FALSE;
331 try {
332 $this->CI->customers_model->validate($customer);
333 } catch (Exception $exc) {
334 $has_thrown_exc = TRUE;
335 }
336
337 $this->CI->unit->run($has_thrown_exc, TRUE, 'Test if validate() method with invalid '
338 . 'email address has thrown exception.');
339 }
340
341
342 private function test_find_record_id() {
343
344 $customer = array(
345 'last_name' => 'Doe',
346 'first_name' => 'John',
347 'email' => 'alextselegidis@gmail.com',
348 'phone_number' => '0123456789',
349 'address' => 'Abbey Road 18',
350 'city' => 'London',
351 'zip_code' => '12345',
352 'id_roles' => $this->customer_role_id
353 );
354 $this->CI->db->insert('ea_users', $customer);
355 $inserted_id = intval($this->CI->db->insert_id());
356
357
358 $method_id = $this->CI->customers_model->find_record_id($customer);
359 $this->CI->unit->run($inserted_id, $method_id, 'Test find_record_id() method.');
360
361
362 $this->CI->db->delete('ea_users', array('id' => $inserted_id));
363 }
364
365 private function test_find_record_id_without_email_address() {
366
367 $customer = array(
368 'last_name' => 'Doe',
369 'first_name' => 'John',
370 'phone_number' => '0123456789',
371 'address' => 'Abbey Road 18',
372 'city' => 'London',
373 'zip_code' => '12345',
374 'id_roles' => $this->customer_role_id
375 );
376 $has_thrown_exception = FALSE;
377 try {
378 $this->CI->customers_model->find_record_id($customer);
379 } catch(Exception $exc) {
380 $has_thrown_exception = TRUE;
381 }
382
383 $this->CI->unit->run($has_thrown_exception, TRUE, 'Test find_record_id() without providing an email address.');
384 }
385
386 private function test_find_record_id_record_does_not_exist() {
387
388 $customer = array(
389 'last_name' => 'Doe',
390 'first_name' => 'John',
391 'email' => 'THIS EMAIL DOES NOT EXIST IN DB',
392 'phone_number' => '0123456789',
393 'address' => 'Abbey Road 18',
394 'city' => 'London',
395 'zip_code' => '12345',
396 'id_roles' => $this->customer_role_id
397 );
398 $has_thrown_exception = FALSE;
399 try {
400 $this->CI->customers_model->find_record_id($customer);
401 } catch(Exception $exc) {
402 $has_thrown_exception = TRUE;
403 }
404
405 $this->CI->unit->run($has_thrown_exception, TRUE, 'Test find_record_id() with email '
406 . 'address that does not exist in db.');
407 }
408
409
410 private function test_get_batch() {
411
412 $db_data = $this->CI->db->get_where('ea_users',
413 array('id_roles' => $this->customer_role_id))->result_array();
414
415 $model_data = $this->CI->customers_model->get_batch();
416
417 $this->CI->unit->run($db_data, $model_data, 'Test get_batch() method.');
418 }
419
420 private function test_get_batch_with_where_clause() {
421
422 $customer = array(
423 'last_name' => 'Doe',
424 'first_name' => 'John',
425 'email' => 'alextselegidis@gmail.com',
426 'phone_number' => '0123456789',
427 'address' => 'Abbey Road 18',
428 'city' => 'London',
429 'zip_code' => '12345',
430 'id_roles' => $this->customer_role_id
431 );
432 $this->CI->db->insert('ea_users', $customer);
433 $customer['id'] = intval($this->CI->db->insert_id());
434
435
436 $no_model_data = $this->CI->db->get_where('ea_users', array('id' => $customer['id']))
437 ->result_array();
438
439
440 $model_data = $this->CI->customers_model->get_batch(array('id' => $customer['id']));
441
442
443 $this->CI->unit->run($no_model_data, $model_data, 'Test get_batch() with where clause.');
444
445
446 $this->CI->db->delete('ea_users', array('id' => $customer['id']));
447 }
448
449 private function unabled_test_get_batch_with_invalid_where_clause() {
450
451 }
452
453
454 private function test_get_row() {
455
456 $customer = array(
457 'last_name' => 'Doe',
458 'first_name' => 'John',
459 'email' => 'alextselegidis@gmail.com',
460 'phone_number' => '0123456789',
461 'address' => 'Abbey Road 18',
462 'city' => 'London',
463 'zip_code' => '12345',
464 'id_roles' => $this->customer_role_id
465 );
466 $this->CI->db->insert('ea_users', $customer);
467 $customer['id'] = intval($this->CI->db->insert_id());
468
469
470 $no_model_data = $this->CI->db->get_where('ea_users', array('id' => $customer['id']))->row_array();
471 $model_data = $this->CI->customers_model->get_row($customer['id']);
472
473
474 $this->CI->unit->run($no_model_data, $model_data, 'Test get_row() method');
475
476
477 $this->CI->db->delete('ea_users', array('id' => $customer['id']));
478 }
479
480 private function test_get_row_that_does_not_exist() {
481 $random_record_id = 486868412;
482 $row_data = $this->CI->customers_model->get_row($random_record_id);
483 $this->CI->unit->run($row_data, NULL, 'Test get_row() with record id that does '
484 . 'not exist in the database.');
485 }
486
487 private function test_get_row_with_invalid_argument() {
488 $invalid_id = 'THIS IS NOT AN INTEGER';
489
490 $has_thrown_exception = FALSE;
491 try {
492 $this->CI->customers_model->get_row($invalid_id);
493 } catch(Exception $exc) {
494 $has_thrown_exception = TRUE;
495 }
496
497 $this->CI->unit->run($has_thrown_exception, TRUE, 'Test get_row() with wrong argument.');
498 }
499
500
501 private function test_get_value() {
502
503 $customer = array(
504 'last_name' => 'Doe',
505 'first_name' => 'John',
506 'email' => 'alextselegidis@gmail.com',
507 'phone_number' => '0123456789',
508 'address' => 'Abbey Road 18',
509 'city' => 'London',
510 'zip_code' => '12345',
511 'id_roles' => $this->customer_role_id
512 );
513 $this->CI->db->insert('ea_users', $customer);
514 $customer['id'] = intval($this->CI->db->insert_id());
515
516
517 $model_value = $this->CI->customers_model->get_value('email', $customer['id']);
518
519
520 $this->CI->unit->run($model_value, $customer['email'], 'Test get_value() method.');
521
522
523 $this->CI->db->delete('ea_users', array('id' => $customer['id']));
524 }
525
526 private function test_get_value_record_does_not_exist() {
527 $random_record_id = 843521368768;
528
529 $has_thrown_exception = FALSE;
530
531 try {
532 $this->CI->customers_model->get_value('email', $random_record_id);
533 } catch(Exception $exc) {
534 $has_thrown_exception = TRUE;
535 }
536
537 $this->CI->unit->run($has_thrown_exception, TRUE, 'Test get_value() with record id '
538 . 'that does not exist.');
539 }
540
541 private function test_get_value_field_does_not_exist() {
542
543 $customer = array(
544 'last_name' => 'Doe',
545 'first_name' => 'John',
546 'email' => 'alextselegidis@gmail.com',
547 'phone_number' => '0123456789',
548 'address' => 'Abbey Road 18',
549 'city' => 'London',
550 'zip_code' => '12345',
551 'id_roles' => $this->customer_role_id
552 );
553 $this->CI->db->insert('ea_users', $customer);
554 $customer['id'] = intval($this->CI->db->insert_id());
555
556
557 $wrong_field_name = 'THIS IS WRONG';
558 $has_thrown_exception = FALSE;
559
560 try {
561 $this->CI->customers_model->get_value($wrong_field_name, $customer['id']);
562 } catch (Exception $exc) {
563 $has_thrown_exception = TRUE;
564 }
565
566 $this->CI->unit->run($has_thrown_exception, TRUE, 'Test get_value() with record id '
567 . 'that does not exist.');
568
569
570 $this->CI->db->delete('ea_users', array('id' => $customer['id']));
571 }
572 }
573
574
575