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
<?php
namespace yiiunit\framework\db\dao;
use yii\db\dao\Connection;
use yii\db\dao\Command;
use yii\db\dao\Query;
class CommandTest extends \yiiunit\TestCase
{
private $connection;
function setUp()
{
if(!extension_loaded('pdo') || !extension_loaded('pdo_mysql'))
$this->markTestSkipped('pdo and pdo_mysql extensions are required.');
$params = $this->getParam('mysql');
$this->connection = new Connection($params['dsn'], $params['username'], $params['password']);
$this->connection->open();
$this->connection->pdo->exec(file_get_contents($params['fixture']));
}
function tearDown()
{
$this->connection->close();
}
function testConstruct()
{
$command = $this->connection->createCommand();
$this->assertEquals("SELECT *\nFROM ", $command->sql);
$sql='SELECT * FROM posts';
$command = $this->connection->createCommand($sql);
$this->assertEquals($sql, $command->sql);
$query = new Query;
$command = $this->connection->createCommand($query);
$this->assertEquals($query, $command->query);
$query = array('select'=>'id', 'from'=>'posts');
$command = $this->connection->createCommand($query);
$this->assertEquals($query, $command->query->toArray());
}
function testReset()
{
}
function testGetSetSql()
{
}
function testPrepare()
{
}
function testBindParam()
{
}
function testBindValue()
{
}
function testExecute()
{
}
function testQuery()
{
}
function testQueryRow()
{
}
function testQueryAll()
{
}
function testQueryColumn()
{
}
function testQueryScalar()
{
}
function testFetchMode()
{
}
/*
function testPrepare()
{
$sql='SELECT title FROM posts';
$command=$this->connection->createCommand($sql);
$this->assertEquals($command->pdoStatement,null);
$command->prepare();
$this->assertTrue($command->pdoStatement instanceof PDOStatement);
$this->assertEquals($command->queryScalar(),'post 1');
$command->text='Bad SQL';
$this->setExpectedException('CException');
$command->prepare();
}
function testCancel()
{
$sql='SELECT title FROM posts';
$command=$this->connection->createCommand($sql);
$command->prepare();
$this->assertTrue($command->pdoStatement instanceof PDOStatement);
$command->cancel();
$this->assertEquals($command->pdoStatement,null);
}
function testExecute()
{
$sql='INSERT INTO comments(content,post_id,author_id) VALUES (\'test comment\', 1, 1)';
$command=$this->connection->createCommand($sql);
$this->assertEquals($command->execute(),1);
$this->assertEquals($command->execute(),1);
$command=$this->connection->createCommand('SELECT * FROM comments WHERE content=\'test comment\'');
$this->assertEquals($command->execute(),0);
$command=$this->connection->createCommand('SELECT COUNT(*) FROM comments WHERE content=\'test comment\'');
$this->assertEquals($command->queryScalar(),2);
$command=$this->connection->createCommand('bad SQL');
$this->setExpectedException('CException');
$command->execute();
}
function testQuery()
{
$sql='SELECT * FROM posts';
$reader=$this->connection->createCommand($sql)->query();
$this->assertTrue($reader instanceof CDbDataReader);
$sql='SELECT * FROM posts';
$command=$this->connection->createCommand($sql);
$command->prepare();
$reader=$command->query();
$this->assertTrue($reader instanceof CDbDataReader);
$command=$this->connection->createCommand('bad SQL');
$this->setExpectedException('CException');
$command->query();
}
function testBindParam()
{
$sql='INSERT INTO posts(title,create_time,author_id) VALUES (:title, :create_time, 1)';
$command=$this->connection->createCommand($sql);
$title='test title';
$createTime=time();
$command->bindParam(':title',$title);
$command->bindParam(':create_time',$createTime);
$command->execute();
$sql='SELECT create_time FROM posts WHERE title=:title';
$command=$this->connection->createCommand($sql);
$command->bindParam(':title',$title);
$this->assertEquals($command->queryScalar(),$createTime);
$sql='INSERT INTO types (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=$this->connection->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 types';
$row=$this->connection->createCommand($sql)->queryRow();
$this->assertEquals($row['int_col'],$intCol);
$this->assertEquals($row['char_col'],$charCol);
$this->assertEquals($row['float_col'],$floatCol);
$this->assertEquals($row['blob_col'],$blobCol);
$this->assertEquals($row['numeric_col'],$numericCol);
}
function testBindValue()
{
$sql='INSERT INTO comments(content,post_id,author_id) VALUES (:content, 1, 1)';
$command=$this->connection->createCommand($sql);
$command->bindValue(':content','test comment');
$command->execute();
$sql='SELECT post_id FROM comments WHERE content=:content';
$command=$this->connection->createCommand($sql);
$command->bindValue(':content','test comment');
$this->assertEquals($command->queryScalar(),1);
}
function testQueryAll()
{
$rows=$this->connection->createCommand('SELECT * FROM posts')->queryAll();
$this->assertEquals(count($rows),5);
$row=$rows[2];
$this->assertEquals($row['id'],3);
$this->assertEquals($row['title'],'post 3');
$rows=$this->connection->createCommand('SELECT * FROM posts WHERE id=10')->queryAll();
$this->assertEquals($rows,array());
}
function testQueryRow()
{
$sql='SELECT * FROM posts';
$row=$this->connection->createCommand($sql)->queryRow();
$this->assertEquals($row['id'],1);
$this->assertEquals($row['title'],'post 1');
$sql='SELECT * FROM posts';
$command=$this->connection->createCommand($sql);
$command->prepare();
$row=$command->queryRow();
$this->assertEquals($row['id'],1);
$this->assertEquals($row['title'],'post 1');
$sql='SELECT * FROM posts WHERE id=10';
$command=$this->connection->createCommand($sql);
$this->assertFalse($command->queryRow());
$command=$this->connection->createCommand('bad SQL');
$this->setExpectedException('CException');
$command->queryRow();
}
function testQueryColumn()
{
$sql='SELECT * FROM posts';
$column=$this->connection->createCommand($sql)->queryColumn();
$this->assertEquals($column,range(1,5));
$command=$this->connection->createCommand('SELECT id FROM posts WHERE id=10');
$this->assertEquals($command->queryColumn(),array());
$command=$this->connection->createCommand('bad SQL');
$this->setExpectedException('CException');
$command->queryColumn();
}
function testQueryScalar()
{
$sql='SELECT * FROM posts';
$this->assertEquals($this->connection->createCommand($sql)->queryScalar(),1);
$sql='SELECT id FROM posts';
$command=$this->connection->createCommand($sql);
$command->prepare();
$this->assertEquals($command->queryScalar(),1);
$command=$this->connection->createCommand('SELECT id FROM posts WHERE id=10');
$this->assertFalse($command->queryScalar());
$command=$this->connection->createCommand('bad SQL');
$this->setExpectedException('CException');
$command->queryScalar();
}
function testFetchMode(){
$sql='SELECT * FROM posts';
$command=$this->connection->createCommand($sql);
$result = $command->queryRow();
$this->assertTrue(is_array($result));
$sql='SELECT * FROM posts';
$command=$this->connection->createCommand($sql);
$command->setFetchMode(PDO::FETCH_OBJ);
$result = $command->queryRow();
$this->assertTrue(is_object($result));
}
*/
}