CommandTest.php 8.84 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
{
13 14 15 16 17 18 19 20 21
    public function testConstruct()
    {
        $db = $this->getConnection(false);

        // null
        $command = $db->createCommand();
        $this->assertEquals(null, $command->sql);

        // string
22
        $sql = 'SELECT * FROM customer';
23 24 25 26 27 28 29 30
        $command = $db->createCommand($sql);
        $this->assertEquals($sql, $command->sql);
    }

    public function testGetSetSql()
    {
        $db = $this->getConnection(false);

31
        $sql = 'SELECT * FROM customer';
32 33 34
        $command = $db->createCommand($sql);
        $this->assertEquals($sql, $command->sql);

35
        $sql2 = 'SELECT * FROM order';
36 37 38 39 40 41 42 43
        $command->sql = $sql2;
        $this->assertEquals($sql2, $command->sql);
    }

    public function testAutoQuoting()
    {
        $db = $this->getConnection(false);

44
        $sql = 'SELECT [[id]], [[t.name]] FROM {{customer}} t';
45
        $command = $db->createCommand($sql);
46
        $this->assertEquals("SELECT `id`, `t`.`name` FROM `customer` t", $command->sql);
47 48 49 50 51 52
    }

    public function testPrepareCancel()
    {
        $db = $this->getConnection(false);

53
        $command = $db->createCommand('SELECT * FROM customer');
54 55 56 57 58 59 60 61 62 63 64
        $this->assertEquals(null, $command->pdoStatement);
        $command->prepare();
        $this->assertNotEquals(null, $command->pdoStatement);
        $command->cancel();
        $this->assertEquals(null, $command->pdoStatement);
    }

    public function testExecute()
    {
        $db = $this->getConnection();

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

69
        $sql = 'SELECT COUNT(*) FROM customer WHERE name =\'user4\'';
70 71 72 73 74 75 76 77 78 79 80 81 82
        $command = $db->createCommand($sql);
        $this->assertEquals(1, $command->queryScalar());

        $command = $db->createCommand('bad SQL');
        $this->setExpectedException('\yii\db\Exception');
        $command->execute();
    }

    public function testQuery()
    {
        $db = $this->getConnection();

        // query
83
        $sql = 'SELECT * FROM customer';
84 85 86 87
        $reader = $db->createCommand($sql)->query();
        $this->assertTrue($reader instanceof DataReader);

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

94
        $rows = $db->createCommand('SELECT * FROM customer WHERE id=10')->queryAll();
95 96 97
        $this->assertEquals([], $rows);

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

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

110
        $sql = 'SELECT * FROM customer WHERE id=10';
111 112 113 114
        $command = $db->createCommand($sql);
        $this->assertFalse($command->queryOne());

        // queryColumn
115
        $sql = 'SELECT * FROM customer';
116 117 118
        $column = $db->createCommand($sql)->queryColumn();
        $this->assertEquals(range(1, 3), $column);

119
        $command = $db->createCommand('SELECT id FROM customer WHERE id=10');
120 121 122
        $this->assertEquals([], $command->queryColumn());

        // queryScalar
123
        $sql = 'SELECT * FROM customer ORDER BY id';
124 125
        $this->assertEquals($db->createCommand($sql)->queryScalar(), 1);

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

131
        $command = $db->createCommand('SELECT id FROM customer WHERE id=10');
132 133 134 135 136 137 138 139 140 141 142 143
        $this->assertFalse($command->queryScalar());

        $command = $db->createCommand('bad SQL');
        $this->setExpectedException('\yii\db\Exception');
        $command->query();
    }

    public function testBindParamValue()
    {
        $db = $this->getConnection();

        // bindParam
144
        $sql = 'INSERT INTO customer(email, name, address) VALUES (:email, :name, :address)';
145 146 147 148 149 150 151 152 153
        $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();

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

159
        $sql = 'INSERT INTO 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)';
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());

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

189
        $sql = 'SELECT email FROM customer WHERE name=:name';
190 191 192 193 194 195 196 197 198 199
        $command = $db->createCommand($sql);
        $command->bindValue(':name', 'user5');
        $this->assertEquals('user5@example.com', $command->queryScalar());
    }

    public function testFetchMode()
    {
        $db = $this->getConnection();

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

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

        // FETCH_NUM, customized in query method
213
        $sql = 'SELECT * FROM customer';
214 215 216 217 218 219 220 221
        $command = $db->createCommand($sql);
        $result = $command->queryOne([], \PDO::FETCH_NUM);
        $this->assertTrue(is_array($result) && isset($result[0]));
    }

    public function testBatchInsert()
    {
        $command = $this->getConnection()->createCommand();
222
        $command->batchInsert('customer',
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
            ['email', 'name', 'address'], [
                ['t1@example.com', 't1', 't1 address'],
                ['t2@example.com', null, false],
            ]
        );
        $this->assertEquals(2, $command->execute());
    }

    public function testInsert()
    {
    }

    public function testUpdate()
    {
    }

    public function testDelete()
    {
    }

    public function testCreateTable()
    {
    }

    public function testRenameTable()
    {
    }

    public function testDropTable()
    {
    }

    public function testTruncateTable()
    {
    }

    public function testAddColumn()
    {
    }

    public function testDropColumn()
    {
    }

    public function testRenameColumn()
    {
    }

    public function testAlterColumn()
    {
    }

    public function testAddForeignKey()
    {
    }

    public function testDropForeignKey()
    {
    }

    public function testCreateIndex()
    {
    }

    public function testDropIndex()
    {
    }
290 291 292 293 294 295 296 297 298 299 300 301

    public function testIntegrityViolation()
    {
        $this->setExpectedException('\yii\db\IntegrityException');

        $db = $this->getConnection();

        $sql = 'INSERT INTO profile(id, description) VALUES (123, \'duplicate\')';
        $command = $db->createCommand($sql);
        $command->execute();
        $command->execute();
    }
Zander Baldwin committed
302
}