From 8ba032d1470cadffb48b20954f5bb8b64aaf4d10 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Mon, 20 Jun 2016 19:26:43 +0200 Subject: [PATCH] refactored database tests - changed test class naming for easier copying and searching of classes - made base tests abstract and made MySQL an equal citisen in the test hierarchie - added missing db specific test classes --- tests/framework/data/ActiveDataProviderTest.php | 2 +- tests/framework/db/ActiveRecordTest.php | 9 +- tests/framework/db/BatchQueryResultTest.php | 11 +- tests/framework/db/ColumnSchemaBuilderTest.php | 11 +- tests/framework/db/ColumnSchemaTest.php | 25 --- tests/framework/db/CommandTest.php | 6 +- tests/framework/db/ConnectionTest.php | 6 +- tests/framework/db/DatabaseTestCase.php | 11 +- tests/framework/db/QueryTest.php | 6 +- tests/framework/db/SchemaTest.php | 14 +- .../framework/db/cubrid/ActiveDataProviderTest.php | 13 ++ tests/framework/db/cubrid/ActiveRecordTest.php | 12 ++ tests/framework/db/cubrid/BatchQueryResultTest.php | 12 ++ .../db/cubrid/ColumnSchemaBuilderTest.php | 6 +- tests/framework/db/cubrid/CommandTest.php | 81 +++++++++ tests/framework/db/cubrid/ConnectionTest.php | 67 +++++++ .../db/cubrid/CubridActiveDataProviderTest.php | 14 -- .../framework/db/cubrid/CubridActiveRecordTest.php | 13 -- tests/framework/db/cubrid/CubridCommandTest.php | 82 --------- tests/framework/db/cubrid/CubridConnectionTest.php | 68 ------- .../framework/db/cubrid/CubridQueryBuilderTest.php | 24 --- tests/framework/db/cubrid/CubridQueryTest.php | 13 -- tests/framework/db/cubrid/CubridSchemaTest.php | 73 -------- tests/framework/db/cubrid/QueryBuilderTest.php | 23 +++ tests/framework/db/cubrid/QueryTest.php | 12 ++ tests/framework/db/cubrid/SchemaTest.php | 73 ++++++++ .../framework/db/mssql/ActiveDataProviderTest.php | 13 ++ tests/framework/db/mssql/ActiveRecordTest.php | 17 ++ tests/framework/db/mssql/BatchQueryResultTest.php | 12 ++ .../framework/db/mssql/ColumnSchemaBuilderTest.php | 24 +++ tests/framework/db/mssql/CommandTest.php | 93 ++++++++++ tests/framework/db/mssql/ConnectionTest.php | 67 +++++++ .../db/mssql/MssqlActiveDataProviderTest.php | 14 -- tests/framework/db/mssql/MssqlActiveRecordTest.php | 19 -- tests/framework/db/mssql/MssqlCommandTest.php | 94 ---------- tests/framework/db/mssql/MssqlConnectionTest.php | 69 ------- tests/framework/db/mssql/MssqlQueryBuilderTest.php | 93 ---------- tests/framework/db/mssql/MssqlQueryTest.php | 14 -- tests/framework/db/mssql/QueryBuilderTest.php | 92 ++++++++++ tests/framework/db/mssql/QueryTest.php | 12 ++ tests/framework/db/mssql/SchemaTest.php | 12 ++ .../framework/db/mysql/ActiveDataProviderTest.php | 13 ++ tests/framework/db/mysql/ActiveRecordTest.php | 12 ++ tests/framework/db/mysql/BatchQueryResultTest.php | 12 ++ .../framework/db/mysql/ColumnSchemaBuilderTest.php | 6 +- tests/framework/db/mysql/CommandTest.php | 12 ++ tests/framework/db/mysql/ConnectionTest.php | 12 ++ tests/framework/db/mysql/MysqlQueryBuilderTest.php | 58 ------ tests/framework/db/mysql/QueryBuilderTest.php | 59 ++++++ tests/framework/db/mysql/QueryTest.php | 12 ++ tests/framework/db/mysql/SchemaTest.php | 12 ++ tests/framework/db/oci/ActiveDataProviderTest.php | 13 ++ tests/framework/db/oci/ActiveRecordTest.php | 119 +++++++++++++ tests/framework/db/oci/BatchQueryResultTest.php | 12 ++ tests/framework/db/oci/ColumnSchemaBuilderTest.php | 6 +- tests/framework/db/oci/CommandTest.php | 33 ++++ tests/framework/db/oci/ConnectionTest.php | 12 ++ .../db/oci/OracleActiveDataProviderTest.php | 16 -- tests/framework/db/oci/OracleActiveRecordTest.php | 119 ------------- tests/framework/db/oci/OracleCommandTest.php | 33 ---- tests/framework/db/oci/OracleQueryBuilderTest.php | 57 ------ tests/framework/db/oci/OracleQueryTest.php | 25 --- tests/framework/db/oci/OracleSchemaTest.php | 97 ---------- tests/framework/db/oci/QueryBuilderTest.php | 56 ++++++ tests/framework/db/oci/QueryTest.php | 25 +++ tests/framework/db/oci/SchemaTest.php | 96 ++++++++++ .../framework/db/pgsql/ActiveDataProviderTest.php | 13 ++ tests/framework/db/pgsql/ActiveRecordTest.php | 197 ++++++++++++++++++++ tests/framework/db/pgsql/BatchQueryResultTest.php | 12 ++ .../framework/db/pgsql/ColumnSchemaBuilderTest.php | 24 +++ tests/framework/db/pgsql/CommandTest.php | 93 ++++++++++ tests/framework/db/pgsql/ConnectionTest.php | 99 +++++++++++ .../db/pgsql/PostgreSQLActiveDataProviderTest.php | 14 -- .../db/pgsql/PostgreSQLActiveRecordTest.php | 198 --------------------- tests/framework/db/pgsql/PostgreSQLCommandTest.php | 94 ---------- .../db/pgsql/PostgreSQLConnectionTest.php | 99 ----------- .../db/pgsql/PostgreSQLQueryBuilderTest.php | 124 ------------- tests/framework/db/pgsql/PostgreSQLQueryTest.php | 40 ----- tests/framework/db/pgsql/PostgreSQLSchemaTest.php | 159 ----------------- tests/framework/db/pgsql/QueryBuilderTest.php | 123 +++++++++++++ tests/framework/db/pgsql/QueryTest.php | 38 ++++ tests/framework/db/pgsql/SchemaTest.php | 158 ++++++++++++++++ .../framework/db/sqlite/ActiveDataProviderTest.php | 13 ++ tests/framework/db/sqlite/ActiveRecordTest.php | 12 ++ tests/framework/db/sqlite/BatchQueryResultTest.php | 12 ++ .../db/sqlite/ColumnSchemaBuilderTest.php | 6 +- tests/framework/db/sqlite/CommandTest.php | 21 +++ tests/framework/db/sqlite/ConnectionTest.php | 139 +++++++++++++++ tests/framework/db/sqlite/QueryBuilderTest.php | 104 +++++++++++ tests/framework/db/sqlite/QueryTest.php | 29 +++ tests/framework/db/sqlite/SchemaTest.php | 32 ++++ .../db/sqlite/SqliteActiveDataProviderTest.php | 14 -- .../framework/db/sqlite/SqliteActiveRecordTest.php | 13 -- tests/framework/db/sqlite/SqliteCommandTest.php | 22 --- tests/framework/db/sqlite/SqliteConnectionTest.php | 139 --------------- .../framework/db/sqlite/SqliteQueryBuilderTest.php | 105 ----------- tests/framework/db/sqlite/SqliteQueryTest.php | 30 ---- tests/framework/db/sqlite/SqliteSchemaTest.php | 33 ---- 98 files changed, 2308 insertions(+), 2160 deletions(-) delete mode 100644 tests/framework/db/ColumnSchemaTest.php create mode 100644 tests/framework/db/cubrid/ActiveDataProviderTest.php create mode 100644 tests/framework/db/cubrid/ActiveRecordTest.php create mode 100644 tests/framework/db/cubrid/BatchQueryResultTest.php create mode 100644 tests/framework/db/cubrid/CommandTest.php create mode 100644 tests/framework/db/cubrid/ConnectionTest.php delete mode 100644 tests/framework/db/cubrid/CubridActiveDataProviderTest.php delete mode 100644 tests/framework/db/cubrid/CubridActiveRecordTest.php delete mode 100644 tests/framework/db/cubrid/CubridCommandTest.php delete mode 100644 tests/framework/db/cubrid/CubridConnectionTest.php delete mode 100644 tests/framework/db/cubrid/CubridQueryBuilderTest.php delete mode 100644 tests/framework/db/cubrid/CubridQueryTest.php delete mode 100644 tests/framework/db/cubrid/CubridSchemaTest.php create mode 100644 tests/framework/db/cubrid/QueryBuilderTest.php create mode 100644 tests/framework/db/cubrid/QueryTest.php create mode 100644 tests/framework/db/cubrid/SchemaTest.php create mode 100644 tests/framework/db/mssql/ActiveDataProviderTest.php create mode 100644 tests/framework/db/mssql/ActiveRecordTest.php create mode 100644 tests/framework/db/mssql/BatchQueryResultTest.php create mode 100644 tests/framework/db/mssql/ColumnSchemaBuilderTest.php create mode 100644 tests/framework/db/mssql/CommandTest.php create mode 100644 tests/framework/db/mssql/ConnectionTest.php delete mode 100644 tests/framework/db/mssql/MssqlActiveDataProviderTest.php delete mode 100644 tests/framework/db/mssql/MssqlActiveRecordTest.php delete mode 100644 tests/framework/db/mssql/MssqlCommandTest.php delete mode 100644 tests/framework/db/mssql/MssqlConnectionTest.php delete mode 100644 tests/framework/db/mssql/MssqlQueryBuilderTest.php delete mode 100644 tests/framework/db/mssql/MssqlQueryTest.php create mode 100644 tests/framework/db/mssql/QueryBuilderTest.php create mode 100644 tests/framework/db/mssql/QueryTest.php create mode 100644 tests/framework/db/mssql/SchemaTest.php create mode 100644 tests/framework/db/mysql/ActiveDataProviderTest.php create mode 100644 tests/framework/db/mysql/ActiveRecordTest.php create mode 100644 tests/framework/db/mysql/BatchQueryResultTest.php create mode 100644 tests/framework/db/mysql/CommandTest.php create mode 100644 tests/framework/db/mysql/ConnectionTest.php delete mode 100644 tests/framework/db/mysql/MysqlQueryBuilderTest.php create mode 100644 tests/framework/db/mysql/QueryBuilderTest.php create mode 100644 tests/framework/db/mysql/QueryTest.php create mode 100644 tests/framework/db/mysql/SchemaTest.php create mode 100644 tests/framework/db/oci/ActiveDataProviderTest.php create mode 100644 tests/framework/db/oci/ActiveRecordTest.php create mode 100644 tests/framework/db/oci/BatchQueryResultTest.php create mode 100644 tests/framework/db/oci/CommandTest.php create mode 100644 tests/framework/db/oci/ConnectionTest.php delete mode 100644 tests/framework/db/oci/OracleActiveDataProviderTest.php delete mode 100644 tests/framework/db/oci/OracleActiveRecordTest.php delete mode 100644 tests/framework/db/oci/OracleCommandTest.php delete mode 100644 tests/framework/db/oci/OracleQueryBuilderTest.php delete mode 100644 tests/framework/db/oci/OracleQueryTest.php delete mode 100644 tests/framework/db/oci/OracleSchemaTest.php create mode 100644 tests/framework/db/oci/QueryBuilderTest.php create mode 100644 tests/framework/db/oci/QueryTest.php create mode 100644 tests/framework/db/oci/SchemaTest.php create mode 100644 tests/framework/db/pgsql/ActiveDataProviderTest.php create mode 100644 tests/framework/db/pgsql/ActiveRecordTest.php create mode 100644 tests/framework/db/pgsql/BatchQueryResultTest.php create mode 100644 tests/framework/db/pgsql/ColumnSchemaBuilderTest.php create mode 100644 tests/framework/db/pgsql/CommandTest.php create mode 100644 tests/framework/db/pgsql/ConnectionTest.php delete mode 100644 tests/framework/db/pgsql/PostgreSQLActiveDataProviderTest.php delete mode 100644 tests/framework/db/pgsql/PostgreSQLActiveRecordTest.php delete mode 100644 tests/framework/db/pgsql/PostgreSQLCommandTest.php delete mode 100644 tests/framework/db/pgsql/PostgreSQLConnectionTest.php delete mode 100644 tests/framework/db/pgsql/PostgreSQLQueryBuilderTest.php delete mode 100644 tests/framework/db/pgsql/PostgreSQLQueryTest.php delete mode 100644 tests/framework/db/pgsql/PostgreSQLSchemaTest.php create mode 100644 tests/framework/db/pgsql/QueryBuilderTest.php create mode 100644 tests/framework/db/pgsql/QueryTest.php create mode 100644 tests/framework/db/pgsql/SchemaTest.php create mode 100644 tests/framework/db/sqlite/ActiveDataProviderTest.php create mode 100644 tests/framework/db/sqlite/ActiveRecordTest.php create mode 100644 tests/framework/db/sqlite/BatchQueryResultTest.php create mode 100644 tests/framework/db/sqlite/CommandTest.php create mode 100644 tests/framework/db/sqlite/ConnectionTest.php create mode 100644 tests/framework/db/sqlite/QueryBuilderTest.php create mode 100644 tests/framework/db/sqlite/QueryTest.php create mode 100644 tests/framework/db/sqlite/SchemaTest.php delete mode 100644 tests/framework/db/sqlite/SqliteActiveDataProviderTest.php delete mode 100644 tests/framework/db/sqlite/SqliteActiveRecordTest.php delete mode 100644 tests/framework/db/sqlite/SqliteCommandTest.php delete mode 100644 tests/framework/db/sqlite/SqliteConnectionTest.php delete mode 100644 tests/framework/db/sqlite/SqliteQueryBuilderTest.php delete mode 100644 tests/framework/db/sqlite/SqliteQueryTest.php delete mode 100644 tests/framework/db/sqlite/SqliteSchemaTest.php diff --git a/tests/framework/data/ActiveDataProviderTest.php b/tests/framework/data/ActiveDataProviderTest.php index f62b012..94f52e8 100644 --- a/tests/framework/data/ActiveDataProviderTest.php +++ b/tests/framework/data/ActiveDataProviderTest.php @@ -22,7 +22,7 @@ use yiiunit\data\ar\Order; * @group data * @group db */ -class ActiveDataProviderTest extends DatabaseTestCase +abstract class ActiveDataProviderTest extends DatabaseTestCase { protected function setUp() { diff --git a/tests/framework/db/ActiveRecordTest.php b/tests/framework/db/ActiveRecordTest.php index 3c3ce4c..3db0e64 100644 --- a/tests/framework/db/ActiveRecordTest.php +++ b/tests/framework/db/ActiveRecordTest.php @@ -1,4 +1,5 @@ Schema::TYPE_CHAR]); - $this->assertSame('', $columnSchema->dbTypecast('')); - } -} diff --git a/tests/framework/db/CommandTest.php b/tests/framework/db/CommandTest.php index 585ec0f..aefd619 100644 --- a/tests/framework/db/CommandTest.php +++ b/tests/framework/db/CommandTest.php @@ -8,11 +8,7 @@ use yii\db\DataReader; use yii\db\Expression; use yii\db\Schema; -/** - * @group db - * @group mysql - */ -class CommandTest extends DatabaseTestCase +abstract class CommandTest extends DatabaseTestCase { public function testConstruct() { diff --git a/tests/framework/db/ConnectionTest.php b/tests/framework/db/ConnectionTest.php index 444fbd8..5bb6444 100644 --- a/tests/framework/db/ConnectionTest.php +++ b/tests/framework/db/ConnectionTest.php @@ -5,11 +5,7 @@ namespace yiiunit\framework\db; use yii\db\Connection; use yii\db\Transaction; -/** - * @group db - * @group mysql - */ -class ConnectionTest extends DatabaseTestCase +abstract class ConnectionTest extends DatabaseTestCase { public function testConstruct() diff --git a/tests/framework/db/DatabaseTestCase.php b/tests/framework/db/DatabaseTestCase.php index ec80f95..774371e 100644 --- a/tests/framework/db/DatabaseTestCase.php +++ b/tests/framework/db/DatabaseTestCase.php @@ -1,4 +1,5 @@ driverName === null) { + throw new \Exception('driverName is not set for a DatabaseTestCase.'); + } + parent::setUp(); $databases = self::getParam('databases'); $this->database = $databases[$this->driverName]; diff --git a/tests/framework/db/QueryTest.php b/tests/framework/db/QueryTest.php index 660ce09..020c490 100644 --- a/tests/framework/db/QueryTest.php +++ b/tests/framework/db/QueryTest.php @@ -5,11 +5,7 @@ namespace yiiunit\framework\db; use yii\db\Expression; use yii\db\Query; -/** - * @group db - * @group mysql - */ -class QueryTest extends DatabaseTestCase +abstract class QueryTest extends DatabaseTestCase { public function testSelect() { diff --git a/tests/framework/db/SchemaTest.php b/tests/framework/db/SchemaTest.php index 4fa1352..a2dd590 100644 --- a/tests/framework/db/SchemaTest.php +++ b/tests/framework/db/SchemaTest.php @@ -4,14 +4,11 @@ namespace yiiunit\framework\db; use PDO; use yii\caching\FileCache; +use yii\db\ColumnSchema; use yii\db\Expression; use yii\db\Schema; -/** - * @group db - * @group mysql - */ -class SchemaTest extends DatabaseTestCase +abstract class SchemaTest extends DatabaseTestCase { public function pdoAttributesProvider() { @@ -373,4 +370,11 @@ class SchemaTest extends DatabaseTestCase } } } + + public function testColumnSchemaDbTypecastWithEmptyCharType() + { + $columnSchema = new ColumnSchema(['type' => Schema::TYPE_CHAR]); + $this->assertSame('', $columnSchema->dbTypecast('')); + } + } diff --git a/tests/framework/db/cubrid/ActiveDataProviderTest.php b/tests/framework/db/cubrid/ActiveDataProviderTest.php new file mode 100644 index 0000000..8a36d67 --- /dev/null +++ b/tests/framework/db/cubrid/ActiveDataProviderTest.php @@ -0,0 +1,13 @@ +getConnection(); + + // bindParam + $sql = 'INSERT INTO customer(email, name, address) VALUES (:email, :name, :address)'; + $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(); + + $sql = 'SELECT name FROM customer WHERE email=:email'; + $command = $db->createCommand($sql); + $command->bindParam(':email', $email); + $this->assertEquals($name, $command->queryScalar()); + + $sql = "INSERT INTO type (int_col, char_col, char_col2, enum_col, float_col, blob_col, numeric_col, bool_col) VALUES (:int_col, '', :char_col, :enum_col, :float_col, CHAR_TO_BLOB(:blob_col), :numeric_col, :bool_col)"; + $command = $db->createCommand($sql); + $intCol = 123; + $charCol = 'abc'; + $enumCol = 'a'; + $floatCol = 1.23; + $blobCol = "\x10\x11\x12"; + $numericCol = '1.23'; + $boolCol = true; + $command->bindParam(':int_col', $intCol); + $command->bindParam(':char_col', $charCol); + $command->bindParam(':enum_col', $enumCol); + $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 type'; + $row = $db->createCommand($sql)->queryOne(); + $this->assertEquals($intCol, $row['int_col']); + $this->assertEquals($enumCol, $row['enum_col']); + $this->assertEquals($charCol, $row['char_col2']); + $this->assertEquals($floatCol, $row['float_col']); + $this->assertEquals($blobCol, fread($row['blob_col'], 3)); + $this->assertEquals($numericCol, $row['numeric_col']); + $this->assertEquals($boolCol, $row['bool_col']); + + // bindValue + $sql = 'INSERT INTO customer(email, name, address) VALUES (:email, \'user5\', \'address5\')'; + $command = $db->createCommand($sql); + $command->bindValue(':email', 'user5@example.com'); + $command->execute(); + + $sql = 'SELECT email FROM customer WHERE name=:name'; + $command = $db->createCommand($sql); + $command->bindValue(':name', 'user5'); + $this->assertEquals('user5@example.com', $command->queryScalar()); + } + + public function testAutoQuoting() + { + $db = $this->getConnection(false); + + $sql = 'SELECT [[id]], [[t.name]] FROM {{customer}} t'; + $command = $db->createCommand($sql); + $this->assertEquals('SELECT "id", "t"."name" FROM "customer" t', $command->sql); + } +} diff --git a/tests/framework/db/cubrid/ConnectionTest.php b/tests/framework/db/cubrid/ConnectionTest.php new file mode 100644 index 0000000..dd930d1 --- /dev/null +++ b/tests/framework/db/cubrid/ConnectionTest.php @@ -0,0 +1,67 @@ +getConnection(false); + $this->assertEquals(123, $connection->quoteValue(123)); + $this->assertEquals("'string'", $connection->quoteValue('string')); + $this->assertEquals("'It''s interesting'", $connection->quoteValue("It's interesting")); + } + + public function testQuoteTableName() + { + $connection = $this->getConnection(false); + $this->assertEquals('"table"', $connection->quoteTableName('table')); + $this->assertEquals('"table"', $connection->quoteTableName('"table"')); + $this->assertEquals('"schema"."table"', $connection->quoteTableName('schema.table')); + $this->assertEquals('"schema"."table"', $connection->quoteTableName('schema."table"')); + $this->assertEquals('"schema"."table"', $connection->quoteTableName('"schema"."table"')); + $this->assertEquals('{{table}}', $connection->quoteTableName('{{table}}')); + $this->assertEquals('(table)', $connection->quoteTableName('(table)')); + } + + public function testQuoteColumnName() + { + $connection = $this->getConnection(false); + $this->assertEquals('"column"', $connection->quoteColumnName('column')); + $this->assertEquals('"column"', $connection->quoteColumnName('"column"')); + $this->assertEquals('[[column]]', $connection->quoteColumnName('[[column]]')); + $this->assertEquals('{{column}}', $connection->quoteColumnName('{{column}}')); + $this->assertEquals('(column)', $connection->quoteColumnName('(column)')); + + $this->assertEquals('"column"', $connection->quoteSql('[[column]]')); + $this->assertEquals('"column"', $connection->quoteSql('{{column}}')); + } + + public function testQuoteFullColumnName() + { + $connection = $this->getConnection(false, false); + $this->assertEquals('"table"."column"', $connection->quoteColumnName('table.column')); + $this->assertEquals('"table"."column"', $connection->quoteColumnName('table."column"')); + $this->assertEquals('"table"."column"', $connection->quoteColumnName('"table".column')); + $this->assertEquals('"table"."column"', $connection->quoteColumnName('"table"."column"')); + + $this->assertEquals('[[table.column]]', $connection->quoteColumnName('[[table.column]]')); + $this->assertEquals('{{table}}."column"', $connection->quoteColumnName('{{table}}.column')); + $this->assertEquals('{{table}}."column"', $connection->quoteColumnName('{{table}}."column"')); + $this->assertEquals('{{table}}.[[column]]', $connection->quoteColumnName('{{table}}.[[column]]')); + $this->assertEquals('{{%table}}."column"', $connection->quoteColumnName('{{%table}}.column')); + $this->assertEquals('{{%table}}."column"', $connection->quoteColumnName('{{%table}}."column"')); + + $this->assertEquals('"table"."column"', $connection->quoteSql('[[table.column]]')); + $this->assertEquals('"table"."column"', $connection->quoteSql('{{table}}.[[column]]')); + $this->assertEquals('"table"."column"', $connection->quoteSql('{{table}}."column"')); + $this->assertEquals('"table"."column"', $connection->quoteSql('{{%table}}.[[column]]')); + $this->assertEquals('"table"."column"', $connection->quoteSql('{{%table}}."column"')); + } +} diff --git a/tests/framework/db/cubrid/CubridActiveDataProviderTest.php b/tests/framework/db/cubrid/CubridActiveDataProviderTest.php deleted file mode 100644 index f01e69f..0000000 --- a/tests/framework/db/cubrid/CubridActiveDataProviderTest.php +++ /dev/null @@ -1,14 +0,0 @@ -getConnection(); - - // bindParam - $sql = 'INSERT INTO customer(email, name, address) VALUES (:email, :name, :address)'; - $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(); - - $sql = 'SELECT name FROM customer WHERE email=:email'; - $command = $db->createCommand($sql); - $command->bindParam(':email', $email); - $this->assertEquals($name, $command->queryScalar()); - - $sql = "INSERT INTO type (int_col, char_col, char_col2, enum_col, float_col, blob_col, numeric_col, bool_col) VALUES (:int_col, '', :char_col, :enum_col, :float_col, CHAR_TO_BLOB(:blob_col), :numeric_col, :bool_col)"; - $command = $db->createCommand($sql); - $intCol = 123; - $charCol = 'abc'; - $enumCol = 'a'; - $floatCol = 1.23; - $blobCol = "\x10\x11\x12"; - $numericCol = '1.23'; - $boolCol = true; - $command->bindParam(':int_col', $intCol); - $command->bindParam(':char_col', $charCol); - $command->bindParam(':enum_col', $enumCol); - $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 type'; - $row = $db->createCommand($sql)->queryOne(); - $this->assertEquals($intCol, $row['int_col']); - $this->assertEquals($enumCol, $row['enum_col']); - $this->assertEquals($charCol, $row['char_col2']); - $this->assertEquals($floatCol, $row['float_col']); - $this->assertEquals($blobCol, fread($row['blob_col'], 3)); - $this->assertEquals($numericCol, $row['numeric_col']); - $this->assertEquals($boolCol, $row['bool_col']); - - // bindValue - $sql = 'INSERT INTO customer(email, name, address) VALUES (:email, \'user5\', \'address5\')'; - $command = $db->createCommand($sql); - $command->bindValue(':email', 'user5@example.com'); - $command->execute(); - - $sql = 'SELECT email FROM customer WHERE name=:name'; - $command = $db->createCommand($sql); - $command->bindValue(':name', 'user5'); - $this->assertEquals('user5@example.com', $command->queryScalar()); - } - - public function testAutoQuoting() - { - $db = $this->getConnection(false); - - $sql = 'SELECT [[id]], [[t.name]] FROM {{customer}} t'; - $command = $db->createCommand($sql); - $this->assertEquals('SELECT "id", "t"."name" FROM "customer" t', $command->sql); - } -} diff --git a/tests/framework/db/cubrid/CubridConnectionTest.php b/tests/framework/db/cubrid/CubridConnectionTest.php deleted file mode 100644 index 7dbd0f3..0000000 --- a/tests/framework/db/cubrid/CubridConnectionTest.php +++ /dev/null @@ -1,68 +0,0 @@ -getConnection(false); - $this->assertEquals(123, $connection->quoteValue(123)); - $this->assertEquals("'string'", $connection->quoteValue('string')); - $this->assertEquals("'It''s interesting'", $connection->quoteValue("It's interesting")); - } - - public function testQuoteTableName() - { - $connection = $this->getConnection(false); - $this->assertEquals('"table"', $connection->quoteTableName('table')); - $this->assertEquals('"table"', $connection->quoteTableName('"table"')); - $this->assertEquals('"schema"."table"', $connection->quoteTableName('schema.table')); - $this->assertEquals('"schema"."table"', $connection->quoteTableName('schema."table"')); - $this->assertEquals('"schema"."table"', $connection->quoteTableName('"schema"."table"')); - $this->assertEquals('{{table}}', $connection->quoteTableName('{{table}}')); - $this->assertEquals('(table)', $connection->quoteTableName('(table)')); - } - - public function testQuoteColumnName() - { - $connection = $this->getConnection(false); - $this->assertEquals('"column"', $connection->quoteColumnName('column')); - $this->assertEquals('"column"', $connection->quoteColumnName('"column"')); - $this->assertEquals('[[column]]', $connection->quoteColumnName('[[column]]')); - $this->assertEquals('{{column}}', $connection->quoteColumnName('{{column}}')); - $this->assertEquals('(column)', $connection->quoteColumnName('(column)')); - - $this->assertEquals('"column"', $connection->quoteSql('[[column]]')); - $this->assertEquals('"column"', $connection->quoteSql('{{column}}')); - } - - public function testQuoteFullColumnName() - { - $connection = $this->getConnection(false, false); - $this->assertEquals('"table"."column"', $connection->quoteColumnName('table.column')); - $this->assertEquals('"table"."column"', $connection->quoteColumnName('table."column"')); - $this->assertEquals('"table"."column"', $connection->quoteColumnName('"table".column')); - $this->assertEquals('"table"."column"', $connection->quoteColumnName('"table"."column"')); - - $this->assertEquals('[[table.column]]', $connection->quoteColumnName('[[table.column]]')); - $this->assertEquals('{{table}}."column"', $connection->quoteColumnName('{{table}}.column')); - $this->assertEquals('{{table}}."column"', $connection->quoteColumnName('{{table}}."column"')); - $this->assertEquals('{{table}}.[[column]]', $connection->quoteColumnName('{{table}}.[[column]]')); - $this->assertEquals('{{%table}}."column"', $connection->quoteColumnName('{{%table}}.column')); - $this->assertEquals('{{%table}}."column"', $connection->quoteColumnName('{{%table}}."column"')); - - $this->assertEquals('"table"."column"', $connection->quoteSql('[[table.column]]')); - $this->assertEquals('"table"."column"', $connection->quoteSql('{{table}}.[[column]]')); - $this->assertEquals('"table"."column"', $connection->quoteSql('{{table}}."column"')); - $this->assertEquals('"table"."column"', $connection->quoteSql('{{%table}}.[[column]]')); - $this->assertEquals('"table"."column"', $connection->quoteSql('{{%table}}."column"')); - } -} diff --git a/tests/framework/db/cubrid/CubridQueryBuilderTest.php b/tests/framework/db/cubrid/CubridQueryBuilderTest.php deleted file mode 100644 index 1211fad..0000000 --- a/tests/framework/db/cubrid/CubridQueryBuilderTest.php +++ /dev/null @@ -1,24 +0,0 @@ -getConnection()->schema; - - foreach ($values as $value) { - $this->assertEquals($value[1], $schema->getPdoType($value[0])); - } - fclose($fp); - } - - - public function getExpectedColumns() - { - $columns = parent::getExpectedColumns(); - $columns['int_col']['dbType'] = 'integer'; - $columns['int_col']['size'] = null; - $columns['int_col']['precision'] = null; - $columns['int_col2']['dbType'] = 'integer'; - $columns['int_col2']['size'] = null; - $columns['int_col2']['precision'] = null; - $columns['smallint_col']['dbType'] = 'short'; - $columns['smallint_col']['size'] = null; - $columns['smallint_col']['precision'] = null; - $columns['char_col3']['type'] = 'string'; - $columns['char_col3']['dbType'] = 'varchar(1073741823)'; - $columns['char_col3']['size'] = 1073741823; - $columns['char_col3']['precision'] = 1073741823; - $columns['enum_col']['dbType'] = "enum('a', 'B')"; - $columns['float_col']['dbType'] = 'double'; - $columns['float_col']['size'] = null; - $columns['float_col']['precision'] = null; - $columns['float_col']['scale'] = null; - $columns['numeric_col']['dbType'] = 'numeric(5,2)'; - $columns['blob_col']['phpType'] = 'resource'; - $columns['blob_col']['type'] = 'binary'; - $columns['bool_col']['dbType'] = 'short'; - $columns['bool_col']['size'] = null; - $columns['bool_col']['precision'] = null; - $columns['bool_col2']['dbType'] = 'short'; - $columns['bool_col2']['size'] = null; - $columns['bool_col2']['precision'] = null; - $columns['time']['defaultValue'] = '12:00:00 AM 01/01/2002'; - $columns['ts_default']['defaultValue'] = new Expression('SYS_TIMESTAMP'); - return $columns; - } -} diff --git a/tests/framework/db/cubrid/QueryBuilderTest.php b/tests/framework/db/cubrid/QueryBuilderTest.php new file mode 100644 index 0000000..fbd8866 --- /dev/null +++ b/tests/framework/db/cubrid/QueryBuilderTest.php @@ -0,0 +1,23 @@ +getConnection()->schema; + + foreach ($values as $value) { + $this->assertEquals($value[1], $schema->getPdoType($value[0])); + } + fclose($fp); + } + + + public function getExpectedColumns() + { + $columns = parent::getExpectedColumns(); + $columns['int_col']['dbType'] = 'integer'; + $columns['int_col']['size'] = null; + $columns['int_col']['precision'] = null; + $columns['int_col2']['dbType'] = 'integer'; + $columns['int_col2']['size'] = null; + $columns['int_col2']['precision'] = null; + $columns['smallint_col']['dbType'] = 'short'; + $columns['smallint_col']['size'] = null; + $columns['smallint_col']['precision'] = null; + $columns['char_col3']['type'] = 'string'; + $columns['char_col3']['dbType'] = 'varchar(1073741823)'; + $columns['char_col3']['size'] = 1073741823; + $columns['char_col3']['precision'] = 1073741823; + $columns['enum_col']['dbType'] = "enum('a', 'B')"; + $columns['float_col']['dbType'] = 'double'; + $columns['float_col']['size'] = null; + $columns['float_col']['precision'] = null; + $columns['float_col']['scale'] = null; + $columns['numeric_col']['dbType'] = 'numeric(5,2)'; + $columns['blob_col']['phpType'] = 'resource'; + $columns['blob_col']['type'] = 'binary'; + $columns['bool_col']['dbType'] = 'short'; + $columns['bool_col']['size'] = null; + $columns['bool_col']['precision'] = null; + $columns['bool_col2']['dbType'] = 'short'; + $columns['bool_col2']['size'] = null; + $columns['bool_col2']['precision'] = null; + $columns['time']['defaultValue'] = '12:00:00 AM 01/01/2002'; + $columns['ts_default']['defaultValue'] = new Expression('SYS_TIMESTAMP'); + return $columns; + } +} diff --git a/tests/framework/db/mssql/ActiveDataProviderTest.php b/tests/framework/db/mssql/ActiveDataProviderTest.php new file mode 100644 index 0000000..2c84e8c --- /dev/null +++ b/tests/framework/db/mssql/ActiveDataProviderTest.php @@ -0,0 +1,13 @@ +markTestSkipped('MSSQL does not support explicit value for an IDENTITY column.'); + } +} diff --git a/tests/framework/db/mssql/BatchQueryResultTest.php b/tests/framework/db/mssql/BatchQueryResultTest.php new file mode 100644 index 0000000..375d413 --- /dev/null +++ b/tests/framework/db/mssql/BatchQueryResultTest.php @@ -0,0 +1,12 @@ +getConnection(false); + + $sql = 'SELECT [[id]], [[t.name]] FROM {{customer}} t'; + $command = $db->createCommand($sql); + $this->assertEquals("SELECT [id], [t].[name] FROM [customer] t", $command->sql); + } + + public function testPrepareCancel() + { + $this->markTestSkipped('MSSQL driver does not support this feature.'); + } + + public function testBindParamValue() + { + $db = $this->getConnection(); + + // bindParam + $sql = 'INSERT INTO customer(email, name, address) VALUES (:email, :name, :address)'; + $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(); + + $sql = 'SELECT name FROM customer WHERE email=:email'; + $command = $db->createCommand($sql); + $command->bindParam(':email', $email); + $this->assertEquals($name, $command->queryScalar()); + + $sql = 'INSERT INTO type (int_col, char_col, float_col, blob_col, numeric_col, bool_col) VALUES (:int_col, :char_col, :float_col, CONVERT([varbinary], :blob_col), :numeric_col, :bool_col)'; + $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()); + + $sql = 'SELECT int_col, char_col, float_col, CONVERT([nvarchar], blob_col) AS blob_col, numeric_col FROM type'; + $row = $db->createCommand($sql)->queryOne(); + $this->assertEquals($intCol, $row['int_col']); + $this->assertEquals($charCol, trim($row['char_col'])); + $this->assertEquals($floatCol, $row['float_col']); + $this->assertEquals($blobCol, $row['blob_col']); + $this->assertEquals($numericCol, $row['numeric_col']); + + // bindValue + $sql = 'INSERT INTO customer(email, name, address) VALUES (:email, \'user5\', \'address5\')'; + $command = $db->createCommand($sql); + $command->bindValue(':email', 'user5@example.com'); + $command->execute(); + + $sql = 'SELECT email FROM customer WHERE name=:name'; + $command = $db->createCommand($sql); + $command->bindValue(':name', 'user5'); + $this->assertEquals('user5@example.com', $command->queryScalar()); + } + + public function paramsNonWhereProvider() + { + return[ + ['SELECT SUBSTRING(name, :len, 6) AS name FROM {{customer}} WHERE [[email]] = :email GROUP BY name'], + ['SELECT SUBSTRING(name, :len, 6) as name FROM {{customer}} WHERE [[email]] = :email ORDER BY name'], + ['SELECT SUBSTRING(name, :len, 6) FROM {{customer}} WHERE [[email]] = :email'], + ]; + } +} diff --git a/tests/framework/db/mssql/ConnectionTest.php b/tests/framework/db/mssql/ConnectionTest.php new file mode 100644 index 0000000..3658b4c --- /dev/null +++ b/tests/framework/db/mssql/ConnectionTest.php @@ -0,0 +1,67 @@ +getConnection(false); + $this->assertEquals(123, $connection->quoteValue(123)); + $this->assertEquals("'string'", $connection->quoteValue('string')); + $this->assertEquals("'It''s interesting'", $connection->quoteValue("It's interesting")); + } + + public function testQuoteTableName() + { + $connection = $this->getConnection(false); + $this->assertEquals('[table]', $connection->quoteTableName('table')); + $this->assertEquals('[table]', $connection->quoteTableName('[table]')); + $this->assertEquals('[schema].[table]', $connection->quoteTableName('schema.table')); + $this->assertEquals('[schema].[table]', $connection->quoteTableName('schema.[table]')); + $this->assertEquals('[schema].[table]', $connection->quoteTableName('[schema].[table]')); + $this->assertEquals('{{table}}', $connection->quoteTableName('{{table}}')); + $this->assertEquals('(table)', $connection->quoteTableName('(table)')); + } + + public function testQuoteColumnName() + { + $connection = $this->getConnection(false); + $this->assertEquals('[column]', $connection->quoteColumnName('column')); + $this->assertEquals('[column]', $connection->quoteColumnName('[column]')); + $this->assertEquals('[[column]]', $connection->quoteColumnName('[[column]]')); + $this->assertEquals('{{column}}', $connection->quoteColumnName('{{column}}')); + $this->assertEquals('(column)', $connection->quoteColumnName('(column)')); + + $this->assertEquals('[column]', $connection->quoteSql('[[column]]')); + $this->assertEquals('[column]', $connection->quoteSql('{{column}}')); + } + + public function testQuoteFullColumnName() + { + $connection = $this->getConnection(false, false); + $this->assertEquals('[table].[column]', $connection->quoteColumnName('table.column')); + $this->assertEquals('[table].[column]', $connection->quoteColumnName('table.[column]')); + $this->assertEquals('[table].[column]', $connection->quoteColumnName('[table].column')); + $this->assertEquals('[table].[column]', $connection->quoteColumnName('[table].[column]')); + + $this->assertEquals('[[table.column]]', $connection->quoteColumnName('[[table.column]]')); + $this->assertEquals('{{table}}.[column]', $connection->quoteColumnName('{{table}}.column')); + $this->assertEquals('{{table}}.[column]', $connection->quoteColumnName('{{table}}.[column]')); + $this->assertEquals('{{table}}.[[column]]', $connection->quoteColumnName('{{table}}.[[column]]')); + $this->assertEquals('{{%table}}.[column]', $connection->quoteColumnName('{{%table}}.column')); + $this->assertEquals('{{%table}}.[column]', $connection->quoteColumnName('{{%table}}.[column]')); + + $this->assertEquals('[table].[column]', $connection->quoteSql('[[table.column]]')); + $this->assertEquals('[table].[column]', $connection->quoteSql('{{table}}.[[column]]')); + $this->assertEquals('[table].[column]', $connection->quoteSql('{{table}}.[column]')); + $this->assertEquals('[table].[column]', $connection->quoteSql('{{%table}}.[[column]]')); + $this->assertEquals('[table].[column]', $connection->quoteSql('{{%table}}.[column]')); + } +} diff --git a/tests/framework/db/mssql/MssqlActiveDataProviderTest.php b/tests/framework/db/mssql/MssqlActiveDataProviderTest.php deleted file mode 100644 index e9f4994..0000000 --- a/tests/framework/db/mssql/MssqlActiveDataProviderTest.php +++ /dev/null @@ -1,14 +0,0 @@ -markTestSkipped('MSSQL does not support explicit value for an IDENTITY column.'); - } -} diff --git a/tests/framework/db/mssql/MssqlCommandTest.php b/tests/framework/db/mssql/MssqlCommandTest.php deleted file mode 100644 index a889f4a..0000000 --- a/tests/framework/db/mssql/MssqlCommandTest.php +++ /dev/null @@ -1,94 +0,0 @@ -getConnection(false); - - $sql = 'SELECT [[id]], [[t.name]] FROM {{customer}} t'; - $command = $db->createCommand($sql); - $this->assertEquals("SELECT [id], [t].[name] FROM [customer] t", $command->sql); - } - - public function testPrepareCancel() - { - $this->markTestSkipped('MSSQL driver does not support this feature.'); - } - - public function testBindParamValue() - { - $db = $this->getConnection(); - - // bindParam - $sql = 'INSERT INTO customer(email, name, address) VALUES (:email, :name, :address)'; - $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(); - - $sql = 'SELECT name FROM customer WHERE email=:email'; - $command = $db->createCommand($sql); - $command->bindParam(':email', $email); - $this->assertEquals($name, $command->queryScalar()); - - $sql = 'INSERT INTO type (int_col, char_col, float_col, blob_col, numeric_col, bool_col) VALUES (:int_col, :char_col, :float_col, CONVERT([varbinary], :blob_col), :numeric_col, :bool_col)'; - $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()); - - $sql = 'SELECT int_col, char_col, float_col, CONVERT([nvarchar], blob_col) AS blob_col, numeric_col FROM type'; - $row = $db->createCommand($sql)->queryOne(); - $this->assertEquals($intCol, $row['int_col']); - $this->assertEquals($charCol, trim($row['char_col'])); - $this->assertEquals($floatCol, $row['float_col']); - $this->assertEquals($blobCol, $row['blob_col']); - $this->assertEquals($numericCol, $row['numeric_col']); - - // bindValue - $sql = 'INSERT INTO customer(email, name, address) VALUES (:email, \'user5\', \'address5\')'; - $command = $db->createCommand($sql); - $command->bindValue(':email', 'user5@example.com'); - $command->execute(); - - $sql = 'SELECT email FROM customer WHERE name=:name'; - $command = $db->createCommand($sql); - $command->bindValue(':name', 'user5'); - $this->assertEquals('user5@example.com', $command->queryScalar()); - } - - public function paramsNonWhereProvider() - { - return[ - ['SELECT SUBSTRING(name, :len, 6) AS name FROM {{customer}} WHERE [[email]] = :email GROUP BY name'], - ['SELECT SUBSTRING(name, :len, 6) as name FROM {{customer}} WHERE [[email]] = :email ORDER BY name'], - ['SELECT SUBSTRING(name, :len, 6) FROM {{customer}} WHERE [[email]] = :email'], - ]; - } -} diff --git a/tests/framework/db/mssql/MssqlConnectionTest.php b/tests/framework/db/mssql/MssqlConnectionTest.php deleted file mode 100644 index 8a3a214..0000000 --- a/tests/framework/db/mssql/MssqlConnectionTest.php +++ /dev/null @@ -1,69 +0,0 @@ -getConnection(false); - $this->assertEquals(123, $connection->quoteValue(123)); - $this->assertEquals("'string'", $connection->quoteValue('string')); - $this->assertEquals("'It''s interesting'", $connection->quoteValue("It's interesting")); - } - - public function testQuoteTableName() - { - $connection = $this->getConnection(false); - $this->assertEquals('[table]', $connection->quoteTableName('table')); - $this->assertEquals('[table]', $connection->quoteTableName('[table]')); - $this->assertEquals('[schema].[table]', $connection->quoteTableName('schema.table')); - $this->assertEquals('[schema].[table]', $connection->quoteTableName('schema.[table]')); - $this->assertEquals('[schema].[table]', $connection->quoteTableName('[schema].[table]')); - $this->assertEquals('{{table}}', $connection->quoteTableName('{{table}}')); - $this->assertEquals('(table)', $connection->quoteTableName('(table)')); - } - - public function testQuoteColumnName() - { - $connection = $this->getConnection(false); - $this->assertEquals('[column]', $connection->quoteColumnName('column')); - $this->assertEquals('[column]', $connection->quoteColumnName('[column]')); - $this->assertEquals('[[column]]', $connection->quoteColumnName('[[column]]')); - $this->assertEquals('{{column}}', $connection->quoteColumnName('{{column}}')); - $this->assertEquals('(column)', $connection->quoteColumnName('(column)')); - - $this->assertEquals('[column]', $connection->quoteSql('[[column]]')); - $this->assertEquals('[column]', $connection->quoteSql('{{column}}')); - } - - public function testQuoteFullColumnName() - { - $connection = $this->getConnection(false, false); - $this->assertEquals('[table].[column]', $connection->quoteColumnName('table.column')); - $this->assertEquals('[table].[column]', $connection->quoteColumnName('table.[column]')); - $this->assertEquals('[table].[column]', $connection->quoteColumnName('[table].column')); - $this->assertEquals('[table].[column]', $connection->quoteColumnName('[table].[column]')); - - $this->assertEquals('[[table.column]]', $connection->quoteColumnName('[[table.column]]')); - $this->assertEquals('{{table}}.[column]', $connection->quoteColumnName('{{table}}.column')); - $this->assertEquals('{{table}}.[column]', $connection->quoteColumnName('{{table}}.[column]')); - $this->assertEquals('{{table}}.[[column]]', $connection->quoteColumnName('{{table}}.[[column]]')); - $this->assertEquals('{{%table}}.[column]', $connection->quoteColumnName('{{%table}}.column')); - $this->assertEquals('{{%table}}.[column]', $connection->quoteColumnName('{{%table}}.[column]')); - - $this->assertEquals('[table].[column]', $connection->quoteSql('[[table.column]]')); - $this->assertEquals('[table].[column]', $connection->quoteSql('{{table}}.[[column]]')); - $this->assertEquals('[table].[column]', $connection->quoteSql('{{table}}.[column]')); - $this->assertEquals('[table].[column]', $connection->quoteSql('{{%table}}.[[column]]')); - $this->assertEquals('[table].[column]', $connection->quoteSql('{{%table}}.[column]')); - } -} diff --git a/tests/framework/db/mssql/MssqlQueryBuilderTest.php b/tests/framework/db/mssql/MssqlQueryBuilderTest.php deleted file mode 100644 index 404079e..0000000 --- a/tests/framework/db/mssql/MssqlQueryBuilderTest.php +++ /dev/null @@ -1,93 +0,0 @@ -select('id')->from('example')->limit(10)->offset(5); - - list($actualQuerySql, $actualQueryParams) = $this->getQueryBuilder()->build($query); - - $this->assertEquals($expectedQuerySql, $actualQuerySql); - $this->assertEquals($expectedQueryParams, $actualQueryParams); - } - - public function testLimit() - { - $expectedQuerySql = 'SELECT [id] FROM [example] ORDER BY (SELECT NULL) OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY'; - $expectedQueryParams = []; - - $query = new Query(); - $query->select('id')->from('example')->limit(10); - - list($actualQuerySql, $actualQueryParams) = $this->getQueryBuilder()->build($query); - - $this->assertEquals($expectedQuerySql, $actualQuerySql); - $this->assertEquals($expectedQueryParams, $actualQueryParams); - } - - public function testOffset() - { - $expectedQuerySql = 'SELECT [id] FROM [example] ORDER BY (SELECT NULL) OFFSET 10 ROWS'; - $expectedQueryParams = []; - - $query = new Query(); - $query->select('id')->from('example')->offset(10); - - list($actualQuerySql, $actualQueryParams) = $this->getQueryBuilder()->build($query); - - $this->assertEquals($expectedQuerySql, $actualQuerySql); - $this->assertEquals($expectedQueryParams, $actualQueryParams); - } - - public function testCommentColumn() - { - $qb = $this->getQueryBuilder(); - - $expected = "sp_updateextendedproperty @name = N'MS_Description', @value = 'This is my column.', @level1type = N'Table', @level1name = comment, @level2type = N'Column', @level2name = text"; - $sql = $qb->addCommentOnColumn('comment', 'text', 'This is my column.'); - $this->assertEquals($expected, $sql); - - $expected = "sp_dropextendedproperty @name = N'MS_Description', @level1type = N'Table', @level1name = comment, @level2type = N'Column', @level2name = text"; - $sql = $qb->dropCommentFromColumn('comment', 'text'); - $this->assertEquals($expected, $sql); - } - - public function testCommentTable() - { - $qb = $this->getQueryBuilder(); - - $expected = "sp_updateextendedproperty @name = N'MS_Description', @value = 'This is my table.', @level1type = N'Table', @level1name = comment"; - $sql = $qb->addCommentOnTable('comment', 'This is my table.'); - $this->assertEquals($expected, $sql); - - $expected = "sp_dropextendedproperty @name = N'MS_Description', @level1type = N'Table', @level1name = comment"; - $sql = $qb->dropCommentFromTable('comment'); - $this->assertEquals($expected, $sql); - } - - /** - * this is not used as a dataprovider for testGetColumnType to speed up the test - * when used as dataprovider every single line will cause a reconnect with the database which is not needed here - */ - public function columnTypes() - { - return array_merge(parent::columnTypes(), []); - } -} diff --git a/tests/framework/db/mssql/MssqlQueryTest.php b/tests/framework/db/mssql/MssqlQueryTest.php deleted file mode 100644 index 89830d0..0000000 --- a/tests/framework/db/mssql/MssqlQueryTest.php +++ /dev/null @@ -1,14 +0,0 @@ -select('id')->from('example')->limit(10)->offset(5); + + list($actualQuerySql, $actualQueryParams) = $this->getQueryBuilder()->build($query); + + $this->assertEquals($expectedQuerySql, $actualQuerySql); + $this->assertEquals($expectedQueryParams, $actualQueryParams); + } + + public function testLimit() + { + $expectedQuerySql = 'SELECT [id] FROM [example] ORDER BY (SELECT NULL) OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY'; + $expectedQueryParams = []; + + $query = new Query(); + $query->select('id')->from('example')->limit(10); + + list($actualQuerySql, $actualQueryParams) = $this->getQueryBuilder()->build($query); + + $this->assertEquals($expectedQuerySql, $actualQuerySql); + $this->assertEquals($expectedQueryParams, $actualQueryParams); + } + + public function testOffset() + { + $expectedQuerySql = 'SELECT [id] FROM [example] ORDER BY (SELECT NULL) OFFSET 10 ROWS'; + $expectedQueryParams = []; + + $query = new Query(); + $query->select('id')->from('example')->offset(10); + + list($actualQuerySql, $actualQueryParams) = $this->getQueryBuilder()->build($query); + + $this->assertEquals($expectedQuerySql, $actualQuerySql); + $this->assertEquals($expectedQueryParams, $actualQueryParams); + } + + public function testCommentColumn() + { + $qb = $this->getQueryBuilder(); + + $expected = "sp_updateextendedproperty @name = N'MS_Description', @value = 'This is my column.', @level1type = N'Table', @level1name = comment, @level2type = N'Column', @level2name = text"; + $sql = $qb->addCommentOnColumn('comment', 'text', 'This is my column.'); + $this->assertEquals($expected, $sql); + + $expected = "sp_dropextendedproperty @name = N'MS_Description', @level1type = N'Table', @level1name = comment, @level2type = N'Column', @level2name = text"; + $sql = $qb->dropCommentFromColumn('comment', 'text'); + $this->assertEquals($expected, $sql); + } + + public function testCommentTable() + { + $qb = $this->getQueryBuilder(); + + $expected = "sp_updateextendedproperty @name = N'MS_Description', @value = 'This is my table.', @level1type = N'Table', @level1name = comment"; + $sql = $qb->addCommentOnTable('comment', 'This is my table.'); + $this->assertEquals($expected, $sql); + + $expected = "sp_dropextendedproperty @name = N'MS_Description', @level1type = N'Table', @level1name = comment"; + $sql = $qb->dropCommentFromTable('comment'); + $this->assertEquals($expected, $sql); + } + + /** + * this is not used as a dataprovider for testGetColumnType to speed up the test + * when used as dataprovider every single line will cause a reconnect with the database which is not needed here + */ + public function columnTypes() + { + return array_merge(parent::columnTypes(), []); + } +} diff --git a/tests/framework/db/mssql/QueryTest.php b/tests/framework/db/mssql/QueryTest.php new file mode 100644 index 0000000..4fcdc23 --- /dev/null +++ b/tests/framework/db/mssql/QueryTest.php @@ -0,0 +1,12 @@ +primaryKey()->after('col_before'), - 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY AFTER `col_before`' - ], - [ - Schema::TYPE_PK . ' FIRST', - $this->primaryKey()->first(), - 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST' - ], - [ - Schema::TYPE_PK . ' FIRST', - $this->primaryKey()->first()->after('col_before'), - 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST' - ], - [ - Schema::TYPE_PK . '(8) AFTER `col_before`', - $this->primaryKey(8)->after('col_before'), - 'int(8) NOT NULL AUTO_INCREMENT PRIMARY KEY AFTER `col_before`' - ], - [ - Schema::TYPE_PK . '(8) FIRST', - $this->primaryKey(8)->first(), - 'int(8) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST' - ], - [ - Schema::TYPE_PK . '(8) FIRST', - $this->primaryKey(8)->first()->after('col_before'), - 'int(8) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST' - ], - [ - Schema::TYPE_PK . " COMMENT 'test' AFTER `col_before`", - $this->primaryKey()->comment('test')->after('col_before'), - "int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'test' AFTER `col_before`" - ], - ]); - } -} diff --git a/tests/framework/db/mysql/QueryBuilderTest.php b/tests/framework/db/mysql/QueryBuilderTest.php new file mode 100644 index 0000000..4ba9904 --- /dev/null +++ b/tests/framework/db/mysql/QueryBuilderTest.php @@ -0,0 +1,59 @@ +primaryKey()->after('col_before'), + 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY AFTER `col_before`' + ], + [ + Schema::TYPE_PK . ' FIRST', + $this->primaryKey()->first(), + 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST' + ], + [ + Schema::TYPE_PK . ' FIRST', + $this->primaryKey()->first()->after('col_before'), + 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST' + ], + [ + Schema::TYPE_PK . '(8) AFTER `col_before`', + $this->primaryKey(8)->after('col_before'), + 'int(8) NOT NULL AUTO_INCREMENT PRIMARY KEY AFTER `col_before`' + ], + [ + Schema::TYPE_PK . '(8) FIRST', + $this->primaryKey(8)->first(), + 'int(8) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST' + ], + [ + Schema::TYPE_PK . '(8) FIRST', + $this->primaryKey(8)->first()->after('col_before'), + 'int(8) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST' + ], + [ + Schema::TYPE_PK . " COMMENT 'test' AFTER `col_before`", + $this->primaryKey()->comment('test')->after('col_before'), + "int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'test' AFTER `col_before`" + ], + ]); + } +} diff --git a/tests/framework/db/mysql/QueryTest.php b/tests/framework/db/mysql/QueryTest.php new file mode 100644 index 0000000..64b2c72 --- /dev/null +++ b/tests/framework/db/mysql/QueryTest.php @@ -0,0 +1,12 @@ +int_col = 123; + $model->int_col2 = 456; + $model->smallint_col = 42; + $model->char_col = '1337'; + $model->char_col2 = 'test'; + $model->char_col3 = 'test123'; + $model->float_col = 3.742; + $model->float_col2 = 42.1337; + $model->bool_col = 1; + $model->bool_col2 = 0; + $model->save(false); + + /* @var $model Type */ + $model = Type::find()->one(); + $this->assertSame(123, $model->int_col); + $this->assertSame(456, $model->int_col2); + $this->assertSame(42, $model->smallint_col); + $this->assertSame('1337', trim($model->char_col)); + $this->assertSame('test', $model->char_col2); + $this->assertSame('test123', $model->char_col3); +// $this->assertSame(1337.42, $model->float_col); +// $this->assertSame(42.1337, $model->float_col2); +// $this->assertSame(true, $model->bool_col); +// $this->assertSame(false, $model->bool_col2); + } + + public function testDefaultValues() + { + $model = new Type(); + $model->loadDefaultValues(); + $this->assertEquals(1, $model->int_col2); + $this->assertEquals('something', $model->char_col2); + $this->assertEquals(1.23, $model->float_col2); + $this->assertEquals(33.22, $model->numeric_col); + $this->assertEquals(true, $model->bool_col2); + + // not testing $model->time, because oci\Schema can't read default value + + $model = new Type(); + $model->char_col2 = 'not something'; + + $model->loadDefaultValues(); + $this->assertEquals('not something', $model->char_col2); + + $model = new Type(); + $model->char_col2 = 'not something'; + + $model->loadDefaultValues(false); + $this->assertEquals('something', $model->char_col2); + } + + public function testFindAsArray() + { + /* @var $customerClass \yii\db\ActiveRecordInterface */ + $customerClass = $this->getCustomerClass(); + + // asArray + $customer = $customerClass::find()->where(['id' => 2])->asArray()->one(); + $this->assertEquals([ + 'id' => 2, + 'email' => 'user2@example.com', + 'name' => 'user2', + 'address' => 'address2', + 'status' => 1, + 'profile_id' => null, + 'bool_status' => true, + ], $customer); + + // find all asArray + $customers = $customerClass::find()->asArray()->all(); + $this->assertEquals(3, count($customers)); + $this->assertArrayHasKey('id', $customers[0]); + $this->assertArrayHasKey('name', $customers[0]); + $this->assertArrayHasKey('email', $customers[0]); + $this->assertArrayHasKey('address', $customers[0]); + $this->assertArrayHasKey('status', $customers[0]); + $this->assertArrayHasKey('bool_status', $customers[0]); + $this->assertArrayHasKey('id', $customers[1]); + $this->assertArrayHasKey('name', $customers[1]); + $this->assertArrayHasKey('email', $customers[1]); + $this->assertArrayHasKey('address', $customers[1]); + $this->assertArrayHasKey('status', $customers[1]); + $this->assertArrayHasKey('bool_status', $customers[1]); + $this->assertArrayHasKey('id', $customers[2]); + $this->assertArrayHasKey('name', $customers[2]); + $this->assertArrayHasKey('email', $customers[2]); + $this->assertArrayHasKey('address', $customers[2]); + $this->assertArrayHasKey('status', $customers[2]); + $this->assertArrayHasKey('bool_status', $customers[2]); + } + + public function testPrimaryKeyAfterSave() + { + $record = new DefaultPk(); + $record->type = 'type'; + $record->save(false); + $this->assertEquals(5, $record->primaryKey); + } +} diff --git a/tests/framework/db/oci/BatchQueryResultTest.php b/tests/framework/db/oci/BatchQueryResultTest.php new file mode 100644 index 0000000..5813791 --- /dev/null +++ b/tests/framework/db/oci/BatchQueryResultTest.php @@ -0,0 +1,12 @@ +getConnection(false); + + $sql = 'SELECT [[id]], [[t.name]] FROM {{customer}} t'; + $command = $db->createCommand($sql); + $this->assertEquals('SELECT "id", "t"."name" FROM "customer" t', $command->sql); + } + + public function testLastInsertId() + { + $db = $this->getConnection(); + + $sql = 'INSERT INTO {{profile}}([[description]]) VALUES (\'non duplicate\')'; + $command = $db->createCommand($sql); + $command->execute(); + $this->assertEquals(3, $db->getSchema()->getLastInsertID('profile_SEQ')); + } +} diff --git a/tests/framework/db/oci/ConnectionTest.php b/tests/framework/db/oci/ConnectionTest.php new file mode 100644 index 0000000..d65b000 --- /dev/null +++ b/tests/framework/db/oci/ConnectionTest.php @@ -0,0 +1,12 @@ +int_col = 123; - $model->int_col2 = 456; - $model->smallint_col = 42; - $model->char_col = '1337'; - $model->char_col2 = 'test'; - $model->char_col3 = 'test123'; - $model->float_col = 3.742; - $model->float_col2 = 42.1337; - $model->bool_col = 1; - $model->bool_col2 = 0; - $model->save(false); - - /* @var $model Type */ - $model = Type::find()->one(); - $this->assertSame(123, $model->int_col); - $this->assertSame(456, $model->int_col2); - $this->assertSame(42, $model->smallint_col); - $this->assertSame('1337', trim($model->char_col)); - $this->assertSame('test', $model->char_col2); - $this->assertSame('test123', $model->char_col3); -// $this->assertSame(1337.42, $model->float_col); -// $this->assertSame(42.1337, $model->float_col2); -// $this->assertSame(true, $model->bool_col); -// $this->assertSame(false, $model->bool_col2); - } - - public function testDefaultValues() - { - $model = new Type(); - $model->loadDefaultValues(); - $this->assertEquals(1, $model->int_col2); - $this->assertEquals('something', $model->char_col2); - $this->assertEquals(1.23, $model->float_col2); - $this->assertEquals(33.22, $model->numeric_col); - $this->assertEquals(true, $model->bool_col2); - - // not testing $model->time, because oci\Schema can't read default value - - $model = new Type(); - $model->char_col2 = 'not something'; - - $model->loadDefaultValues(); - $this->assertEquals('not something', $model->char_col2); - - $model = new Type(); - $model->char_col2 = 'not something'; - - $model->loadDefaultValues(false); - $this->assertEquals('something', $model->char_col2); - } - - public function testFindAsArray() - { - /* @var $customerClass \yii\db\ActiveRecordInterface */ - $customerClass = $this->getCustomerClass(); - - // asArray - $customer = $customerClass::find()->where(['id' => 2])->asArray()->one(); - $this->assertEquals([ - 'id' => 2, - 'email' => 'user2@example.com', - 'name' => 'user2', - 'address' => 'address2', - 'status' => 1, - 'profile_id' => null, - 'bool_status' => true, - ], $customer); - - // find all asArray - $customers = $customerClass::find()->asArray()->all(); - $this->assertEquals(3, count($customers)); - $this->assertArrayHasKey('id', $customers[0]); - $this->assertArrayHasKey('name', $customers[0]); - $this->assertArrayHasKey('email', $customers[0]); - $this->assertArrayHasKey('address', $customers[0]); - $this->assertArrayHasKey('status', $customers[0]); - $this->assertArrayHasKey('bool_status', $customers[0]); - $this->assertArrayHasKey('id', $customers[1]); - $this->assertArrayHasKey('name', $customers[1]); - $this->assertArrayHasKey('email', $customers[1]); - $this->assertArrayHasKey('address', $customers[1]); - $this->assertArrayHasKey('status', $customers[1]); - $this->assertArrayHasKey('bool_status', $customers[1]); - $this->assertArrayHasKey('id', $customers[2]); - $this->assertArrayHasKey('name', $customers[2]); - $this->assertArrayHasKey('email', $customers[2]); - $this->assertArrayHasKey('address', $customers[2]); - $this->assertArrayHasKey('status', $customers[2]); - $this->assertArrayHasKey('bool_status', $customers[2]); - } - - public function testPrimaryKeyAfterSave() - { - $record = new DefaultPk(); - $record->type = 'type'; - $record->save(false); - $this->assertEquals(5, $record->primaryKey); - } -} diff --git a/tests/framework/db/oci/OracleCommandTest.php b/tests/framework/db/oci/OracleCommandTest.php deleted file mode 100644 index 435b536..0000000 --- a/tests/framework/db/oci/OracleCommandTest.php +++ /dev/null @@ -1,33 +0,0 @@ -getConnection(false); - - $sql = 'SELECT [[id]], [[t.name]] FROM {{customer}} t'; - $command = $db->createCommand($sql); - $this->assertEquals('SELECT "id", "t"."name" FROM "customer" t', $command->sql); - } - - public function testLastInsertId() - { - $db = $this->getConnection(); - - $sql = 'INSERT INTO {{profile}}([[description]]) VALUES (\'non duplicate\')'; - $command = $db->createCommand($sql); - $command->execute(); - $this->assertEquals(3, $db->getSchema()->getLastInsertID('profile_SEQ')); - } -} diff --git a/tests/framework/db/oci/OracleQueryBuilderTest.php b/tests/framework/db/oci/OracleQueryBuilderTest.php deleted file mode 100644 index 00f200d..0000000 --- a/tests/framework/db/oci/OracleQueryBuilderTest.php +++ /dev/null @@ -1,57 +0,0 @@ -boolean()->notNull()->defaultValue(1), - 'NUMBER(1) DEFAULT 1 NOT NULL' - ], - ]); - } - - - public function testCommentColumn() - { - $qb = $this->getQueryBuilder(); - - $expected = "COMMENT ON COLUMN [[comment]].[[text]] IS 'This is my column.'"; - $sql = $qb->addCommentOnColumn('comment', 'text', 'This is my column.'); - $this->assertEquals($this->replaceQuotes($expected), $sql); - - $expected = "COMMENT ON COLUMN [[comment]].[[text]] IS ''"; - $sql = $qb->dropCommentFromColumn('comment', 'text'); - $this->assertEquals($this->replaceQuotes($expected), $sql); - } - - public function testCommentTable() - { - $qb = $this->getQueryBuilder(); - - $expected = "COMMENT ON TABLE [[comment]] IS 'This is my table.'"; - $sql = $qb->addCommentOnTable('comment', 'This is my table.'); - $this->assertEquals($this->replaceQuotes($expected), $sql); - - $expected = "COMMENT ON TABLE [[comment]] IS ''"; - $sql = $qb->dropCommentFromTable('comment'); - $this->assertEquals($this->replaceQuotes($expected), $sql); - } -} diff --git a/tests/framework/db/oci/OracleQueryTest.php b/tests/framework/db/oci/OracleQueryTest.php deleted file mode 100644 index 4c0e50f..0000000 --- a/tests/framework/db/oci/OracleQueryTest.php +++ /dev/null @@ -1,25 +0,0 @@ -getConnection(); - - $result = (new Query)->from('customer')->where(['[[status]]' => 2])->one($db); - $this->assertEquals('user3', $result['name']); - - $result = (new Query)->from('customer')->where(['[[status]]' => 3])->one($db); - $this->assertFalse($result); - } -} diff --git a/tests/framework/db/oci/OracleSchemaTest.php b/tests/framework/db/oci/OracleSchemaTest.php deleted file mode 100644 index 747d06e..0000000 --- a/tests/framework/db/oci/OracleSchemaTest.php +++ /dev/null @@ -1,97 +0,0 @@ -getConnection(false)->schema->getTableSchema('order', true); - $this->assertSame(false, $table->columns['id']->autoIncrement); - } -} diff --git a/tests/framework/db/oci/QueryBuilderTest.php b/tests/framework/db/oci/QueryBuilderTest.php new file mode 100644 index 0000000..fe8f932 --- /dev/null +++ b/tests/framework/db/oci/QueryBuilderTest.php @@ -0,0 +1,56 @@ +boolean()->notNull()->defaultValue(1), + 'NUMBER(1) DEFAULT 1 NOT NULL' + ], + ]); + } + + + public function testCommentColumn() + { + $qb = $this->getQueryBuilder(); + + $expected = "COMMENT ON COLUMN [[comment]].[[text]] IS 'This is my column.'"; + $sql = $qb->addCommentOnColumn('comment', 'text', 'This is my column.'); + $this->assertEquals($this->replaceQuotes($expected), $sql); + + $expected = "COMMENT ON COLUMN [[comment]].[[text]] IS ''"; + $sql = $qb->dropCommentFromColumn('comment', 'text'); + $this->assertEquals($this->replaceQuotes($expected), $sql); + } + + public function testCommentTable() + { + $qb = $this->getQueryBuilder(); + + $expected = "COMMENT ON TABLE [[comment]] IS 'This is my table.'"; + $sql = $qb->addCommentOnTable('comment', 'This is my table.'); + $this->assertEquals($this->replaceQuotes($expected), $sql); + + $expected = "COMMENT ON TABLE [[comment]] IS ''"; + $sql = $qb->dropCommentFromTable('comment'); + $this->assertEquals($this->replaceQuotes($expected), $sql); + } +} diff --git a/tests/framework/db/oci/QueryTest.php b/tests/framework/db/oci/QueryTest.php new file mode 100644 index 0000000..306e739 --- /dev/null +++ b/tests/framework/db/oci/QueryTest.php @@ -0,0 +1,25 @@ +getConnection(); + + $result = (new Query)->from('customer')->where(['[[status]]' => 2])->one($db); + $this->assertEquals('user3', $result['name']); + + $result = (new Query)->from('customer')->where(['[[status]]' => 3])->one($db); + $this->assertFalse($result); + } +} diff --git a/tests/framework/db/oci/SchemaTest.php b/tests/framework/db/oci/SchemaTest.php new file mode 100644 index 0000000..267ec24 --- /dev/null +++ b/tests/framework/db/oci/SchemaTest.php @@ -0,0 +1,96 @@ +getConnection(false)->schema->getTableSchema('order', true); + $this->assertSame(false, $table->columns['id']->autoIncrement); + } +} diff --git a/tests/framework/db/pgsql/ActiveDataProviderTest.php b/tests/framework/db/pgsql/ActiveDataProviderTest.php new file mode 100644 index 0000000..21d9a3b --- /dev/null +++ b/tests/framework/db/pgsql/ActiveDataProviderTest.php @@ -0,0 +1,13 @@ +getCustomerClass(); + /* @var $this TestCase|ActiveRecordTestTrait */ + $customer = new $customerClass(); + $customer->name = 'boolean customer'; + $customer->email = 'mail@example.com'; + $customer->bool_status = false; + $customer->save(false); + + $customer->refresh(); + $this->assertSame(false, $customer->bool_status); + + $customer->bool_status = true; + $customer->save(false); + + $customer->refresh(); + $this->assertSame(true, $customer->bool_status); + + $customers = $customerClass::find()->where(['bool_status' => true])->all(); + $this->assertEquals(3, count($customers)); + + $customers = $customerClass::find()->where(['bool_status' => false])->all(); + $this->assertEquals(1, count($customers)); + } + + public function testFindAsArray() + { + /* @var $customerClass \yii\db\ActiveRecordInterface */ + $customerClass = $this->getCustomerClass(); + + // asArray + $customer = $customerClass::find()->where(['id' => 2])->asArray()->one(); + $this->assertEquals([ + 'id' => 2, + 'email' => 'user2@example.com', + 'name' => 'user2', + 'address' => 'address2', + 'status' => 1, + 'profile_id' => null, + 'bool_status' => true, + ], $customer); + + // find all asArray + $customers = $customerClass::find()->asArray()->all(); + $this->assertEquals(3, count($customers)); + $this->assertArrayHasKey('id', $customers[0]); + $this->assertArrayHasKey('name', $customers[0]); + $this->assertArrayHasKey('email', $customers[0]); + $this->assertArrayHasKey('address', $customers[0]); + $this->assertArrayHasKey('status', $customers[0]); + $this->assertArrayHasKey('bool_status', $customers[0]); + $this->assertArrayHasKey('id', $customers[1]); + $this->assertArrayHasKey('name', $customers[1]); + $this->assertArrayHasKey('email', $customers[1]); + $this->assertArrayHasKey('address', $customers[1]); + $this->assertArrayHasKey('status', $customers[1]); + $this->assertArrayHasKey('bool_status', $customers[1]); + $this->assertArrayHasKey('id', $customers[2]); + $this->assertArrayHasKey('name', $customers[2]); + $this->assertArrayHasKey('email', $customers[2]); + $this->assertArrayHasKey('address', $customers[2]); + $this->assertArrayHasKey('status', $customers[2]); + $this->assertArrayHasKey('bool_status', $customers[2]); + } + + public function testBooleanValues() + { + $db = $this->getConnection(); + $command = $db->createCommand(); + $command->batchInsert('bool_values', + ['bool_col'], [ + [true], + [false], + ] + )->execute(); + + $this->assertEquals(1, BoolAR::find()->where('bool_col = TRUE')->count('*', $db)); + $this->assertEquals(1, BoolAR::find()->where('bool_col = FALSE')->count('*', $db)); + $this->assertEquals(2, BoolAR::find()->where('bool_col IN (TRUE, FALSE)')->count('*', $db)); + + $this->assertEquals(1, BoolAR::find()->where(['bool_col' => true])->count('*', $db)); + $this->assertEquals(1, BoolAR::find()->where(['bool_col' => false])->count('*', $db)); + $this->assertEquals(2, BoolAR::find()->where(['bool_col' => [true, false]])->count('*', $db)); + + $this->assertEquals(1, BoolAR::find()->where('bool_col = :bool_col', ['bool_col' => true])->count('*', $db)); + $this->assertEquals(1, BoolAR::find()->where('bool_col = :bool_col', ['bool_col' => false])->count('*', $db)); + + $this->assertSame(true, BoolAR::find()->where(['bool_col' => true])->one($db)->bool_col); + $this->assertSame(false, BoolAR::find()->where(['bool_col' => false])->one($db)->bool_col); + } + + /** + * https://github.com/yiisoft/yii2/issues/4672 + */ + public function testBooleanValues2() + { + $db = $this->getConnection(); + $db->charset = 'utf8'; + + $db->createCommand("DROP TABLE IF EXISTS bool_user;")->execute(); + $db->createCommand()->createTable('bool_user', [ + 'id' => Schema::TYPE_PK, + 'username' => Schema::TYPE_STRING . ' NOT NULL', + 'auth_key' => Schema::TYPE_STRING . '(32) NOT NULL', + 'password_hash' => Schema::TYPE_STRING . ' NOT NULL', + 'password_reset_token' => Schema::TYPE_STRING, + 'email' => Schema::TYPE_STRING . ' NOT NULL', + 'role' => Schema::TYPE_SMALLINT . ' NOT NULL DEFAULT 10', + + 'status' => Schema::TYPE_SMALLINT . ' NOT NULL DEFAULT 10', + 'created_at' => Schema::TYPE_INTEGER . ' NOT NULL', + 'updated_at' => Schema::TYPE_INTEGER . ' NOT NULL', + ])->execute(); + $db->createCommand()->addColumn('bool_user', 'is_deleted', Schema::TYPE_BOOLEAN . ' NOT NULL DEFAULT FALSE')->execute(); + + $user = new UserAR(); + $user->username = 'test'; + $user->auth_key = 'test'; + $user->password_hash = 'test'; + $user->email = 'test@example.com'; + $user->save(false); + + $this->assertEquals(1, count(UserAR::find()->where(['is_deleted' => false])->all($db))); + $this->assertEquals(0, count(UserAR::find()->where(['is_deleted' => true])->all($db))); + $this->assertEquals(1, count(UserAR::find()->where(['is_deleted' => [true, false]])->all($db))); + } + + public function testBooleanDefaultValues() + { + $model = new BoolAR(); + $this->assertNull($model->bool_col); + $this->assertNull($model->default_true); + $this->assertNull($model->default_false); + $model->loadDefaultValues(); + $this->assertNull($model->bool_col); + $this->assertSame(true, $model->default_true); + $this->assertSame(false, $model->default_false); + + $this->assertTrue($model->save(false)); + } + + public function testPrimaryKeyAfterSave() + { + $record = new DefaultPk(); + $record->type = 'type'; + $record->save(false); + $this->assertEquals(5, $record->primaryKey); + } +} + +class BoolAR extends ActiveRecord +{ + public static function tableName() + { + return 'bool_values'; + } +} + +class UserAR extends ActiveRecord +{ + const STATUS_DELETED = 0; + const STATUS_ACTIVE = 10; + const ROLE_USER = 10; + + public static function tableName() + { + return '{{%bool_user}}'; + } + + public function behaviors() + { + return [ + TimestampBehavior::className(), + ]; + } +} diff --git a/tests/framework/db/pgsql/BatchQueryResultTest.php b/tests/framework/db/pgsql/BatchQueryResultTest.php new file mode 100644 index 0000000..9261baa --- /dev/null +++ b/tests/framework/db/pgsql/BatchQueryResultTest.php @@ -0,0 +1,12 @@ +getConnection(false); + + $sql = 'SELECT [[id]], [[t.name]] FROM {{customer}} t'; + $command = $db->createCommand($sql); + $this->assertEquals('SELECT "id", "t"."name" FROM "customer" t', $command->sql); + } + + public function testBooleanValuesInsert() + { + $db = $this->getConnection(); + $command = $db->createCommand(); + $command->insert('bool_values', ['bool_col' => true]); + $this->assertEquals(1, $command->execute()); + + $command = $db->createCommand(); + $command->insert('bool_values', ['bool_col' => false]); + $this->assertEquals(1, $command->execute()); + + $command = $db->createCommand('SELECT COUNT(*) FROM "bool_values" WHERE bool_col = TRUE;'); + $this->assertEquals(1, $command->queryScalar()); + $command = $db->createCommand('SELECT COUNT(*) FROM "bool_values" WHERE bool_col = FALSE;'); + $this->assertEquals(1, $command->queryScalar()); + } + + public function testBooleanValuesBatchInsert() + { + $db = $this->getConnection(); + $command = $db->createCommand(); + $command->batchInsert('bool_values', + ['bool_col'], [ + [true], + [false], + ] + ); + $this->assertEquals(2, $command->execute()); + + $command = $db->createCommand('SELECT COUNT(*) FROM "bool_values" WHERE bool_col = TRUE;'); + $this->assertEquals(1, $command->queryScalar()); + $command = $db->createCommand('SELECT COUNT(*) FROM "bool_values" WHERE bool_col = FALSE;'); + $this->assertEquals(1, $command->queryScalar()); + } + + public function testLastInsertId() + { + $db = $this->getConnection(); + + $sql = 'INSERT INTO {{profile}}([[description]]) VALUES (\'non duplicate\')'; + $command = $db->createCommand($sql); + $command->execute(); + $this->assertEquals(3, $db->getSchema()->getLastInsertID('public.profile_id_seq')); + + $sql = 'INSERT INTO {{schema1.profile}}([[description]]) VALUES (\'non duplicate\')'; + $command = $db->createCommand($sql); + $command->execute(); + $this->assertEquals(3, $db->getSchema()->getLastInsertID('schema1.profile_id_seq')); + } + + /** + * @see https://github.com/yiisoft/yii2/issues/11498 + */ + public function testSaveSerializedObject() + { + $db = $this->getConnection(); + + $command = $db->createCommand()->insert('type', [ + 'int_col' => 1, + 'char_col' => 'serialize', + 'float_col' => 5.6, + 'bool_col' => true, + 'blob_col' => serialize($db), + ]); + $this->assertEquals(1, $command->execute()); + + $command = $db->createCommand()->update('type', [ + 'blob_col' => serialize($db), + ], ['char_col' => 'serialize']); + $this->assertEquals(1, $command->execute()); + } +} \ No newline at end of file diff --git a/tests/framework/db/pgsql/ConnectionTest.php b/tests/framework/db/pgsql/ConnectionTest.php new file mode 100644 index 0000000..429c545 --- /dev/null +++ b/tests/framework/db/pgsql/ConnectionTest.php @@ -0,0 +1,99 @@ +getConnection(true); + } + + public function testQuoteValue() + { + $connection = $this->getConnection(false); + $this->assertEquals(123, $connection->quoteValue(123)); + $this->assertEquals("'string'", $connection->quoteValue('string')); + $this->assertEquals("'It''s interesting'", $connection->quoteValue("It's interesting")); + } + + public function testQuoteTableName() + { + $connection = $this->getConnection(false); + $this->assertEquals('"table"', $connection->quoteTableName('table')); + $this->assertEquals('"table"', $connection->quoteTableName('"table"')); + $this->assertEquals('"schema"."table"', $connection->quoteTableName('schema.table')); + $this->assertEquals('"schema"."table"', $connection->quoteTableName('schema."table"')); + $this->assertEquals('"schema"."table"', $connection->quoteTableName('"schema"."table"')); + $this->assertEquals('{{table}}', $connection->quoteTableName('{{table}}')); + $this->assertEquals('(table)', $connection->quoteTableName('(table)')); + } + + public function testQuoteColumnName() + { + $connection = $this->getConnection(false); + $this->assertEquals('"column"', $connection->quoteColumnName('column')); + $this->assertEquals('"column"', $connection->quoteColumnName('"column"')); + $this->assertEquals('[[column]]', $connection->quoteColumnName('[[column]]')); + $this->assertEquals('{{column}}', $connection->quoteColumnName('{{column}}')); + $this->assertEquals('(column)', $connection->quoteColumnName('(column)')); + + $this->assertEquals('"column"', $connection->quoteSql('[[column]]')); + $this->assertEquals('"column"', $connection->quoteSql('{{column}}')); + } + + public function testQuoteFullColumnName() + { + $connection = $this->getConnection(false, false); + $this->assertEquals('"table"."column"', $connection->quoteColumnName('table.column')); + $this->assertEquals('"table"."column"', $connection->quoteColumnName('table."column"')); + $this->assertEquals('"table"."column"', $connection->quoteColumnName('"table".column')); + $this->assertEquals('"table"."column"', $connection->quoteColumnName('"table"."column"')); + + $this->assertEquals('[[table.column]]', $connection->quoteColumnName('[[table.column]]')); + $this->assertEquals('{{table}}."column"', $connection->quoteColumnName('{{table}}.column')); + $this->assertEquals('{{table}}."column"', $connection->quoteColumnName('{{table}}."column"')); + $this->assertEquals('{{table}}.[[column]]', $connection->quoteColumnName('{{table}}.[[column]]')); + $this->assertEquals('{{%table}}."column"', $connection->quoteColumnName('{{%table}}.column')); + $this->assertEquals('{{%table}}."column"', $connection->quoteColumnName('{{%table}}."column"')); + + $this->assertEquals('"table"."column"', $connection->quoteSql('[[table.column]]')); + $this->assertEquals('"table"."column"', $connection->quoteSql('{{table}}.[[column]]')); + $this->assertEquals('"table"."column"', $connection->quoteSql('{{table}}."column"')); + $this->assertEquals('"table"."column"', $connection->quoteSql('{{%table}}.[[column]]')); + $this->assertEquals('"table"."column"', $connection->quoteSql('{{%table}}."column"')); + } + + public function testTransactionIsolation() + { + $connection = $this->getConnection(true); + + $transaction = $connection->beginTransaction(); + $transaction->setIsolationLevel(Transaction::READ_UNCOMMITTED); + $transaction->commit(); + + $transaction = $connection->beginTransaction(); + $transaction->setIsolationLevel(Transaction::READ_COMMITTED); + $transaction->commit(); + + $transaction = $connection->beginTransaction(); + $transaction->setIsolationLevel(Transaction::REPEATABLE_READ); + $transaction->commit(); + + $transaction = $connection->beginTransaction(); + $transaction->setIsolationLevel(Transaction::SERIALIZABLE); + $transaction->commit(); + + $transaction = $connection->beginTransaction(); + $transaction->setIsolationLevel(Transaction::SERIALIZABLE . ' READ ONLY DEFERRABLE'); + $transaction->commit(); + } +} diff --git a/tests/framework/db/pgsql/PostgreSQLActiveDataProviderTest.php b/tests/framework/db/pgsql/PostgreSQLActiveDataProviderTest.php deleted file mode 100644 index 081f680..0000000 --- a/tests/framework/db/pgsql/PostgreSQLActiveDataProviderTest.php +++ /dev/null @@ -1,14 +0,0 @@ -getCustomerClass(); - /* @var $this TestCase|ActiveRecordTestTrait */ - $customer = new $customerClass(); - $customer->name = 'boolean customer'; - $customer->email = 'mail@example.com'; - $customer->bool_status = false; - $customer->save(false); - - $customer->refresh(); - $this->assertSame(false, $customer->bool_status); - - $customer->bool_status = true; - $customer->save(false); - - $customer->refresh(); - $this->assertSame(true, $customer->bool_status); - - $customers = $customerClass::find()->where(['bool_status' => true])->all(); - $this->assertEquals(3, count($customers)); - - $customers = $customerClass::find()->where(['bool_status' => false])->all(); - $this->assertEquals(1, count($customers)); - } - - public function testFindAsArray() - { - /* @var $customerClass \yii\db\ActiveRecordInterface */ - $customerClass = $this->getCustomerClass(); - - // asArray - $customer = $customerClass::find()->where(['id' => 2])->asArray()->one(); - $this->assertEquals([ - 'id' => 2, - 'email' => 'user2@example.com', - 'name' => 'user2', - 'address' => 'address2', - 'status' => 1, - 'profile_id' => null, - 'bool_status' => true, - ], $customer); - - // find all asArray - $customers = $customerClass::find()->asArray()->all(); - $this->assertEquals(3, count($customers)); - $this->assertArrayHasKey('id', $customers[0]); - $this->assertArrayHasKey('name', $customers[0]); - $this->assertArrayHasKey('email', $customers[0]); - $this->assertArrayHasKey('address', $customers[0]); - $this->assertArrayHasKey('status', $customers[0]); - $this->assertArrayHasKey('bool_status', $customers[0]); - $this->assertArrayHasKey('id', $customers[1]); - $this->assertArrayHasKey('name', $customers[1]); - $this->assertArrayHasKey('email', $customers[1]); - $this->assertArrayHasKey('address', $customers[1]); - $this->assertArrayHasKey('status', $customers[1]); - $this->assertArrayHasKey('bool_status', $customers[1]); - $this->assertArrayHasKey('id', $customers[2]); - $this->assertArrayHasKey('name', $customers[2]); - $this->assertArrayHasKey('email', $customers[2]); - $this->assertArrayHasKey('address', $customers[2]); - $this->assertArrayHasKey('status', $customers[2]); - $this->assertArrayHasKey('bool_status', $customers[2]); - } - - public function testBooleanValues() - { - $db = $this->getConnection(); - $command = $db->createCommand(); - $command->batchInsert('bool_values', - ['bool_col'], [ - [true], - [false], - ] - )->execute(); - - $this->assertEquals(1, BoolAR::find()->where('bool_col = TRUE')->count('*', $db)); - $this->assertEquals(1, BoolAR::find()->where('bool_col = FALSE')->count('*', $db)); - $this->assertEquals(2, BoolAR::find()->where('bool_col IN (TRUE, FALSE)')->count('*', $db)); - - $this->assertEquals(1, BoolAR::find()->where(['bool_col' => true])->count('*', $db)); - $this->assertEquals(1, BoolAR::find()->where(['bool_col' => false])->count('*', $db)); - $this->assertEquals(2, BoolAR::find()->where(['bool_col' => [true, false]])->count('*', $db)); - - $this->assertEquals(1, BoolAR::find()->where('bool_col = :bool_col', ['bool_col' => true])->count('*', $db)); - $this->assertEquals(1, BoolAR::find()->where('bool_col = :bool_col', ['bool_col' => false])->count('*', $db)); - - $this->assertSame(true, BoolAR::find()->where(['bool_col' => true])->one($db)->bool_col); - $this->assertSame(false, BoolAR::find()->where(['bool_col' => false])->one($db)->bool_col); - } - - /** - * https://github.com/yiisoft/yii2/issues/4672 - */ - public function testBooleanValues2() - { - $db = $this->getConnection(); - $db->charset = 'utf8'; - - $db->createCommand("DROP TABLE IF EXISTS bool_user;")->execute(); - $db->createCommand()->createTable('bool_user', [ - 'id' => Schema::TYPE_PK, - 'username' => Schema::TYPE_STRING . ' NOT NULL', - 'auth_key' => Schema::TYPE_STRING . '(32) NOT NULL', - 'password_hash' => Schema::TYPE_STRING . ' NOT NULL', - 'password_reset_token' => Schema::TYPE_STRING, - 'email' => Schema::TYPE_STRING . ' NOT NULL', - 'role' => Schema::TYPE_SMALLINT . ' NOT NULL DEFAULT 10', - - 'status' => Schema::TYPE_SMALLINT . ' NOT NULL DEFAULT 10', - 'created_at' => Schema::TYPE_INTEGER . ' NOT NULL', - 'updated_at' => Schema::TYPE_INTEGER . ' NOT NULL', - ])->execute(); - $db->createCommand()->addColumn('bool_user', 'is_deleted', Schema::TYPE_BOOLEAN . ' NOT NULL DEFAULT FALSE')->execute(); - - $user = new UserAR(); - $user->username = 'test'; - $user->auth_key = 'test'; - $user->password_hash = 'test'; - $user->email = 'test@example.com'; - $user->save(false); - - $this->assertEquals(1, count(UserAR::find()->where(['is_deleted' => false])->all($db))); - $this->assertEquals(0, count(UserAR::find()->where(['is_deleted' => true])->all($db))); - $this->assertEquals(1, count(UserAR::find()->where(['is_deleted' => [true, false]])->all($db))); - } - - public function testBooleanDefaultValues() - { - $model = new BoolAR(); - $this->assertNull($model->bool_col); - $this->assertNull($model->default_true); - $this->assertNull($model->default_false); - $model->loadDefaultValues(); - $this->assertNull($model->bool_col); - $this->assertSame(true, $model->default_true); - $this->assertSame(false, $model->default_false); - - $this->assertTrue($model->save(false)); - } - - public function testPrimaryKeyAfterSave() - { - $record = new DefaultPk(); - $record->type = 'type'; - $record->save(false); - $this->assertEquals(5, $record->primaryKey); - } -} - -class BoolAR extends ActiveRecord -{ - public static function tableName() - { - return 'bool_values'; - } -} - -class UserAR extends ActiveRecord -{ - const STATUS_DELETED = 0; - const STATUS_ACTIVE = 10; - const ROLE_USER = 10; - - public static function tableName() - { - return '{{%bool_user}}'; - } - - public function behaviors() - { - return [ - TimestampBehavior::className(), - ]; - } -} diff --git a/tests/framework/db/pgsql/PostgreSQLCommandTest.php b/tests/framework/db/pgsql/PostgreSQLCommandTest.php deleted file mode 100644 index 50dff3a..0000000 --- a/tests/framework/db/pgsql/PostgreSQLCommandTest.php +++ /dev/null @@ -1,94 +0,0 @@ -getConnection(false); - - $sql = 'SELECT [[id]], [[t.name]] FROM {{customer}} t'; - $command = $db->createCommand($sql); - $this->assertEquals('SELECT "id", "t"."name" FROM "customer" t', $command->sql); - } - - public function testBooleanValuesInsert() - { - $db = $this->getConnection(); - $command = $db->createCommand(); - $command->insert('bool_values', ['bool_col' => true]); - $this->assertEquals(1, $command->execute()); - - $command = $db->createCommand(); - $command->insert('bool_values', ['bool_col' => false]); - $this->assertEquals(1, $command->execute()); - - $command = $db->createCommand('SELECT COUNT(*) FROM "bool_values" WHERE bool_col = TRUE;'); - $this->assertEquals(1, $command->queryScalar()); - $command = $db->createCommand('SELECT COUNT(*) FROM "bool_values" WHERE bool_col = FALSE;'); - $this->assertEquals(1, $command->queryScalar()); - } - - public function testBooleanValuesBatchInsert() - { - $db = $this->getConnection(); - $command = $db->createCommand(); - $command->batchInsert('bool_values', - ['bool_col'], [ - [true], - [false], - ] - ); - $this->assertEquals(2, $command->execute()); - - $command = $db->createCommand('SELECT COUNT(*) FROM "bool_values" WHERE bool_col = TRUE;'); - $this->assertEquals(1, $command->queryScalar()); - $command = $db->createCommand('SELECT COUNT(*) FROM "bool_values" WHERE bool_col = FALSE;'); - $this->assertEquals(1, $command->queryScalar()); - } - - public function testLastInsertId() - { - $db = $this->getConnection(); - - $sql = 'INSERT INTO {{profile}}([[description]]) VALUES (\'non duplicate\')'; - $command = $db->createCommand($sql); - $command->execute(); - $this->assertEquals(3, $db->getSchema()->getLastInsertID('public.profile_id_seq')); - - $sql = 'INSERT INTO {{schema1.profile}}([[description]]) VALUES (\'non duplicate\')'; - $command = $db->createCommand($sql); - $command->execute(); - $this->assertEquals(3, $db->getSchema()->getLastInsertID('schema1.profile_id_seq')); - } - - /** - * @see https://github.com/yiisoft/yii2/issues/11498 - */ - public function testSaveSerializedObject() - { - $db = $this->getConnection(); - - $command = $db->createCommand()->insert('type', [ - 'int_col' => 1, - 'char_col' => 'serialize', - 'float_col' => 5.6, - 'bool_col' => true, - 'blob_col' => serialize($db), - ]); - $this->assertEquals(1, $command->execute()); - - $command = $db->createCommand()->update('type', [ - 'blob_col' => serialize($db), - ], ['char_col' => 'serialize']); - $this->assertEquals(1, $command->execute()); - } -} \ No newline at end of file diff --git a/tests/framework/db/pgsql/PostgreSQLConnectionTest.php b/tests/framework/db/pgsql/PostgreSQLConnectionTest.php deleted file mode 100644 index 2206b75..0000000 --- a/tests/framework/db/pgsql/PostgreSQLConnectionTest.php +++ /dev/null @@ -1,99 +0,0 @@ -getConnection(true); - } - - public function testQuoteValue() - { - $connection = $this->getConnection(false); - $this->assertEquals(123, $connection->quoteValue(123)); - $this->assertEquals("'string'", $connection->quoteValue('string')); - $this->assertEquals("'It''s interesting'", $connection->quoteValue("It's interesting")); - } - - public function testQuoteTableName() - { - $connection = $this->getConnection(false); - $this->assertEquals('"table"', $connection->quoteTableName('table')); - $this->assertEquals('"table"', $connection->quoteTableName('"table"')); - $this->assertEquals('"schema"."table"', $connection->quoteTableName('schema.table')); - $this->assertEquals('"schema"."table"', $connection->quoteTableName('schema."table"')); - $this->assertEquals('"schema"."table"', $connection->quoteTableName('"schema"."table"')); - $this->assertEquals('{{table}}', $connection->quoteTableName('{{table}}')); - $this->assertEquals('(table)', $connection->quoteTableName('(table)')); - } - - public function testQuoteColumnName() - { - $connection = $this->getConnection(false); - $this->assertEquals('"column"', $connection->quoteColumnName('column')); - $this->assertEquals('"column"', $connection->quoteColumnName('"column"')); - $this->assertEquals('[[column]]', $connection->quoteColumnName('[[column]]')); - $this->assertEquals('{{column}}', $connection->quoteColumnName('{{column}}')); - $this->assertEquals('(column)', $connection->quoteColumnName('(column)')); - - $this->assertEquals('"column"', $connection->quoteSql('[[column]]')); - $this->assertEquals('"column"', $connection->quoteSql('{{column}}')); - } - - public function testQuoteFullColumnName() - { - $connection = $this->getConnection(false, false); - $this->assertEquals('"table"."column"', $connection->quoteColumnName('table.column')); - $this->assertEquals('"table"."column"', $connection->quoteColumnName('table."column"')); - $this->assertEquals('"table"."column"', $connection->quoteColumnName('"table".column')); - $this->assertEquals('"table"."column"', $connection->quoteColumnName('"table"."column"')); - - $this->assertEquals('[[table.column]]', $connection->quoteColumnName('[[table.column]]')); - $this->assertEquals('{{table}}."column"', $connection->quoteColumnName('{{table}}.column')); - $this->assertEquals('{{table}}."column"', $connection->quoteColumnName('{{table}}."column"')); - $this->assertEquals('{{table}}.[[column]]', $connection->quoteColumnName('{{table}}.[[column]]')); - $this->assertEquals('{{%table}}."column"', $connection->quoteColumnName('{{%table}}.column')); - $this->assertEquals('{{%table}}."column"', $connection->quoteColumnName('{{%table}}."column"')); - - $this->assertEquals('"table"."column"', $connection->quoteSql('[[table.column]]')); - $this->assertEquals('"table"."column"', $connection->quoteSql('{{table}}.[[column]]')); - $this->assertEquals('"table"."column"', $connection->quoteSql('{{table}}."column"')); - $this->assertEquals('"table"."column"', $connection->quoteSql('{{%table}}.[[column]]')); - $this->assertEquals('"table"."column"', $connection->quoteSql('{{%table}}."column"')); - } - - public function testTransactionIsolation() - { - $connection = $this->getConnection(true); - - $transaction = $connection->beginTransaction(); - $transaction->setIsolationLevel(Transaction::READ_UNCOMMITTED); - $transaction->commit(); - - $transaction = $connection->beginTransaction(); - $transaction->setIsolationLevel(Transaction::READ_COMMITTED); - $transaction->commit(); - - $transaction = $connection->beginTransaction(); - $transaction->setIsolationLevel(Transaction::REPEATABLE_READ); - $transaction->commit(); - - $transaction = $connection->beginTransaction(); - $transaction->setIsolationLevel(Transaction::SERIALIZABLE); - $transaction->commit(); - - $transaction = $connection->beginTransaction(); - $transaction->setIsolationLevel(Transaction::SERIALIZABLE . ' READ ONLY DEFERRABLE'); - $transaction->commit(); - } -} diff --git a/tests/framework/db/pgsql/PostgreSQLQueryBuilderTest.php b/tests/framework/db/pgsql/PostgreSQLQueryBuilderTest.php deleted file mode 100644 index 775f1b5..0000000 --- a/tests/framework/db/pgsql/PostgreSQLQueryBuilderTest.php +++ /dev/null @@ -1,124 +0,0 @@ -boolean()->notNull()->defaultValue(true), - 'boolean NOT NULL DEFAULT TRUE' - ], - [ - Schema::TYPE_CHAR . ' CHECK (value LIKE \'test%\')', - $this->char()->check('value LIKE \'test%\''), - 'char(1) CHECK (value LIKE \'test%\')' - ], - [ - Schema::TYPE_CHAR . '(6) CHECK (value LIKE \'test%\')', - $this->char(6)->check('value LIKE \'test%\''), - 'char(6) CHECK (value LIKE \'test%\')' - ], - [ - Schema::TYPE_CHAR . '(6)', - $this->char(6)->unsigned(), - 'char(6)' - ], - [ - Schema::TYPE_INTEGER . '(8)', - $this->integer(8)->unsigned(), - 'integer' - ], - [ - Schema::TYPE_TIMESTAMP . '(4)', - $this->timestamp(4), - 'timestamp(4)' - ], - ]); - } - - public function conditionProvider() - { - return array_merge(parent::conditionProvider(), [ - // adding conditions for ILIKE i.e. case insensitive LIKE - // http://www.postgresql.org/docs/8.3/static/functions-matching.html#FUNCTIONS-LIKE - - // empty values - [ ['ilike', 'name', []], '0=1', [] ], - [ ['not ilike', 'name', []], '', [] ], - [ ['or ilike', 'name', []], '0=1', [] ], - [ ['or not ilike', 'name', []], '', [] ], - - // simple ilike - [ ['ilike', 'name', 'heyho'], '"name" ILIKE :qp0', [':qp0' => '%heyho%'] ], - [ ['not ilike', 'name', 'heyho'], '"name" NOT ILIKE :qp0', [':qp0' => '%heyho%'] ], - [ ['or ilike', 'name', 'heyho'], '"name" ILIKE :qp0', [':qp0' => '%heyho%'] ], - [ ['or not ilike', 'name', 'heyho'], '"name" NOT ILIKE :qp0', [':qp0' => '%heyho%'] ], - - // ilike for many values - [ ['ilike', 'name', ['heyho', 'abc']], '"name" ILIKE :qp0 AND "name" ILIKE :qp1', [':qp0' => '%heyho%', ':qp1' => '%abc%'] ], - [ ['not ilike', 'name', ['heyho', 'abc']], '"name" NOT ILIKE :qp0 AND "name" NOT ILIKE :qp1', [':qp0' => '%heyho%', ':qp1' => '%abc%'] ], - [ ['or ilike', 'name', ['heyho', 'abc']], '"name" ILIKE :qp0 OR "name" ILIKE :qp1', [':qp0' => '%heyho%', ':qp1' => '%abc%'] ], - [ ['or not ilike', 'name', ['heyho', 'abc']], '"name" NOT ILIKE :qp0 OR "name" NOT ILIKE :qp1', [':qp0' => '%heyho%', ':qp1' => '%abc%'] ], - ]); - } - - public function testAlterColumn() - { - $qb = $this->getQueryBuilder(); - - $expected = 'ALTER TABLE "foo1" ALTER COLUMN "bar" TYPE varchar(255)'; - $sql = $qb->alterColumn('foo1', 'bar', 'varchar(255)'); - $this->assertEquals($expected, $sql); - - $expected = 'ALTER TABLE "foo1" ALTER COLUMN "bar" SET NOT null'; - $sql = $qb->alterColumn('foo1', 'bar', 'SET NOT null'); - $this->assertEquals($expected, $sql); - - $expected = 'ALTER TABLE "foo1" ALTER COLUMN "bar" drop default'; - $sql = $qb->alterColumn('foo1', 'bar', 'drop default'); - $this->assertEquals($expected, $sql); - - $expected = 'ALTER TABLE "foo1" ALTER COLUMN "bar" reset xyz'; - $sql = $qb->alterColumn('foo1', 'bar', 'reset xyz'); - $this->assertEquals($expected, $sql); - } - - public function testCommentColumn() - { - $qb = $this->getQueryBuilder(); - - $expected = "COMMENT ON COLUMN [[comment]].[[text]] IS 'This is my column.'"; - $sql = $qb->addCommentOnColumn('comment', 'text', 'This is my column.'); - $this->assertEquals($this->replaceQuotes($expected), $sql); - - $expected = "COMMENT ON COLUMN [[comment]].[[text]] IS NULL"; - $sql = $qb->dropCommentFromColumn('comment', 'text'); - $this->assertEquals($this->replaceQuotes($expected), $sql); - } - - public function testCommentTable() - { - $qb = $this->getQueryBuilder(); - - $expected = "COMMENT ON TABLE [[comment]] IS 'This is my table.'"; - $sql = $qb->addCommentOnTable('comment', 'This is my table.'); - $this->assertEquals($this->replaceQuotes($expected), $sql); - - $expected = "COMMENT ON TABLE [[comment]] IS NULL"; - $sql = $qb->dropCommentFromTable('comment'); - $this->assertEquals($this->replaceQuotes($expected), $sql); - } -} diff --git a/tests/framework/db/pgsql/PostgreSQLQueryTest.php b/tests/framework/db/pgsql/PostgreSQLQueryTest.php deleted file mode 100644 index af2c383..0000000 --- a/tests/framework/db/pgsql/PostgreSQLQueryTest.php +++ /dev/null @@ -1,40 +0,0 @@ -getConnection(); - $command = $db->createCommand(); - $command->batchInsert('bool_values', - ['bool_col'], [ - [true], - [false], - ] - )->execute(); - - $this->assertEquals(1, (new Query())->from('bool_values')->where('bool_col = TRUE')->count('*', $db)); - $this->assertEquals(1, (new Query())->from('bool_values')->where('bool_col = FALSE')->count('*', $db)); - $this->assertEquals(2, (new Query())->from('bool_values')->where('bool_col IN (TRUE, FALSE)')->count('*', $db)); - - $this->assertEquals(1, (new Query())->from('bool_values')->where(['bool_col' => true])->count('*', $db)); - $this->assertEquals(1, (new Query())->from('bool_values')->where(['bool_col' => false])->count('*', $db)); - $this->assertEquals(2, (new Query())->from('bool_values')->where(['bool_col' => [true, false]])->count('*', $db)); - - $this->assertEquals(1, (new Query())->from('bool_values')->where('bool_col = :bool_col', ['bool_col' => true])->count('*', $db)); - $this->assertEquals(1, (new Query())->from('bool_values')->where('bool_col = :bool_col', ['bool_col' => false])->count('*', $db)); - } -} diff --git a/tests/framework/db/pgsql/PostgreSQLSchemaTest.php b/tests/framework/db/pgsql/PostgreSQLSchemaTest.php deleted file mode 100644 index c811847..0000000 --- a/tests/framework/db/pgsql/PostgreSQLSchemaTest.php +++ /dev/null @@ -1,159 +0,0 @@ - 'bigint', - 'dbType' => 'int8', - 'phpType' => 'integer', - 'allowNull' => true, - 'autoIncrement' => false, - 'enumValues' => null, - 'size' => null, - 'precision' => 64, - 'scale' => 0, - 'defaultValue' => null, - ]; - - return $columns; - } - - public function testGetPDOType() - { - $values = [ - [null, \PDO::PARAM_NULL], - ['', \PDO::PARAM_STR], - ['hello', \PDO::PARAM_STR], - [0, \PDO::PARAM_INT], - [1, \PDO::PARAM_INT], - [1337, \PDO::PARAM_INT], - [true, \PDO::PARAM_BOOL], - [false, \PDO::PARAM_BOOL], - [$fp = fopen(__FILE__, 'rb'), \PDO::PARAM_LOB], - ]; - - /* @var $schema Schema */ - $schema = $this->getConnection()->schema; - - foreach ($values as $value) { - $this->assertEquals($value[1], $schema->getPdoType($value[0])); - } - fclose($fp); - } - - public function testBooleanDefaultValues() - { - /* @var $schema Schema */ - $schema = $this->getConnection()->schema; - - $table = $schema->getTableSchema('bool_values'); - $this->assertSame(true, $table->getColumn('default_true')->defaultValue); - $this->assertSame(false, $table->getColumn('default_false')->defaultValue); - } - - public function testFindSchemaNames() - { - $schema = $this->getConnection()->schema; - - $this->assertEquals(3, count($schema->getSchemaNames())); - } - - public function bigintValueProvider() - { - return [ - [8817806877], - [3797444208], - [3199585540], - [1389831585], - [922337203685477580], - [9223372036854775807], - [-9223372036854775808] - ]; - } - - /** - * @dataProvider bigintValueProvider - */ - public function testBigintValue($bigint) - { - $this->mockApplication(); - ActiveRecord::$db = $this->getConnection(); - - Type::deleteAll(); - - $type = new Type(); - $type->setAttributes([ - 'bigint_col' => $bigint, - // whatever just to satisfy NOT NULL columns - 'int_col' => 1, 'char_col' => 'a', 'float_col' => 0.1, 'bool_col' => true, - ], false); - $type->save(false); - - $actual = Type::find()->one(); - $this->assertEquals($bigint, $actual->bigint_col); - } -} diff --git a/tests/framework/db/pgsql/QueryBuilderTest.php b/tests/framework/db/pgsql/QueryBuilderTest.php new file mode 100644 index 0000000..c30ec58 --- /dev/null +++ b/tests/framework/db/pgsql/QueryBuilderTest.php @@ -0,0 +1,123 @@ +boolean()->notNull()->defaultValue(true), + 'boolean NOT NULL DEFAULT TRUE' + ], + [ + Schema::TYPE_CHAR . ' CHECK (value LIKE \'test%\')', + $this->char()->check('value LIKE \'test%\''), + 'char(1) CHECK (value LIKE \'test%\')' + ], + [ + Schema::TYPE_CHAR . '(6) CHECK (value LIKE \'test%\')', + $this->char(6)->check('value LIKE \'test%\''), + 'char(6) CHECK (value LIKE \'test%\')' + ], + [ + Schema::TYPE_CHAR . '(6)', + $this->char(6)->unsigned(), + 'char(6)' + ], + [ + Schema::TYPE_INTEGER . '(8)', + $this->integer(8)->unsigned(), + 'integer' + ], + [ + Schema::TYPE_TIMESTAMP . '(4)', + $this->timestamp(4), + 'timestamp(4)' + ], + ]); + } + + public function conditionProvider() + { + return array_merge(parent::conditionProvider(), [ + // adding conditions for ILIKE i.e. case insensitive LIKE + // http://www.postgresql.org/docs/8.3/static/functions-matching.html#FUNCTIONS-LIKE + + // empty values + [ ['ilike', 'name', []], '0=1', [] ], + [ ['not ilike', 'name', []], '', [] ], + [ ['or ilike', 'name', []], '0=1', [] ], + [ ['or not ilike', 'name', []], '', [] ], + + // simple ilike + [ ['ilike', 'name', 'heyho'], '"name" ILIKE :qp0', [':qp0' => '%heyho%'] ], + [ ['not ilike', 'name', 'heyho'], '"name" NOT ILIKE :qp0', [':qp0' => '%heyho%'] ], + [ ['or ilike', 'name', 'heyho'], '"name" ILIKE :qp0', [':qp0' => '%heyho%'] ], + [ ['or not ilike', 'name', 'heyho'], '"name" NOT ILIKE :qp0', [':qp0' => '%heyho%'] ], + + // ilike for many values + [ ['ilike', 'name', ['heyho', 'abc']], '"name" ILIKE :qp0 AND "name" ILIKE :qp1', [':qp0' => '%heyho%', ':qp1' => '%abc%'] ], + [ ['not ilike', 'name', ['heyho', 'abc']], '"name" NOT ILIKE :qp0 AND "name" NOT ILIKE :qp1', [':qp0' => '%heyho%', ':qp1' => '%abc%'] ], + [ ['or ilike', 'name', ['heyho', 'abc']], '"name" ILIKE :qp0 OR "name" ILIKE :qp1', [':qp0' => '%heyho%', ':qp1' => '%abc%'] ], + [ ['or not ilike', 'name', ['heyho', 'abc']], '"name" NOT ILIKE :qp0 OR "name" NOT ILIKE :qp1', [':qp0' => '%heyho%', ':qp1' => '%abc%'] ], + ]); + } + + public function testAlterColumn() + { + $qb = $this->getQueryBuilder(); + + $expected = 'ALTER TABLE "foo1" ALTER COLUMN "bar" TYPE varchar(255)'; + $sql = $qb->alterColumn('foo1', 'bar', 'varchar(255)'); + $this->assertEquals($expected, $sql); + + $expected = 'ALTER TABLE "foo1" ALTER COLUMN "bar" SET NOT null'; + $sql = $qb->alterColumn('foo1', 'bar', 'SET NOT null'); + $this->assertEquals($expected, $sql); + + $expected = 'ALTER TABLE "foo1" ALTER COLUMN "bar" drop default'; + $sql = $qb->alterColumn('foo1', 'bar', 'drop default'); + $this->assertEquals($expected, $sql); + + $expected = 'ALTER TABLE "foo1" ALTER COLUMN "bar" reset xyz'; + $sql = $qb->alterColumn('foo1', 'bar', 'reset xyz'); + $this->assertEquals($expected, $sql); + } + + public function testCommentColumn() + { + $qb = $this->getQueryBuilder(); + + $expected = "COMMENT ON COLUMN [[comment]].[[text]] IS 'This is my column.'"; + $sql = $qb->addCommentOnColumn('comment', 'text', 'This is my column.'); + $this->assertEquals($this->replaceQuotes($expected), $sql); + + $expected = "COMMENT ON COLUMN [[comment]].[[text]] IS NULL"; + $sql = $qb->dropCommentFromColumn('comment', 'text'); + $this->assertEquals($this->replaceQuotes($expected), $sql); + } + + public function testCommentTable() + { + $qb = $this->getQueryBuilder(); + + $expected = "COMMENT ON TABLE [[comment]] IS 'This is my table.'"; + $sql = $qb->addCommentOnTable('comment', 'This is my table.'); + $this->assertEquals($this->replaceQuotes($expected), $sql); + + $expected = "COMMENT ON TABLE [[comment]] IS NULL"; + $sql = $qb->dropCommentFromTable('comment'); + $this->assertEquals($this->replaceQuotes($expected), $sql); + } +} diff --git a/tests/framework/db/pgsql/QueryTest.php b/tests/framework/db/pgsql/QueryTest.php new file mode 100644 index 0000000..bf3de41 --- /dev/null +++ b/tests/framework/db/pgsql/QueryTest.php @@ -0,0 +1,38 @@ +getConnection(); + $command = $db->createCommand(); + $command->batchInsert('bool_values', + ['bool_col'], [ + [true], + [false], + ] + )->execute(); + + $this->assertEquals(1, (new Query())->from('bool_values')->where('bool_col = TRUE')->count('*', $db)); + $this->assertEquals(1, (new Query())->from('bool_values')->where('bool_col = FALSE')->count('*', $db)); + $this->assertEquals(2, (new Query())->from('bool_values')->where('bool_col IN (TRUE, FALSE)')->count('*', $db)); + + $this->assertEquals(1, (new Query())->from('bool_values')->where(['bool_col' => true])->count('*', $db)); + $this->assertEquals(1, (new Query())->from('bool_values')->where(['bool_col' => false])->count('*', $db)); + $this->assertEquals(2, (new Query())->from('bool_values')->where(['bool_col' => [true, false]])->count('*', $db)); + + $this->assertEquals(1, (new Query())->from('bool_values')->where('bool_col = :bool_col', ['bool_col' => true])->count('*', $db)); + $this->assertEquals(1, (new Query())->from('bool_values')->where('bool_col = :bool_col', ['bool_col' => false])->count('*', $db)); + } +} diff --git a/tests/framework/db/pgsql/SchemaTest.php b/tests/framework/db/pgsql/SchemaTest.php new file mode 100644 index 0000000..73bd2ea --- /dev/null +++ b/tests/framework/db/pgsql/SchemaTest.php @@ -0,0 +1,158 @@ + 'bigint', + 'dbType' => 'int8', + 'phpType' => 'integer', + 'allowNull' => true, + 'autoIncrement' => false, + 'enumValues' => null, + 'size' => null, + 'precision' => 64, + 'scale' => 0, + 'defaultValue' => null, + ]; + + return $columns; + } + + public function testGetPDOType() + { + $values = [ + [null, \PDO::PARAM_NULL], + ['', \PDO::PARAM_STR], + ['hello', \PDO::PARAM_STR], + [0, \PDO::PARAM_INT], + [1, \PDO::PARAM_INT], + [1337, \PDO::PARAM_INT], + [true, \PDO::PARAM_BOOL], + [false, \PDO::PARAM_BOOL], + [$fp = fopen(__FILE__, 'rb'), \PDO::PARAM_LOB], + ]; + + /* @var $schema Schema */ + $schema = $this->getConnection()->schema; + + foreach ($values as $value) { + $this->assertEquals($value[1], $schema->getPdoType($value[0])); + } + fclose($fp); + } + + public function testBooleanDefaultValues() + { + /* @var $schema Schema */ + $schema = $this->getConnection()->schema; + + $table = $schema->getTableSchema('bool_values'); + $this->assertSame(true, $table->getColumn('default_true')->defaultValue); + $this->assertSame(false, $table->getColumn('default_false')->defaultValue); + } + + public function testFindSchemaNames() + { + $schema = $this->getConnection()->schema; + + $this->assertEquals(3, count($schema->getSchemaNames())); + } + + public function bigintValueProvider() + { + return [ + [8817806877], + [3797444208], + [3199585540], + [1389831585], + [922337203685477580], + [9223372036854775807], + [-9223372036854775808] + ]; + } + + /** + * @dataProvider bigintValueProvider + */ + public function testBigintValue($bigint) + { + $this->mockApplication(); + ActiveRecord::$db = $this->getConnection(); + + Type::deleteAll(); + + $type = new Type(); + $type->setAttributes([ + 'bigint_col' => $bigint, + // whatever just to satisfy NOT NULL columns + 'int_col' => 1, 'char_col' => 'a', 'float_col' => 0.1, 'bool_col' => true, + ], false); + $type->save(false); + + $actual = Type::find()->one(); + $this->assertEquals($bigint, $actual->bigint_col); + } +} diff --git a/tests/framework/db/sqlite/ActiveDataProviderTest.php b/tests/framework/db/sqlite/ActiveDataProviderTest.php new file mode 100644 index 0000000..e1d7b69 --- /dev/null +++ b/tests/framework/db/sqlite/ActiveDataProviderTest.php @@ -0,0 +1,13 @@ +getConnection(false); + + $sql = 'SELECT [[id]], [[t.name]] FROM {{customer}} t'; + $command = $db->createCommand($sql); + $this->assertEquals("SELECT `id`, `t`.`name` FROM `customer` t", $command->sql); + } +} diff --git a/tests/framework/db/sqlite/ConnectionTest.php b/tests/framework/db/sqlite/ConnectionTest.php new file mode 100644 index 0000000..21d4b95 --- /dev/null +++ b/tests/framework/db/sqlite/ConnectionTest.php @@ -0,0 +1,139 @@ +getConnection(false); + $params = $this->database; + + $this->assertEquals($params['dsn'], $connection->dsn); + } + + public function testQuoteValue() + { + $connection = $this->getConnection(false); + $this->assertEquals(123, $connection->quoteValue(123)); + $this->assertEquals("'string'", $connection->quoteValue('string')); + $this->assertEquals("'It''s interesting'", $connection->quoteValue("It's interesting")); + } + + public function testTransactionIsolation() + { + $connection = $this->getConnection(true); + + $transaction = $connection->beginTransaction(Transaction::READ_UNCOMMITTED); + $transaction->rollBack(); + + $transaction = $connection->beginTransaction(Transaction::SERIALIZABLE); + $transaction->rollBack(); + } + + public function testMasterSlave() + { + $counts = [[0, 2], [1, 2], [2, 2]]; + + foreach ($counts as $count) { + list($masterCount, $slaveCount) = $count; + + $db = $this->prepareMasterSlave($masterCount, $slaveCount); + + $this->assertTrue($db->getSlave() instanceof Connection); + $this->assertTrue($db->getSlave()->isActive); + $this->assertFalse($db->isActive); + + // test SELECT uses slave + $this->assertEquals(2, $db->createCommand('SELECT COUNT(*) FROM profile')->queryScalar()); + $this->assertFalse($db->isActive); + + // test UPDATE uses master + $db->createCommand("UPDATE profile SET description='test' WHERE id=1")->execute(); + $this->assertTrue($db->isActive); + $this->assertNotEquals('test', $db->createCommand("SELECT description FROM profile WHERE id=1")->queryScalar()); + $result = $db->useMaster(function (Connection $db) { + return $db->createCommand("SELECT description FROM profile WHERE id=1")->queryScalar(); + }); + $this->assertEquals('test', $result); + + // test ActiveRecord read/write split + ActiveRecord::$db = $db = $this->prepareMasterSlave($masterCount, $slaveCount); + $this->assertFalse($db->isActive); + + $customer = Customer::findOne(1); + $this->assertTrue($customer instanceof Customer); + $this->assertEquals('user1', $customer->name); + $this->assertFalse($db->isActive); + + $customer->name = 'test'; + $customer->save(); + $this->assertTrue($db->isActive); + $customer = Customer::findOne(1); + $this->assertTrue($customer instanceof Customer); + $this->assertEquals('user1', $customer->name); + $result = $db->useMaster(function () { + return Customer::findOne(1)->name; + }); + $this->assertEquals('test', $result); + } + } + + /** + * @param integer $masterCount + * @param integer $slaveCount + * @return Connection + */ + protected function prepareMasterSlave($masterCount, $slaveCount) + { + $databases = self::getParam('databases'); + $fixture = $databases[$this->driverName]['fixture']; + $basePath = \Yii::getAlias('@yiiunit/runtime'); + + $config = [ + 'class' => 'yii\db\Connection', + 'dsn' => "sqlite:$basePath/yii2test.sq3", + ]; + $this->prepareDatabase($config, $fixture)->close(); + + for ($i = 0; $i < $masterCount; ++$i) { + $master = ['dsn' => "sqlite:$basePath/yii2test_master{$i}.sq3"]; + $db = $this->prepareDatabase($master, $fixture); + $db->close(); + $config['masters'][] = $master; + } + + for ($i = 0; $i < $slaveCount; ++$i) { + $slave = ['dsn' => "sqlite:$basePath/yii2test_slave{$i}.sq3"]; + $db = $this->prepareDatabase($slave, $fixture); + $db->close(); + $config['slaves'][] = $slave; + } + + return \Yii::createObject($config); + } + + public function testAliasDbPath() + { + $config = [ + 'dsn' => "sqlite:@yiiunit/runtime/yii2aliastest.sq3", + ]; + $connection = new Connection($config); + $connection->open(); + $this->assertTrue($connection->isActive); + $this->assertEquals($config['dsn'], $connection->dsn); + + $connection->close(); + } +} diff --git a/tests/framework/db/sqlite/QueryBuilderTest.php b/tests/framework/db/sqlite/QueryBuilderTest.php new file mode 100644 index 0000000..ec1934a --- /dev/null +++ b/tests/framework/db/sqlite/QueryBuilderTest.php @@ -0,0 +1,104 @@ +primaryKey()->first()->after('col_before'), + 'integer PRIMARY KEY AUTOINCREMENT NOT NULL' + ], + ]); + } + + public function conditionProvider() + { + return array_merge(parent::conditionProvider(), [ + 'composite in using array objects' => [ + ['in', new TraversableObject(['id', 'name']), new TraversableObject([ + ['id' => 1, 'name' => 'oy'], + ['id' => 2, 'name' => 'yo'], + ])], + '(([[id]] = :qp0 AND [[name]] = :qp1) OR ([[id]] = :qp2 AND [[name]] = :qp3))', + [':qp0' => 1, ':qp1' => 'oy', ':qp2' => 2, ':qp3' => 'yo'] + ], + 'composite in' => [ + ['in', ['id', 'name'], [['id' =>1, 'name' => 'oy']]], + '(([[id]] = :qp0 AND [[name]] = :qp1))', + [':qp0' => 1, ':qp1' => 'oy'] + ], + ]); + } + + public function testAddDropPrimaryKey() + { + $this->markTestSkipped('Comments are not supported in SQLite'); + } + + public function testCommentColumn() + { + $this->markTestSkipped('Comments are not supported in SQLite'); + } + + public function testCommentTable() + { + $this->markTestSkipped('Comments are not supported in SQLite'); + } + + public function testBatchInsert() + { + $db = $this->getConnection(); + if (version_compare($db->pdo->getAttribute(\PDO::ATTR_SERVER_VERSION), '3.7.11', '>=')) { + $this->markTestSkipped('This test is only relevant for SQLite < 3.7.11'); + } + $sql = $this->getQueryBuilder()->batchInsert('{{customer}} t', ['t.id', 't.name'], [[1, 'a'], [2, 'b']]); + $this->assertEquals("INSERT INTO {{customer}} t (`t`.`id`, `t`.`name`) SELECT 1, 'a' UNION SELECT 2, 'b'", $sql); + } + + public function testRenameTable() + { + $sql = $this->getQueryBuilder()->renameTable('table_from', 'table_to'); + $this->assertEquals("ALTER TABLE `table_from` RENAME TO `table_to`", $sql); + } + + /** + * @inheritdoc + */ + public function testBuildUnion() + { + $expectedQuerySql = $this->replaceQuotes( + "SELECT `id` FROM `TotalExample` `t1` WHERE (w > 0) AND (x < 2) UNION SELECT `id` FROM `TotalTotalExample` `t2` WHERE w > 5 UNION ALL SELECT `id` FROM `TotalTotalExample` `t3` WHERE w = 3" + ); + $query = new Query(); + $secondQuery = new Query(); + $secondQuery->select('id') + ->from('TotalTotalExample t2') + ->where('w > 5'); + $thirdQuery = new Query(); + $thirdQuery->select('id') + ->from('TotalTotalExample t3') + ->where('w = 3'); + $query->select('id') + ->from('TotalExample t1') + ->where(['and', 'w > 0', 'x < 2']) + ->union($secondQuery) + ->union($thirdQuery, TRUE); + list($actualQuerySql, $queryParams) = $this->getQueryBuilder()->build($query); + $this->assertEquals($expectedQuerySql, $actualQuerySql); + $this->assertEquals([], $queryParams); + } +} diff --git a/tests/framework/db/sqlite/QueryTest.php b/tests/framework/db/sqlite/QueryTest.php new file mode 100644 index 0000000..17baad4 --- /dev/null +++ b/tests/framework/db/sqlite/QueryTest.php @@ -0,0 +1,29 @@ +getConnection(); + $query = new Query; + $query->select(['id', 'name']) + ->from('item') + ->union( + (new Query()) + ->select(['id', 'name']) + ->from(['category']) + ); + $result = $query->all($connection); + $this->assertNotEmpty($result); + $this->assertSame(7, count($result)); + } +} diff --git a/tests/framework/db/sqlite/SchemaTest.php b/tests/framework/db/sqlite/SchemaTest.php new file mode 100644 index 0000000..2761c4b --- /dev/null +++ b/tests/framework/db/sqlite/SchemaTest.php @@ -0,0 +1,32 @@ +getConnection(false); - - $sql = 'SELECT [[id]], [[t.name]] FROM {{customer}} t'; - $command = $db->createCommand($sql); - $this->assertEquals("SELECT `id`, `t`.`name` FROM `customer` t", $command->sql); - } -} diff --git a/tests/framework/db/sqlite/SqliteConnectionTest.php b/tests/framework/db/sqlite/SqliteConnectionTest.php deleted file mode 100644 index e7868df..0000000 --- a/tests/framework/db/sqlite/SqliteConnectionTest.php +++ /dev/null @@ -1,139 +0,0 @@ -getConnection(false); - $params = $this->database; - - $this->assertEquals($params['dsn'], $connection->dsn); - } - - public function testQuoteValue() - { - $connection = $this->getConnection(false); - $this->assertEquals(123, $connection->quoteValue(123)); - $this->assertEquals("'string'", $connection->quoteValue('string')); - $this->assertEquals("'It''s interesting'", $connection->quoteValue("It's interesting")); - } - - public function testTransactionIsolation() - { - $connection = $this->getConnection(true); - - $transaction = $connection->beginTransaction(Transaction::READ_UNCOMMITTED); - $transaction->rollBack(); - - $transaction = $connection->beginTransaction(Transaction::SERIALIZABLE); - $transaction->rollBack(); - } - - public function testMasterSlave() - { - $counts = [[0, 2], [1, 2], [2, 2]]; - - foreach ($counts as $count) { - list($masterCount, $slaveCount) = $count; - - $db = $this->prepareMasterSlave($masterCount, $slaveCount); - - $this->assertTrue($db->getSlave() instanceof Connection); - $this->assertTrue($db->getSlave()->isActive); - $this->assertFalse($db->isActive); - - // test SELECT uses slave - $this->assertEquals(2, $db->createCommand('SELECT COUNT(*) FROM profile')->queryScalar()); - $this->assertFalse($db->isActive); - - // test UPDATE uses master - $db->createCommand("UPDATE profile SET description='test' WHERE id=1")->execute(); - $this->assertTrue($db->isActive); - $this->assertNotEquals('test', $db->createCommand("SELECT description FROM profile WHERE id=1")->queryScalar()); - $result = $db->useMaster(function (Connection $db) { - return $db->createCommand("SELECT description FROM profile WHERE id=1")->queryScalar(); - }); - $this->assertEquals('test', $result); - - // test ActiveRecord read/write split - ActiveRecord::$db = $db = $this->prepareMasterSlave($masterCount, $slaveCount); - $this->assertFalse($db->isActive); - - $customer = Customer::findOne(1); - $this->assertTrue($customer instanceof Customer); - $this->assertEquals('user1', $customer->name); - $this->assertFalse($db->isActive); - - $customer->name = 'test'; - $customer->save(); - $this->assertTrue($db->isActive); - $customer = Customer::findOne(1); - $this->assertTrue($customer instanceof Customer); - $this->assertEquals('user1', $customer->name); - $result = $db->useMaster(function () { - return Customer::findOne(1)->name; - }); - $this->assertEquals('test', $result); - } - } - - /** - * @param integer $masterCount - * @param integer $slaveCount - * @return Connection - */ - protected function prepareMasterSlave($masterCount, $slaveCount) - { - $databases = self::getParam('databases'); - $fixture = $databases[$this->driverName]['fixture']; - $basePath = \Yii::getAlias('@yiiunit/runtime'); - - $config = [ - 'class' => 'yii\db\Connection', - 'dsn' => "sqlite:$basePath/yii2test.sq3", - ]; - $this->prepareDatabase($config, $fixture)->close(); - - for ($i = 0; $i < $masterCount; ++$i) { - $master = ['dsn' => "sqlite:$basePath/yii2test_master{$i}.sq3"]; - $db = $this->prepareDatabase($master, $fixture); - $db->close(); - $config['masters'][] = $master; - } - - for ($i = 0; $i < $slaveCount; ++$i) { - $slave = ['dsn' => "sqlite:$basePath/yii2test_slave{$i}.sq3"]; - $db = $this->prepareDatabase($slave, $fixture); - $db->close(); - $config['slaves'][] = $slave; - } - - return \Yii::createObject($config); - } - - public function testAliasDbPath() - { - $config = [ - 'dsn' => "sqlite:@yiiunit/runtime/yii2aliastest.sq3", - ]; - $connection = new Connection($config); - $connection->open(); - $this->assertTrue($connection->isActive); - $this->assertEquals($config['dsn'], $connection->dsn); - - $connection->close(); - } -} diff --git a/tests/framework/db/sqlite/SqliteQueryBuilderTest.php b/tests/framework/db/sqlite/SqliteQueryBuilderTest.php deleted file mode 100644 index c4834a1..0000000 --- a/tests/framework/db/sqlite/SqliteQueryBuilderTest.php +++ /dev/null @@ -1,105 +0,0 @@ -primaryKey()->first()->after('col_before'), - 'integer PRIMARY KEY AUTOINCREMENT NOT NULL' - ], - ]); - } - - public function conditionProvider() - { - return array_merge(parent::conditionProvider(), [ - 'composite in using array objects' => [ - ['in', new TraversableObject(['id', 'name']), new TraversableObject([ - ['id' => 1, 'name' => 'oy'], - ['id' => 2, 'name' => 'yo'], - ])], - '(([[id]] = :qp0 AND [[name]] = :qp1) OR ([[id]] = :qp2 AND [[name]] = :qp3))', - [':qp0' => 1, ':qp1' => 'oy', ':qp2' => 2, ':qp3' => 'yo'] - ], - 'composite in' => [ - ['in', ['id', 'name'], [['id' =>1, 'name' => 'oy']]], - '(([[id]] = :qp0 AND [[name]] = :qp1))', - [':qp0' => 1, ':qp1' => 'oy'] - ], - ]); - } - - public function testAddDropPrimaryKey() - { - $this->markTestSkipped('Comments are not supported in SQLite'); - } - - public function testCommentColumn() - { - $this->markTestSkipped('Comments are not supported in SQLite'); - } - - public function testCommentTable() - { - $this->markTestSkipped('Comments are not supported in SQLite'); - } - - public function testBatchInsert() - { - $db = $this->getConnection(); - if (version_compare($db->pdo->getAttribute(\PDO::ATTR_SERVER_VERSION), '3.7.11', '>=')) { - $this->markTestSkipped('This test is only relevant for SQLite < 3.7.11'); - } - $sql = $this->getQueryBuilder()->batchInsert('{{customer}} t', ['t.id', 't.name'], [[1, 'a'], [2, 'b']]); - $this->assertEquals("INSERT INTO {{customer}} t (`t`.`id`, `t`.`name`) SELECT 1, 'a' UNION SELECT 2, 'b'", $sql); - } - - public function testRenameTable() - { - $sql = $this->getQueryBuilder()->renameTable('table_from', 'table_to'); - $this->assertEquals("ALTER TABLE `table_from` RENAME TO `table_to`", $sql); - } - - /** - * @inheritdoc - */ - public function testBuildUnion() - { - $expectedQuerySql = $this->replaceQuotes( - "SELECT `id` FROM `TotalExample` `t1` WHERE (w > 0) AND (x < 2) UNION SELECT `id` FROM `TotalTotalExample` `t2` WHERE w > 5 UNION ALL SELECT `id` FROM `TotalTotalExample` `t3` WHERE w = 3" - ); - $query = new Query(); - $secondQuery = new Query(); - $secondQuery->select('id') - ->from('TotalTotalExample t2') - ->where('w > 5'); - $thirdQuery = new Query(); - $thirdQuery->select('id') - ->from('TotalTotalExample t3') - ->where('w = 3'); - $query->select('id') - ->from('TotalExample t1') - ->where(['and', 'w > 0', 'x < 2']) - ->union($secondQuery) - ->union($thirdQuery, TRUE); - list($actualQuerySql, $queryParams) = $this->getQueryBuilder()->build($query); - $this->assertEquals($expectedQuerySql, $actualQuerySql); - $this->assertEquals([], $queryParams); - } -} diff --git a/tests/framework/db/sqlite/SqliteQueryTest.php b/tests/framework/db/sqlite/SqliteQueryTest.php deleted file mode 100644 index 82a074a..0000000 --- a/tests/framework/db/sqlite/SqliteQueryTest.php +++ /dev/null @@ -1,30 +0,0 @@ -getConnection(); - $query = new Query; - $query->select(['id', 'name']) - ->from('item') - ->union( - (new Query()) - ->select(['id', 'name']) - ->from(['category']) - ); - $result = $query->all($connection); - $this->assertNotEmpty($result); - $this->assertSame(7, count($result)); - } -} diff --git a/tests/framework/db/sqlite/SqliteSchemaTest.php b/tests/framework/db/sqlite/SqliteSchemaTest.php deleted file mode 100644 index b66e5c3..0000000 --- a/tests/framework/db/sqlite/SqliteSchemaTest.php +++ /dev/null @@ -1,33 +0,0 @@ -