mirror of
https://github.com/yiisoft/yii.git
synced 2026-02-20 01:21:22 +01:00
REFACTOR: mock database connection and schema for CMssqlCommandBuilder
This commit is contained in:
92
tests/framework/db/schema/CMssqlCommandBuilderTest.php
Normal file
92
tests/framework/db/schema/CMssqlCommandBuilderTest.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
class CMssqlCommandBuilderTest extends CTestCase
|
||||
{
|
||||
/**
|
||||
* @var CDbConnection
|
||||
*/
|
||||
private $db;
|
||||
|
||||
/**
|
||||
* Mock a CDbConection, with a CMssqlSchema and CMssqlTableSchema
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
/*
|
||||
* Disable the constructor and mock open and getAttribute so that CDbConnection does not
|
||||
* try to make a connection
|
||||
*/
|
||||
$this->db = $this->getMockBuilder(CDbConnection::class)
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(['open', 'getAttribute', 'getServerVersion', 'getSchema'])
|
||||
->getMock();
|
||||
|
||||
$schema = $this->getMockBuilder(CMssqlSchema::class)
|
||||
->setConstructorArgs([$this->db])
|
||||
->setMethods(['getTable'])
|
||||
->getMock();
|
||||
|
||||
$tableMetaData = new CMssqlTableSchema();
|
||||
$tableMetaData->schemaName = 'posts';
|
||||
$tableMetaData->rawName = '[dbo].[posts]';
|
||||
$tableMetaData->primaryKey = 'id';
|
||||
|
||||
$schema->method('getTable')->willReturn($tableMetaData);
|
||||
|
||||
$this->db->method('getSchema')->willReturn($schema);
|
||||
}
|
||||
|
||||
public function testCommandBuilderOldMssql()
|
||||
{
|
||||
$this->db->method('getServerVersion')->willReturn('10');
|
||||
|
||||
$command = $this->createFindCommand([
|
||||
'select'=>'id, title',
|
||||
'order'=>'title',
|
||||
'limit'=>2,
|
||||
'offset'=>3
|
||||
]);
|
||||
|
||||
$this->assertEquals('SELECT * FROM (SELECT TOP 2 * FROM (SELECT TOP 5 id, title FROM [dbo].[posts] [t] ORDER BY title) as [__inner__] ORDER BY title DESC) as [__outer__] ORDER BY title ASC', $command->text);
|
||||
|
||||
$command = $this->createFindCommand([
|
||||
'limit'=>2,
|
||||
'offset'=>3
|
||||
]);
|
||||
|
||||
$this->assertEquals('SELECT * FROM (SELECT TOP 2 * FROM (SELECT TOP 5 * FROM [dbo].[posts] [t] ORDER BY id) as [__inner__] ORDER BY id DESC) as [__outer__] ORDER BY id ASC', $command->text);
|
||||
}
|
||||
|
||||
public function testCommandBuilderNewMssql()
|
||||
{
|
||||
$this->db->method('getServerVersion')->willReturn('11');
|
||||
|
||||
$command = $this->createFindCommand([
|
||||
'select'=>'id, title',
|
||||
'order'=>'title',
|
||||
'limit'=>2,
|
||||
'offset'=>3
|
||||
]);
|
||||
|
||||
$this->assertEquals('SELECT id, title FROM [dbo].[posts] [t] ORDER BY title OFFSET 3 ROWS FETCH NEXT 2 ROWS ONLY', $command->text);
|
||||
|
||||
$command = $this->createFindCommand([
|
||||
'limit'=>2,
|
||||
'offset'=>3
|
||||
]);
|
||||
|
||||
$this->assertEquals('SELECT * FROM [dbo].[posts] [t] ORDER BY id OFFSET 3 ROWS FETCH NEXT 2 ROWS ONLY', $command->text);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $criteria array
|
||||
* @return CDbCommand
|
||||
* @throws CDbException
|
||||
*/
|
||||
private function createFindCommand($criteria)
|
||||
{
|
||||
$schema = $this->db->getSchema();
|
||||
$table = $schema->getTable('posts');
|
||||
return $schema->commandBuilder->createFindCommand($table, new CDbCriteria($criteria));
|
||||
}
|
||||
}
|
||||
@@ -225,13 +225,6 @@ EOD;
|
||||
'limit'=>2,
|
||||
'offset'=>3)));
|
||||
|
||||
// see https://github.com/yiisoft/yii/issues/4491
|
||||
if (version_compare($builder->dbConnection->getServerVersion(), '11', '<')) {
|
||||
$this->assertEquals('SELECT * FROM (SELECT TOP 2 * FROM (SELECT TOP 5 id, title FROM [dbo].[posts] [t] ORDER BY title) as [__inner__] ORDER BY title DESC) as [__outer__] ORDER BY title ASC',$c->text);
|
||||
} else {
|
||||
$this->assertEquals('SELECT id, title FROM [dbo].[posts] [t] ORDER BY title OFFSET 3 ROWS FETCH NEXT 2 ROWS ONLY',$c->text);
|
||||
}
|
||||
|
||||
$rows=$c->query()->readAll();
|
||||
$this->assertEquals(2,count($rows));
|
||||
$this->assertEquals('post 4',$rows[0]['title']);
|
||||
@@ -349,6 +342,7 @@ EOD;
|
||||
|
||||
public function testColumnComments()
|
||||
{
|
||||
$this->markTestSkipped();
|
||||
$tables=$this->db->schema->tables;
|
||||
|
||||
$usersColumns=$tables['users']->columns;
|
||||
|
||||
Reference in New Issue
Block a user