Conditions | 1 |
Paths | 1 |
Total Lines | 242 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
60 | function TestDbDiff () { |
||
61 | // Create test table |
||
62 | $this->oModule->oDb->Execute(' |
||
63 | CREATE TABLE t1 ( |
||
64 | uuid CHAR(36) NOT NULL, |
||
65 | i INTEGER NOT NULL DEFAULT 0, |
||
66 | ii INTEGER NULL DEFAULT 0, |
||
67 | s VARCHAR(20) NULL, |
||
68 | d DATETIME NULL, |
||
69 | PRIMARY KEY (uuid, i) |
||
70 | ); |
||
71 | '); |
||
72 | $this->oModule->oDb->Execute(' |
||
73 | CREATE TABLE t2 ( |
||
74 | uuid CHAR(36) NOT NULL, |
||
75 | i INTEGER NULL DEFAULT 0, |
||
76 | ii INTEGER NULL DEFAULT 0, |
||
77 | s VARCHAR(20) NULL, |
||
78 | d DATETIME NULL, |
||
79 | PRIMARY KEY (uuid) |
||
80 | ); |
||
81 | '); |
||
82 | |||
83 | |||
84 | // Test Adodb::GetDataByPk() |
||
85 | $uuid = Uuid(); |
||
86 | $this->oModule->oDb->Execute(' |
||
87 | INSERT INTO t1 |
||
88 | VALUES ("' . $uuid . '", 12, 11, "blah" |
||
89 | , "' . date('Y-m-d H:i:s') . '") |
||
90 | '); |
||
91 | $this->assertEqual(12, $this->oModule->oDb->GetDataByPk( |
||
92 | 't1', $uuid, 'i', 'uuid')); |
||
93 | $this->assertEqual(array('i' => 12, 's' => 'blah') |
||
94 | , $this->oModule->oDb->GetDataByPk( |
||
95 | 't1', array($uuid, 12), ' i , s ,')); |
||
96 | |||
97 | |||
98 | // Write data using DbDiff() |
||
99 | $uuid = Uuid(); |
||
100 | $uuid2 = Uuid(); |
||
101 | |||
102 | // Error: New array has few PK |
||
103 | $ar_new = array( |
||
104 | 'uuid' => $uuid, |
||
105 | // 'i' => mt_rand(0, 100), |
||
106 | 's' => RandomString(10), |
||
107 | 'd' => date('Y-m-d H:i:s'), |
||
108 | ); |
||
109 | $ar_diff = $this->oModule->DbDiff(array('t1' => $ar_new)); |
||
110 | $this->assertEqual(-2, $ar_diff['code']); |
||
111 | |||
112 | // New array has only PK |
||
113 | $ar_new = array( |
||
114 | 'uuid' => $uuid, |
||
115 | 'i' => mt_rand(0, 100), |
||
116 | ); |
||
117 | $ar_diff = $this->oModule->DbDiff(array('t1' => $ar_new)); |
||
118 | $this->assertEqual($ar_diff['diff']['t1'][0]['mode'], 'INSERT'); |
||
119 | $this->assertEqual(count($ar_diff['diff']['t1'][0]['pk']), 2); |
||
120 | $this->assertEqual(count($ar_diff['diff']['t1'][0]['col']), 0); |
||
121 | $ar_new = array( |
||
122 | 'uuid' => $uuid, |
||
123 | ); |
||
124 | $ar_diff = $this->oModule->DbDiff(array('t2' => $ar_new)); |
||
125 | $this->assertEqual($ar_diff['diff']['t2'][0]['mode'], 'INSERT'); |
||
126 | $this->assertEqual(count($ar_diff['diff']['t2'][0]['pk']), 1); |
||
127 | $this->assertEqual(count($ar_diff['diff']['t2'][0]['col']), 0); |
||
128 | |||
129 | // Insert data |
||
130 | $ar_new = array( |
||
131 | 'uuid' => $uuid, |
||
132 | 'i' => mt_rand(0, 100), |
||
133 | 's' => RandomString(10), |
||
134 | 'd' => date('Y-m-d H:i:s'), |
||
135 | ); |
||
136 | $ar_diff = $this->oModule->DbDiffExec(array('t1' => $ar_new)); |
||
137 | $this->assertEqual($ar_diff['diff']['t1'][0]['mode'], 'INSERT'); |
||
138 | $this->assertEqual(count($ar_diff['diff']['t1'][0]['pk']), 2); |
||
139 | $this->assertEqual(count($ar_diff['diff']['t1'][0]['col']), 2); |
||
140 | $this->assertEqual($ar_diff['code'], 1); |
||
141 | $this->assertEqual($ar_diff['flag'], 100); |
||
142 | $ar_diff = $this->oModule->DbDiffExec(array('t2' => $ar_new)); |
||
143 | $this->assertEqual($ar_diff['diff']['t2'][0]['mode'], 'INSERT'); |
||
144 | $this->assertEqual(count($ar_diff['diff']['t2'][0]['pk']), 1); |
||
145 | $this->assertEqual(count($ar_diff['diff']['t2'][0]['col']), 3); |
||
146 | $this->assertEqual($ar_diff['code'], 1); |
||
147 | $this->assertEqual($ar_diff['flag'], 100); |
||
148 | |||
149 | // Insert mixed with update, multi table |
||
150 | $ar_new2 = array($ar_new, array( |
||
151 | 'uuid' => $uuid2, |
||
152 | 'i' => mt_rand(0, 100), |
||
153 | 's' => RandomString(10), |
||
154 | 'd' => date('Y-m-d H:i:s'), |
||
155 | )); |
||
156 | $ar_new3 = $ar_new2; |
||
157 | $ar_new2[0]['s'] = RandomString(10); // Make a update in t1 |
||
158 | $ar_diff = $this->oModule->DbDiffExec(array( |
||
159 | 't1' => $ar_new2, |
||
160 | 't2' => $ar_new3, |
||
161 | )); |
||
162 | $this->assertEqual($ar_diff['diff']['t1'][0]['mode'], 'UPDATE'); |
||
163 | $this->assertEqual($ar_diff['diff']['t1'][1]['mode'], 'INSERT'); |
||
164 | $this->assertEqual($ar_diff['diff']['t2'][0]['mode'], 'INSERT'); |
||
165 | $this->assertEqual(count($ar_diff['diff']['t1'][0]['pk']), 2); |
||
166 | $this->assertEqual(count($ar_diff['diff']['t1'][0]['col']), 1); |
||
167 | $this->assertEqual(count($ar_diff['diff']['t2'][0]['pk']), 1); |
||
168 | $this->assertEqual(count($ar_diff['diff']['t2'][0]['col']), 3); |
||
169 | $this->assertEqual($ar_diff['code'], 3); |
||
170 | $this->assertEqual($ar_diff['flag'], 100); |
||
171 | |||
172 | // Db query fail |
||
173 | // $ar_new2[1]['ii'] = 'blah'; |
||
174 | // $ar_diff = $this->oModule->DbDiffExec(array( |
||
175 | // 't1' => $ar_new2, |
||
176 | // 't2' => $ar_new2, |
||
177 | // )); |
||
178 | // $this->assertEqual($ar_diff['diff']['t1'][0]['mode'], 'UPDATE'); |
||
179 | // $this->assertEqual($ar_diff['diff']['t2'][1]['mode'], 'UPDATE'); |
||
180 | // // Unknow column in fields list |
||
181 | // $this->assertEqual($ar_diff['code'], -1054); |
||
182 | // $this->assertEqual($ar_diff['flag'], 0); |
||
183 | |||
184 | // Delete op |
||
185 | // PK value NULL means delete |
||
186 | $ar_new4 = array($ar_new, array( |
||
187 | 'uuid' => NULL, |
||
188 | 'i' => NULL, |
||
189 | )); |
||
190 | $ar_diff = $this->oModule->DbDiffExec(array( |
||
191 | 't1' => $ar_new4, |
||
192 | 't2' => $ar_new4, |
||
193 | ), NULL, array( |
||
194 | 't1' => $ar_new3, // Notice: Not same with exists value |
||
195 | 't2' => $ar_new3, |
||
196 | )); |
||
197 | $this->assertEqual($ar_diff['diff']['t1'][0]['mode'], 'DELETE'); |
||
198 | $this->assertEqual($ar_diff['diff']['t2'][0]['mode'], 'DELETE'); |
||
199 | $this->assertEqual($ar_diff['code'], 2); |
||
200 | $this->assertEqual($ar_diff['flag'], 100); |
||
201 | |||
202 | |||
203 | // Rollback |
||
204 | $uuid = Uuid(); |
||
205 | $ar_new = array( |
||
206 | 'uuid' => $uuid, |
||
207 | 'i' => mt_rand(100, 200), |
||
208 | 's' => 'aaa', |
||
209 | 'd' => date('Y-m-d H:i:s'), |
||
210 | ); |
||
211 | $ar_new2 = array( |
||
212 | 'uuid' => $uuid2, |
||
213 | 'i' => mt_rand(100, 200), |
||
214 | 's' => 'aaa', |
||
215 | 'd' => date('Y-m-d H:i:s'), |
||
216 | ); |
||
217 | // 1. insert |
||
218 | $ar_new3 = array($ar_new, $ar_new2); |
||
219 | $ar_diff_ins = $this->oModule->DbDiffExec(array( |
||
220 | 't1' => $ar_new3, |
||
221 | 't2' => $ar_new3, |
||
222 | )); |
||
223 | $this->assertEqual('aaa', $this->oModule->oDb->GetDataByPk('t1' |
||
224 | , array($ar_new['uuid'], $ar_new['i']), 's')); |
||
225 | $this->assertEqual('aaa', $this->oModule->oDb->GetDataByPk('t1' |
||
226 | , array($ar_new2['uuid'], $ar_new2['i']), 's')); |
||
227 | $this->assertEqual('aaa', $this->oModule->oDb->GetDataByPk('t2' |
||
228 | , $ar_new['uuid'], 's')); |
||
229 | $this->assertEqual('aaa', $this->oModule->oDb->GetDataByPk('t2' |
||
230 | , $ar_new2['uuid'], 's')); |
||
231 | // 2. update 1, delete 1 |
||
232 | $ar_new4 = $ar_new3; |
||
233 | $ar_new4[0]['s'] = 'bbb'; |
||
234 | $ar_new4[0]['s'] = 'bbb'; |
||
235 | $ar_new4[1]['uuid'] = NULL; |
||
236 | $ar_new4[1]['i'] = NULL; |
||
237 | $ar_diff = $this->oModule->DbDiffExec(array( |
||
238 | 't1' => $ar_new4, |
||
239 | 't2' => $ar_new4, |
||
240 | ), NULL, array( |
||
241 | 't1' => $ar_new3, |
||
242 | 't2' => $ar_new3, |
||
243 | )); |
||
244 | $this->assertEqual('bbb', $this->oModule->oDb->GetDataByPk('t1' |
||
245 | , array($ar_new['uuid'], $ar_new['i']), 's')); |
||
246 | $this->assertEqual(NULL, $this->oModule->oDb->GetDataByPk('t1' |
||
247 | , array($ar_new2['uuid'], $ar_new2['i']), 's')); |
||
248 | $this->assertEqual('bbb', $this->oModule->oDb->GetDataByPk('t2' |
||
249 | , $ar_new['uuid'], 's')); |
||
250 | $this->assertEqual(NULL, $this->oModule->oDb->GetDataByPk('t2' |
||
251 | , $ar_new2['uuid'], 's')); |
||
252 | // 3. rollback update and delete |
||
253 | $i = $this->oModule->DbDiffRollback($ar_diff); |
||
254 | $this->assertEqual($i, 4); |
||
255 | $this->assertEqual($ar_diff['flag'], -100); |
||
256 | $this->assertEqual('aaa', $this->oModule->oDb->GetDataByPk('t1' |
||
257 | , array($ar_new['uuid'], $ar_new['i']), 's')); |
||
258 | $this->assertEqual('aaa', $this->oModule->oDb->GetDataByPk('t1' |
||
259 | , array($ar_new2['uuid'], $ar_new2['i']), 's')); |
||
260 | $this->assertEqual('aaa', $this->oModule->oDb->GetDataByPk('t2' |
||
261 | , $ar_new['uuid'], 's')); |
||
262 | $this->assertEqual('aaa', $this->oModule->oDb->GetDataByPk('t2' |
||
263 | , $ar_new2['uuid'], 's')); |
||
264 | // 4. after rollback, re-commit |
||
265 | $i = $this->oModule->DbDiffCommit($ar_diff); |
||
266 | $this->assertEqual($i, 4); |
||
267 | $this->assertEqual($ar_diff['flag'], 100); |
||
268 | $this->assertEqual('bbb', $this->oModule->oDb->GetDataByPk('t1' |
||
269 | , array($ar_new['uuid'], $ar_new['i']), 's')); |
||
270 | $this->assertEqual(NULL, $this->oModule->oDb->GetDataByPk('t1' |
||
271 | , array($ar_new2['uuid'], $ar_new2['i']), 's')); |
||
272 | $this->assertEqual('bbb', $this->oModule->oDb->GetDataByPk('t2' |
||
273 | , $ar_new['uuid'], 's')); |
||
274 | $this->assertEqual(NULL, $this->oModule->oDb->GetDataByPk('t2' |
||
275 | , $ar_new2['uuid'], 's')); |
||
276 | // 5. rollback insert done at beginning |
||
277 | $i = $this->oModule->DbDiffRollback($ar_diff_ins); |
||
278 | $this->assertEqual($i, 2); // 2 rows is alread deleted previous |
||
279 | $this->assertEqual($ar_diff_ins['flag'], -100); |
||
280 | $this->assertEqual(NULL, $this->oModule->oDb->GetDataByPk('t1' |
||
281 | , array($ar_new['uuid'], $ar_new['i']), 's')); |
||
282 | $this->assertEqual(NULL, $this->oModule->oDb->GetDataByPk('t1' |
||
283 | , array($ar_new2['uuid'], $ar_new2['i']), 's')); |
||
284 | $this->assertEqual(NULL, $this->oModule->oDb->GetDataByPk('t2' |
||
285 | , $ar_new['uuid'], 's')); |
||
286 | $this->assertEqual(NULL, $this->oModule->oDb->GetDataByPk('t2' |
||
287 | , $ar_new2['uuid'], 's')); |
||
288 | |||
289 | // :DEBUG: |
||
290 | //$this->oModule->oDb->debug = true; |
||
291 | ///Ecl('<pre>' . var_export($ar_diff, true) . '</pre>'); |
||
292 | |||
293 | |||
294 | // Clean up |
||
295 | $this->oModule->oDb->Execute(' |
||
296 | DROP TABLE t1; |
||
297 | '); |
||
298 | $this->oModule->oDb->Execute(' |
||
299 | DROP TABLE t2; |
||
300 | '); |
||
301 | } // end of func TestDbDiff |
||
302 | |||
367 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: