1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2
3 class Unit_tests_settings_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('settings_model');
13 }
14
15 16 17
18 public function run_all() {
19
20
21
22 $class_methods = get_class_methods('Unit_tests_settings_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_get_setting() {
36
37 $setting = array(
38 'name' => 'test_name',
39 'value' => 'test_value'
40 );
41 $this->CI->db->insert('ea_settings', $setting);
42 $setting['id'] = intval($this->CI->db->insert_id());
43
44
45 $model_value = $this->CI->settings_model->get_setting('test_name');
46 $this->CI->unit->run($model_value, $setting['value'], 'Test get_setting() method.');
47
48
49 $this->CI->db->delete('ea_settings', array('id' => $setting['id']));
50 }
51
52 private function test_get_setting_invalid_argument() {
53 $invalid_argument = 564658765;
54 $has_thrown_exception = FALSE;
55 try {
56 $this->CI->settings_model->get_setting($invalid_argument);
57 } catch (Exception $exc) {
58 $has_thrown_exception = TRUE;
59 }
60 $this->CI->unit->run($has_thrown_exception, TRUE, 'Test get_setting() with invalid '
61 . 'method argument.');
62 }
63
64 private function test_get_setting_that_does_not_exist() {
65 $setting_name = 'THIS NAME DOES NOT EXIST IN DB';
66 $has_thrown_exception = FALSE;
67 try {
68 $this->CI->settings_model->get_setting($setting_name);
69 } catch (Exception $exc) {
70 $has_thrown_exception = TRUE;
71 }
72 $this->CI->unit->run($has_thrown_exception, TRUE, 'Test get_setting() with a name that '
73 . 'does not exist in database.');
74 }
75
76
77 private function test_set_setting_insert() {
78 $setting = array(
79 'name' => 'test_setting',
80 'value' => 'test_value'
81 );
82
83
84 $setting['id'] = $this->CI->settings_model->set_setting($setting['name'],
85 $setting['value']);
86 $this->CI->unit->run($setting['id'], 'is_int', 'Test that set_setting() method '
87 . '(insert operation) has returned the setting database id.');
88
89
90 $db_data = $this->CI->db->get_where('ea_settings', array('id' => $setting['id']))
91 ->row_array();
92 $this->CI->unit->run($setting, $db_data, 'Test set_setting() method has '
93 . 'successfully inserted the setting into the database.');
94
95
96 $this->CI->db->delete('ea_settings', array('id' => $setting['id']));
97 }
98
99 private function test_set_setting_update() {
100
101 $setting = array(
102 'name' => 'test_name',
103 'value' => 'test_value'
104 );
105 $this->CI->db->insert('ea_settings', $setting);
106 $setting['id'] = intval($this->CI->db->insert_id());
107
108
109 $new_setting_value = 'new_test_value';
110 $set_setting_result = $this->CI->settings_model->set_setting($setting['name'],
111 $new_setting_value);
112 $this->CI->unit->run($set_setting_result, 'is_int', 'Test that set_setting() method '
113 . '(update operation) has returned the setting database id.');
114
115
116 $db_setting_value = $this->CI->db->get_where('ea_settings', array('id' => $setting['id']))
117 ->row()->value;
118 $this->CI->unit->run($db_setting_value, $new_setting_value, 'Test set_setting() method '
119 . 'has successfully updated a setting value.');
120
121
122 $this->CI->db->delete('ea_settings', array('id' => $setting['id']));
123 }
124
125 private function test_set_setting_invalid_setting_name() {
126 $invalid_setting_name = 1219087912;
127 $has_thrown_exception = FALSE;
128 try {
129 $this->CI->settings_model->set_setting($invalid_setting_name, 'test_value');
130 } catch (Exception $exc) {
131 $has_thrown_exception = TRUE;
132 }
133 $this->CI->unit->run($has_thrown_exception, TRUE, 'Test set_setting() method with '
134 . 'invalid argument.');
135 }
136
137
138 private function test_remove_setting() {
139
140 $setting = array(
141 'name' => 'test_name',
142 'value' => 'test_value'
143 );
144 $this->CI->db->insert('ea_settings', $setting);
145 $setting['id'] = intval($this->CI->db->insert_id());
146
147
148 $remove_setting_result = $this->CI->settings_model->remove_setting($setting['name']);
149 $this->CI->unit->run($remove_setting_result, TRUE, 'Test remove_setting() return value.');
150
151
152 $num_rows = $this->CI->db->get_where('ea_settings', array('id' => $setting['id']))->num_rows();
153 $this->CI->unit->run($num_rows, 0, 'Test if remove_setting() method has successfully '
154 . 'removed the setting from the database.');
155 if ($num_rows > 0) {
156 $this->CI->db->delete('ea_settings', array('id' => $setting['id']));
157 }
158 }
159
160 private function test_remove_setting_record_does_not_exist() {
161 $random_setting_name = 'THIS IS TOTALLY RANDOM';
162 $remove_setting_result = $this->CI->settings_model->remove_setting($random_setting_name);
163 $this->CI->unit->run($remove_setting_result, FALSE, 'Test remove_setting() with record '
164 . 'that does not exist.');
165 }
166
167 private function test_remove_setting_invalid_setting_name() {
168 $invalid_setting_name = 12092130968;
169 $has_thrown_exception = FALSE;
170 try {
171 $this->CI->settings_model->remove_setting($invalid_setting_name);
172 } catch (Exception $exc) {
173 $has_thrown_exception = TRUE;
174 }
175 $this->CI->unit->run($has_thrown_exception, TRUE, 'Test remove_setting() with invalid '
176 . 'setting name.');
177 }
178 }
179
180
181