1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2
3 class Unit_tests_appointments_model extends CI_Driver {
4 private $ci;
5
6 private $provider_id;
7 private $customer_id;
8 private $service_id;
9
10 11 12
13 public function __construct() {
14 $this->ci =& get_instance();
15
16 $this->ci->load->library('Unit_test');
17 $this->ci->load->model('appointments_model');
18
19
20
21 $provider = array(
22 'first_name' => 'John',
23 'last_name' => 'Doe',
24 'email' => 'test@test.com',
25 'mobile_number' => '000000',
26 'phone_number' => '111111',
27 'address' => 'Some Str',
28 'city' => 'Some City',
29 'state' => 'Some State',
30 'zip_code' => '12345',
31 'notes' => 'This is a test provider',
32 'id_roles' => $this->ci->db->get_where('ea_roles',
33 array('slug' => DB_SLUG_PROVIDER))->row()->id
34 );
35 $this->ci->db->insert('ea_users', $provider);
36 $this->provider_id = $this->ci->db->insert_id();
37
38 $service = array(
39 'name' => 'Test Service',
40 'duration' => 30,
41 'price' => '20.00',
42 'currency' => 'Euro',
43 'description' => 'Some description here ...',
44 'id_service_categories' => NULL
45 );
46 $this->ci->db->insert('ea_services', $service);
47 $this->service_id = $this->ci->db->insert_id();
48
49 $customer = array(
50 'last_name' => 'Doe',
51 'first_name' => 'John',
52 'email' => 'alextselegidis@gmail.com',
53 'phone_number' => '0123456789',
54 'address' => 'Abbey Road 18',
55 'city' => 'London',
56 'zip_code' => '12345',
57 'id_roles' => $this->ci->db->get_where('ea_roles',
58 array('slug' => DB_SLUG_CUSTOMER))->row()->id
59 );
60 $this->ci->db->insert('ea_users', $customer);
61 $this->customer_id = $this->ci->db->insert_id();
62 }
63
64 65 66
67 public function run_all() {
68
69
70
71 $class_methods = get_class_methods('Unit_tests_appointments_model');
72 foreach ($class_methods as $method_name) {
73 if (substr($method_name, 0, 5) === 'test_') {
74 call_user_func(array($this, $method_name));
75 }
76 }
77
78 $this->ci->db->delete('ea_users', array('id' => $this->customer_id));
79 $this->ci->db->delete('ea_users', array('id' => $this->provider_id));
80 $this->ci->db->delete('ea_services', array('id' => $this->service_id));
81 }
82
83
84
85
86 87 88
89 private function test_add_appointment_insert() {
90
91 $appointment = array(
92 'start_datetime' => '2013-05-01 12:30:00',
93 'end_datetime' => '2013-05-01 13:00:00',
94 'notes' => 'Some notes right here...',
95 'is_unavailable' => FALSE,
96 'id_users_provider' => $this->provider_id,
97 'id_users_customer' => $this->customer_id,
98 'id_services' => $this->service_id
99 );
100 $appointment['id'] = $this->ci->appointments_model->add($appointment);
101 $this->ci->unit->run($appointment['id'], 'is_int', 'Test if add() appointment (insert '
102 . 'operation) returned the db row id.');
103
104
105 $db_data = $this->ci->db->get_where('ea_appointments', array('id' => $appointment['id']))
106 ->row_array();
107
108
109
110 unset($db_data['hash']);
111 unset($db_data['book_datetime']);
112 unset($db_data['id_google_calendar']);
113
114 $this->ci->unit->run($appointment, $db_data, 'Test if add() appointment (insert '
115 . 'operation) has successfully inserted a record.');
116
117
118 $this->ci->db->delete('ea_appointments', array('id' => $appointment['id']));
119 }
120
121 122 123
124 private function test_add_appointment_update() {
125
126 $appointment = array(
127 'start_datetime' => '2013-05-01 12:30:00',
128 'end_datetime' => '2013-05-01 13:00:00',
129 'notes' => 'Some notes right here...',
130 'is_unavailable' => FALSE,
131 'id_users_provider' => $this->provider_id,
132 'id_users_customer' => $this->customer_id,
133 'id_services' => $this->service_id
134 );
135 $this->ci->db->insert('ea_appointments', $appointment);
136 $appointment['id'] = $this->ci->db->insert_id();
137
138
139 $changed_notes = 'Some CHANGED notes right here ...';
140 $appointment['notes'] = $changed_notes;
141
142 $update_result = $this->ci->appointments_model->add($appointment);
143 $this->ci->unit->run($update_result, 'is_int', 'Test if add() appointment (update '
144 . 'operation) has returned the row id.');
145
146 $db_notes = $this->ci->db->get_where('ea_appointments', array('id' => $update_result))
147 ->row()->notes;
148 $this->ci->unit->run($changed_notes, $db_notes, 'Test add() appointment (update '
149 . 'operation) has successfully updated record.');
150
151
152 $this->ci->db->delete('ea_appointments', array('id' => $appointment['id']));
153 }
154
155 156 157 158
159 private function test_add_appointment_wrong_date_format() {
160
161 $appointment = array(
162 'start_datetime' => '2013-0WRONG5-01 12:30WRONG:00',
163 'end_datetime' => '2013-0WRONG5-01WRONG 13:00WRONG:00',
164 'notes' => 'Some notes right here...',
165 'is_unavailable' => FALSE,
166 'id_users_provider' => $this->provider_id,
167 'id_users_customer' => $this->customer_id,
168 'id_services' => $this->service_id
169 );
170
171 $has_thrown_exception = FALSE;
172 try {
173 $this->ci->appointments_model->add($appointment);
174 } catch(Exception $exc) {
175 $has_thrown_exception = TRUE;
176 }
177
178 $this->ci->unit->run($has_thrown_exception, TRUE, 'Test add() appointment with wrong '
179 . 'date format.', 'A validation exception must be thrown.');
180 }
181
182 183 184 185 186
187 private function test_exists() {
188
189 $appointment = array(
190 'start_datetime' => '2013-05-01 12:30:00',
191 'end_datetime' => '2013-05-01 13:00:00',
192 'notes' => 'Some notes right here...',
193 'is_unavailable' => FALSE,
194 'id_users_provider' => $this->provider_id,
195 'id_users_customer' => $this->customer_id,
196 'id_services' => $this->service_id
197 );
198 $this->ci->db->insert('ea_appointments', $appointment);
199 $appointment['id'] = $this->ci->db->insert_id();
200
201
202 $this->ci->unit->run($this->ci->appointments_model->exists($appointment), TRUE,
203 'Test exists() method with an inserted record.');
204
205
206 $this->ci->db->delete('ea_appointments', array('id' => $appointment['id']));
207 }
208
209 private function test_exists_record_does_not_exist() {
210
211 $appointment = array(
212 'start_datetime' => '2013-05-01 08:33:45',
213 'end_datetime' => '2013-05-02 13:13:13',
214 'notes' => 'This is totally random!',
215 'is_unavailable' => FALSE,
216 'id_users_provider' => '198678',
217 'id_users_customer' => '194702',
218 'id_services' => '8766293'
219 );
220
221 $this->ci->unit->run($this->ci->appointments_model->exists($appointment), FALSE,
222 'Test exists() method with an appointment that does not exist');
223 }
224
225 private function test_exists_with_wrong_data() {
226
227 $appointment = array(
228 'start_datetime' => '2WRONG013-05-01 0WRONG8:33:45',
229 'end_datetime' => '2013-0WRONG5-02 13:13:WRONG13',
230 'notes' => 'This is totally random!',
231 'is_unavailable' => FALSE,
232 'id_users_provider' => '1986WRONG78',
233 'id_users_customer' => '1WRONG94702',
234 'id_services' => '876WRONG6293'
235 );
236
237 $this->ci->unit->run($this->ci->appointments_model->exists($appointment), FALSE,
238 'Test exists() method with wrong appointment data.');
239 }
240
241 242 243 244
245 private function test_find_record_id() {
246
247 $appointment = array(
248 'start_datetime' => '2013-05-01 12:30:00',
249 'end_datetime' => '2013-05-01 13:00:00',
250 'notes' => 'Some notes right here...',
251 'is_unavailable' => FALSE,
252 'id_users_provider' => $this->provider_id,
253 'id_users_customer' => $this->customer_id,
254 'id_services' => $this->service_id
255 );
256 $this->ci->db->insert('ea_appointments', $appointment);
257 $appointment['id'] = $this->ci->db->insert_id();
258
259
260 $method_result_id = $this->ci->appointments_model->find_record_id($appointment);
261
262 $this->ci->unit->run($method_result_id, $appointment['id'], 'Test find_record_id() '
263 . 'successfully returned the correct record id.');
264
265
266 $this->ci->db->delete('ea_appointments', array('id' => $method_result_id));
267 }
268
269 270 271 272 273 274
275 private function test_find_record_id_appointment_does_not_exist() {
276
277 $appointment = array(
278 'start_datetime' => '2013-05-01 12:30:00',
279 'end_datetime' => '2013-05-01 13:00:00',
280 'notes' => 'Some notes right here...',
281 'is_unavailable' => FALSE,
282 'id_users_provider' => $this->provider_id,
283 'id_users_customer' => $this->customer_id,
284 'id_services' => $this->service_id
285 );
286
287
288 $has_thrown_exception = FALSE;
289
290 try {
291 $this->ci->appointments_model->find_record_id($appointment);
292 } catch(Exception $exc) {
293 $has_thrown_exception = TRUE;
294 }
295
296 $this->ci->unit->run($has_thrown_exception, TRUE, 'Test find_record_id() with appointment '
297 . 'data that does not exist in the database.', 'A database exception is expected '
298 . 'to be raised.');
299 }
300
301 302 303 304 305 306
307 private function test_find_record_id_wrong_data() {
308
309 $appointment = array(
310 'start_datetime' => '2013WRONG-05-0WRONG1 12:WRONG30:00',
311 'end_datetime' => '2013-05-01 13:00:00WRONG',
312 'notes' => 'Some notes righWRONGt here...',
313 'is_unavailable' => FALSE,
314 'id_users_provider' => 'WRONG',
315 'id_users_customer' => 'WRONG',
316 'id_services' => 'WRONG'
317 );
318
319
320
321 $has_thrown_exception = FALSE;
322
323 try {
324 $this->ci->appointments_model->find_record_id($appointment);
325 } catch(Exception $exc) {
326 $has_thrown_exception = TRUE;
327 }
328
329 $this->ci->unit->run($has_thrown_exception, TRUE, 'Test find_record_id() with appointment '
330 . 'data array with wrong values.', 'A database exception is expected to be raised.');
331 }
332
333 334 335
336 private function test_delete() {
337
338 $appointment = array(
339 'start_datetime' => '2013-05-01 12:30:00',
340 'end_datetime' => '2013-05-01 13:00:00',
341 'notes' => 'Some notes right here...',
342 'is_unavailable' => FALSE,
343 'id_users_provider' => $this->provider_id,
344 'id_users_customer' => $this->customer_id,
345 'id_services' => $this->service_id
346 );
347 $this->ci->db->insert('ea_appointments', $appointment);
348 $appointment['id'] = $this->ci->db->insert_id();
349
350
351 $delete_result = $this->ci->appointments_model->delete($appointment['id']);
352 $this->ci->unit->run($delete_result, TRUE, 'Test delete() method result (should be TRUE).');
353
354
355 $num_rows = $this->ci->db->get_where('ea_appointments', array('id' => $appointment['id']))
356 ->num_rows();
357 $this->ci->unit->run($num_rows, 0, 'Test if the record was successfully deleted.');
358 }
359
360 361 362 363
364 private function test_delete_record_does_not_exist() {
365 $random_record_id = 1233265;
366 $delete_result = $this->ci->appointments_model->delete($random_record_id);
367 $this->ci->unit->run($delete_result, FALSE, 'Test delete() method with a record id'
368 . ' that does not exist');
369 }
370
371 372 373 374
375 private function test_delete_record_wrong_parameter_given() {
376 $wrong_record_id = 'not_numeric';
377
378 $has_thrown_exception = FALSE;
379
380 try {
381 $this->ci->appointments_model->delete($wrong_record_id);
382 } catch(Exception $exc) {
383 $has_thrown_exception = TRUE;
384 }
385
386 $this->ci->unit->run($has_thrown_exception, TRUE, 'Test delete() method with argument '
387 . 'that is not an numeric.');
388 }
389
390 391 392
393 private function test_get_batch() {
394
395 $db_data = $this->ci->db->get('ea_appointments')->result_array();
396
397
398 $model_data = $this->ci->appointments_model->get_batch();
399
400
401 $this->ci->unit->run($model_data, $db_data, 'Test get_batch() method.');
402 }
403
404 405 406 407
408 private function test_get_batch_where_clause() {
409
410 $appointment = array(
411 'start_datetime' => '2013-05-01 12:30:00',
412 'end_datetime' => '2013-05-01 13:00:00',
413 'notes' => 'Some notes right here...',
414 'is_unavailable' => FALSE,
415 'id_users_provider' => $this->provider_id,
416 'id_users_customer' => $this->customer_id,
417 'id_services' => $this->service_id
418 );
419 $this->ci->db->insert('ea_appointments', $appointment);
420 $appointment['id'] = $this->ci->db->insert_id();
421
422
423 $db_data = $this->ci->db->get_where('ea_appointments', array('id' => $appointment['id']))
424 ->result_array();
425
426
427 $model_data = $this->ci->appointments_model->get_batch(array('id' => $appointment['id']));
428
429
430 $this->ci->unit->run($model_data, $db_data, 'Test get_batch() method.');
431
432
433 $this->ci->db->delete('ea_appointments', array('id' => $appointment['id']));
434 }
435
436 437 438 439 440 441 442 443 444
445 private function unabled_test_get_batch_wrong_where_clause() {
446 $has_thrown_exception = FALSE;
447
448 try {
449 $this->ci->appointments_model->get_batch('WRONG QUERY HERE');
450 } catch(Exception $db_exc) {
451 $has_thrown_exception = TRUE;
452 }
453
454 $this->ci->unit->run($has_thrown_exception, TRUE, 'Test get_batch() with wrong where clause.',
455 'A database exception is expected to be thrown.');
456 }
457
458 459 460
461 private function test_get_row() {
462
463 $appointment = array(
464 'start_datetime' => '2013-05-01 12:30:00',
465 'end_datetime' => '2013-05-01 13:00:00',
466 'notes' => 'Some notes right here...',
467 'is_unavailable' => FALSE,
468 'hash' => '91de2d31f5cbb6d26a5b1b3e710d38d1',
469 'id_users_provider' => $this->provider_id,
470 'id_users_customer' => $this->customer_id,
471 'id_services' => $this->service_id
472 );
473 $this->ci->db->insert('ea_appointments', $appointment);
474 $appointment['id'] = $this->ci->db->insert_id();
475
476
477 $db_data = $this->ci->appointments_model->get_row($appointment['id']);
478 unset($db_data['book_datetime']);
479 unset($db_data['id_google_calendar']);
480
481
482 $this->ci->unit->run($db_data, $appointment, 'Test get_row() method.');
483
484
485 $this->ci->db->delete('ea_appointments', array('id' => $appointment['id']));
486 }
487
488 489 490
491 private function test_get_row_that_does_not_exist() {
492 $random_record_id = 789453486;
493
494 $row_data = $this->ci->appointments_model->get_row($random_record_id);
495
496 $this->ci->unit->run($row_data, NULL, 'Test get_row() with record id that does '
497 . 'not exist in the database.');
498 }
499
500 501 502 503 504
505 private function test_get_row_with_invalid_argument() {
506 $invalid_id = 'THIS IS NOT NUMERIC';
507
508 $has_thrown_exception = FALSE;
509 try {
510 $this->ci->appointments_model->get_row($invalid_id);
511 } catch(Exception $exc) {
512 $has_thrown_exception = TRUE;
513 }
514
515 $this->ci->unit->run($has_thrown_exception, TRUE, 'Test get_row() with wrong argument.');
516 }
517
518 519 520
521 private function test_get_value() {
522
523 $appointment = array(
524 'start_datetime' => '2013-05-01 12:30:00',
525 'end_datetime' => '2013-05-01 13:00:00',
526 'notes' => 'Some notes right here...',
527 'is_unavailable' => FALSE,
528 'id_users_provider' => $this->provider_id,
529 'id_users_customer' => $this->customer_id,
530 'id_services' => $this->service_id
531 );
532 $this->ci->db->insert('ea_appointments', $appointment);
533 $appointment['id'] = $this->ci->db->insert_id();
534
535
536 $db_value = $this->ci->appointments_model->get_value('start_datetime', $appointment['id']);
537
538
539 $this->ci->unit->run($db_value, $appointment['start_datetime'], 'Test get_value() method.');
540
541
542 $this->ci->db->delete('ea_appointments', array('id' => $appointment['id']));
543 }
544
545 546 547 548 549 550
551 private function test_get_value_record_does_not_exist() {
552 $random_record_id = 843521368768;
553
554 $has_thrown_exception = FALSE;
555
556 try {
557 $this->ci->appointments_model->get_value('start_datetime', $random_record_id);
558 } catch(Exception $exc) {
559 $has_thrown_exception = TRUE;
560 }
561
562 $this->ci->unit->run($has_thrown_exception, TRUE, 'Test get_value() with record id that '
563 . 'does not exist.');
564 }
565
566 567 568 569 570 571
572 private function test_get_value_field_does_not_exist() {
573
574 $appointment = array(
575 'start_datetime' => '2013-05-01 12:30:00',
576 'end_datetime' => '2013-05-01 13:00:00',
577 'notes' => 'Some notes right here...',
578 'is_unavailable' => FALSE,
579 'id_users_provider' => $this->provider_id,
580 'id_users_customer' => $this->customer_id,
581 'id_services' => $this->service_id
582 );
583 $this->ci->db->insert('ea_appointments', $appointment);
584 $appointment['id'] = $this->ci->db->insert_id();
585
586
587 $wrong_field_name = 'THIS IS WRONG';
588 $has_thrown_exception = FALSE;
589
590 try {
591 $this->ci->appointments_model->get_value($wrong_field_name, $appointment['id']);
592 } catch(Exception $exc) {
593 $has_thrown_exception = TRUE;
594 }
595
596 $this->ci->unit->run($has_thrown_exception, TRUE, 'Test get_value() with record id that '
597 . 'does not exist.');
598
599
600 $this->ci->db->delete('ea_appointments', array('id' => $appointment['id']));
601 }
602
603 private function test_validate_data() {
604 $appointment = array(
605 'start_datetime' => '2013-05-01 12:30:00',
606 'end_datetime' => '2013-05-01 13:00:00',
607 'notes' => 'Some notes right here...',
608 'is_unavailable' => FALSE,
609 'id_users_provider' => $this->provider_id,
610 'id_users_customer' => $this->customer_id,
611 'id_services' => $this->service_id
612 );
613 $validation_result = $this->ci->appointments_model->validate($appointment);
614 $this->ci->unit->run($validation_result, TRUE, 'Test validate() method.');
615 }
616
617 private function test_validate_data_wrong_date_format() {
618 $appointment = array(
619 'start_datetime' => '20WRONG13-05-01 12WRONG:30:00',
620 'end_datetime' => '2013-05WRONG-01 13:00WRONG:00',
621 'notes' => 'Some notes right here...',
622 'is_unavailable' => FALSE,
623 'id_users_provider' => $this->provider_id,
624 'id_users_customer' => $this->customer_id,
625 'id_services' => $this->service_id
626 );
627
628 $has_thrown_exc = FALSE;
629 try {
630 $this->ci->appointments_model->validate($appointment);
631 } catch(Exception $exc) {
632 $has_thrown_exc = TRUE;
633 }
634
635
636 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if validate() method with '
637 . 'wrong date formats has thrown exception.');
638 }
639
640 private function test_validate_data_invalid_provider_id() {
641 $appointment = array(
642 'start_datetime' => '2013-05-01 12:30:00',
643 'end_datetime' => '2013-05-01 13:00:00',
644 'notes' => 'Some notes right here...',
645 'is_unavailable' => FALSE,
646 'id_users_provider' => 'THIS IS WRONG',
647 'id_users_customer' => $this->customer_id,
648 'id_services' => $this->service_id
649 );
650
651 $has_thrown_exc = FALSE;
652 try {
653 $this->ci->appointments_model->validate($appointment);
654 } catch(Exception $exc) {
655 $has_thrown_exc = TRUE;
656 }
657
658 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if validate() method with '
659 . 'invalid provider id has thrown exception.');
660 }
661
662 private function test_validate_data_invalid_customer_id() {
663 $appointment = array(
664 'start_datetime' => '2013-05-01 12:30:00',
665 'end_datetime' => '2013-05-01 13:00:00',
666 'notes' => 'Some notes right here...',
667 'is_unavailable' => FALSE,
668 'id_users_provider' => $this->provider_id,
669 'id_users_customer' => 'THIS IS WRONG',
670 'id_services' => $this->service_id
671 );
672
673 $has_thrown_exc = FALSE;
674 try {
675 $this->ci->appointments_model->validate($appointment);
676 } catch(Exception $exc) {
677 $has_thrown_exc = TRUE;
678 }
679
680 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if validate() method with '
681 . 'invalid customer id has thrown exception.');
682 }
683
684 private function test_validate_data_invalid_service_id() {
685 $appointment = array(
686 'start_datetime' => '2013-05-01 12:30:00',
687 'end_datetime' => '2013-05-01 13:00:00',
688 'notes' => 'Some notes right here...',
689 'is_unavailable' => FALSE,
690 'id_users_provider' => $this->provider_id,
691 'id_users_customer' => $this->customer_id,
692 'id_services' => 'THIS IS WRONG'
693 );
694
695 $has_thrown_exc = FALSE;
696 try {
697 $this->ci->appointments_model->validate($appointment);
698 } catch(Exception $exc) {
699 $has_thrown_exc = TRUE;
700 }
701
702 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if validate() method with '
703 . 'invalid service id has thrown exception.');
704 }
705 }
706
707
708