From d15378ef43d08ef29090f77a84cabf2f82f7bc42 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Sat, 5 Jan 2013 21:29:42 -0500 Subject: [PATCH] moved db tests. --- framework/db/ActiveRecord.php | 21 +- tests/unit/framework/db/ActiveRecordTest.php | 404 ++++++++++++++++++++++++ tests/unit/framework/db/CommandTest.php | 322 +++++++++++++++++++ tests/unit/framework/db/ConnectionTest.php | 86 +++++ tests/unit/framework/db/QueryTest.php | 110 +++++++ tests/unit/framework/db/ar/ActiveRecordTest.php | 404 ------------------------ tests/unit/framework/db/dao/CommandTest.php | 216 ------------- tests/unit/framework/db/dao/ConnectionTest.php | 86 ----- tests/unit/framework/db/dao/QueryTest.php | 215 ------------- 9 files changed, 932 insertions(+), 932 deletions(-) create mode 100644 tests/unit/framework/db/ActiveRecordTest.php create mode 100644 tests/unit/framework/db/CommandTest.php create mode 100644 tests/unit/framework/db/ConnectionTest.php create mode 100644 tests/unit/framework/db/QueryTest.php delete mode 100644 tests/unit/framework/db/ar/ActiveRecordTest.php delete mode 100644 tests/unit/framework/db/dao/CommandTest.php delete mode 100644 tests/unit/framework/db/dao/ConnectionTest.php delete mode 100644 tests/unit/framework/db/dao/QueryTest.php diff --git a/framework/db/ActiveRecord.php b/framework/db/ActiveRecord.php index 2424446..1a8c6bd 100644 --- a/framework/db/ActiveRecord.php +++ b/framework/db/ActiveRecord.php @@ -189,9 +189,9 @@ abstract class ActiveRecord extends Model */ public static function updateAll($attributes, $condition = '', $params = array()) { - $query = new Query; - $query->update(static::tableName(), $attributes, $condition, $params); - return $query->createCommand(static::getDbConnection())->execute(); + $command = static::getDbConnection()->createCommand(); + $command->update(static::tableName(), $attributes, $condition, $params); + return $command->execute(); } /** @@ -210,9 +210,9 @@ abstract class ActiveRecord extends Model $quotedName = $db->quoteColumnName($name, true); $counters[$name] = new Expression($value >= 0 ? "$quotedName+$value" : "$quotedName$value"); } - $query = new Query; - $query->update(static::tableName(), $counters, $condition, $params); - return $query->createCommand($db)->execute(); + $command = $db->createCommand(); + $command->update(static::tableName(), $counters, $condition, $params); + return $command->execute(); } /** @@ -224,9 +224,9 @@ abstract class ActiveRecord extends Model */ public static function deleteAll($condition = '', $params = array()) { - $query = new Query; - $query->delete(static::tableName(), $condition, $params); - return $query->createCommand(static::getDbConnection())->execute(); + $command = static::getDbConnection()->createCommand(); + $command->delete(static::tableName(), $condition, $params); + return $command->execute(); } /** @@ -587,10 +587,9 @@ abstract class ActiveRecord extends Model public function insert($attributes = null) { if ($this->beforeInsert()) { - $query = new Query; $values = $this->getChangedAttributes($attributes); $db = $this->getDbConnection(); - $command = $query->insert($this->tableName(), $values)->createCommand($db); + $command = $db->createCommand()->insert($this->tableName(), $values); if ($command->execute()) { $table = $this->getTableSchema(); if ($table->sequenceName !== null) { diff --git a/tests/unit/framework/db/ActiveRecordTest.php b/tests/unit/framework/db/ActiveRecordTest.php new file mode 100644 index 0000000..dc47010 --- /dev/null +++ b/tests/unit/framework/db/ActiveRecordTest.php @@ -0,0 +1,404 @@ +getConnection(); + } + + public function testFind() + { + // find one + $result = Customer::find(); + $this->assertTrue($result instanceof ActiveQuery); + $customer = $result->one(); + $this->assertTrue($customer instanceof Customer); + + // find all + $customers = Customer::find()->all(); + $this->assertEquals(3, count($customers)); + $this->assertTrue($customers[0] instanceof Customer); + $this->assertTrue($customers[1] instanceof Customer); + $this->assertTrue($customers[2] instanceof Customer); + + // find by a single primary key + $customer = Customer::find(2); + $this->assertTrue($customer instanceof Customer); + $this->assertEquals('user2', $customer->name); + + // find by attributes + $customer = Customer::find()->where(array('name' => 'user2'))->one(); + $this->assertTrue($customer instanceof Customer); + $this->assertEquals(2, $customer->id); + + // find by Query array + $query = array( + 'where' => 'id=:id', + 'params' => array(':id' => 2), + ); + $customer = Customer::find($query)->one(); + $this->assertTrue($customer instanceof Customer); + $this->assertEquals('user2', $customer->name); + + // find count + $this->assertEquals(3, Customer::count()->value()); + $this->assertEquals(2, Customer::count(array( + 'where' => 'id=1 OR id=2', + ))->value()); + $this->assertEquals(2, Customer::find()->select('COUNT(*)')->where('id=1 OR id=2')->value()); + + // asArray + $customer = Customer::find()->where('id=2')->asArray()->one(); + $this->assertEquals(array( + 'id' => '2', + 'email' => 'user2@example.com', + 'name' => 'user2', + 'address' => 'address2', + 'status' => '1', + ), $customer); + + // indexBy + $customers = Customer::find()->indexBy('name')->orderBy('id')->all(); + $this->assertEquals(3, count($customers)); + $this->assertTrue($customers['user1'] instanceof Customer); + $this->assertTrue($customers['user2'] instanceof Customer); + $this->assertTrue($customers['user3'] instanceof Customer); + } + + public function testFindBySql() + { + // find one + $customer = Customer::findBySql('SELECT * FROM tbl_customer ORDER BY id DESC')->one(); + $this->assertTrue($customer instanceof Customer); + $this->assertEquals('user3', $customer->name); + + // find all + $customers = Customer::findBySql('SELECT * FROM tbl_customer')->all(); + $this->assertEquals(3, count($customers)); + + // find with parameter binding + $customer = Customer::findBySql('SELECT * FROM tbl_customer WHERE id=:id', array(':id' => 2))->one(); + $this->assertTrue($customer instanceof Customer); + $this->assertEquals('user2', $customer->name); + } + + public function testScope() + { + $customers = Customer::find(array( + 'scopes' => array('active'), + ))->all(); + $this->assertEquals(2, count($customers)); + + $customers = Customer::find()->active()->all(); + $this->assertEquals(2, count($customers)); + } + + public function testFindLazy() + { + /** @var $customer Customer */ + $customer = Customer::find(2); + $orders = $customer->orders; + $this->assertEquals(2, count($orders)); + + $orders = $customer->orders()->where('id=3')->all(); + $this->assertEquals(1, count($orders)); + $this->assertEquals(3, $orders[0]->id); + } + + public function testFindEager() + { + $customers = Customer::find()->with('orders')->all(); + $this->assertEquals(3, count($customers)); + $this->assertEquals(1, count($customers[0]->orders)); + $this->assertEquals(2, count($customers[1]->orders)); + } + + public function testFindLazyVia() + { + /** @var $order Order */ + $order = Order::find(1); + $this->assertEquals(1, $order->id); + $this->assertEquals(2, count($order->items)); + $this->assertEquals(1, $order->items[0]->id); + $this->assertEquals(2, $order->items[1]->id); + + $order = Order::find(1); + $order->id = 100; + $this->assertEquals(array(), $order->items); + } + + public function testFindEagerViaRelation() + { + $orders = Order::find()->with('items')->orderBy('id')->all(); + $this->assertEquals(3, count($orders)); + $order = $orders[0]; + $this->assertEquals(1, $order->id); + $this->assertEquals(2, count($order->items)); + $this->assertEquals(1, $order->items[0]->id); + $this->assertEquals(2, $order->items[1]->id); + } + + public function testFindLazyViaTable() + { + /** @var $order Order */ + $order = Order::find(1); + $this->assertEquals(1, $order->id); + $this->assertEquals(2, count($order->books)); + $this->assertEquals(1, $order->items[0]->id); + $this->assertEquals(2, $order->items[1]->id); + + $order = Order::find(2); + $this->assertEquals(2, $order->id); + $this->assertEquals(0, count($order->books)); + } + + public function testFindEagerViaTable() + { + $orders = Order::find()->with('books')->orderBy('id')->all(); + $this->assertEquals(3, count($orders)); + + $order = $orders[0]; + $this->assertEquals(1, $order->id); + $this->assertEquals(2, count($order->books)); + $this->assertEquals(1, $order->books[0]->id); + $this->assertEquals(2, $order->books[1]->id); + + $order = $orders[1]; + $this->assertEquals(2, $order->id); + $this->assertEquals(0, count($order->books)); + + $order = $orders[2]; + $this->assertEquals(3, $order->id); + $this->assertEquals(1, count($order->books)); + $this->assertEquals(2, $order->books[0]->id); + } + + public function testFindNestedRelation() + { + $customers = Customer::find()->with('orders', 'orders.items')->all(); + $this->assertEquals(3, count($customers)); + $this->assertEquals(1, count($customers[0]->orders)); + $this->assertEquals(2, count($customers[1]->orders)); + $this->assertEquals(0, count($customers[2]->orders)); + $this->assertEquals(2, count($customers[0]->orders[0]->items)); + $this->assertEquals(3, count($customers[1]->orders[0]->items)); + $this->assertEquals(1, count($customers[1]->orders[1]->items)); + } + +// public function testInsert() +// { +// $customer = new Customer; +// $customer->email = 'user4@example.com'; +// $customer->name = 'user4'; +// $customer->address = 'address4'; +// +// $this->assertNull($customer->id); +// $this->assertTrue($customer->isNewRecord); +// +// $customer->save(); +// +// $this->assertEquals(4, $customer->id); +// $this->assertFalse($customer->isNewRecord); +// } +// +// public function testUpdate() +// { +// // save +// $customer = Customer::find(2); +// $this->assertTrue($customer instanceof Customer); +// $this->assertEquals('user2', $customer->name); +// $this->assertFalse($customer->isNewRecord); +// $customer->name = 'user2x'; +// $customer->save(); +// $this->assertEquals('user2x', $customer->name); +// $this->assertFalse($customer->isNewRecord); +// $customer2 = Customer::find(2); +// $this->assertEquals('user2x', $customer2->name); +// +// // updateCounters +// $pk = array('order_id' => 2, 'item_id' => 4); +// $orderItem = OrderItem::find()->where($pk)->one(); +// $this->assertEquals(1, $orderItem->quantity); +// $ret = $orderItem->updateCounters(array('quantity' => -1)); +// $this->assertTrue($ret); +// $this->assertEquals(0, $orderItem->quantity); +// $orderItem = OrderItem::find()->where($pk)->one(); +// $this->assertEquals(0, $orderItem->quantity); +// +// // updateAll +// $customer = Customer::find(3); +// $this->assertEquals('user3', $customer->name); +// $ret = Customer::updateAll(array( +// 'name' => 'temp', +// ), array('id' => 3)); +// $this->assertEquals(1, $ret); +// $customer = Customer::find(3); +// $this->assertEquals('temp', $customer->name); +// +// // updateCounters +// $pk = array('order_id' => 1, 'item_id' => 2); +// $orderItem = OrderItem::find()->where($pk)->one(); +// $this->assertEquals(2, $orderItem->quantity); +// $ret = OrderItem::updateAllCounters(array( +// 'quantity' => 3, +// ), $pk); +// $this->assertEquals(1, $ret); +// $orderItem = OrderItem::find()->where($pk)->one(); +// $this->assertEquals(5, $orderItem->quantity); +// } +// +// public function testDelete() +// { +// // delete +// $customer = Customer::find(2); +// $this->assertTrue($customer instanceof Customer); +// $this->assertEquals('user2', $customer->name); +// $customer->delete(); +// $customer = Customer::find(2); +// $this->assertNull($customer); +// +// // deleteAll +// $customers = Customer::find()->all(); +// $this->assertEquals(2, count($customers)); +// $ret = Customer::deleteAll(); +// $this->assertEquals(2, $ret); +// $customers = Customer::find()->all(); +// $this->assertEquals(0, count($customers)); +// } +// +// public function testFind() +// { +// // find one +// $result = Customer::find(); +// $this->assertTrue($result instanceof ActiveQuery); +// $customer = $result->one(); +// $this->assertTrue($customer instanceof Customer); +// $this->assertEquals(1, $result->count); +// +// // find all +// $result = Customer::find(); +// $customers = $result->all(); +// $this->assertTrue(is_array($customers)); +// $this->assertEquals(3, count($customers)); +// $this->assertTrue($customers[0] instanceof Customer); +// $this->assertTrue($customers[1] instanceof Customer); +// $this->assertTrue($customers[2] instanceof Customer); +// $this->assertEquals(3, $result->count); +// $this->assertEquals(3, count($result)); +// +// // check count first +// $result = Customer::find(); +// $this->assertEquals(3, $result->count); +// $customer = $result->one(); +// $this->assertTrue($customer instanceof Customer); +// $this->assertEquals(3, $result->count); +// +// // iterator +// $result = Customer::find(); +// $count = 0; +// foreach ($result as $customer) { +// $this->assertTrue($customer instanceof Customer); +// $count++; +// } +// $this->assertEquals($count, $result->count); +// +// // array access +// $result = Customer::find(); +// $this->assertTrue($result[0] instanceof Customer); +// $this->assertTrue($result[1] instanceof Customer); +// $this->assertTrue($result[2] instanceof Customer); +// +// // find by a single primary key +// $customer = Customer::find(2); +// $this->assertTrue($customer instanceof Customer); +// $this->assertEquals('user2', $customer->name); +// +// // find by attributes +// $customer = Customer::find()->where(array('name' => 'user2'))->one(); +// $this->assertTrue($customer instanceof Customer); +// $this->assertEquals(2, $customer->id); +// +// // find by Query +// $query = array( +// 'where' => 'id=:id', +// 'params' => array(':id' => 2), +// ); +// $customer = Customer::find($query)->one(); +// $this->assertTrue($customer instanceof Customer); +// $this->assertEquals('user2', $customer->name); +// +// // find count +// $this->assertEquals(3, Customer::find()->count()); +// $this->assertEquals(3, Customer::count()); +// $this->assertEquals(2, Customer::count(array( +// 'where' => 'id=1 OR id=2', +// ))); +// } +// +// public function testFindBySql() +// { +// // find one +// $customer = Customer::findBySql('SELECT * FROM tbl_customer ORDER BY id DESC')->one(); +// $this->assertTrue($customer instanceof Customer); +// $this->assertEquals('user3', $customer->name); +// +// // find all +// $customers = Customer::findBySql('SELECT * FROM tbl_customer')->all(); +// $this->assertEquals(3, count($customers)); +// +// // find with parameter binding +// $customer = Customer::findBySql('SELECT * FROM tbl_customer WHERE id=:id', array(':id' => 2))->one(); +// $this->assertTrue($customer instanceof Customer); +// $this->assertEquals('user2', $customer->name); +// +// // count +// $query = Customer::findBySql('SELECT * FROM tbl_customer ORDER BY id DESC'); +// $query->one(); +// $this->assertEquals(3, $query->count); +// $query = Customer::findBySql('SELECT * FROM tbl_customer ORDER BY id DESC'); +// $this->assertEquals(3, $query->count); +// } +// +// public function testQueryMethods() +// { +// $customer = Customer::find()->where('id=:id', array(':id' => 2))->one(); +// $this->assertTrue($customer instanceof Customer); +// $this->assertEquals('user2', $customer->name); +// +// $customer = Customer::find()->where(array('name' => 'user3'))->one(); +// $this->assertTrue($customer instanceof Customer); +// $this->assertEquals('user3', $customer->name); +// +// $customer = Customer::find()->select('id')->orderBy('id DESC')->one(); +// $this->assertTrue($customer instanceof Customer); +// $this->assertEquals(3, $customer->id); +// $this->assertEquals(null, $customer->name); +// +// // scopes +// $customers = Customer::find()->active()->all(); +// $this->assertEquals(2, count($customers)); +// $customers = Customer::find(array( +// 'scopes' => array('active'), +// ))->all(); +// $this->assertEquals(2, count($customers)); +// +// // asArray +// $customers = Customer::find()->orderBy('id')->asArray()->all(); +// $this->assertEquals('user2', $customers[1]['name']); +// +// // index +// $customers = Customer::find()->orderBy('id')->index('name')->all(); +// $this->assertEquals(2, $customers['user2']['id']); +// } + +} \ No newline at end of file diff --git a/tests/unit/framework/db/CommandTest.php b/tests/unit/framework/db/CommandTest.php new file mode 100644 index 0000000..cd3c273 --- /dev/null +++ b/tests/unit/framework/db/CommandTest.php @@ -0,0 +1,322 @@ +getConnection(false); + + // null + $command = $db->createCommand(); + $this->assertEquals(null, $command->sql); + + // string + $sql = 'SELECT * FROM tbl_customer'; + $command = $db->createCommand($sql); + $this->assertEquals($sql, $command->sql); + } + + function testGetSetSql() + { + $db = $this->getConnection(false); + + $sql = 'SELECT * FROM tbl_customer'; + $command = $db->createCommand($sql); + $this->assertEquals($sql, $command->sql); + + $sql2 = 'SELECT * FROM tbl_order'; + $command->sql = $sql2; + $this->assertEquals($sql2, $command->sql); + } + + function testPrepareCancel() + { + $db = $this->getConnection(false); + + $command = $db->createCommand('SELECT * FROM tbl_customer'); + $this->assertEquals(null, $command->pdoStatement); + $command->prepare(); + $this->assertNotEquals(null, $command->pdoStatement); + $command->cancel(); + $this->assertEquals(null, $command->pdoStatement); + } + + function testExecute() + { + $db = $this->getConnection(); + + $sql = 'INSERT INTO tbl_customer(email, name , address) VALUES (\'user4@example.com\', \'user4\', \'address4\')'; + $command = $db->createCommand($sql); + $this->assertEquals(1, $command->execute()); + + $sql = 'SELECT COUNT(*) FROM tbl_customer WHERE name =\'user4\''; + $command = $db->createCommand($sql); + $this->assertEquals(1, $command->queryScalar()); + + $command = $db->createCommand('bad SQL'); + $this->setExpectedException('\yii\db\Exception'); + $command->execute(); + } + + function testQuery() + { + $db = $this->getConnection(); + + // query + $sql = 'SELECT * FROM tbl_customer'; + $reader = $db->createCommand($sql)->query(); + $this->assertTrue($reader instanceof DataReader); + + // queryAll + $rows = $db->createCommand('SELECT * FROM tbl_customer')->queryAll(); + $this->assertEquals(3, count($rows)); + $row = $rows[2]; + $this->assertEquals(3, $row['id']); + $this->assertEquals('user3', $row['name']); + + $rows = $db->createCommand('SELECT * FROM tbl_customer WHERE id=10')->queryAll(); + $this->assertEquals(array(), $rows); + + // queryRow + $sql = 'SELECT * FROM tbl_customer ORDER BY id'; + $row = $db->createCommand($sql)->queryRow(); + $this->assertEquals(1, $row['id']); + $this->assertEquals('user1', $row['name']); + + $sql = 'SELECT * FROM tbl_customer ORDER BY id'; + $command = $db->createCommand($sql); + $command->prepare(); + $row = $command->queryRow(); + $this->assertEquals(1, $row['id']); + $this->assertEquals('user1', $row['name']); + + $sql = 'SELECT * FROM tbl_customer WHERE id=10'; + $command = $db->createCommand($sql); + $this->assertFalse($command->queryRow()); + + // queryColumn + $sql = 'SELECT * FROM tbl_customer'; + $column = $db->createCommand($sql)->queryColumn(); + $this->assertEquals(range(1, 3), $column); + + $command = $db->createCommand('SELECT id FROM tbl_customer WHERE id=10'); + $this->assertEquals(array(), $command->queryColumn()); + + // queryScalar + $sql = 'SELECT * FROM tbl_customer ORDER BY id'; + $this->assertEquals($db->createCommand($sql)->queryScalar(), 1); + + $sql = 'SELECT id FROM tbl_customer ORDER BY id'; + $command = $db->createCommand($sql); + $command->prepare(); + $this->assertEquals(1, $command->queryScalar()); + + $command = $db->createCommand('SELECT id FROM tbl_customer WHERE id=10'); + $this->assertFalse($command->queryScalar()); + + $command = $db->createCommand('bad SQL'); + $this->setExpectedException('\yii\db\Exception'); + $command->query(); + } + + function testBindParamValue() + { + $db = $this->getConnection(); + + // bindParam + $sql = 'INSERT INTO tbl_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 tbl_customer WHERE email=:email'; + $command = $db->createCommand($sql); + $command->bindParam(':email', $email); + $this->assertEquals($name, $command->queryScalar()); + + $sql = 'INSERT INTO tbl_type (int_col, char_col, float_col, blob_col, numeric_col, bool_col) VALUES (:int_col, :char_col, :float_col, :blob_col, :numeric_col, :bool_col)'; + $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 * FROM tbl_type'; + $row = $db->createCommand($sql)->queryRow(); + $this->assertEquals($intCol, $row['int_col']); + $this->assertEquals($charCol, $row['char_col']); + $this->assertEquals($floatCol, $row['float_col']); + $this->assertEquals($blobCol, $row['blob_col']); + $this->assertEquals($numericCol, $row['numeric_col']); + + // bindValue + $sql = 'INSERT INTO tbl_customer(email, name, address) VALUES (:email, \'user5\', \'address5\')'; + $command = $db->createCommand($sql); + $command->bindValue(':email', 'user5@example.com'); + $command->execute(); + + $sql = 'SELECT email FROM tbl_customer WHERE name=:name'; + $command = $db->createCommand($sql); + $command->bindValue(':name', 'user5'); + $this->assertEquals('user5@example.com', $command->queryScalar()); + + // bind value via query or execute method + $sql = 'INSERT INTO tbl_customer(email, name, address) VALUES (:email, \'user6\', \'address6\')'; + $command = $db->createCommand($sql); + $command->execute(array(':email' => 'user6@example.com')); + $sql = 'SELECT email FROM tbl_customer WHERE name=:name'; + $command = $db->createCommand($sql); + $this->assertEquals('user5@example.com', $command->queryScalar(array(':name' => 'user5'))); + } + + function testFetchMode() + { + $db = $this->getConnection(); + + // default: FETCH_ASSOC + $sql = 'SELECT * FROM tbl_customer'; + $command = $db->createCommand($sql); + $result = $command->queryRow(); + $this->assertTrue(is_array($result) && isset($result['id'])); + + // FETCH_OBJ, customized via fetchMode property + $sql = 'SELECT * FROM tbl_customer'; + $command = $db->createCommand($sql); + $command->fetchMode = \PDO::FETCH_OBJ; + $result = $command->queryRow(); + $this->assertTrue(is_object($result)); + + // FETCH_NUM, customized in query method + $sql = 'SELECT * FROM tbl_customer'; + $command = $db->createCommand($sql); + $result = $command->queryRow(array(), \PDO::FETCH_NUM); + $this->assertTrue(is_array($result) && isset($result[0])); + } + + + function testInsert() + { + + } + + function testUpdate() + { + + } + + function testDelete() + { + + } + + function testCreateTable() + { + + } + + function testRenameTable() + { + + } + + function testDropTable() + { + + } + + function testTruncateTable() + { + + } + + function testAddColumn() + { + + } + + function testDropColumn() + { + + } + + function testRenameColumn() + { + + } + + function testAlterColumn() + { + + } + + function testAddForeignKey() + { + + } + + function testDropForeignKey() + { + + } + + function testCreateIndex() + { + + } + + function testDropIndex() + { + + } + + function testParams() + { + + } + + function testGetSql() + { + + } + + function testCreateCommand() + { + + } + + function testReset() + { + + } + + function testToArray() + { + + } + + function testMergeWith() + { + + } +} \ No newline at end of file diff --git a/tests/unit/framework/db/ConnectionTest.php b/tests/unit/framework/db/ConnectionTest.php new file mode 100644 index 0000000..7f3482c --- /dev/null +++ b/tests/unit/framework/db/ConnectionTest.php @@ -0,0 +1,86 @@ +getConnection(false); + $params = $this->getParam('mysql'); + + $this->assertEquals($params['dsn'], $connection->dsn); + $this->assertEquals($params['username'], $connection->username); + $this->assertEquals($params['password'], $connection->password); + } + + function testOpenClose() + { + $connection = $this->getConnection(false); + + $this->assertFalse($connection->active); + $this->assertEquals(null, $connection->pdo); + + $connection->open(); + $this->assertTrue($connection->active); + $this->assertTrue($connection->pdo instanceof \PDO); + + $connection->close(); + $this->assertFalse($connection->active); + $this->assertEquals(null, $connection->pdo); + + $connection = new Connection; + $connection->dsn = 'unknown::memory:'; + $this->setExpectedException('yii\db\Exception'); + $connection->open(); + } + + function testGetDriverName() + { + $connection = $this->getConnection(false); + $this->assertEquals('mysql', $connection->driverName); + $this->assertFalse($connection->active); + } + + 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")); + } + + 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', true)); + } + + function testQuoteColumnName() + { + $connection = $this->getConnection(false); + $this->assertEquals('`column`', $connection->quoteColumnName('column')); + $this->assertEquals('`column`', $connection->quoteColumnName('`column`')); + $this->assertEquals('`table`.`column`', $connection->quoteColumnName('table.column')); + $this->assertEquals('`table.column`', $connection->quoteColumnName('table.column', true)); + } + + function testGetPdoType() + { + $connection = $this->getConnection(false); + $this->assertEquals(\PDO::PARAM_BOOL, $connection->getPdoType('boolean')); + $this->assertEquals(\PDO::PARAM_INT, $connection->getPdoType('integer')); + $this->assertEquals(\PDO::PARAM_STR, $connection->getPdoType('string')); + $this->assertEquals(\PDO::PARAM_NULL, $connection->getPdoType('NULL')); + } + + function testAttribute() + { + + } +} diff --git a/tests/unit/framework/db/QueryTest.php b/tests/unit/framework/db/QueryTest.php new file mode 100644 index 0000000..645b844 --- /dev/null +++ b/tests/unit/framework/db/QueryTest.php @@ -0,0 +1,110 @@ +select('*'); + $this->assertEquals('*', $query->select); + $this->assertNull($query->distinct); + $this->assertEquals(null, $query->selectOption); + + $query = new Query; + $query->select('id, name', 'something')->distinct(true); + $this->assertEquals('id, name', $query->select); + $this->assertTrue($query->distinct); + $this->assertEquals('something', $query->selectOption); + } + + function testFrom() + { + $query = new Query; + $query->from('tbl_user'); + $this->assertEquals('tbl_user', $query->from); + } + + function testWhere() + { + $query = new Query; + $query->where('id = :id', array(':id' => 1)); + $this->assertEquals('id = :id', $query->where); + $this->assertEquals(array(':id' => 1), $query->params); + + $query->andWhere('name = :name', array(':name' => 'something')); + $this->assertEquals(array('and', 'id = :id', 'name = :name'), $query->where); + $this->assertEquals(array(':id' => 1, ':name' => 'something'), $query->params); + + $query->orWhere('age = :age', array(':age' => '30')); + $this->assertEquals(array('or', array('and', 'id = :id', 'name = :name'), 'age = :age'), $query->where); + $this->assertEquals(array(':id' => 1, ':name' => 'something', ':age' => '30'), $query->params); + } + + function testJoin() + { + + } + + function testGroup() + { + $query = new Query; + $query->groupBy('team'); + $this->assertEquals('team', $query->groupBy); + + $query->addGroup('company'); + $this->assertEquals(array('team', 'company'), $query->groupBy); + + $query->addGroup('age'); + $this->assertEquals(array('team', 'company', 'age'), $query->groupBy); + } + + function testHaving() + { + $query = new Query; + $query->having('id = :id', array(':id' => 1)); + $this->assertEquals('id = :id', $query->having); + $this->assertEquals(array(':id' => 1), $query->params); + + $query->andHaving('name = :name', array(':name' => 'something')); + $this->assertEquals(array('and', 'id = :id', 'name = :name'), $query->having); + $this->assertEquals(array(':id' => 1, ':name' => 'something'), $query->params); + + $query->orHaving('age = :age', array(':age' => '30')); + $this->assertEquals(array('or', array('and', 'id = :id', 'name = :name'), 'age = :age'), $query->having); + $this->assertEquals(array(':id' => 1, ':name' => 'something', ':age' => '30'), $query->params); + } + + function testOrder() + { + $query = new Query; + $query->orderBy('team'); + $this->assertEquals('team', $query->orderBy); + + $query->addOrderBy('company'); + $this->assertEquals(array('team', 'company'), $query->orderBy); + + $query->addOrderBy('age'); + $this->assertEquals(array('team', 'company', 'age'), $query->orderBy); + } + + function testLimitOffset() + { + $query = new Query; + $query->limit(10)->offset(5); + $this->assertEquals(10, $query->limit); + $this->assertEquals(5, $query->offset); + } + + function testUnion() + { + + } +} \ No newline at end of file diff --git a/tests/unit/framework/db/ar/ActiveRecordTest.php b/tests/unit/framework/db/ar/ActiveRecordTest.php deleted file mode 100644 index e0414a8..0000000 --- a/tests/unit/framework/db/ar/ActiveRecordTest.php +++ /dev/null @@ -1,404 +0,0 @@ -getConnection(); - } - - public function testFind() - { - // find one - $result = Customer::find(); - $this->assertTrue($result instanceof ActiveQuery); - $customer = $result->one(); - $this->assertTrue($customer instanceof Customer); - - // find all - $customers = Customer::find()->all(); - $this->assertEquals(3, count($customers)); - $this->assertTrue($customers[0] instanceof Customer); - $this->assertTrue($customers[1] instanceof Customer); - $this->assertTrue($customers[2] instanceof Customer); - - // find by a single primary key - $customer = Customer::find(2); - $this->assertTrue($customer instanceof Customer); - $this->assertEquals('user2', $customer->name); - - // find by attributes - $customer = Customer::find()->where(array('name' => 'user2'))->one(); - $this->assertTrue($customer instanceof Customer); - $this->assertEquals(2, $customer->id); - - // find by Query array - $query = array( - 'where' => 'id=:id', - 'params' => array(':id' => 2), - ); - $customer = Customer::find($query)->one(); - $this->assertTrue($customer instanceof Customer); - $this->assertEquals('user2', $customer->name); - - // find count - $this->assertEquals(3, Customer::count()->value()); - $this->assertEquals(2, Customer::count(array( - 'where' => 'id=1 OR id=2', - ))->value()); - $this->assertEquals(2, Customer::find()->select('COUNT(*)')->where('id=1 OR id=2')->value()); - - // asArray - $customer = Customer::find()->where('id=2')->asArray()->one(); - $this->assertEquals(array( - 'id' => '2', - 'email' => 'user2@example.com', - 'name' => 'user2', - 'address' => 'address2', - 'status' => '1', - ), $customer); - - // indexBy - $customers = Customer::find()->indexBy('name')->orderBy('id')->all(); - $this->assertEquals(3, count($customers)); - $this->assertTrue($customers['user1'] instanceof Customer); - $this->assertTrue($customers['user2'] instanceof Customer); - $this->assertTrue($customers['user3'] instanceof Customer); - } - - public function testFindBySql() - { - // find one - $customer = Customer::findBySql('SELECT * FROM tbl_customer ORDER BY id DESC')->one(); - $this->assertTrue($customer instanceof Customer); - $this->assertEquals('user3', $customer->name); - - // find all - $customers = Customer::findBySql('SELECT * FROM tbl_customer')->all(); - $this->assertEquals(3, count($customers)); - - // find with parameter binding - $customer = Customer::findBySql('SELECT * FROM tbl_customer WHERE id=:id', array(':id' => 2))->one(); - $this->assertTrue($customer instanceof Customer); - $this->assertEquals('user2', $customer->name); - } - - public function testScope() - { - $customers = Customer::find(array( - 'scopes' => array('active'), - ))->all(); - $this->assertEquals(2, count($customers)); - - $customers = Customer::find()->active()->all(); - $this->assertEquals(2, count($customers)); - } - - public function testFindLazy() - { - /** @var $customer Customer */ - $customer = Customer::find(2); - $orders = $customer->orders; - $this->assertEquals(2, count($orders)); - - $orders = $customer->orders()->where('id=3')->all(); - $this->assertEquals(1, count($orders)); - $this->assertEquals(3, $orders[0]->id); - } - - public function testFindEager() - { - $customers = Customer::find()->with('orders')->all(); - $this->assertEquals(3, count($customers)); - $this->assertEquals(1, count($customers[0]->orders)); - $this->assertEquals(2, count($customers[1]->orders)); - } - - public function testFindLazyVia() - { - /** @var $order Order */ - $order = Order::find(1); - $this->assertEquals(1, $order->id); - $this->assertEquals(2, count($order->items)); - $this->assertEquals(1, $order->items[0]->id); - $this->assertEquals(2, $order->items[1]->id); - - $order = Order::find(1); - $order->id = 100; - $this->assertEquals(array(), $order->items); - } - - public function testFindEagerViaRelation() - { - $orders = Order::find()->with('items')->orderBy('id')->all(); - $this->assertEquals(3, count($orders)); - $order = $orders[0]; - $this->assertEquals(1, $order->id); - $this->assertEquals(2, count($order->items)); - $this->assertEquals(1, $order->items[0]->id); - $this->assertEquals(2, $order->items[1]->id); - } - - public function testFindLazyViaTable() - { - /** @var $order Order */ - $order = Order::find(1); - $this->assertEquals(1, $order->id); - $this->assertEquals(2, count($order->books)); - $this->assertEquals(1, $order->items[0]->id); - $this->assertEquals(2, $order->items[1]->id); - - $order = Order::find(2); - $this->assertEquals(2, $order->id); - $this->assertEquals(0, count($order->books)); - } - - public function testFindEagerViaTable() - { - $orders = Order::find()->with('books')->orderBy('id')->all(); - $this->assertEquals(3, count($orders)); - - $order = $orders[0]; - $this->assertEquals(1, $order->id); - $this->assertEquals(2, count($order->books)); - $this->assertEquals(1, $order->books[0]->id); - $this->assertEquals(2, $order->books[1]->id); - - $order = $orders[1]; - $this->assertEquals(2, $order->id); - $this->assertEquals(0, count($order->books)); - - $order = $orders[2]; - $this->assertEquals(3, $order->id); - $this->assertEquals(1, count($order->books)); - $this->assertEquals(2, $order->books[0]->id); - } - - public function testFindNestedRelation() - { - $customers = Customer::find()->with('orders', 'orders.items')->all(); - $this->assertEquals(3, count($customers)); - $this->assertEquals(1, count($customers[0]->orders)); - $this->assertEquals(2, count($customers[1]->orders)); - $this->assertEquals(0, count($customers[2]->orders)); - $this->assertEquals(2, count($customers[0]->orders[0]->items)); - $this->assertEquals(3, count($customers[1]->orders[0]->items)); - $this->assertEquals(1, count($customers[1]->orders[1]->items)); - } - -// public function testInsert() -// { -// $customer = new Customer; -// $customer->email = 'user4@example.com'; -// $customer->name = 'user4'; -// $customer->address = 'address4'; -// -// $this->assertNull($customer->id); -// $this->assertTrue($customer->isNewRecord); -// -// $customer->save(); -// -// $this->assertEquals(4, $customer->id); -// $this->assertFalse($customer->isNewRecord); -// } -// -// public function testUpdate() -// { -// // save -// $customer = Customer::find(2); -// $this->assertTrue($customer instanceof Customer); -// $this->assertEquals('user2', $customer->name); -// $this->assertFalse($customer->isNewRecord); -// $customer->name = 'user2x'; -// $customer->save(); -// $this->assertEquals('user2x', $customer->name); -// $this->assertFalse($customer->isNewRecord); -// $customer2 = Customer::find(2); -// $this->assertEquals('user2x', $customer2->name); -// -// // updateCounters -// $pk = array('order_id' => 2, 'item_id' => 4); -// $orderItem = OrderItem::find()->where($pk)->one(); -// $this->assertEquals(1, $orderItem->quantity); -// $ret = $orderItem->updateCounters(array('quantity' => -1)); -// $this->assertTrue($ret); -// $this->assertEquals(0, $orderItem->quantity); -// $orderItem = OrderItem::find()->where($pk)->one(); -// $this->assertEquals(0, $orderItem->quantity); -// -// // updateAll -// $customer = Customer::find(3); -// $this->assertEquals('user3', $customer->name); -// $ret = Customer::updateAll(array( -// 'name' => 'temp', -// ), array('id' => 3)); -// $this->assertEquals(1, $ret); -// $customer = Customer::find(3); -// $this->assertEquals('temp', $customer->name); -// -// // updateCounters -// $pk = array('order_id' => 1, 'item_id' => 2); -// $orderItem = OrderItem::find()->where($pk)->one(); -// $this->assertEquals(2, $orderItem->quantity); -// $ret = OrderItem::updateAllCounters(array( -// 'quantity' => 3, -// ), $pk); -// $this->assertEquals(1, $ret); -// $orderItem = OrderItem::find()->where($pk)->one(); -// $this->assertEquals(5, $orderItem->quantity); -// } -// -// public function testDelete() -// { -// // delete -// $customer = Customer::find(2); -// $this->assertTrue($customer instanceof Customer); -// $this->assertEquals('user2', $customer->name); -// $customer->delete(); -// $customer = Customer::find(2); -// $this->assertNull($customer); -// -// // deleteAll -// $customers = Customer::find()->all(); -// $this->assertEquals(2, count($customers)); -// $ret = Customer::deleteAll(); -// $this->assertEquals(2, $ret); -// $customers = Customer::find()->all(); -// $this->assertEquals(0, count($customers)); -// } -// -// public function testFind() -// { -// // find one -// $result = Customer::find(); -// $this->assertTrue($result instanceof ActiveQuery); -// $customer = $result->one(); -// $this->assertTrue($customer instanceof Customer); -// $this->assertEquals(1, $result->count); -// -// // find all -// $result = Customer::find(); -// $customers = $result->all(); -// $this->assertTrue(is_array($customers)); -// $this->assertEquals(3, count($customers)); -// $this->assertTrue($customers[0] instanceof Customer); -// $this->assertTrue($customers[1] instanceof Customer); -// $this->assertTrue($customers[2] instanceof Customer); -// $this->assertEquals(3, $result->count); -// $this->assertEquals(3, count($result)); -// -// // check count first -// $result = Customer::find(); -// $this->assertEquals(3, $result->count); -// $customer = $result->one(); -// $this->assertTrue($customer instanceof Customer); -// $this->assertEquals(3, $result->count); -// -// // iterator -// $result = Customer::find(); -// $count = 0; -// foreach ($result as $customer) { -// $this->assertTrue($customer instanceof Customer); -// $count++; -// } -// $this->assertEquals($count, $result->count); -// -// // array access -// $result = Customer::find(); -// $this->assertTrue($result[0] instanceof Customer); -// $this->assertTrue($result[1] instanceof Customer); -// $this->assertTrue($result[2] instanceof Customer); -// -// // find by a single primary key -// $customer = Customer::find(2); -// $this->assertTrue($customer instanceof Customer); -// $this->assertEquals('user2', $customer->name); -// -// // find by attributes -// $customer = Customer::find()->where(array('name' => 'user2'))->one(); -// $this->assertTrue($customer instanceof Customer); -// $this->assertEquals(2, $customer->id); -// -// // find by Query -// $query = array( -// 'where' => 'id=:id', -// 'params' => array(':id' => 2), -// ); -// $customer = Customer::find($query)->one(); -// $this->assertTrue($customer instanceof Customer); -// $this->assertEquals('user2', $customer->name); -// -// // find count -// $this->assertEquals(3, Customer::find()->count()); -// $this->assertEquals(3, Customer::count()); -// $this->assertEquals(2, Customer::count(array( -// 'where' => 'id=1 OR id=2', -// ))); -// } -// -// public function testFindBySql() -// { -// // find one -// $customer = Customer::findBySql('SELECT * FROM tbl_customer ORDER BY id DESC')->one(); -// $this->assertTrue($customer instanceof Customer); -// $this->assertEquals('user3', $customer->name); -// -// // find all -// $customers = Customer::findBySql('SELECT * FROM tbl_customer')->all(); -// $this->assertEquals(3, count($customers)); -// -// // find with parameter binding -// $customer = Customer::findBySql('SELECT * FROM tbl_customer WHERE id=:id', array(':id' => 2))->one(); -// $this->assertTrue($customer instanceof Customer); -// $this->assertEquals('user2', $customer->name); -// -// // count -// $query = Customer::findBySql('SELECT * FROM tbl_customer ORDER BY id DESC'); -// $query->one(); -// $this->assertEquals(3, $query->count); -// $query = Customer::findBySql('SELECT * FROM tbl_customer ORDER BY id DESC'); -// $this->assertEquals(3, $query->count); -// } -// -// public function testQueryMethods() -// { -// $customer = Customer::find()->where('id=:id', array(':id' => 2))->one(); -// $this->assertTrue($customer instanceof Customer); -// $this->assertEquals('user2', $customer->name); -// -// $customer = Customer::find()->where(array('name' => 'user3'))->one(); -// $this->assertTrue($customer instanceof Customer); -// $this->assertEquals('user3', $customer->name); -// -// $customer = Customer::find()->select('id')->orderBy('id DESC')->one(); -// $this->assertTrue($customer instanceof Customer); -// $this->assertEquals(3, $customer->id); -// $this->assertEquals(null, $customer->name); -// -// // scopes -// $customers = Customer::find()->active()->all(); -// $this->assertEquals(2, count($customers)); -// $customers = Customer::find(array( -// 'scopes' => array('active'), -// ))->all(); -// $this->assertEquals(2, count($customers)); -// -// // asArray -// $customers = Customer::find()->orderBy('id')->asArray()->all(); -// $this->assertEquals('user2', $customers[1]['name']); -// -// // index -// $customers = Customer::find()->orderBy('id')->index('name')->all(); -// $this->assertEquals(2, $customers['user2']['id']); -// } - -} \ No newline at end of file diff --git a/tests/unit/framework/db/dao/CommandTest.php b/tests/unit/framework/db/dao/CommandTest.php deleted file mode 100644 index e5ece88..0000000 --- a/tests/unit/framework/db/dao/CommandTest.php +++ /dev/null @@ -1,216 +0,0 @@ -getConnection(false); - - // null - $command = $db->createCommand(); - $this->assertEquals(null, $command->sql); - - // string - $sql = 'SELECT * FROM tbl_customer'; - $command = $db->createCommand($sql); - $this->assertEquals($sql, $command->sql); - } - - function testGetSetSql() - { - $db = $this->getConnection(false); - - $sql = 'SELECT * FROM tbl_customer'; - $command = $db->createCommand($sql); - $this->assertEquals($sql, $command->sql); - - $sql2 = 'SELECT * FROM tbl_order'; - $command->sql = $sql2; - $this->assertEquals($sql2, $command->sql); - } - - function testPrepareCancel() - { - $db = $this->getConnection(false); - - $command = $db->createCommand('SELECT * FROM tbl_customer'); - $this->assertEquals(null, $command->pdoStatement); - $command->prepare(); - $this->assertNotEquals(null, $command->pdoStatement); - $command->cancel(); - $this->assertEquals(null, $command->pdoStatement); - } - - function testExecute() - { - $db = $this->getConnection(); - - $sql = 'INSERT INTO tbl_customer(email, name , address) VALUES (\'user4@example.com\', \'user4\', \'address4\')'; - $command = $db->createCommand($sql); - $this->assertEquals(1, $command->execute()); - - $sql = 'SELECT COUNT(*) FROM tbl_customer WHERE name =\'user4\''; - $command = $db->createCommand($sql); - $this->assertEquals(1, $command->queryScalar()); - - $command = $db->createCommand('bad SQL'); - $this->setExpectedException('\yii\db\Exception'); - $command->execute(); - } - - function testQuery() - { - $db = $this->getConnection(); - - // query - $sql = 'SELECT * FROM tbl_customer'; - $reader = $db->createCommand($sql)->query(); - $this->assertTrue($reader instanceof DataReader); - - // queryAll - $rows = $db->createCommand('SELECT * FROM tbl_customer')->queryAll(); - $this->assertEquals(3, count($rows)); - $row = $rows[2]; - $this->assertEquals(3, $row['id']); - $this->assertEquals('user3', $row['name']); - - $rows = $db->createCommand('SELECT * FROM tbl_customer WHERE id=10')->queryAll(); - $this->assertEquals(array(), $rows); - - // queryRow - $sql = 'SELECT * FROM tbl_customer ORDER BY id'; - $row = $db->createCommand($sql)->queryRow(); - $this->assertEquals(1, $row['id']); - $this->assertEquals('user1', $row['name']); - - $sql = 'SELECT * FROM tbl_customer ORDER BY id'; - $command = $db->createCommand($sql); - $command->prepare(); - $row = $command->queryRow(); - $this->assertEquals(1, $row['id']); - $this->assertEquals('user1', $row['name']); - - $sql = 'SELECT * FROM tbl_customer WHERE id=10'; - $command = $db->createCommand($sql); - $this->assertFalse($command->queryRow()); - - // queryColumn - $sql = 'SELECT * FROM tbl_customer'; - $column = $db->createCommand($sql)->queryColumn(); - $this->assertEquals(range(1, 3), $column); - - $command = $db->createCommand('SELECT id FROM tbl_customer WHERE id=10'); - $this->assertEquals(array(), $command->queryColumn()); - - // queryScalar - $sql = 'SELECT * FROM tbl_customer ORDER BY id'; - $this->assertEquals($db->createCommand($sql)->queryScalar(), 1); - - $sql = 'SELECT id FROM tbl_customer ORDER BY id'; - $command = $db->createCommand($sql); - $command->prepare(); - $this->assertEquals(1, $command->queryScalar()); - - $command = $db->createCommand('SELECT id FROM tbl_customer WHERE id=10'); - $this->assertFalse($command->queryScalar()); - - $command = $db->createCommand('bad SQL'); - $this->setExpectedException('\yii\db\Exception'); - $command->query(); - } - - function testBindParamValue() - { - $db = $this->getConnection(); - - // bindParam - $sql = 'INSERT INTO tbl_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 tbl_customer WHERE email=:email'; - $command = $db->createCommand($sql); - $command->bindParam(':email', $email); - $this->assertEquals($name, $command->queryScalar()); - - $sql = 'INSERT INTO tbl_type (int_col, char_col, float_col, blob_col, numeric_col, bool_col) VALUES (:int_col, :char_col, :float_col, :blob_col, :numeric_col, :bool_col)'; - $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 * FROM tbl_type'; - $row = $db->createCommand($sql)->queryRow(); - $this->assertEquals($intCol, $row['int_col']); - $this->assertEquals($charCol, $row['char_col']); - $this->assertEquals($floatCol, $row['float_col']); - $this->assertEquals($blobCol, $row['blob_col']); - $this->assertEquals($numericCol, $row['numeric_col']); - - // bindValue - $sql = 'INSERT INTO tbl_customer(email, name, address) VALUES (:email, \'user5\', \'address5\')'; - $command = $db->createCommand($sql); - $command->bindValue(':email', 'user5@example.com'); - $command->execute(); - - $sql = 'SELECT email FROM tbl_customer WHERE name=:name'; - $command = $db->createCommand($sql); - $command->bindValue(':name', 'user5'); - $this->assertEquals('user5@example.com', $command->queryScalar()); - - // bind value via query or execute method - $sql = 'INSERT INTO tbl_customer(email, name, address) VALUES (:email, \'user6\', \'address6\')'; - $command = $db->createCommand($sql); - $command->execute(array(':email' => 'user6@example.com')); - $sql = 'SELECT email FROM tbl_customer WHERE name=:name'; - $command = $db->createCommand($sql); - $this->assertEquals('user5@example.com', $command->queryScalar(array(':name' => 'user5'))); - } - - function testFetchMode() - { - $db = $this->getConnection(); - - // default: FETCH_ASSOC - $sql = 'SELECT * FROM tbl_customer'; - $command = $db->createCommand($sql); - $result = $command->queryRow(); - $this->assertTrue(is_array($result) && isset($result['id'])); - - // FETCH_OBJ, customized via fetchMode property - $sql = 'SELECT * FROM tbl_customer'; - $command = $db->createCommand($sql); - $command->fetchMode = \PDO::FETCH_OBJ; - $result = $command->queryRow(); - $this->assertTrue(is_object($result)); - - // FETCH_NUM, customized in query method - $sql = 'SELECT * FROM tbl_customer'; - $command = $db->createCommand($sql); - $result = $command->queryRow(array(), \PDO::FETCH_NUM); - $this->assertTrue(is_array($result) && isset($result[0])); - } -} \ No newline at end of file diff --git a/tests/unit/framework/db/dao/ConnectionTest.php b/tests/unit/framework/db/dao/ConnectionTest.php deleted file mode 100644 index 63adc84..0000000 --- a/tests/unit/framework/db/dao/ConnectionTest.php +++ /dev/null @@ -1,86 +0,0 @@ -getConnection(false); - $params = $this->getParam('mysql'); - - $this->assertEquals($params['dsn'], $connection->dsn); - $this->assertEquals($params['username'], $connection->username); - $this->assertEquals($params['password'], $connection->password); - } - - function testOpenClose() - { - $connection = $this->getConnection(false); - - $this->assertFalse($connection->active); - $this->assertEquals(null, $connection->pdo); - - $connection->open(); - $this->assertTrue($connection->active); - $this->assertTrue($connection->pdo instanceof \PDO); - - $connection->close(); - $this->assertFalse($connection->active); - $this->assertEquals(null, $connection->pdo); - - $connection = new Connection; - $connection->dsn = 'unknown::memory:'; - $this->setExpectedException('yii\db\Exception'); - $connection->open(); - } - - function testGetDriverName() - { - $connection = $this->getConnection(false); - $this->assertEquals('mysql', $connection->driverName); - $this->assertFalse($connection->active); - } - - 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")); - } - - 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', true)); - } - - function testQuoteColumnName() - { - $connection = $this->getConnection(false); - $this->assertEquals('`column`', $connection->quoteColumnName('column')); - $this->assertEquals('`column`', $connection->quoteColumnName('`column`')); - $this->assertEquals('`table`.`column`', $connection->quoteColumnName('table.column')); - $this->assertEquals('`table.column`', $connection->quoteColumnName('table.column', true)); - } - - function testGetPdoType() - { - $connection = $this->getConnection(false); - $this->assertEquals(\PDO::PARAM_BOOL, $connection->getPdoType('boolean')); - $this->assertEquals(\PDO::PARAM_INT, $connection->getPdoType('integer')); - $this->assertEquals(\PDO::PARAM_STR, $connection->getPdoType('string')); - $this->assertEquals(\PDO::PARAM_NULL, $connection->getPdoType('NULL')); - } - - function testAttribute() - { - - } -} diff --git a/tests/unit/framework/db/dao/QueryTest.php b/tests/unit/framework/db/dao/QueryTest.php deleted file mode 100644 index 424eacc..0000000 --- a/tests/unit/framework/db/dao/QueryTest.php +++ /dev/null @@ -1,215 +0,0 @@ -select('*'); - $this->assertEquals('*', $query->select); - $this->assertNull($query->distinct); - $this->assertEquals(null, $query->selectOption); - - $query = new Query; - $query->select('id, name', 'something')->distinct(true); - $this->assertEquals('id, name', $query->select); - $this->assertTrue($query->distinct); - $this->assertEquals('something', $query->selectOption); - } - - function testFrom() - { - $query = new Query; - $query->from('tbl_user'); - $this->assertEquals('tbl_user', $query->from); - } - - function testWhere() - { - $query = new Query; - $query->where('id = :id', array(':id' => 1)); - $this->assertEquals('id = :id', $query->where); - $this->assertEquals(array(':id' => 1), $query->params); - - $query->andWhere('name = :name', array(':name' => 'something')); - $this->assertEquals(array('and', 'id = :id', 'name = :name'), $query->where); - $this->assertEquals(array(':id' => 1, ':name' => 'something'), $query->params); - - $query->orWhere('age = :age', array(':age' => '30')); - $this->assertEquals(array('or', array('and', 'id = :id', 'name = :name'), 'age = :age'), $query->where); - $this->assertEquals(array(':id' => 1, ':name' => 'something', ':age' => '30'), $query->params); - } - - function testJoin() - { - - } - - function testGroup() - { - $query = new Query; - $query->groupBy('team'); - $this->assertEquals('team', $query->groupBy); - - $query->addGroup('company'); - $this->assertEquals(array('team', 'company'), $query->groupBy); - - $query->addGroup('age'); - $this->assertEquals(array('team', 'company', 'age'), $query->groupBy); - } - - function testHaving() - { - $query = new Query; - $query->having('id = :id', array(':id' => 1)); - $this->assertEquals('id = :id', $query->having); - $this->assertEquals(array(':id' => 1), $query->params); - - $query->andHaving('name = :name', array(':name' => 'something')); - $this->assertEquals(array('and', 'id = :id', 'name = :name'), $query->having); - $this->assertEquals(array(':id' => 1, ':name' => 'something'), $query->params); - - $query->orHaving('age = :age', array(':age' => '30')); - $this->assertEquals(array('or', array('and', 'id = :id', 'name = :name'), 'age = :age'), $query->having); - $this->assertEquals(array(':id' => 1, ':name' => 'something', ':age' => '30'), $query->params); - } - - function testOrder() - { - $query = new Query; - $query->orderBy('team'); - $this->assertEquals('team', $query->orderBy); - - $query->addOrderBy('company'); - $this->assertEquals(array('team', 'company'), $query->orderBy); - - $query->addOrderBy('age'); - $this->assertEquals(array('team', 'company', 'age'), $query->orderBy); - } - - function testLimitOffset() - { - $query = new Query; - $query->limit(10)->offset(5); - $this->assertEquals(10, $query->limit); - $this->assertEquals(5, $query->offset); - } - - function testUnion() - { - - } - - function testInsert() - { - - } - - function testUpdate() - { - - } - - function testDelete() - { - - } - - function testCreateTable() - { - - } - - function testRenameTable() - { - - } - - function testDropTable() - { - - } - - function testTruncateTable() - { - - } - - function testAddColumn() - { - - } - - function testDropColumn() - { - - } - - function testRenameColumn() - { - - } - - function testAlterColumn() - { - - } - - function testAddForeignKey() - { - - } - - function testDropForeignKey() - { - - } - - function testCreateIndex() - { - - } - - function testDropIndex() - { - - } - - function testParams() - { - - } - - function testGetSql() - { - - } - - function testCreateCommand() - { - - } - - function testReset() - { - - } - - function testToArray() - { - - } - - function testMergeWith() - { - - } -} \ No newline at end of file