1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php
namespace yiiunit\framework\db;
use yii\db\Connection;
use yii\db\Command;
use yii\db\Query;
use yii\db\DataReader;
class CommandTest extends \yiiunit\MysqlTestCase
{
function testConstruct()
{
$db = $this->getConnection(false);
// null
$command = $db->createCommand();
$this->assertEquals(null, $command->sql);
// string
$sql = 'SELECT * FROM tbl_customer';
$command = $db->createCommand($sql);
$this->assertEquals($sql, $command->sql);
}
function testGetSetSql()
{
$db = $this->getConnection(false);
$sql = 'SELECT * FROM tbl_customer';
$command = $db->createCommand($sql);
$this->assertEquals($sql, $command->sql);
$sql2 = 'SELECT * FROM tbl_order';
$command->sql = $sql2;
$this->assertEquals($sql2, $command->sql);
}
function testAutoQuoting()
{
$db = $this->getConnection(false);
$sql = 'SELECT [[id]], [[t.name]] FROM {{tbl_customer}} t';
$command = $db->createCommand($sql);
$this->assertEquals("SELECT `id`, `t`.`name` FROM `tbl_customer` t", $command->sql);
}
function testPrepareCancel()
{
$db = $this->getConnection(false);
$command = $db->createCommand('SELECT * FROM tbl_customer');
$this->assertEquals(null, $command->pdoStatement);
$command->prepare();
$this->assertNotEquals(null, $command->pdoStatement);
$command->cancel();
$this->assertEquals(null, $command->pdoStatement);
}
function testExecute()
{
$db = $this->getConnection();
$sql = 'INSERT INTO tbl_customer(email, name , address) VALUES (\'user4@example.com\', \'user4\', \'address4\')';
$command = $db->createCommand($sql);
$this->assertEquals(1, $command->execute());
$sql = 'SELECT COUNT(*) FROM tbl_customer WHERE name =\'user4\'';
$command = $db->createCommand($sql);
$this->assertEquals(1, $command->queryScalar());
$command = $db->createCommand('bad SQL');
$this->setExpectedException('\yii\db\Exception');
$command->execute();
}
function testQuery()
{
$db = $this->getConnection();
// query
$sql = 'SELECT * FROM tbl_customer';
$reader = $db->createCommand($sql)->query();
$this->assertTrue($reader instanceof DataReader);
// queryAll
$rows = $db->createCommand('SELECT * FROM tbl_customer')->queryAll();
$this->assertEquals(3, count($rows));
$row = $rows[2];
$this->assertEquals(3, $row['id']);
$this->assertEquals('user3', $row['name']);
$rows = $db->createCommand('SELECT * FROM tbl_customer WHERE id=10')->queryAll();
$this->assertEquals(array(), $rows);
// queryRow
$sql = 'SELECT * FROM tbl_customer ORDER BY id';
$row = $db->createCommand($sql)->queryRow();
$this->assertEquals(1, $row['id']);
$this->assertEquals('user1', $row['name']);
$sql = 'SELECT * FROM tbl_customer ORDER BY id';
$command = $db->createCommand($sql);
$command->prepare();
$row = $command->queryRow();
$this->assertEquals(1, $row['id']);
$this->assertEquals('user1', $row['name']);
$sql = 'SELECT * FROM tbl_customer WHERE id=10';
$command = $db->createCommand($sql);
$this->assertFalse($command->queryRow());
// queryColumn
$sql = 'SELECT * FROM tbl_customer';
$column = $db->createCommand($sql)->queryColumn();
$this->assertEquals(range(1, 3), $column);
$command = $db->createCommand('SELECT id FROM tbl_customer WHERE id=10');
$this->assertEquals(array(), $command->queryColumn());
// queryScalar
$sql = 'SELECT * FROM tbl_customer ORDER BY id';
$this->assertEquals($db->createCommand($sql)->queryScalar(), 1);
$sql = 'SELECT id FROM tbl_customer ORDER BY id';
$command = $db->createCommand($sql);
$command->prepare();
$this->assertEquals(1, $command->queryScalar());
$command = $db->createCommand('SELECT id FROM tbl_customer WHERE id=10');
$this->assertFalse($command->queryScalar());
$command = $db->createCommand('bad SQL');
$this->setExpectedException('\yii\db\Exception');
$command->query();
}
function testBindParamValue()
{
$db = $this->getConnection();
// bindParam
$sql = 'INSERT INTO tbl_customer(email,name,address) VALUES (:email, :name, :address)';
$command = $db->createCommand($sql);
$email = 'user4@example.com';
$name = 'user4';
$address = 'address4';
$command->bindParam(':email', $email);
$command->bindParam(':name', $name);
$command->bindParam(':address', $address);
$command->execute();
$sql = 'SELECT name FROM tbl_customer WHERE email=:email';
$command = $db->createCommand($sql);
$command->bindParam(':email', $email);
$this->assertEquals($name, $command->queryScalar());
$sql = 'INSERT INTO tbl_type (int_col, char_col, float_col, blob_col, numeric_col, bool_col) VALUES (:int_col, :char_col, :float_col, :blob_col, :numeric_col, :bool_col)';
$command = $db->createCommand($sql);
$intCol = 123;
$charCol = 'abc';
$floatCol = 1.23;
$blobCol = "\x10\x11\x12";
$numericCol = '1.23';
$boolCol = false;
$command->bindParam(':int_col', $intCol);
$command->bindParam(':char_col', $charCol);
$command->bindParam(':float_col', $floatCol);
$command->bindParam(':blob_col', $blobCol);
$command->bindParam(':numeric_col', $numericCol);
$command->bindParam(':bool_col', $boolCol);
$this->assertEquals(1, $command->execute());
$sql = 'SELECT * FROM tbl_type';
$row = $db->createCommand($sql)->queryRow();
$this->assertEquals($intCol, $row['int_col']);
$this->assertEquals($charCol, $row['char_col']);
$this->assertEquals($floatCol, $row['float_col']);
$this->assertEquals($blobCol, $row['blob_col']);
$this->assertEquals($numericCol, $row['numeric_col']);
// bindValue
$sql = 'INSERT INTO tbl_customer(email, name, address) VALUES (:email, \'user5\', \'address5\')';
$command = $db->createCommand($sql);
$command->bindValue(':email', 'user5@example.com');
$command->execute();
$sql = 'SELECT email FROM tbl_customer WHERE name=:name';
$command = $db->createCommand($sql);
$command->bindValue(':name', 'user5');
$this->assertEquals('user5@example.com', $command->queryScalar());
// bind value via query or execute method
$sql = 'INSERT INTO tbl_customer(email, name, address) VALUES (:email, \'user6\', \'address6\')';
$command = $db->createCommand($sql);
$command->execute(array(':email' => 'user6@example.com'));
$sql = 'SELECT email FROM tbl_customer WHERE name=:name';
$command = $db->createCommand($sql);
$this->assertEquals('user5@example.com', $command->queryScalar(array(':name' => 'user5')));
}
function testFetchMode()
{
$db = $this->getConnection();
// default: FETCH_ASSOC
$sql = 'SELECT * FROM tbl_customer';
$command = $db->createCommand($sql);
$result = $command->queryRow();
$this->assertTrue(is_array($result) && isset($result['id']));
// FETCH_OBJ, customized via fetchMode property
$sql = 'SELECT * FROM tbl_customer';
$command = $db->createCommand($sql);
$command->fetchMode = \PDO::FETCH_OBJ;
$result = $command->queryRow();
$this->assertTrue(is_object($result));
// FETCH_NUM, customized in query method
$sql = 'SELECT * FROM tbl_customer';
$command = $db->createCommand($sql);
$result = $command->queryRow(array(), \PDO::FETCH_NUM);
$this->assertTrue(is_array($result) && isset($result[0]));
}
function testInsert()
{
}
function testUpdate()
{
}
function testDelete()
{
}
function testCreateTable()
{
}
function testRenameTable()
{
}
function testDropTable()
{
}
function testTruncateTable()
{
}
function testAddColumn()
{
}
function testDropColumn()
{
}
function testRenameColumn()
{
}
function testAlterColumn()
{
}
function testAddForeignKey()
{
}
function testDropForeignKey()
{
}
function testCreateIndex()
{
}
function testDropIndex()
{
}
function testParams()
{
}
function testGetSql()
{
}
function testCreateCommand()
{
}
function testReset()
{
}
function testToArray()
{
}
function testMergeWith()
{
}
}