1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2
3 class Unit_tests_services_model extends CI_Driver {
4 private $ci;
5
6 7 8
9 public function __construct() {
10 $this->ci =& get_instance();
11 $this->ci->load->library('Unit_test');
12 $this->ci->load->model('services_model');
13 }
14
15 16 17
18 public function run_all() {
19
20
21
22 $class_methods = get_class_methods('Unit_tests_services_model');
23 foreach ($class_methods as $method_name) {
24 if (substr($method_name, 0, 5) === 'test_') {
25 call_user_func(array($this, $method_name));
26 }
27 }
28 }
29
30
31
32
33
34
35 private function test_add_insert() {
36 $service = array(
37 'name' => 'Test Service',
38 'duration' => 30,
39 'price' => '20.00',
40 'currency' => 'Euro',
41 'description' => 'Some description here ...',
42 'id_service_categories' => NULL
43 );
44
45 $service['id'] = $this->ci->services_model->add($service);
46 $this->ci->unit->run($service['id'], 'is_int', 'Test if add() - insert operation has '
47 . 'returned a valid record id.');
48
49 $db_record = $this->ci->db->get_where('ea_services', array('id' => $service['id']))->row_array();
50 $this->ci->unit->run($service, $db_record, 'Test if add() - insert operation, has '
51 . 'successfully inserted a new record.');
52
53 $this->ci->db->delete('ea_services', array('id' => $service['id']));
54 }
55
56 private function test_add_update() {
57 $service = array(
58 'name' => 'Test Service',
59 'duration' => 30,
60 'price' => '20.00',
61 'currency' => 'Euro',
62 'description' => 'Some description here ...',
63 'id_service_categories' => NULL
64 );
65
66 $this->ci->db->insert('ea_services', $service);
67 $service['id'] = $this->ci->db->insert_id();
68
69
70 $service['name'] = 'Updated Name';
71 $service['duration'] = 60;
72 $service['price'] = '60.45';
73 $service['description'] = 'Updated description ...';
74
75 $update_result = $this->ci->services_model->add($service);
76 $this->ci->unit->run($update_result, 'is_int', 'Test if add() - update operation, has '
77 . 'returned the record id.');
78
79 $db_record = $this->ci->db->get_where('ea_services', array('id' => $service['id']))->row_array();
80 $this->ci->unit->run($service, $db_record, 'Test if add() - update operation, has '
81 . 'successfully updated a record.');
82
83 $this->ci->db->delete('ea_services', array('id' => $service['id']));
84 }
85
86 private function test_add_invalid_data() {
87 $service = array(
88 'name' => '',
89 'duration' => 'invalid',
90 'price' => 'invalid',
91 'currency' => 'Euro',
92 'description' => 'Some description here ...',
93 'id_service_categories' => NULL
94 );
95
96 $has_thrown_exc = FALSE;
97 try {
98 $this->ci->services_model->add($service);
99 } catch(Exception $exc) {
100 $has_thrown_exc = TRUE;
101 }
102
103 $this->ci->unit->run($has_thrown_exc, TRUE, 'Check if add() with invalid data has '
104 . 'thrown exception.');
105 }
106
107
108 private function test_exists_record_exists() {
109
110 $service = array(
111 'name' => 'Test Service',
112 'duration' => 30,
113 'price' => '20.00',
114 'currency' => 'Euro',
115 'description' => 'Some description here ...',
116 'id_service_categories' => NULL
117 );
118
119 $this->ci->db->insert('ea_services', $service);
120 $service['id'] = $this->ci->db->insert_id();
121
122
123 $exists = $this->ci->services_model->exists($service);
124 $this->ci->unit->run($exists, TRUE, 'Check if exists() returned true on existing record.');
125
126
127 $this->ci->db->delete('ea_services', array('id' => $service['id']));
128 }
129
130 private function test_exists_record_does_not_exist() {
131 $service = array(
132 'name' => 'Random Name',
133 'duration' => 'Random Duration',
134 'price' => 'Random Price'
135 );
136
137 $exists = $this->ci->services_model->exists($service);
138 $this->ci->unit->run($exists, FALSE, 'Check if exists() returned false on non-existing record.');
139 }
140
141 private function test_exists_invalid_arguments() {
142 $invalid_arg = '';
143
144 $has_thrown_exc = FALSE;
145 try {
146 $exists = $this->ci->services_model->exists($invalid_arg);
147 } catch(Exception $exc) {
148 $has_thrown_exc = TRUE;
149 }
150 $this->ci->unit->run($has_thrown_exc, TRUE, 'Check if exists() with invalid argument has '
151 . 'thrown an exception.');
152 }
153
154
155 private function test_validate() {
156 $service = array(
157 'name' => 'Test Service',
158 'duration' => 30,
159 'price' => '20.00',
160 'currency' => 'Euro',
161 'description' => 'Some description here ...',
162 'id_service_categories' => NULL
163 );
164
165 $is_valid = $this->ci->services_model->validate($service);
166 $this->ci->unit->run($is_valid, TRUE, 'Test validate() method with valid data.');
167 }
168
169 private function test_validate_invalid_record_id() {
170 $service = array(
171 'id' => 'THIS IS INVALID',
172 'name' => 'Test Service',
173 'duration' => 30,
174 'price' => '20.00',
175 'currency' => 'Euro',
176 'description' => 'Some description here ...',
177 'id_service_categories' => NULL
178 );
179
180 $has_thrown_exc = FALSE;
181 try {
182 $this->ci->services_model->validate($service);
183 } catch (Exception $exc) {
184 $has_thrown_exc = TRUE;
185 }
186 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if validate() has thrown an exception '
187 . 'with invalid record id.');
188 }
189
190 private function test_validate_invalid_service_category_id() {
191 $service = array(
192 'name' => 'Test Service',
193 'duration' => 30,
194 'price' => '20.00',
195 'currency' => 'Euro',
196 'description' => 'Some description here ...',
197 'id_service_categories' => 'THIS IS INVALID'
198 );
199
200 $has_thrown_exc = FALSE;
201 try {
202 $this->ci->services_model->validate($service);
203 } catch (Exception $exc) {
204 $has_thrown_exc = TRUE;
205 }
206 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if validate() has thrown an exception '
207 . 'with invalid service category id.');
208 }
209
210 private function test_validate_invalid_service_name() {
211 $service = array(
212 'name' => '',
213 'duration' => 30,
214 'price' => '20.00',
215 'currency' => 'Euro',
216 'description' => 'Some description here ...',
217 'id_service_categories' => 'THIS IS INVALID'
218 );
219
220 $has_thrown_exc = FALSE;
221 try {
222 $this->ci->services_model->validate($service);
223 } catch (Exception $exc) {
224 $has_thrown_exc = TRUE;
225 }
226 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if validate() has thrown an exception '
227 . 'with invalid service service name.');
228 }
229
230
231 private function test_find_record_id() {
232
233 $service = array(
234 'name' => 'Test Service',
235 'duration' => 30,
236 'price' => '20.00',
237 'currency' => 'Euro',
238 'description' => 'Some description here ...',
239 'id_service_categories' => NULL
240 );
241
242 $this->ci->db->insert('ea_services', $service);
243 $service['id'] = intval($this->ci->db->insert_id());
244
245
246 $service_id = $this->ci->services_model->find_record_id($service);
247 $this->ci->unit->run($service_id, $service['id'], 'Test find_record_id() with record '
248 . 'that exist.');
249
250
251 $this->ci->db->delete('ea_services', array('id' => $service['id']));
252 }
253
254 private function test_find_record_id_invalid_service_data() {
255 $service = array(
256
257 'currency' => 'Euro',
258 'description' => 'Some description here ...',
259 'id_service_categories' => NULL
260 );
261
262 $has_thrown_exc = FALSE;
263 try {
264 $this->ci->services_model->find_record_id($service);
265 } catch(Exception $exc) {
266 $has_thrown_exc = TRUE;
267 }
268 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test find_record_id() with invalid service data.');
269 }
270
271 private function test_find_record_id_record_does_not_exist() {
272 $service = array(
273 'name' => 'Does not exist',
274 'duration' => 'Does not exist',
275 'price' => 'Does not exist'
276 );
277
278 $has_thrown_exc = FALSE;
279 try {
280 $this->ci->services_model->find_record_id($service);
281 } catch(Exception $exc) {
282 $has_thrown_exc = TRUE;
283 }
284 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test find_record_id() with record that '
285 . 'does not exist.');
286 }
287
288
289 private function test_delete() {
290
291 $service = array(
292 'name' => 'Test Service',
293 'duration' => 30,
294 'price' => '20.00',
295 'currency' => 'Euro',
296 'description' => 'Some description here ...',
297 'id_service_categories' => NULL
298 );
299
300 $this->ci->db->insert('ea_services', $service);
301 $service['id'] = intval($this->ci->db->insert_id());
302
303
304 $delete_result = $this->ci->services_model->delete($service['id']);
305 $this->ci->unit->run($delete_result, TRUE, 'Test delete() method result.');
306
307
308 $num_rows = $this->ci->db->get_where('ea_services', array('name' => $service['name']))
309 ->num_rows();
310 $this->ci->unit->run($num_rows, 0, 'Test if delete() method has actually deleted the record.');
311 }
312
313 private function test_delete_invalid_argument() {
314 $invalid_arg = 'THIS IS INVALID';
315 $has_thrown_exc = FALSE;
316 try {
317 $this->ci->services_model->delete($invalid_arg);
318 } catch(Exception $exc) {
319 $has_thrown_exc = TRUE;
320 }
321 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if delete() method has thrown '
322 . 'exception on invalid argument.');
323 }
324
325 private function test_delete_record_does_not_exist() {
326 $random_id = 1029800987;
327 $has_thrown_exc = FALSE;
328 $delete_result = $this->ci->services_model->delete($random_id);
329 $this->ci->unit->run($delete_result, FALSE, 'Test if delete() method result on record '
330 . 'that does not exist.');
331 }
332
333
334
335 private function test_get_batch() {
336
337 $db_data = $this->ci->db->get('ea_services')->result_array();
338
339 $model_data = $this->ci->services_model->get_batch();
340
341 $this->ci->unit->run($db_data, $model_data, 'Test get_batch() method.');
342 }
343
344 private function test_get_batch_with_where_clause() {
345
346 $service = array(
347 'name' => 'General Examination',
348 'duration' => 30,
349 'price' => 50.00,
350 'currency' => 'euro',
351 'description' => 'This is some service description.'
352 );
353
354 $this->ci->db->insert('ea_services', $service);
355 $service['id'] = intval($this->ci->db->insert_id());
356
357
358 $no_model_data = $this->ci->db->get_where('ea_services', array('id' => $service['id']))
359 ->result_array();
360
361
362 $model_data = $this->ci->services_model->get_batch(array('id' => $service['id']));
363
364
365 $this->ci->unit->run($no_model_data, $model_data, 'Test get_batch() with where clause.');
366
367
368 $this->ci->db->delete('ea_services', array('id' => $service['id']));
369 }
370
371 private function unabled_test_get_batch_with_invalid_where_clause() {
372
373 }
374
375
376 private function test_get_row() {
377
378 $service = array(
379 'name' => 'General Examination',
380 'duration' => 30,
381 'price' => 50.00,
382 'currency' => 'euro',
383 'description' => 'This is some service description.'
384 );
385
386 $this->ci->db->insert('ea_services', $service);
387 $service['id'] = intval($this->ci->db->insert_id());
388
389
390 $no_model_data = $this->ci->db->get_where('ea_services', array('id' => $service['id']))
391 ->row_array();
392 $model_data = $this->ci->services_model->get_row($service['id']);
393
394
395 $this->ci->unit->run($no_model_data, $model_data, 'Test get_row() method');
396
397
398 $this->ci->db->delete('ea_services', array('id' => $service['id']));
399 }
400
401 private function test_get_row_that_does_not_exist() {
402 $random_record_id = 486868412;
403 $row_data = $this->ci->services_model->get_row($random_record_id);
404 $this->ci->unit->run($row_data, NULL, 'Test get_row() with record id that does '
405 . 'not exist in the database.');
406 }
407
408 private function test_get_row_with_invalid_argument() {
409 $invalid_id = 'THIS IS NOT AN INTEGER';
410
411 $has_thrown_exception = FALSE;
412 try {
413 $this->ci->services_model->get_row($invalid_id);
414 } catch (Exception $exc) {
415 $has_thrown_exception = TRUE;
416 }
417
418 $this->ci->unit->run($has_thrown_exception, TRUE, 'Test get_row() with wrong argument.');
419 }
420
421
422 private function test_get_value() {
423
424 $service = array(
425 'name' => 'General Examination',
426 'duration' => 30,
427 'price' => 50.00,
428 'currency' => 'euro',
429 'description' => 'This is some service description.'
430 );
431 $this->ci->db->insert('ea_services', $service);
432 $service['id'] = intval($this->ci->db->insert_id());
433
434
435 $model_value = $this->ci->services_model->get_value('name', $service['id']);
436
437
438 $this->ci->unit->run($model_value, $service['name'], 'Test get_value() method.');
439
440
441 $this->ci->db->delete('ea_services', array('id' => $service['id']));
442 }
443
444 private function test_get_value_record_does_not_exist() {
445 $random_record_id = 843521368768;
446
447 $has_thrown_exception = FALSE;
448
449 try {
450 $this->ci->services_model->get_value('name', $random_record_id);
451 } catch (Exception $exc) {
452 $has_thrown_exception = TRUE;
453 }
454
455 $this->ci->unit->run($has_thrown_exception, TRUE, 'Test get_value() with record id that '
456 . 'does not exist.');
457 }
458
459 private function test_get_value_field_does_not_exist() {
460
461 $service = array(
462 'name' => 'General Examination',
463 'duration' => 30,
464 'price' => 50.00,
465 'currency' => 'euro',
466 'description' => 'This is some service description.'
467 );
468 $this->ci->db->insert('ea_services', $service);
469 $service['id'] = intval($this->ci->db->insert_id());
470
471
472 $wrong_field_name = 'THIS IS WRONG';
473 $has_thrown_exception = FALSE;
474
475 try {
476 $this->ci->services_model->get_value($wrong_field_name, $service['id']);
477 } catch (Exception $exc) {
478 $has_thrown_exception = TRUE;
479 }
480
481 $this->ci->unit->run($has_thrown_exception, TRUE, 'Test get_value() with record id that '
482 . 'does not exist.');
483
484
485 $this->ci->db->delete('ea_services', array('id' => $service['id']));
486 }
487
488
489 private function test_add_category_insert() {
490 $category = array(
491 'name' => 'Test Category',
492 'description' => 'Test Description ...'
493 );
494
495 $category['id'] = $this->ci->services_model->add_category($category);
496 $this->ci->unit->run($category['id'], 'is_int', 'Test if add_category() - insert '
497 . 'operation has returned a valid record id');
498
499 $db_record = $this->ci->db->get_where('ea_service_categories', array('id' => $category['id']))->row_array();
500 $this->ci->unit->run($category, $db_record, 'Test if add_category() - insert '
501 . 'operation has successfully inserted the record.');
502
503 $this->ci->db->delete('ea_service_categories', array('id' => $category['id']));
504 }
505
506 private function test_add_category_update() {
507 $category = array(
508 'name' => 'Test Category',
509 'description' => 'Test Description ...'
510 );
511
512 $this->ci->db->insert('ea_service_categories', $category);
513 $category['id'] = intval($this->ci->db->insert_id());
514
515
516 $category['name'] = 'new name';
517 $category['description'] = 'new description';
518
519 $update_result = $this->ci->services_model->add_category($category);
520 $this->ci->unit->run($update_result, 'is_int', 'Check if add_category() - update '
521 . 'operation has returned a valid record id.');
522
523 $db_record = $this->ci->db->get_where('ea_service_categories',
524 array('id' => $category['id']))->row_array();
525 $this->ci->unit->run($category, $db_record, 'Test if add_category() - update operation '
526 . 'has successfully updated a record.');
527
528 $this->ci->db->delete('ea_service_categories', array('id' => $category['id']));
529 }
530
531 private function test_add_category_invalid_data() {
532 $category = array(
533 'name' => '',
534 'descrption' => ''
535 );
536
537 $has_thrown_exc = FALSE;
538 try {
539 $this->ci->services_model->add_category($category);
540 } catch(Exception $exc) {
541 $has_thrown_exc = TRUE;
542 }
543 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if add_category() with invalid data '
544 . 'has thrown an exception.');
545 }
546
547
548 private function test_delete_category() {
549
550 $category = array(
551 'name' => 'Test Category',
552 'description' => 'Test Description ...'
553 );
554
555 $this->ci->db->insert('ea_service_categories', $category);
556 $category['id'] = intval($this->ci->db->insert_id());
557
558
559 $delete_result = $this->ci->services_model->delete_category($category['id']);
560 $this->ci->unit->run($delete_result, TRUE, 'Test if delete_category() method has '
561 . 'returned a valid result.');
562
563
564 $num_rows = $this->ci->db->get_where('ea_service_categories',
565 array('id' => $category['id']))->num_rows();
566 $this->ci->unit->run($num_rows, 0, 'Check if delete_category() has actually deleted '
567 . 'the record.');
568
569 if ($num_rows > 0) {
570 $this->ci->db->delete('ea_service_categories', array('id' => $category['id']));
571 }
572 }
573
574 private function test_delete_category_invalid_argument() {
575 $invalid_arg = 'This is invalid';
576 $has_thrown_exc = TRUE;
577 try {
578 $this->ci->services_model->delete_category($invalid_arg);
579 } catch(Exception $exc) {
580 $has_thrown_exc = TRUE;
581 }
582 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if delete_category() with invalid '
583 . 'argument has thrown an exception.');
584 }
585
586 private function test_delete_category_record_does_not_exist() {
587 $random_id = 09182093;
588 $has_thrown_exc = TRUE;
589 try {
590 $this->ci->services_model->delete_category($random_id);
591 } catch(Exception $exc) {
592 $has_thrown_exc = TRUE;
593 }
594 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if delete_category() with random id '
595 . 'has thrown an exception.');
596 }
597
598
599 private function test_get_category() {
600
601 $category = array(
602 'name' => 'Test Category',
603 'description' => 'Test Description ...'
604 );
605
606 $this->ci->db->insert('ea_service_categories', $category);
607 $category['id'] = intval($this->ci->db->insert_id());
608
609
610 $db_record = $this->ci->services_model->get_category($category['id']);
611 $this->ci->unit->run($db_record, $category, 'Test if get_category() has successfully '
612 . 'returned the record.');
613
614 $this->ci->db->delete('ea_service_categories', array('id' => $category['id']));
615 }
616
617 private function test_get_category_invalid_argument() {
618 $invalid_arg = 'THIS IS INVALID';
619 $has_thrown_exc = FALSE;
620 try {
621 $this->ci->services_model->get_category($invalid_arg);
622 } catch(Exception $exc) {
623 $has_thrown_exc = TRUE;
624 }
625 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if get_category() with invalid argument '
626 . 'has thrown an exception.');
627 }
628
629 private function test_get_category_record_does_not_exist() {
630 $random_id = 123412343;
631 $has_thrown_exc = TRUE;
632 try {
633 $this->ci->services_model->get_category($random_id);
634 } catch(Exception $exc) {
635 $has_thrown_exc = TRUE;
636 }
637 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if delete_category() with random id '
638 . 'has thrown an exception.');
639 }
640
641
642 private function test_get_all_categories() {
643 $all_categories = $this->ci->services_model->get_all_categories();
644 $db_categories = $this->ci->db->get('ea_service_categories')->result_array();
645 $this->ci->unit->run($all_categories, $db_categories, 'Test if get_all_categories() method '
646 . 'has successfully returned all the services categories.');
647 }
648
649
650 private function test_validate_category() {
651 $category = array(
652 'name' => 'Test Name',
653 'description' => 'Test Description ...'
654 );
655
656 $is_valid = $this->ci->services_model->validate_category($category);
657 $this->ci->unit->run($is_valid, TRUE, 'Test if validate_category() has returned true '
658 . 'with valid data.');
659 }
660
661 private function test_validate_category_invalid_name() {
662 $category = array(
663 'name' => '',
664 'description' => 'Test Description ...'
665 );
666
667 $is_valid = $this->ci->services_model->validate_category($category);
668 $this->ci->unit->run($is_valid, FALSE, 'Test if validate_category() has returned false '
669 . 'with invalid data.');
670 }
671 }
672
673
674
675