Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 239 lines (201 sloc) 7.674 kb
1f757cb improved unit tests
alexander.makarow authored
1 <?php
2
3 Yii::import('system.db.CDbConnection');
4
5 class CDbCommandTest extends CTestCase
6 {
7 private $_connection;
8
9 public function setUp()
10 {
11 if(!extension_loaded('pdo') || !extension_loaded('pdo_sqlite'))
12 $this->markTestSkipped('PDO and SQLite extensions are required.');
13
14 $this->_connection=new CDbConnection('sqlite::memory:');
15 $this->_connection->active=true;
16 $this->_connection->pdoInstance->exec(file_get_contents(dirname(__FILE__).'/data/sqlite.sql'));
17 }
18
19 public function tearDown()
20 {
21 $this->_connection->active=false;
22 }
23
24 public function testGetText()
25 {
26 $sql='SELECT * FROM posts';
27 $command=$this->_connection->createCommand($sql);
28 $this->assertEquals($command->text,$sql);
29 }
30
31 public function testSetText()
32 {
33 $sql='SELECT title FROM posts';
34 $command=$this->_connection->createCommand($sql);
35 $this->assertEquals($command->queryScalar(),'post 1');
36
37 $newSql='SELECT id FROM posts';
38 $command->text=$newSql;
39 $this->assertEquals($command->text,$newSql);
40 $this->assertEquals($command->queryScalar(),1);
41 }
42
43 public function testConnection()
44 {
45 $sql='SELECT title FROM posts';
46 $command=$this->_connection->createCommand($sql);
47 $this->assertEquals($command->connection,$this->_connection);
48 }
49
50 public function testPrepare()
51 {
52 $sql='SELECT title FROM posts';
53 $command=$this->_connection->createCommand($sql);
54 $this->assertEquals($command->pdoStatement,null);
55 $command->prepare();
56 $this->assertTrue($command->pdoStatement instanceof PDOStatement);
57 $this->assertEquals($command->queryScalar(),'post 1');
58
59 $command->text='Bad SQL';
60 $this->setExpectedException('CException');
61 $command->prepare();
62 }
63
64 public function testCancel()
65 {
66 $sql='SELECT title FROM posts';
67 $command=$this->_connection->createCommand($sql);
68 $command->prepare();
69 $this->assertTrue($command->pdoStatement instanceof PDOStatement);
70 $command->cancel();
71 $this->assertEquals($command->pdoStatement,null);
72 }
73
74 public function testExecute()
75 {
76 $sql='INSERT INTO comments(content,post_id,author_id) VALUES (\'test comment\', 1, 1)';
77 $command=$this->_connection->createCommand($sql);
78 $this->assertEquals($command->execute(),1);
79 $this->assertEquals($command->execute(),1);
80 $command=$this->_connection->createCommand('SELECT * FROM comments WHERE content=\'test comment\'');
81 $this->assertEquals($command->execute(),0);
82 $command=$this->_connection->createCommand('SELECT COUNT(*) FROM comments WHERE content=\'test comment\'');
83 $this->assertEquals($command->queryScalar(),2);
84
85 $command=$this->_connection->createCommand('bad SQL');
86 $this->setExpectedException('CException');
87 $command->execute();
88 }
89
90 public function testQuery()
91 {
92 $sql='SELECT * FROM posts';
93 $reader=$this->_connection->createCommand($sql)->query();
94 $this->assertTrue($reader instanceof CDbDataReader);
95
96 $sql='SELECT * FROM posts';
97 $command=$this->_connection->createCommand($sql);
98 $command->prepare();
99 $reader=$command->query();
100 $this->assertTrue($reader instanceof CDbDataReader);
101
102 $command=$this->_connection->createCommand('bad SQL');
103 $this->setExpectedException('CException');
104 $command->query();
105 }
106
107 public function testBindParam()
108 {
109 $sql='INSERT INTO posts(title,create_time,author_id) VALUES (:title, :create_time, 1)';
110 $command=$this->_connection->createCommand($sql);
111 $title='test title';
112 $createTime=time();
113 $command->bindParam(':title',$title);
114 $command->bindParam(':create_time',$createTime);
115 $command->execute();
116
117 $sql='SELECT create_time FROM posts WHERE title=:title';
118 $command=$this->_connection->createCommand($sql);
119 $command->bindParam(':title',$title);
120 $this->assertEquals($command->queryScalar(),$createTime);
121
122 $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)';
123 $command=$this->_connection->createCommand($sql);
124 $intCol=123;
125 $charCol='abc';
126 $floatCol=1.23;
127 $blobCol="\x10\x11\x12";
128 $numericCol='1.23';
129 $boolCol=false;
130 $command->bindParam(':int_col',$intCol);
131 $command->bindParam(':char_col',$charCol);
132 $command->bindParam(':float_col',$floatCol);
133 $command->bindParam(':blob_col',$blobCol);
134 $command->bindParam(':numeric_col',$numericCol);
135 $command->bindParam(':bool_col',$boolCol);
136 $this->assertEquals(1,$command->execute());
137
138 $sql='SELECT * FROM types';
139 $row=$this->_connection->createCommand($sql)->queryRow();
140 $this->assertEquals($row['int_col'],$intCol);
141 $this->assertEquals($row['char_col'],$charCol);
142 $this->assertEquals($row['float_col'],$floatCol);
143 $this->assertEquals($row['blob_col'],$blobCol);
144 $this->assertEquals($row['numeric_col'],$numericCol);
145 }
146
147 public function testBindValue()
148 {
149 $sql='INSERT INTO comments(content,post_id,author_id) VALUES (:content, 1, 1)';
150 $command=$this->_connection->createCommand($sql);
151 $command->bindValue(':content','test comment');
152 $command->execute();
153
154 $sql='SELECT post_id FROM comments WHERE content=:content';
155 $command=$this->_connection->createCommand($sql);
156 $command->bindValue(':content','test comment');
157 $this->assertEquals($command->queryScalar(),1);
158 }
159
160 public function testQueryAll()
161 {
162 $rows=$this->_connection->createCommand('SELECT * FROM posts')->queryAll();
163 $this->assertEquals(count($rows),5);
164 $row=$rows[2];
165 $this->assertEquals($row['id'],3);
166 $this->assertEquals($row['title'],'post 3');
167
168 $rows=$this->_connection->createCommand('SELECT * FROM posts WHERE id=10')->queryAll();
169 $this->assertEquals($rows,array());
170 }
171
172 public function testQueryRow()
173 {
174 $sql='SELECT * FROM posts';
175 $row=$this->_connection->createCommand($sql)->queryRow();
176 $this->assertEquals($row['id'],1);
177 $this->assertEquals($row['title'],'post 1');
178
179 $sql='SELECT * FROM posts';
180 $command=$this->_connection->createCommand($sql);
181 $command->prepare();
182 $row=$command->queryRow();
183 $this->assertEquals($row['id'],1);
184 $this->assertEquals($row['title'],'post 1');
185
186 $sql='SELECT * FROM posts WHERE id=10';
187 $command=$this->_connection->createCommand($sql);
188 $this->assertFalse($command->queryRow());
189
190 $command=$this->_connection->createCommand('bad SQL');
191 $this->setExpectedException('CException');
192 $command->queryRow();
193 }
194
195 public function testQueryColumn()
196 {
197 $sql='SELECT * FROM posts';
198 $column=$this->_connection->createCommand($sql)->queryColumn();
199 $this->assertEquals($column,range(1,5));
200
201 $command=$this->_connection->createCommand('SELECT id FROM posts WHERE id=10');
202 $this->assertEquals($command->queryColumn(),array());
203
204 $command=$this->_connection->createCommand('bad SQL');
205 $this->setExpectedException('CException');
206 $command->queryColumn();
207 }
208
209 public function testQueryScalar()
210 {
211 $sql='SELECT * FROM posts';
212 $this->assertEquals($this->_connection->createCommand($sql)->queryScalar(),1);
213
214 $sql='SELECT id FROM posts';
215 $command=$this->_connection->createCommand($sql);
216 $command->prepare();
217 $this->assertEquals($command->queryScalar(),1);
218
219 $command=$this->_connection->createCommand('SELECT id FROM posts WHERE id=10');
220 $this->assertFalse($command->queryScalar());
221
222 $command=$this->_connection->createCommand('bad SQL');
223 $this->setExpectedException('CException');
224 $command->queryScalar();
225 }
226
227 public function testFetchMode(){
228 $sql='SELECT * FROM posts';
229 $command=$this->_connection->createCommand($sql);
230 $result = $command->queryRow();
231 $this->assertTrue(is_array($result));
232
233 $sql='SELECT * FROM posts';
234 $command=$this->_connection->createCommand($sql);
235 $command->setFetchMode(PDO::FETCH_OBJ);
236 $result = $command->queryRow();
237 $this->assertTrue(is_object($result));
238 }
239 }
Something went wrong with that request. Please try again.