Browse Source

..

tags/2.0.0-beta
Qiang Xue 13 years ago
parent
commit
376dd0d41e
  1. 12
      framework/db/ar/ActiveQuery.php
  2. 5
      tests/unit/MysqlTestCase.php
  3. 28
      tests/unit/data/ar/ActiveRecord.php
  4. 11
      tests/unit/data/ar/Customer.php
  5. 11
      tests/unit/data/ar/Order.php
  6. 173
      tests/unit/framework/db/ar/ActiveRecordTest.php
  7. 3
      tests/unit/framework/db/dao/ConnectionTest.php
  8. 4
      tests/unit/framework/db/dao/QueryTest.php

12
framework/db/ar/ActiveQuery.php

@ -683,16 +683,24 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
* public $with; * public $with;
*/ */
if ($this->sql === null) {
if ($this->query->from === null) { if ($this->query->from === null) {
$modelClass = $this->modelClass; $modelClass = $this->modelClass;
$this->query->from = $modelClass::tableName(); $tableName = $modelClass::tableName();
if ($this->tableAlias !== null) { if ($this->tableAlias !== null) {
$this->query->from .= $this->tableAlias; $tableName .= ' ' . $this->tableAlias;
} }
$this->query->from = array($tableName);
} }
$command = $this->query->createCommand($this->getDbConnection()); $command = $this->query->createCommand($this->getDbConnection());
$this->sql = $command->getSql(); $this->sql = $command->getSql();
} else {
$command = $this->getDbConnection()->createCommand($this->sql);
$command->bindValues($this->query->params);
}
$rows = $command->queryAll(); $rows = $command->queryAll();
if ($this->asArray) { if ($this->asArray) {
if ($this->indexBy === null) { if ($this->indexBy === null) {
return $rows; return $rows;

5
tests/unit/MysqlTestCase.php

@ -18,7 +18,10 @@ class MysqlTestCase extends TestCase
function getConnection($reset = true) function getConnection($reset = true)
{ {
$params = $this->getParam('mysql'); $params = $this->getParam('mysql');
$db = new \yii\db\dao\Connection($params['dsn'], $params['username'], $params['password']); $db = new \yii\db\dao\Connection;
$db->dsn = $params['dsn'];
$db->username = $params['username'];
$db->password = $params['password'];
if ($reset) { if ($reset) {
$db->active = true; $db->active = true;
$lines = explode(';', file_get_contents($params['fixture'])); $lines = explode(';', file_get_contents($params['fixture']));

28
tests/unit/data/ar/ActiveRecord.php

@ -0,0 +1,28 @@
<?php
/**
* ActiveRecord class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yiiunit\data\ar;
use yii\db\dao\Connection;
/**
* ActiveRecord is ...
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ActiveRecord extends \yii\db\ar\ActiveRecord
{
public static $db;
public static function getDbConnection()
{
return self::$db;
}
}

11
tests/unit/data/ar/Customer.php

@ -0,0 +1,11 @@
<?php
namespace yiiunit\data\ar;
class Customer extends ActiveRecord
{
public static function tableName()
{
return 'tbl_customer';
}
}

11
tests/unit/data/ar/Order.php

@ -0,0 +1,11 @@
<?php
namespace yiiunit\data\ar;
class Order extends ActiveRecord
{
public static function tableName()
{
return 'tbl_order';
}
}

173
tests/unit/framework/db/ar/ActiveRecordTest.php

@ -0,0 +1,173 @@
<?php
namespace yiiunit\framework\db\ar;
use yii\db\dao\Query;
use yii\db\ar\ActiveQuery;
use yiiunit\data\ar\ActiveRecord;
use yiiunit\data\ar\Customer;
class ActiveRecordTest extends \yiiunit\MysqlTestCase
{
public function setUp()
{
ActiveRecord::$db = $this->getConnection();
}
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);
// check count first
$result = Customer::find();
$this->assertEquals(3, $result->count);
$customer = $result->one();
$this->assertTrue($customer instanceof Customer);
$this->assertEquals(1, $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)->one();
$this->assertTrue($customer instanceof Customer);
$this->assertEquals('user2', $customer->name);
// find by attributes
$customer = Customer::find(array('name'=>'user2'))->one();
$this->assertTrue($customer instanceof Customer);
$this->assertEquals(2, $customer->id);
// find by Query
$query = new ActiveQuery;
$query->where('id=:id', array(':id'=>2));
$customer = Customer::find($query)->one();
$this->assertTrue($customer instanceof Customer);
$this->assertEquals('user2', $customer->name);
}
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
$finder = Customer::findBySql('SELECT * FROM tbl_customer ORDER BY id DESC');
$finder->one();
$this->assertEquals(1, $finder->count);
$finder = Customer::findBySql('SELECT * FROM tbl_customer ORDER BY id DESC');
// todo
// $this->assertEquals(3, $finder->count);
}
public function testQueryMethods()
{
$customer = Customer::find()->where('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);
}
public function testGetSql()
{
// sql for all
$sql = Customer::find()->sql;
$this->assertEquals('SELECT * FROM tbl_customer', $sql);
// sql for one row
$sql = Customer::find()->oneSql;
$this->assertEquals('SELECT * FROM tbl_customer LIMIT 1', $sql);
// sql for count
$sql = Customer::find()->countSql;
$this->assertEquals('SELECT COUNT(*) FROM tbl_customer', $sql);
}
public function testArrayResult()
{
Customer::find()->asArray()->one();
Customer::find()->asArray()->all();
}
public function testMisc()
{
/*
* Customer::exists()
* Customer::updateAll()
* Customer::updateCounters()
* Customer::deleteAll()
*/
}
public function testInsert()
{
}
public function testUpdate()
{
}
public function testDelete()
{
}
public function testLazyLoading()
{
}
public function testEagerLoading()
{
}
}

3
tests/unit/framework/db/dao/ConnectionTest.php

@ -31,7 +31,8 @@ class ConnectionTest extends \yiiunit\MysqlTestCase
$this->assertFalse($connection->active); $this->assertFalse($connection->active);
$this->assertEquals(null, $connection->pdo); $this->assertEquals(null, $connection->pdo);
$connection = new Connection('unknown::memory:'); $connection = new Connection;
$connection->dsn = 'unknown::memory:';
$this->setExpectedException('yii\db\Exception'); $this->setExpectedException('yii\db\Exception');
$connection->open(); $connection->open();
} }

4
tests/unit/framework/db/dao/QueryTest.php

@ -15,11 +15,11 @@ class QueryTest extends \yiiunit\MysqlTestCase
$query = new Query; $query = new Query;
$query->select(); $query->select();
$this->assertEquals('*', $query->select); $this->assertEquals('*', $query->select);
$this->assertFalse($query->distinct); $this->assertNull($query->distinct);
$this->assertEquals(null, $query->selectOption); $this->assertEquals(null, $query->selectOption);
$query = new Query; $query = new Query;
$query->select('id, name', true, 'something'); $query->select('id, name', 'something')->distinct(true);
$this->assertEquals('id, name', $query->select); $this->assertEquals('id, name', $query->select);
$this->assertTrue($query->distinct); $this->assertTrue($query->distinct);
$this->assertEquals('something', $query->selectOption); $this->assertEquals('something', $query->selectOption);

Loading…
Cancel
Save