CommandTest.php 7.19 KB
Newer Older
w  
Qiang Xue committed
1 2
<?php

Qiang Xue committed
3
namespace yiiunit\framework\db;
Qiang Xue committed
4

Qiang Xue committed
5
use yii\db\DataReader;
w  
Qiang Xue committed
6

7 8 9 10
/**
 * @group db
 * @group mysql
 */
Alexander Makarov committed
11
class CommandTest extends DatabaseTestCase
w  
Qiang Xue committed
12
{
Alexander Makarov committed
13
	public function testConstruct()
w  
Qiang Xue committed
14
	{
Qiang Xue committed
15 16
		$db = $this->getConnection(false);

Qiang Xue committed
17
		// null
Qiang Xue committed
18
		$command = $db->createCommand();
Qiang Xue committed
19
		$this->assertEquals(null, $command->sql);
w  
Qiang Xue committed
20

Qiang Xue committed
21
		// string
Qiang Xue committed
22
		$sql = 'SELECT * FROM tbl_customer';
Qiang Xue committed
23
		$command = $db->createCommand($sql);
w  
Qiang Xue committed
24 25 26
		$this->assertEquals($sql, $command->sql);
	}

Alexander Makarov committed
27
	public function testGetSetSql()
w  
Qiang Xue committed
28
	{
Qiang Xue committed
29
		$db = $this->getConnection(false);
w  
Qiang Xue committed
30

Qiang Xue committed
31
		$sql = 'SELECT * FROM tbl_customer';
Qiang Xue committed
32 33
		$command = $db->createCommand($sql);
		$this->assertEquals($sql, $command->sql);
w  
Qiang Xue committed
34

Qiang Xue committed
35
		$sql2 = 'SELECT * FROM tbl_order';
Qiang Xue committed
36 37
		$command->sql = $sql2;
		$this->assertEquals($sql2, $command->sql);
w  
Qiang Xue committed
38 39
	}

Alexander Makarov committed
40
	public function testAutoQuoting()
41 42 43 44 45 46 47 48
	{
		$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);
	}

Alexander Makarov committed
49
	public function testPrepareCancel()
w  
Qiang Xue committed
50
	{
Qiang Xue committed
51
		$db = $this->getConnection(false);
w  
Qiang Xue committed
52

Qiang Xue committed
53
		$command = $db->createCommand('SELECT * FROM tbl_customer');
Qiang Xue committed
54 55 56 57 58
		$this->assertEquals(null, $command->pdoStatement);
		$command->prepare();
		$this->assertNotEquals(null, $command->pdoStatement);
		$command->cancel();
		$this->assertEquals(null, $command->pdoStatement);
w  
Qiang Xue committed
59 60
	}

Alexander Makarov committed
61
	public function testExecute()
w  
Qiang Xue committed
62
	{
Qiang Xue committed
63
		$db = $this->getConnection();
w  
Qiang Xue committed
64

Qiang Xue committed
65
		$sql = 'INSERT INTO tbl_customer(email, name , address) VALUES (\'user4@example.com\', \'user4\', \'address4\')';
Qiang Xue committed
66 67
		$command = $db->createCommand($sql);
		$this->assertEquals(1, $command->execute());
w  
Qiang Xue committed
68

Qiang Xue committed
69
		$sql = 'SELECT COUNT(*) FROM tbl_customer WHERE name =\'user4\'';
Qiang Xue committed
70 71
		$command = $db->createCommand($sql);
		$this->assertEquals(1, $command->queryScalar());
w  
Qiang Xue committed
72

Qiang Xue committed
73 74 75
		$command = $db->createCommand('bad SQL');
		$this->setExpectedException('\yii\db\Exception');
		$command->execute();
w  
Qiang Xue committed
76 77
	}

Alexander Makarov committed
78
	public function testQuery()
w  
Qiang Xue committed
79
	{
Qiang Xue committed
80
		$db = $this->getConnection();
w  
Qiang Xue committed
81

Qiang Xue committed
82
		// query
Qiang Xue committed
83
		$sql = 'SELECT * FROM tbl_customer';
Qiang Xue committed
84 85
		$reader = $db->createCommand($sql)->query();
		$this->assertTrue($reader instanceof DataReader);
w  
Qiang Xue committed
86

Qiang Xue committed
87
		// queryAll
Qiang Xue committed
88 89
		$rows = $db->createCommand('SELECT * FROM tbl_customer')->queryAll();
		$this->assertEquals(3, count($rows));
Qiang Xue committed
90 91
		$row = $rows[2];
		$this->assertEquals(3, $row['id']);
Qiang Xue committed
92
		$this->assertEquals('user3', $row['name']);
w  
Qiang Xue committed
93

Qiang Xue committed
94
		$rows = $db->createCommand('SELECT * FROM tbl_customer WHERE id=10')->queryAll();
Alexander Makarov committed
95
		$this->assertEquals([], $rows);
w  
Qiang Xue committed
96

97
		// queryOne
Qiang Xue committed
98
		$sql = 'SELECT * FROM tbl_customer ORDER BY id';
99
		$row = $db->createCommand($sql)->queryOne();
Qiang Xue committed
100
		$this->assertEquals(1, $row['id']);
Qiang Xue committed
101
		$this->assertEquals('user1', $row['name']);
w  
Qiang Xue committed
102

Qiang Xue committed
103
		$sql = 'SELECT * FROM tbl_customer ORDER BY id';
Qiang Xue committed
104
		$command = $db->createCommand($sql);
w  
Qiang Xue committed
105
		$command->prepare();
106
		$row = $command->queryOne();
Qiang Xue committed
107
		$this->assertEquals(1, $row['id']);
Qiang Xue committed
108
		$this->assertEquals('user1', $row['name']);
w  
Qiang Xue committed
109

Qiang Xue committed
110
		$sql = 'SELECT * FROM tbl_customer WHERE id=10';
Qiang Xue committed
111
		$command = $db->createCommand($sql);
112
		$this->assertFalse($command->queryOne());
w  
Qiang Xue committed
113

Qiang Xue committed
114
		// queryColumn
Qiang Xue committed
115
		$sql = 'SELECT * FROM tbl_customer';
Qiang Xue committed
116
		$column = $db->createCommand($sql)->queryColumn();
Qiang Xue committed
117
		$this->assertEquals(range(1, 3), $column);
w  
Qiang Xue committed
118

Qiang Xue committed
119
		$command = $db->createCommand('SELECT id FROM tbl_customer WHERE id=10');
Alexander Makarov committed
120
		$this->assertEquals([], $command->queryColumn());
w  
Qiang Xue committed
121

Qiang Xue committed
122
		// queryScalar
Qiang Xue committed
123
		$sql = 'SELECT * FROM tbl_customer ORDER BY id';
Qiang Xue committed
124
		$this->assertEquals($db->createCommand($sql)->queryScalar(), 1);
w  
Qiang Xue committed
125

Qiang Xue committed
126
		$sql = 'SELECT id FROM tbl_customer ORDER BY id';
Qiang Xue committed
127
		$command = $db->createCommand($sql);
w  
Qiang Xue committed
128
		$command->prepare();
Qiang Xue committed
129 130
		$this->assertEquals(1, $command->queryScalar());

Qiang Xue committed
131
		$command = $db->createCommand('SELECT id FROM tbl_customer WHERE id=10');
Qiang Xue committed
132
		$this->assertFalse($command->queryScalar());
w  
Qiang Xue committed
133

Qiang Xue committed
134 135
		$command = $db->createCommand('bad SQL');
		$this->setExpectedException('\yii\db\Exception');
w  
Qiang Xue committed
136 137 138
		$command->query();
	}

Alexander Makarov committed
139
	public function testBindParamValue()
w  
Qiang Xue committed
140
	{
Qiang Xue committed
141
		$db = $this->getConnection();
w  
Qiang Xue committed
142

Qiang Xue committed
143
		// bindParam
resurtm committed
144
		$sql = 'INSERT INTO tbl_customer(email, name, address) VALUES (:email, :name, :address)';
Qiang Xue committed
145 146 147 148 149 150 151
		$command = $db->createCommand($sql);
		$email = 'user4@example.com';
		$name = 'user4';
		$address = 'address4';
		$command->bindParam(':email', $email);
		$command->bindParam(':name', $name);
		$command->bindParam(':address', $address);
w  
Qiang Xue committed
152 153
		$command->execute();

Qiang Xue committed
154
		$sql = 'SELECT name FROM tbl_customer WHERE email=:email';
Qiang Xue committed
155
		$command = $db->createCommand($sql);
Qiang Xue committed
156 157
		$command->bindParam(':email', $email);
		$this->assertEquals($name, $command->queryScalar());
w  
Qiang Xue committed
158

Qiang Xue committed
159
		$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)';
Qiang Xue committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
		$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());

Qiang Xue committed
175
		$sql = 'SELECT * FROM tbl_type';
176
		$row = $db->createCommand($sql)->queryOne();
Qiang Xue committed
177 178 179 180 181 182 183
		$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
Qiang Xue committed
184
		$sql = 'INSERT INTO tbl_customer(email, name, address) VALUES (:email, \'user5\', \'address5\')';
Qiang Xue committed
185
		$command = $db->createCommand($sql);
Qiang Xue committed
186
		$command->bindValue(':email', 'user5@example.com');
Qiang Xue committed
187
		$command->execute();
w  
Qiang Xue committed
188

Qiang Xue committed
189
		$sql = 'SELECT email FROM tbl_customer WHERE name=:name';
Qiang Xue committed
190
		$command = $db->createCommand($sql);
Qiang Xue committed
191 192
		$command->bindValue(':name', 'user5');
		$this->assertEquals('user5@example.com', $command->queryScalar());
w  
Qiang Xue committed
193 194
	}

Alexander Makarov committed
195
	public function testFetchMode()
w  
Qiang Xue committed
196
	{
Qiang Xue committed
197
		$db = $this->getConnection();
w  
Qiang Xue committed
198

Qiang Xue committed
199
		// default: FETCH_ASSOC
Qiang Xue committed
200
		$sql = 'SELECT * FROM tbl_customer';
Qiang Xue committed
201
		$command = $db->createCommand($sql);
202
		$result = $command->queryOne();
Qiang Xue committed
203
		$this->assertTrue(is_array($result) && isset($result['id']));
w  
Qiang Xue committed
204

Qiang Xue committed
205
		// FETCH_OBJ, customized via fetchMode property
Qiang Xue committed
206
		$sql = 'SELECT * FROM tbl_customer';
Qiang Xue committed
207 208
		$command = $db->createCommand($sql);
		$command->fetchMode = \PDO::FETCH_OBJ;
209
		$result = $command->queryOne();
w  
Qiang Xue committed
210
		$this->assertTrue(is_object($result));
Qiang Xue committed
211 212

		// FETCH_NUM, customized in query method
Qiang Xue committed
213
		$sql = 'SELECT * FROM tbl_customer';
Qiang Xue committed
214
		$command = $db->createCommand($sql);
Alexander Makarov committed
215
		$result = $command->queryOne([], \PDO::FETCH_NUM);
Qiang Xue committed
216
		$this->assertTrue(is_array($result) && isset($result[0]));
w  
Qiang Xue committed
217
	}
Qiang Xue committed
218

Alexander Makarov committed
219
	public function testInsert()
Qiang Xue committed
220 221 222
	{
	}

Alexander Makarov committed
223
	public function testUpdate()
Qiang Xue committed
224 225 226
	{
	}

Alexander Makarov committed
227
	public function testDelete()
Qiang Xue committed
228 229 230
	{
	}

Alexander Makarov committed
231
	public function testCreateTable()
Qiang Xue committed
232 233 234
	{
	}

Alexander Makarov committed
235
	public function testRenameTable()
Qiang Xue committed
236 237 238
	{
	}

Alexander Makarov committed
239
	public function testDropTable()
Qiang Xue committed
240 241 242
	{
	}

Alexander Makarov committed
243
	public function testTruncateTable()
Qiang Xue committed
244 245 246
	{
	}

Alexander Makarov committed
247
	public function testAddColumn()
Qiang Xue committed
248 249 250
	{
	}

Alexander Makarov committed
251
	public function testDropColumn()
Qiang Xue committed
252 253 254
	{
	}

Alexander Makarov committed
255
	public function testRenameColumn()
Qiang Xue committed
256 257 258
	{
	}

Alexander Makarov committed
259
	public function testAlterColumn()
Qiang Xue committed
260 261 262
	{
	}

Alexander Makarov committed
263
	public function testAddForeignKey()
Qiang Xue committed
264 265 266
	{
	}

Alexander Makarov committed
267
	public function testDropForeignKey()
Qiang Xue committed
268 269 270
	{
	}

Alexander Makarov committed
271
	public function testCreateIndex()
Qiang Xue committed
272 273 274
	{
	}

Alexander Makarov committed
275
	public function testDropIndex()
Qiang Xue committed
276 277
	{
	}
Zander Baldwin committed
278
}