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.

291 lines
7.2 KiB

13 years ago
<?php
namespace yiiunit\framework\db\dao;
13 years ago
use yii\db\dao\Connection;
use yii\db\dao\Command;
use yii\db\dao\Query;
13 years ago
class CommandTest extends \yiiunit\MysqlTestCase
13 years ago
{
function testConstruct()
{
13 years ago
$db = $this->getConnection(false);
$command = $db->createCommand();
13 years ago
$this->assertEquals("SELECT *\nFROM ", $command->sql);
$sql='SELECT * FROM posts';
13 years ago
$command = $db->createCommand($sql);
13 years ago
$this->assertEquals($sql, $command->sql);
$query = new Query;
13 years ago
$command = $db->createCommand($query);
13 years ago
$this->assertEquals($query, $command->query);
$query = array('select'=>'id', 'from'=>'posts');
13 years ago
$command = $db->createCommand($query);
13 years ago
$this->assertEquals($query, $command->query->toArray());
}
function testReset()
{
13 years ago
$db = $this->getConnection();
$command = $db->createCommand('SELECT * FROM yii_user');
$command->queryRow();
$this->assertNotEquals(null, $command->pdoStatement);
$this->assertEquals('SELECT * FROM yii_user', $command->sql);
13 years ago
13 years ago
$command->reset();
$this->assertEquals(null, $command->pdoStatement);
$this->assertNotEquals('SELECT * FROM yii_user', $command->sql);
13 years ago
}
function testGetSetSql()
{
}
function testPrepare()
{
}
function testBindParam()
{
}
function testBindValue()
{
}
function testExecute()
{
}
function testQuery()
{
}
function testQueryRow()
{
}
function testQueryAll()
{
}
function testQueryColumn()
{
}
function testQueryScalar()
{
}
function testFetchMode()
{
}
/*
13 years ago
function testPrepare()
{
$sql='SELECT title FROM posts';
13 years ago
$command=$db->createCommand($sql);
13 years ago
$this->assertEquals($command->pdoStatement,null);
$command->prepare();
$this->assertTrue($command->pdoStatement instanceof PDOStatement);
$this->assertEquals($command->queryScalar(),'post 1');
$command->text='Bad SQL';
$this->setExpectedException('CException');
$command->prepare();
}
function testCancel()
{
$sql='SELECT title FROM posts';
13 years ago
$command=$db->createCommand($sql);
13 years ago
$command->prepare();
$this->assertTrue($command->pdoStatement instanceof PDOStatement);
$command->cancel();
$this->assertEquals($command->pdoStatement,null);
}
function testExecute()
{
$sql='INSERT INTO comments(content,post_id,author_id) VALUES (\'test comment\', 1, 1)';
13 years ago
$command=$db->createCommand($sql);
13 years ago
$this->assertEquals($command->execute(),1);
$this->assertEquals($command->execute(),1);
13 years ago
$command=$db->createCommand('SELECT * FROM comments WHERE content=\'test comment\'');
13 years ago
$this->assertEquals($command->execute(),0);
13 years ago
$command=$db->createCommand('SELECT COUNT(*) FROM comments WHERE content=\'test comment\'');
13 years ago
$this->assertEquals($command->queryScalar(),2);
13 years ago
$command=$db->createCommand('bad SQL');
13 years ago
$this->setExpectedException('CException');
$command->execute();
}
function testQuery()
{
$sql='SELECT * FROM posts';
13 years ago
$reader=$db->createCommand($sql)->query();
13 years ago
$this->assertTrue($reader instanceof CDbDataReader);
$sql='SELECT * FROM posts';
13 years ago
$command=$db->createCommand($sql);
13 years ago
$command->prepare();
$reader=$command->query();
$this->assertTrue($reader instanceof CDbDataReader);
13 years ago
$command=$db->createCommand('bad SQL');
13 years ago
$this->setExpectedException('CException');
$command->query();
}
function testBindParam()
{
$sql='INSERT INTO posts(title,create_time,author_id) VALUES (:title, :create_time, 1)';
13 years ago
$command=$db->createCommand($sql);
13 years ago
$title='test title';
$createTime=time();
$command->bindParam(':title',$title);
$command->bindParam(':create_time',$createTime);
$command->execute();
$sql='SELECT create_time FROM posts WHERE title=:title';
13 years ago
$command=$db->createCommand($sql);
13 years ago
$command->bindParam(':title',$title);
$this->assertEquals($command->queryScalar(),$createTime);
$sql='INSERT INTO types (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);
13 years ago
$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());
$sql='SELECT * FROM types';
13 years ago
$row=$db->createCommand($sql)->queryRow();
13 years ago
$this->assertEquals($row['int_col'],$intCol);
$this->assertEquals($row['char_col'],$charCol);
$this->assertEquals($row['float_col'],$floatCol);
$this->assertEquals($row['blob_col'],$blobCol);
$this->assertEquals($row['numeric_col'],$numericCol);
}
function testBindValue()
{
$sql='INSERT INTO comments(content,post_id,author_id) VALUES (:content, 1, 1)';
13 years ago
$command=$db->createCommand($sql);
13 years ago
$command->bindValue(':content','test comment');
$command->execute();
$sql='SELECT post_id FROM comments WHERE content=:content';
13 years ago
$command=$db->createCommand($sql);
13 years ago
$command->bindValue(':content','test comment');
$this->assertEquals($command->queryScalar(),1);
}
function testQueryAll()
{
13 years ago
$rows=$db->createCommand('SELECT * FROM posts')->queryAll();
13 years ago
$this->assertEquals(count($rows),5);
$row=$rows[2];
$this->assertEquals($row['id'],3);
$this->assertEquals($row['title'],'post 3');
13 years ago
$rows=$db->createCommand('SELECT * FROM posts WHERE id=10')->queryAll();
13 years ago
$this->assertEquals($rows,array());
}
function testQueryRow()
{
$sql='SELECT * FROM posts';
13 years ago
$row=$db->createCommand($sql)->queryRow();
13 years ago
$this->assertEquals($row['id'],1);
$this->assertEquals($row['title'],'post 1');
$sql='SELECT * FROM posts';
13 years ago
$command=$db->createCommand($sql);
13 years ago
$command->prepare();
$row=$command->queryRow();
$this->assertEquals($row['id'],1);
$this->assertEquals($row['title'],'post 1');
$sql='SELECT * FROM posts WHERE id=10';
13 years ago
$command=$db->createCommand($sql);
13 years ago
$this->assertFalse($command->queryRow());
13 years ago
$command=$db->createCommand('bad SQL');
13 years ago
$this->setExpectedException('CException');
$command->queryRow();
}
function testQueryColumn()
{
$sql='SELECT * FROM posts';
13 years ago
$column=$db->createCommand($sql)->queryColumn();
13 years ago
$this->assertEquals($column,range(1,5));
13 years ago
$command=$db->createCommand('SELECT id FROM posts WHERE id=10');
13 years ago
$this->assertEquals($command->queryColumn(),array());
13 years ago
$command=$db->createCommand('bad SQL');
13 years ago
$this->setExpectedException('CException');
$command->queryColumn();
}
function testQueryScalar()
{
$sql='SELECT * FROM posts';
13 years ago
$this->assertEquals($db->createCommand($sql)->queryScalar(),1);
13 years ago
$sql='SELECT id FROM posts';
13 years ago
$command=$db->createCommand($sql);
13 years ago
$command->prepare();
$this->assertEquals($command->queryScalar(),1);
13 years ago
$command=$db->createCommand('SELECT id FROM posts WHERE id=10');
13 years ago
$this->assertFalse($command->queryScalar());
13 years ago
$command=$db->createCommand('bad SQL');
13 years ago
$this->setExpectedException('CException');
$command->queryScalar();
}
function testFetchMode(){
$sql='SELECT * FROM posts';
13 years ago
$command=$db->createCommand($sql);
13 years ago
$result = $command->queryRow();
$this->assertTrue(is_array($result));
$sql='SELECT * FROM posts';
13 years ago
$command=$db->createCommand($sql);
13 years ago
$command->setFetchMode(PDO::FETCH_OBJ);
$result = $command->queryRow();
$this->assertTrue(is_object($result));
}
*/
}