CommandTest.php 7.31 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 6 7 8
use yii\db\Connection;
use yii\db\Command;
use yii\db\Query;
use yii\db\DataReader;
w  
Qiang Xue committed
9

Alexander Makarov committed
10
class CommandTest extends DatabaseTestCase
w  
Qiang Xue committed
11
{
Qiang Xue committed
12 13 14 15 16 17
	protected function setUp()
	{
		parent::setUp();
		$this->mockApplication();
	}

Alexander Makarov committed
18
	public function testConstruct()
w  
Qiang Xue committed
19
	{
Qiang Xue committed
20 21
		$db = $this->getConnection(false);

Qiang Xue committed
22
		// null
Qiang Xue committed
23
		$command = $db->createCommand();
Qiang Xue committed
24
		$this->assertEquals(null, $command->sql);
w  
Qiang Xue committed
25

Qiang Xue committed
26
		// string
Qiang Xue committed
27
		$sql = 'SELECT * FROM tbl_customer';
Qiang Xue committed
28
		$command = $db->createCommand($sql);
w  
Qiang Xue committed
29 30 31
		$this->assertEquals($sql, $command->sql);
	}

Alexander Makarov committed
32
	public function testGetSetSql()
w  
Qiang Xue committed
33
	{
Qiang Xue committed
34
		$db = $this->getConnection(false);
w  
Qiang Xue committed
35

Qiang Xue committed
36
		$sql = 'SELECT * FROM tbl_customer';
Qiang Xue committed
37 38
		$command = $db->createCommand($sql);
		$this->assertEquals($sql, $command->sql);
w  
Qiang Xue committed
39

Qiang Xue committed
40
		$sql2 = 'SELECT * FROM tbl_order';
Qiang Xue committed
41 42
		$command->sql = $sql2;
		$this->assertEquals($sql2, $command->sql);
w  
Qiang Xue committed
43 44
	}

Alexander Makarov committed
45
	public function testAutoQuoting()
46 47 48 49 50 51 52 53
	{
		$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
54
	public function testPrepareCancel()
w  
Qiang Xue committed
55
	{
Qiang Xue committed
56
		$db = $this->getConnection(false);
w  
Qiang Xue committed
57

Qiang Xue committed
58
		$command = $db->createCommand('SELECT * FROM tbl_customer');
Qiang Xue committed
59 60 61 62 63
		$this->assertEquals(null, $command->pdoStatement);
		$command->prepare();
		$this->assertNotEquals(null, $command->pdoStatement);
		$command->cancel();
		$this->assertEquals(null, $command->pdoStatement);
w  
Qiang Xue committed
64 65
	}

Alexander Makarov committed
66
	public function testExecute()
w  
Qiang Xue committed
67
	{
Qiang Xue committed
68
		$db = $this->getConnection();
w  
Qiang Xue committed
69

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

Qiang Xue committed
74
		$sql = 'SELECT COUNT(*) FROM tbl_customer WHERE name =\'user4\'';
Qiang Xue committed
75 76
		$command = $db->createCommand($sql);
		$this->assertEquals(1, $command->queryScalar());
w  
Qiang Xue committed
77

Qiang Xue committed
78 79 80
		$command = $db->createCommand('bad SQL');
		$this->setExpectedException('\yii\db\Exception');
		$command->execute();
w  
Qiang Xue committed
81 82
	}

Alexander Makarov committed
83
	public function testQuery()
w  
Qiang Xue committed
84
	{
Qiang Xue committed
85
		$db = $this->getConnection();
w  
Qiang Xue committed
86

Qiang Xue committed
87
		// query
Qiang Xue committed
88
		$sql = 'SELECT * FROM tbl_customer';
Qiang Xue committed
89 90
		$reader = $db->createCommand($sql)->query();
		$this->assertTrue($reader instanceof DataReader);
w  
Qiang Xue committed
91

Qiang Xue committed
92
		// queryAll
Qiang Xue committed
93 94
		$rows = $db->createCommand('SELECT * FROM tbl_customer')->queryAll();
		$this->assertEquals(3, count($rows));
Qiang Xue committed
95 96
		$row = $rows[2];
		$this->assertEquals(3, $row['id']);
Qiang Xue committed
97
		$this->assertEquals('user3', $row['name']);
w  
Qiang Xue committed
98

Qiang Xue committed
99
		$rows = $db->createCommand('SELECT * FROM tbl_customer WHERE id=10')->queryAll();
Qiang Xue committed
100
		$this->assertEquals(array(), $rows);
w  
Qiang Xue committed
101

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

Qiang Xue committed
108
		$sql = 'SELECT * FROM tbl_customer ORDER BY id';
Qiang Xue committed
109
		$command = $db->createCommand($sql);
w  
Qiang Xue committed
110
		$command->prepare();
111
		$row = $command->queryOne();
Qiang Xue committed
112
		$this->assertEquals(1, $row['id']);
Qiang Xue committed
113
		$this->assertEquals('user1', $row['name']);
w  
Qiang Xue committed
114

Qiang Xue committed
115
		$sql = 'SELECT * FROM tbl_customer WHERE id=10';
Qiang Xue committed
116
		$command = $db->createCommand($sql);
117
		$this->assertFalse($command->queryOne());
w  
Qiang Xue committed
118

Qiang Xue committed
119
		// queryColumn
Qiang Xue committed
120
		$sql = 'SELECT * FROM tbl_customer';
Qiang Xue committed
121
		$column = $db->createCommand($sql)->queryColumn();
Qiang Xue committed
122
		$this->assertEquals(range(1, 3), $column);
w  
Qiang Xue committed
123

Qiang Xue committed
124
		$command = $db->createCommand('SELECT id FROM tbl_customer WHERE id=10');
Qiang Xue committed
125
		$this->assertEquals(array(), $command->queryColumn());
w  
Qiang Xue committed
126

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

Qiang Xue committed
131
		$sql = 'SELECT id FROM tbl_customer ORDER BY id';
Qiang Xue committed
132
		$command = $db->createCommand($sql);
w  
Qiang Xue committed
133
		$command->prepare();
Qiang Xue committed
134 135
		$this->assertEquals(1, $command->queryScalar());

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

Qiang Xue committed
139 140
		$command = $db->createCommand('bad SQL');
		$this->setExpectedException('\yii\db\Exception');
w  
Qiang Xue committed
141 142 143
		$command->query();
	}

Alexander Makarov committed
144
	public function testBindParamValue()
w  
Qiang Xue committed
145
	{
Qiang Xue committed
146
		$db = $this->getConnection();
w  
Qiang Xue committed
147

Qiang Xue committed
148
		// bindParam
resurtm committed
149
		$sql = 'INSERT INTO tbl_customer(email, name, address) VALUES (:email, :name, :address)';
Qiang Xue committed
150 151 152 153 154 155 156
		$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
157 158
		$command->execute();

Qiang Xue committed
159
		$sql = 'SELECT name FROM tbl_customer WHERE email=:email';
Qiang Xue committed
160
		$command = $db->createCommand($sql);
Qiang Xue committed
161 162
		$command->bindParam(':email', $email);
		$this->assertEquals($name, $command->queryScalar());
w  
Qiang Xue committed
163

Qiang Xue committed
164
		$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
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
		$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
180
		$sql = 'SELECT * FROM tbl_type';
181
		$row = $db->createCommand($sql)->queryOne();
Qiang Xue committed
182 183 184 185 186 187 188
		$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
189
		$sql = 'INSERT INTO tbl_customer(email, name, address) VALUES (:email, \'user5\', \'address5\')';
Qiang Xue committed
190
		$command = $db->createCommand($sql);
Qiang Xue committed
191
		$command->bindValue(':email', 'user5@example.com');
Qiang Xue committed
192
		$command->execute();
w  
Qiang Xue committed
193

Qiang Xue committed
194
		$sql = 'SELECT email FROM tbl_customer WHERE name=:name';
Qiang Xue committed
195
		$command = $db->createCommand($sql);
Qiang Xue committed
196 197
		$command->bindValue(':name', 'user5');
		$this->assertEquals('user5@example.com', $command->queryScalar());
w  
Qiang Xue committed
198 199
	}

Alexander Makarov committed
200
	public function testFetchMode()
w  
Qiang Xue committed
201
	{
Qiang Xue committed
202
		$db = $this->getConnection();
w  
Qiang Xue committed
203

Qiang Xue committed
204
		// default: FETCH_ASSOC
Qiang Xue committed
205
		$sql = 'SELECT * FROM tbl_customer';
Qiang Xue committed
206
		$command = $db->createCommand($sql);
207
		$result = $command->queryOne();
Qiang Xue committed
208
		$this->assertTrue(is_array($result) && isset($result['id']));
w  
Qiang Xue committed
209

Qiang Xue committed
210
		// FETCH_OBJ, customized via fetchMode property
Qiang Xue committed
211
		$sql = 'SELECT * FROM tbl_customer';
Qiang Xue committed
212 213
		$command = $db->createCommand($sql);
		$command->fetchMode = \PDO::FETCH_OBJ;
214
		$result = $command->queryOne();
w  
Qiang Xue committed
215
		$this->assertTrue(is_object($result));
Qiang Xue committed
216 217

		// FETCH_NUM, customized in query method
Qiang Xue committed
218
		$sql = 'SELECT * FROM tbl_customer';
Qiang Xue committed
219
		$command = $db->createCommand($sql);
220
		$result = $command->queryOne(array(), \PDO::FETCH_NUM);
Qiang Xue committed
221
		$this->assertTrue(is_array($result) && isset($result[0]));
w  
Qiang Xue committed
222
	}
Qiang Xue committed
223

Alexander Makarov committed
224
	public function testInsert()
Qiang Xue committed
225 226 227
	{
	}

Alexander Makarov committed
228
	public function testUpdate()
Qiang Xue committed
229 230 231
	{
	}

Alexander Makarov committed
232
	public function testDelete()
Qiang Xue committed
233 234 235
	{
	}

Alexander Makarov committed
236
	public function testCreateTable()
Qiang Xue committed
237 238 239
	{
	}

Alexander Makarov committed
240
	public function testRenameTable()
Qiang Xue committed
241 242 243
	{
	}

Alexander Makarov committed
244
	public function testDropTable()
Qiang Xue committed
245 246 247
	{
	}

Alexander Makarov committed
248
	public function testTruncateTable()
Qiang Xue committed
249 250 251
	{
	}

Alexander Makarov committed
252
	public function testAddColumn()
Qiang Xue committed
253 254 255
	{
	}

Alexander Makarov committed
256
	public function testDropColumn()
Qiang Xue committed
257 258 259
	{
	}

Alexander Makarov committed
260
	public function testRenameColumn()
Qiang Xue committed
261 262 263
	{
	}

Alexander Makarov committed
264
	public function testAlterColumn()
Qiang Xue committed
265 266 267
	{
	}

Alexander Makarov committed
268
	public function testAddForeignKey()
Qiang Xue committed
269 270 271
	{
	}

Alexander Makarov committed
272
	public function testDropForeignKey()
Qiang Xue committed
273 274 275
	{
	}

Alexander Makarov committed
276
	public function testCreateIndex()
Qiang Xue committed
277 278 279
	{
	}

Alexander Makarov committed
280
	public function testDropIndex()
Qiang Xue committed
281 282
	{
	}
Zander Baldwin committed
283
}