You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

278 lines
7.2 KiB

13 years ago
<?php
12 years ago
namespace yiiunit\framework\db;
use yii\db\Connection;
use yii\db\Command;
use yii\db\Query;
use yii\db\DataReader;
13 years ago
class CommandTest extends DatabaseTestCase
13 years ago
{
11 years ago
public function testConstruct()
13 years ago
{
13 years ago
$db = $this->getConnection(false);
13 years ago
// null
13 years ago
$command = $db->createCommand();
13 years ago
$this->assertEquals(null, $command->sql);
13 years ago
13 years ago
// string
13 years ago
$sql = 'SELECT * FROM tbl_customer';
13 years ago
$command = $db->createCommand($sql);
13 years ago
$this->assertEquals($sql, $command->sql);
}
11 years ago
public function testGetSetSql()
13 years ago
{
13 years ago
$db = $this->getConnection(false);
13 years ago
13 years ago
$sql = 'SELECT * FROM tbl_customer';
13 years ago
$command = $db->createCommand($sql);
$this->assertEquals($sql, $command->sql);
13 years ago
13 years ago
$sql2 = 'SELECT * FROM tbl_order';
13 years ago
$command->sql = $sql2;
$this->assertEquals($sql2, $command->sql);
13 years ago
}
11 years ago
public function testAutoQuoting()
{
$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);
}
11 years ago
public function testPrepareCancel()
13 years ago
{
13 years ago
$db = $this->getConnection(false);
13 years ago
13 years ago
$command = $db->createCommand('SELECT * FROM tbl_customer');
13 years ago
$this->assertEquals(null, $command->pdoStatement);
$command->prepare();
$this->assertNotEquals(null, $command->pdoStatement);
$command->cancel();
$this->assertEquals(null, $command->pdoStatement);
13 years ago
}
11 years ago
public function testExecute()
13 years ago
{
13 years ago
$db = $this->getConnection();
13 years ago
13 years ago
$sql = 'INSERT INTO tbl_customer(email, name , address) VALUES (\'user4@example.com\', \'user4\', \'address4\')';
13 years ago
$command = $db->createCommand($sql);
$this->assertEquals(1, $command->execute());
13 years ago
13 years ago
$sql = 'SELECT COUNT(*) FROM tbl_customer WHERE name =\'user4\'';
13 years ago
$command = $db->createCommand($sql);
$this->assertEquals(1, $command->queryScalar());
13 years ago
13 years ago
$command = $db->createCommand('bad SQL');
$this->setExpectedException('\yii\db\Exception');
$command->execute();
13 years ago
}
11 years ago
public function testQuery()
13 years ago
{
13 years ago
$db = $this->getConnection();
13 years ago
13 years ago
// query
13 years ago
$sql = 'SELECT * FROM tbl_customer';
13 years ago
$reader = $db->createCommand($sql)->query();
$this->assertTrue($reader instanceof DataReader);
13 years ago
13 years ago
// queryAll
13 years ago
$rows = $db->createCommand('SELECT * FROM tbl_customer')->queryAll();
$this->assertEquals(3, count($rows));
13 years ago
$row = $rows[2];
$this->assertEquals(3, $row['id']);
13 years ago
$this->assertEquals('user3', $row['name']);
13 years ago
13 years ago
$rows = $db->createCommand('SELECT * FROM tbl_customer WHERE id=10')->queryAll();
13 years ago
$this->assertEquals(array(), $rows);
13 years ago
// queryOne
13 years ago
$sql = 'SELECT * FROM tbl_customer ORDER BY id';
$row = $db->createCommand($sql)->queryOne();
13 years ago
$this->assertEquals(1, $row['id']);
13 years ago
$this->assertEquals('user1', $row['name']);
13 years ago
13 years ago
$sql = 'SELECT * FROM tbl_customer ORDER BY id';
13 years ago
$command = $db->createCommand($sql);
13 years ago
$command->prepare();
$row = $command->queryOne();
13 years ago
$this->assertEquals(1, $row['id']);
13 years ago
$this->assertEquals('user1', $row['name']);
13 years ago
13 years ago
$sql = 'SELECT * FROM tbl_customer WHERE id=10';
13 years ago
$command = $db->createCommand($sql);
$this->assertFalse($command->queryOne());
13 years ago
13 years ago
// queryColumn
13 years ago
$sql = 'SELECT * FROM tbl_customer';
13 years ago
$column = $db->createCommand($sql)->queryColumn();
13 years ago
$this->assertEquals(range(1, 3), $column);
13 years ago
13 years ago
$command = $db->createCommand('SELECT id FROM tbl_customer WHERE id=10');
13 years ago
$this->assertEquals(array(), $command->queryColumn());
13 years ago
13 years ago
// queryScalar
13 years ago
$sql = 'SELECT * FROM tbl_customer ORDER BY id';
13 years ago
$this->assertEquals($db->createCommand($sql)->queryScalar(), 1);
13 years ago
13 years ago
$sql = 'SELECT id FROM tbl_customer ORDER BY id';
13 years ago
$command = $db->createCommand($sql);
13 years ago
$command->prepare();
13 years ago
$this->assertEquals(1, $command->queryScalar());
13 years ago
$command = $db->createCommand('SELECT id FROM tbl_customer WHERE id=10');
13 years ago
$this->assertFalse($command->queryScalar());
13 years ago
13 years ago
$command = $db->createCommand('bad SQL');
$this->setExpectedException('\yii\db\Exception');
13 years ago
$command->query();
}
11 years ago
public function testBindParamValue()
13 years ago
{
13 years ago
$db = $this->getConnection();
13 years ago
13 years ago
// bindParam
12 years ago
$sql = 'INSERT INTO tbl_customer(email, name, address) VALUES (:email, :name, :address)';
13 years ago
$command = $db->createCommand($sql);
$email = 'user4@example.com';
$name = 'user4';
$address = 'address4';
$command->bindParam(':email', $email);
$command->bindParam(':name', $name);
$command->bindParam(':address', $address);
13 years ago
$command->execute();
13 years ago
$sql = 'SELECT name FROM tbl_customer WHERE email=:email';
13 years ago
$command = $db->createCommand($sql);
13 years ago
$command->bindParam(':email', $email);
$this->assertEquals($name, $command->queryScalar());
13 years ago
13 years ago
$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)';
13 years ago
$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());
13 years ago
$sql = 'SELECT * FROM tbl_type';
$row = $db->createCommand($sql)->queryOne();
13 years ago
$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
13 years ago
$sql = 'INSERT INTO tbl_customer(email, name, address) VALUES (:email, \'user5\', \'address5\')';
13 years ago
$command = $db->createCommand($sql);
13 years ago
$command->bindValue(':email', 'user5@example.com');
13 years ago
$command->execute();
13 years ago
13 years ago
$sql = 'SELECT email FROM tbl_customer WHERE name=:name';
13 years ago
$command = $db->createCommand($sql);
13 years ago
$command->bindValue(':name', 'user5');
$this->assertEquals('user5@example.com', $command->queryScalar());
13 years ago
}
11 years ago
public function testFetchMode()
13 years ago
{
13 years ago
$db = $this->getConnection();
13 years ago
13 years ago
// default: FETCH_ASSOC
13 years ago
$sql = 'SELECT * FROM tbl_customer';
13 years ago
$command = $db->createCommand($sql);
$result = $command->queryOne();
13 years ago
$this->assertTrue(is_array($result) && isset($result['id']));
13 years ago
13 years ago
// FETCH_OBJ, customized via fetchMode property
13 years ago
$sql = 'SELECT * FROM tbl_customer';
13 years ago
$command = $db->createCommand($sql);
$command->fetchMode = \PDO::FETCH_OBJ;
$result = $command->queryOne();
13 years ago
$this->assertTrue(is_object($result));
13 years ago
// FETCH_NUM, customized in query method
13 years ago
$sql = 'SELECT * FROM tbl_customer';
13 years ago
$command = $db->createCommand($sql);
$result = $command->queryOne(array(), \PDO::FETCH_NUM);
13 years ago
$this->assertTrue(is_array($result) && isset($result[0]));
13 years ago
}
12 years ago
11 years ago
public function testInsert()
12 years ago
{
}
11 years ago
public function testUpdate()
12 years ago
{
}
11 years ago
public function testDelete()
12 years ago
{
}
11 years ago
public function testCreateTable()
12 years ago
{
}
11 years ago
public function testRenameTable()
12 years ago
{
}
11 years ago
public function testDropTable()
12 years ago
{
}
11 years ago
public function testTruncateTable()
12 years ago
{
}
11 years ago
public function testAddColumn()
12 years ago
{
}
11 years ago
public function testDropColumn()
12 years ago
{
}
11 years ago
public function testRenameColumn()
12 years ago
{
}
11 years ago
public function testAlterColumn()
12 years ago
{
}
11 years ago
public function testAddForeignKey()
12 years ago
{
}
11 years ago
public function testDropForeignKey()
12 years ago
{
}
11 years ago
public function testCreateIndex()
12 years ago
{
}
11 years ago
public function testDropIndex()
12 years ago
{
}
}