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

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

5 6
use yii\caching\FileCache;
use yii\db\Connection;
Qiang Xue committed
7
use yii\db\DataReader;
w  
Qiang Xue committed
8

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

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

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

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

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

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

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

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

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

55
        $command = $db->createCommand('SELECT * FROM customer');
56 57 58 59 60 61 62 63 64 65 66
        $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();

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

71
        $sql = 'SELECT COUNT(*) FROM customer WHERE name =\'user4\'';
72 73 74 75 76 77 78 79 80 81 82 83 84
        $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
85
        $sql = 'SELECT * FROM customer';
86 87 88 89
        $reader = $db->createCommand($sql)->query();
        $this->assertTrue($reader instanceof DataReader);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

161
        $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)';
162 163
        $command = $db->createCommand($sql);
        $intCol = 123;
Carsten Brandt committed
164
        $charCol = str_repeat('abc', 33) . 'x'; // a 100 char string
165 166 167 168 169 170 171 172 173 174 175 176
        $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());

Carsten Brandt committed
177 178 179 180
        $command = $db->createCommand('SELECT int_col, char_col, float_col, blob_col, numeric_col, bool_col FROM type');
//        $command->prepare();
//        $command->pdoStatement->bindColumn('blob_col', $bc, \PDO::PARAM_LOB);
        $row = $command->queryOne();
181 182 183
        $this->assertEquals($intCol, $row['int_col']);
        $this->assertEquals($charCol, $row['char_col']);
        $this->assertEquals($floatCol, $row['float_col']);
Carsten Brandt committed
184 185 186 187 188 189
        if ($this->driverName === 'mysql' || $this->driverName === 'sqlite') {
            $this->assertEquals($blobCol, $row['blob_col']);
        } else {
            $this->assertTrue(is_resource($row['blob_col']));
            $this->assertEquals($blobCol, stream_get_contents($row['blob_col']));
        }
190
        $this->assertEquals($numericCol, $row['numeric_col']);
Carsten Brandt committed
191
        if ($this->driverName === 'mysql' || defined('HHVM_VERSION') && $this->driverName === 'sqlite') {
Carsten Brandt committed
192 193 194 195
            $this->assertEquals($boolCol, (int)$row['bool_col']);
        } else {
            $this->assertEquals($boolCol, $row['bool_col']);
        }
196 197

        // bindValue
198
        $sql = 'INSERT INTO customer(email, name, address) VALUES (:email, \'user5\', \'address5\')';
199 200 201 202
        $command = $db->createCommand($sql);
        $command->bindValue(':email', 'user5@example.com');
        $command->execute();

203
        $sql = 'SELECT email FROM customer WHERE name=:name';
204 205 206 207 208 209 210 211 212 213
        $command = $db->createCommand($sql);
        $command->bindValue(':name', 'user5');
        $this->assertEquals('user5@example.com', $command->queryScalar());
    }

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

        // default: FETCH_ASSOC
214
        $sql = 'SELECT * FROM customer';
215 216 217 218 219
        $command = $db->createCommand($sql);
        $result = $command->queryOne();
        $this->assertTrue(is_array($result) && isset($result['id']));

        // FETCH_OBJ, customized via fetchMode property
220
        $sql = 'SELECT * FROM customer';
221 222 223 224 225 226
        $command = $db->createCommand($sql);
        $command->fetchMode = \PDO::FETCH_OBJ;
        $result = $command->queryOne();
        $this->assertTrue(is_object($result));

        // FETCH_NUM, customized in query method
227
        $sql = 'SELECT * FROM customer';
228 229 230 231 232 233 234 235
        $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();
236
        $command->batchInsert('customer',
237 238 239 240 241 242 243 244
            ['email', 'name', 'address'], [
                ['t1@example.com', 't1', 't1 address'],
                ['t2@example.com', null, false],
            ]
        );
        $this->assertEquals(2, $command->execute());
    }

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 299 300 301 302 303 304
    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()
    {
    }
305
    */
306 307 308 309 310 311 312 313 314 315 316 317

    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();
    }
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362

    public function testQueryCache()
    {
        $db = $this->getConnection();
        $db->enableQueryCache = true;
        $db->queryCache = new FileCache(['cachePath' => '@yiiunit/runtime/cache']);
        $command = $db->createCommand('SELECT name FROM customer WHERE id=:id');

        $this->assertEquals('user1', $command->bindValue(':id', 1)->queryScalar());
        $update = $db->createCommand('UPDATE customer SET name=:name WHERE id=:id');
        $update->bindValues([':id' => 1, ':name' => 'user11'])->execute();
        $this->assertEquals('user11', $command->bindValue(':id', 1)->queryScalar());

        $db->cache(function (Connection $db) use ($command, $update) {
            $this->assertEquals('user2', $command->bindValue(':id', 2)->queryScalar());
            $update->bindValues([':id' => 2, ':name' => 'user22'])->execute();
            $this->assertEquals('user2', $command->bindValue(':id', 2)->queryScalar());

            $db->noCache(function () use ($command) {
                $this->assertEquals('user22', $command->bindValue(':id', 2)->queryScalar());
            });

            $this->assertEquals('user2', $command->bindValue(':id', 2)->queryScalar());
        }, 10);

        $db->enableQueryCache = false;
        $db->cache(function ($db) use ($command, $update) {
            $this->assertEquals('user22', $command->bindValue(':id', 2)->queryScalar());
            $update->bindValues([':id' => 2, ':name' => 'user2'])->execute();
            $this->assertEquals('user2', $command->bindValue(':id', 2)->queryScalar());
        }, 10);

        $db->enableQueryCache = true;
        $command = $db->createCommand('SELECT name FROM customer WHERE id=:id')->cache();
        $this->assertEquals('user11', $command->bindValue(':id', 1)->queryScalar());
        $update->bindValues([':id' => 1, ':name' => 'user1'])->execute();
        $this->assertEquals('user11', $command->bindValue(':id', 1)->queryScalar());
        $this->assertEquals('user1', $command->noCache()->bindValue(':id', 1)->queryScalar());

        $command = $db->createCommand('SELECT name FROM customer WHERE id=:id');
        $db->cache(function (Connection $db) use ($command, $update) {
            $this->assertEquals('user11', $command->bindValue(':id', 1)->queryScalar());
            $this->assertEquals('user1', $command->noCache()->bindValue(':id', 1)->queryScalar());
        }, 10);
    }
Zander Baldwin committed
363
}