|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Test - MVC Module class |
|
4
|
|
|
* |
|
5
|
|
|
* @package fwolflib |
|
6
|
|
|
* @subpackage class.test |
|
7
|
|
|
* @copyright Copyright 2012, Fwolf |
|
8
|
|
|
* @author Fwolf <[email protected]> |
|
9
|
|
|
* @since 2012-12-10 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
// Define like this, so test can run both under eclipse and web alone. |
|
14
|
|
|
// {{{ |
|
15
|
|
|
if (! defined('SIMPLE_TEST')) { |
|
16
|
|
|
define('SIMPLE_TEST', 'simpletest/'); |
|
17
|
|
|
require_once(SIMPLE_TEST . 'autorun.php'); |
|
18
|
|
|
} |
|
19
|
|
|
// Then set output encoding |
|
20
|
|
|
//header('Content-Type: text/html; charset=utf-8'); |
|
21
|
|
|
// }}} |
|
22
|
|
|
|
|
23
|
|
|
// Require library define file which need test |
|
24
|
|
|
require_once(dirname(__FILE__) . '/fwolflib.php'); |
|
25
|
|
|
require_once(dirname(__FILE__) . '/adodb.php'); |
|
26
|
|
|
require_once(dirname(__FILE__) . '/mvc-module.php'); |
|
27
|
|
|
require_once(dirname(__FILE__) . '/../func/ecl.php'); |
|
28
|
|
|
require_once(dirname(__FILE__) . '/../func/request.php'); |
|
29
|
|
|
require_once(dirname(__FILE__) . '/../func/uuid.php'); |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
class TestModule extends UnitTestCase { |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Module object |
|
36
|
|
|
* @var object |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $oModule = NULL; |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Constructor |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct () { |
|
45
|
|
|
$this->oModule = new ModuleTest(); |
|
46
|
|
|
|
|
47
|
|
|
// Define dbprofile |
|
48
|
|
|
$this->oModule->SetCfg('dbprofile', array( |
|
|
|
|
|
|
49
|
|
|
'type' => 'mysqli', |
|
50
|
|
|
'host' => 'localhost', |
|
51
|
|
|
'user' => 'test', |
|
52
|
|
|
'pass' => '', |
|
53
|
|
|
'name' => 'test', |
|
54
|
|
|
'lang' => 'utf-8', |
|
55
|
|
|
)); |
|
56
|
|
|
$this->oModule->oDb; |
|
57
|
|
|
} // end of func __construct |
|
58
|
|
|
|
|
59
|
|
|
|
|
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
|
|
|
|
|
303
|
|
|
|
|
304
|
|
|
} // end of class TestModule |
|
305
|
|
|
|
|
306
|
|
|
|
|
307
|
|
|
class ModuleTest extends Module { |
|
|
|
|
|
|
308
|
|
|
|
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* Constructor |
|
312
|
|
|
*/ |
|
313
|
|
|
public function __construct () { |
|
314
|
|
|
parent::__construct(); |
|
315
|
|
|
|
|
316
|
|
|
} // end of func __construct |
|
317
|
|
|
|
|
318
|
|
|
|
|
319
|
|
|
/** |
|
320
|
|
|
* Connect to db, using func defined in include file, check error here. |
|
321
|
|
|
* |
|
322
|
|
|
* <code> |
|
323
|
|
|
* $s = array(type, host, user, pass, name, lang); |
|
324
|
|
|
* type is mysql/sybase_ase etc, |
|
325
|
|
|
* name is dbname to select, |
|
326
|
|
|
* lang is db server charset. |
|
327
|
|
|
* </code> |
|
328
|
|
|
* |
|
329
|
|
|
* Useing my extended ADODB class now, little difference when new object. |
|
330
|
|
|
* @var array $dbprofile Server config array |
|
331
|
|
|
* @return object Db connection object |
|
332
|
|
|
*/ |
|
333
|
|
|
protected function DbConn ($dbprofile) { |
|
334
|
|
|
$conn = new Adodb($dbprofile); |
|
|
|
|
|
|
335
|
|
|
$conn->Connect(); |
|
336
|
|
|
|
|
337
|
|
|
if (0 !=$conn->ErrorNo()) { |
|
|
|
|
|
|
338
|
|
|
// Display error |
|
339
|
|
|
$s = 'ErrorNo: ' . $conn->ErrorNo() . "<br />\nErrorMsg: " . $conn->ErrorMsg(); |
|
|
|
|
|
|
340
|
|
|
return NULL; |
|
341
|
|
|
} |
|
342
|
|
|
else |
|
343
|
|
|
return $conn; |
|
344
|
|
|
} // end of func DbConn |
|
345
|
|
|
|
|
346
|
|
|
|
|
347
|
|
|
public function Init () { |
|
348
|
|
|
parent::Init(); |
|
|
|
|
|
|
349
|
|
|
|
|
350
|
|
|
return $this; |
|
351
|
|
|
} // end of func Init |
|
352
|
|
|
|
|
353
|
|
|
|
|
354
|
|
|
} // end of class ModuleTest |
|
355
|
|
|
|
|
356
|
|
|
|
|
357
|
|
|
// Change output charset in this way. |
|
358
|
|
|
// {{{ |
|
359
|
|
|
$s_url = GetSelfUrl(false); |
|
|
|
|
|
|
360
|
|
|
$s_url = substr($s_url, strrpos($s_url, '/') + 1); |
|
361
|
|
|
if ('mvc-module.test.php' == $s_url) { |
|
362
|
|
|
$test = new TestModule(); |
|
363
|
|
|
$test->run(new HtmlReporter('utf-8')); |
|
364
|
|
|
} |
|
365
|
|
|
// }}} |
|
366
|
|
|
?> |
|
|
|
|
|
|
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: