@ -16,87 +16,6 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
ActiveRecord::$db = $this->getConnection();
ActiveRecord::$db = $this->getConnection();
}
}
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()
public function testFind()
{
{
// find one
// find one
@ -105,6 +24,7 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
$customer = $result->one();
$customer = $result->one();
$this->assertTrue($customer instanceof Customer);
$this->assertTrue($customer instanceof Customer);
$this->assertEquals(1, $result->count);
$this->assertEquals(1, $result->count);
$this->assertEquals(1, count($result));
// find all
// find all
$result = Customer::find();
$result = Customer::find();
@ -138,6 +58,7 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
$this->assertTrue($result[0] instanceof Customer);
$this->assertTrue($result[0] instanceof Customer);
$this->assertTrue($result[1] instanceof Customer);
$this->assertTrue($result[1] instanceof Customer);
$this->assertTrue($result[2] instanceof Customer);
$this->assertTrue($result[2] instanceof Customer);
$this->assertEquals(3, count($result));
// find by a single primary key
// find by a single primary key
$customer = Customer::find(2);
$customer = Customer::find(2);
@ -164,186 +85,337 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
$this->assertEquals(2, Customer::count(array(
$this->assertEquals(2, Customer::count(array(
'where' => 'id=1 OR id=2',
'where' => 'id=1 OR id=2',
)));
)));
$this->assertEquals(2, Customer::count()->where('id=1 OR id=2'));
}
}
public function testFindBySql()
// public function testInsert()
{
// {
// find one
// $customer = new Customer;
$customer = Customer::findBySql('SELECT * FROM tbl_customer ORDER BY id DESC')->one();
// $customer->email = 'user4@example.com';
$this->assertTrue($customer instanceof Customer);
// $customer->name = 'user4';
$this->assertEquals('user3', $customer->name);
// $customer->address = 'address4';
//
// find all
// $this->assertNull($customer->id);
$customers = Customer::findBySql('SELECT * FROM tbl_customer')->all();
// $this->assertTrue($customer->isNewRecord);
$this->assertEquals(3, count($customers));
//
// $customer->save();
// find with parameter binding
//
$customer = Customer::findBySql('SELECT * FROM tbl_customer WHERE id=:id', array(':id' => 2))->one();
// $this->assertEquals(4, $customer->id);
$this->assertTrue($customer instanceof Customer);
// $this->assertFalse($customer->isNewRecord);
$this->assertEquals('user2', $customer->name);
// }
//
// count
// public function testUpdate()
$query = Customer::findBySql('SELECT * FROM tbl_customer ORDER BY id DESC');
// {
$query->one();
// // save
$this->assertEquals(3, $query->count);
// $customer = Customer::find(2);
$query = Customer::findBySql('SELECT * FROM tbl_customer ORDER BY id DESC');
// $this->assertTrue($customer instanceof Customer);
$this->assertEquals(3, $query->count);
// $this->assertEquals('user2', $customer->name);
}
// $this->assertFalse($customer->isNewRecord);
// $customer->name = 'user2x';
public function testQueryMethods()
// $customer->save();
{
// $this->assertEquals('user2x', $customer->name);
$customer = Customer::find()->where('id=:id', array(':id' => 2))->one();
// $this->assertFalse($customer->isNewRecord);
$this->assertTrue($customer instanceof Customer);
// $customer2 = Customer::find(2);
$this->assertEquals('user2', $customer->name);
// $this->assertEquals('user2x', $customer2->name);
//
$customer = Customer::find()->where(array('name' => 'user3'))->one();
// // updateCounters
$this->assertTrue($customer instanceof Customer);
// $pk = array('order_id' => 2, 'item_id' => 4);
$this->assertEquals('user3', $customer->name);
// $orderItem = OrderItem::find()->where($pk)->one();
// $this->assertEquals(1, $orderItem->quantity);
$customer = Customer::find()->select('id')->order('id DESC')->one();
// $ret = $orderItem->updateCounters(array('quantity' => -1));
$this->assertTrue($customer instanceof Customer);
// $this->assertTrue($ret);
$this->assertEquals(3, $customer->id);
// $this->assertEquals(0, $orderItem->quantity);
$this->assertEquals(null, $customer->name);
// $orderItem = OrderItem::find()->where($pk)->one();
// $this->assertEquals(0, $orderItem->quantity);
// scopes
//
$customers = Customer::find()->active()->all();
// // updateAll
$this->assertEquals(2, count($customers));
// $customer = Customer::find(3);
$customers = Customer::find(array(
// $this->assertEquals('user3', $customer->name);
'scopes' => array('active'),
// $ret = Customer::updateAll(array(
))->all();
// 'name' => 'temp',
$this->assertEquals(2, count($customers));
// ), array('id' => 3));
// $this->assertEquals(1, $ret);
// asArray
// $customer = Customer::find(3);
$customers = Customer::find()->order('id')->asArray()->all();
// $this->assertEquals('temp', $customer->name);
$this->assertEquals('user2', $customers[1]['name']);
//
// // updateCounters
// index
// $pk = array('order_id' => 1, 'item_id' => 2);
$customers = Customer::find()->order('id')->index('name')->all();
// $orderItem = OrderItem::find()->where($pk)->one();
$this->assertEquals(2, $customers['user2']['id']);
// $this->assertEquals(2, $orderItem->quantity);
}
// $ret = OrderItem::updateAllCounters(array(
// 'quantity' => 3,
public function testEagerLoading()
// ), $pk);
{
// $this->assertEquals(1, $ret);
// has many
// $orderItem = OrderItem::find()->where($pk)->one();
$customers = Customer::find()->with('orders')->order('@.id')->all();
// $this->assertEquals(5, $orderItem->quantity);
$this->assertEquals(3, count($customers));
// }
$this->assertEquals(1, count($customers[0]->orders));
//
$this->assertEquals(2, count($customers[1]->orders));
// public function testDelete()
$this->assertEquals(0, count($customers[2]->orders));
// {
// // delete
// nested
// $customer = Customer::find(2);
$customers = Customer::find()->with('orders.customer')->order('@.id')->all();
// $this->assertTrue($customer instanceof Customer);
$this->assertEquals(3, count($customers));
// $this->assertEquals('user2', $customer->name);
$this->assertEquals(1, $customers[0]->orders[0]->customer->id);
// $customer->delete();
$this->assertEquals(2, $customers[1]->orders[0]->customer->id);
// $customer = Customer::find(2);
$this->assertEquals(2, $customers[1]->orders[1]->customer->id);
// $this->assertNull($customer);
//
// has many via relation
// // deleteAll
$orders = Order::find()->with('items')->order('@.id')->all();
// $customers = Customer::find()->all();
$this->assertEquals(3, count($orders));
// $this->assertEquals(2, count($customers));
$this->assertEquals(1, $orders[0]->items[0]->id);
// $ret = Customer::deleteAll();
$this->assertEquals(2, $orders[0]->items[1]->id);
// $this->assertEquals(2, $ret);
$this->assertEquals(3, $orders[1]->items[0]->id);
// $customers = Customer::find()->all();
$this->assertEquals(4, $orders[1]->items[1]->id);
// $this->assertEquals(0, count($customers));
$this->assertEquals(5, $orders[1]->items[2]->id);
// }
//
// has many via join table
// public function testFind()
$orders = Order::find()->with('books')->order('@.id')->all();
// {
$this->assertEquals(2, count($orders));
// // find one
$this->assertEquals(1, $orders[0]->books[0]->id);
// $result = Customer::find();
$this->assertEquals(2, $orders[0]->books[1]->id);
// $this->assertTrue($result instanceof ActiveQuery);
$this->assertEquals(2, $orders[1]->books[0]->id);
// $customer = $result->one();
// $this->assertTrue($customer instanceof Customer);
// has many and base limited
// $this->assertEquals(1, $result->count);
$orders = Order::find()->with('items')->order('@.id')->limit(2)->all();
//
$this->assertEquals(2, count($orders));
// // find all
$this->assertEquals(1, $orders[0]->items[0]->id);
// $result = Customer::find();
// $customers = $result->all();
/// customize "with" query
// $this->assertTrue(is_array($customers));
$orders = Order::find()->with(array('items' => function($q) {
// $this->assertEquals(3, count($customers));
$q->order('@.id DESC');
// $this->assertTrue($customers[0] instanceof Customer);
}))->order('@.id')->limit(2)->all();
// $this->assertTrue($customers[1] instanceof Customer);
$this->assertEquals(2, count($orders));
// $this->assertTrue($customers[2] instanceof Customer);
$this->assertEquals(2, $orders[0]->items[0]->id);
// $this->assertEquals(3, $result->count);
// $this->assertEquals(3, count($result));
// findBySql with
//
$orders = Order::findBySql('SELECT * FROM tbl_order WHERE customer_id=2')->with('items')->all();
// // check count first
$this->assertEquals(2, count($orders));
// $result = Customer::find();
// $this->assertEquals(3, $result->count);
// index and array
// $customer = $result->one();
$customers = Customer::find()->with('orders.customer')->order('@.id')->index('id')->asArray()->all();
// $this->assertTrue($customer instanceof Customer);
$this->assertEquals(3, count($customers));
// $this->assertEquals(3, $result->count);
$this->assertTrue(isset($customers[1], $customers[2], $customers[3]));
//
$this->assertTrue(is_array($customers[1]));
// // iterator
$this->assertEquals(1, count($customers[1]['orders']));
// $result = Customer::find();
$this->assertEquals(2, count($customers[2]['orders']));
// $count = 0;
$this->assertEquals(0, count($customers[3]['orders']));
// foreach ($result as $customer) {
$this->assertTrue(is_array($customers[1]['orders'][0]['customer']));
// $this->assertTrue($customer instanceof Customer);
// $count++;
// count with
// }
$this->assertEquals(3, Order::count());
// $this->assertEquals($count, $result->count);
$value = Order::count(array(
//
'select' => array('COUNT(DISTINCT @.id, @.customer_id)'),
// // array access
'with' => 'books',
// $result = Customer::find();
));
// $this->assertTrue($result[0] instanceof Customer);
$this->assertEquals(2, $value);
// $this->assertTrue($result[1] instanceof Customer);
// $this->assertTrue($result[2] instanceof Customer);
}
//
// // find by a single primary key
public function testLazyLoading()
// $customer = Customer::find(2);
{
// $this->assertTrue($customer instanceof Customer);
// has one
// $this->assertEquals('user2', $customer->name);
$order = Order::find(3);
//
$this->assertTrue($order->customer instanceof Customer);
// // find by attributes
$this->assertEquals(2, $order->customer->id);
// $customer = Customer::find()->where(array('name' => 'user2'))->one();
// $this->assertTrue($customer instanceof Customer);
// has many
// $this->assertEquals(2, $customer->id);
$customer = Customer::find(2);
//
$orders = $customer->orders;
// // find by Query
$this->assertEquals(2, count($orders));
// $query = array(
$this->assertEquals(2, $orders[0]->id);
// 'where' => 'id=:id',
$this->assertEquals(3, $orders[1]->id);
// 'params' => array(':id' => 2),
// );
// has many via join table
// $customer = Customer::find($query)->one();
$orders = Order::find()->order('@.id')->all();
// $this->assertTrue($customer instanceof Customer);
$this->assertEquals(3, count($orders));
// $this->assertEquals('user2', $customer->name);
$this->assertEquals(2, count($orders[0]->books));
//
$this->assertEquals(1, $orders[0]->books[0]->id);
// // find count
$this->assertEquals(2, $orders[0]->books[1]->id);
// $this->assertEquals(3, Customer::find()->count());
$this->assertEquals(array(), $orders[1]->books);
// $this->assertEquals(3, Customer::count());
$this->assertEquals(1, count($orders[2]->books));
// $this->assertEquals(2, Customer::count(array(
$this->assertEquals(2, $orders[2]->books[0]->id);
// 'where' => 'id=1 OR id=2',
// )));
// customized relation query
// }
$customer = Customer::find(2);
//
$orders = $customer->orders(array(
// public function testFindBySql()
'where' => '@.id = 3',
// {
));
// // find one
$this->assertEquals(1, count($orders));
// $customer = Customer::findBySql('SELECT * FROM tbl_customer ORDER BY id DESC')->one();
$this->assertEquals(3, $orders[0]->id);
// $this->assertTrue($customer instanceof Customer);
// $this->assertEquals('user3', $customer->name);
// original results are kept after customized query
//
$orders = $customer->orders;
// // find all
$this->assertEquals(2, count($orders));
// $customers = Customer::findBySql('SELECT * FROM tbl_customer')->all();
$this->assertEquals(2, $orders[0]->id);
// $this->assertEquals(3, count($customers));
$this->assertEquals(3, $orders[1]->id);
//
// // find with parameter binding
// as array
// $customer = Customer::findBySql('SELECT * FROM tbl_customer WHERE id=:id', array(':id' => 2))->one();
$orders = $customer->orders(array(
// $this->assertTrue($customer instanceof Customer);
'asArray' => true,
// $this->assertEquals('user2', $customer->name);
));
//
$this->assertEquals(2, count($orders));
// // count
$this->assertTrue(is_array($orders[0]));
// $query = Customer::findBySql('SELECT * FROM tbl_customer ORDER BY id DESC');
$this->assertEquals(2, $orders[0]['id']);
// $query->one();
$this->assertEquals(3, $orders[1]['id']);
// $this->assertEquals(3, $query->count);
// $query = Customer::findBySql('SELECT * FROM tbl_customer ORDER BY id DESC');
// using anonymous function to customize query condition
// $this->assertEquals(3, $query->count);
$orders = $customer->orders(function($q) {
// }
$q->order('@.id DESC')->asArray();
//
});
// public function testQueryMethods()
$this->assertEquals(2, count($orders));
// {
$this->assertTrue(is_array($orders[0]));
// $customer = Customer::find()->where('id=:id', array(':id' => 2))->one();
$this->assertEquals(3, $orders[0]['id']);
// $this->assertTrue($customer instanceof Customer);
$this->assertEquals(2, $orders[1]['id']);
// $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']);
// }
//
// public function testEagerLoading()
// {
// // has many
// $customers = Customer::find()->with('orders')->orderBy('@.id')->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));
//
// // nested
// $customers = Customer::find()->with('orders.customer')->orderBy('@.id')->all();
// $this->assertEquals(3, count($customers));
// $this->assertEquals(1, $customers[0]->orders[0]->customer->id);
// $this->assertEquals(2, $customers[1]->orders[0]->customer->id);
// $this->assertEquals(2, $customers[1]->orders[1]->customer->id);
//
// // has many via relation
// $orders = Order::find()->with('items')->orderBy('@.id')->all();
// $this->assertEquals(3, count($orders));
// $this->assertEquals(1, $orders[0]->items[0]->id);
// $this->assertEquals(2, $orders[0]->items[1]->id);
// $this->assertEquals(3, $orders[1]->items[0]->id);
// $this->assertEquals(4, $orders[1]->items[1]->id);
// $this->assertEquals(5, $orders[1]->items[2]->id);
//
// // has many via join table
// $orders = Order::find()->with('books')->orderBy('@.id')->all();
// $this->assertEquals(2, count($orders));
// $this->assertEquals(1, $orders[0]->books[0]->id);
// $this->assertEquals(2, $orders[0]->books[1]->id);
// $this->assertEquals(2, $orders[1]->books[0]->id);
//
// // has many and base limited
// $orders = Order::find()->with('items')->orderBy('@.id')->limit(2)->all();
// $this->assertEquals(2, count($orders));
// $this->assertEquals(1, $orders[0]->items[0]->id);
//
// /// customize "with" query
// $orders = Order::find()->with(array('items' => function($q) {
// $q->orderBy('@.id DESC');
// }))->orderBy('@.id')->limit(2)->all();
// $this->assertEquals(2, count($orders));
// $this->assertEquals(2, $orders[0]->items[0]->id);
//
// // findBySql with
// $orders = Order::findBySql('SELECT * FROM tbl_order WHERE customer_id=2')->with('items')->all();
// $this->assertEquals(2, count($orders));
//
// // index and array
// $customers = Customer::find()->with('orders.customer')->orderBy('@.id')->index('id')->asArray()->all();
// $this->assertEquals(3, count($customers));
// $this->assertTrue(isset($customers[1], $customers[2], $customers[3]));
// $this->assertTrue(is_array($customers[1]));
// $this->assertEquals(1, count($customers[1]['orders']));
// $this->assertEquals(2, count($customers[2]['orders']));
// $this->assertEquals(0, count($customers[3]['orders']));
// $this->assertTrue(is_array($customers[1]['orders'][0]['customer']));
//
// // count with
// $this->assertEquals(3, Order::count());
// $value = Order::count(array(
// 'select' => array('COUNT(DISTINCT @.id, @.customer_id)'),
// 'with' => 'books',
// ));
// $this->assertEquals(2, $value);
//
// }
//
// public function testLazyLoading()
// {
// // has one
// $order = Order::find(3);
// $this->assertTrue($order->customer instanceof Customer);
// $this->assertEquals(2, $order->customer->id);
//
// // has many
// $customer = Customer::find(2);
// $orders = $customer->orders;
// $this->assertEquals(2, count($orders));
// $this->assertEquals(2, $orders[0]->id);
// $this->assertEquals(3, $orders[1]->id);
//
// // has many via join table
// $orders = Order::find()->orderBy('@.id')->all();
// $this->assertEquals(3, count($orders));
// $this->assertEquals(2, count($orders[0]->books));
// $this->assertEquals(1, $orders[0]->books[0]->id);
// $this->assertEquals(2, $orders[0]->books[1]->id);
// $this->assertEquals(array(), $orders[1]->books);
// $this->assertEquals(1, count($orders[2]->books));
// $this->assertEquals(2, $orders[2]->books[0]->id);
//
// // customized relation query
// $customer = Customer::find(2);
// $orders = $customer->orders(array(
// 'where' => '@.id = 3',
// ));
// $this->assertEquals(1, count($orders));
// $this->assertEquals(3, $orders[0]->id);
//
// // original results are kept after customized query
// $orders = $customer->orders;
// $this->assertEquals(2, count($orders));
// $this->assertEquals(2, $orders[0]->id);
// $this->assertEquals(3, $orders[1]->id);
//
// // as array
// $orders = $customer->orders(array(
// 'asArray' => true,
// ));
// $this->assertEquals(2, count($orders));
// $this->assertTrue(is_array($orders[0]));
// $this->assertEquals(2, $orders[0]['id']);
// $this->assertEquals(3, $orders[1]['id']);
//
// // using anonymous function to customize query condition
// $orders = $customer->orders(function($q) {
// $q->orderBy('@.id DESC')->asArray();
// });
// $this->assertEquals(2, count($orders));
// $this->assertTrue(is_array($orders[0]));
// $this->assertEquals(3, $orders[0]['id']);
// $this->assertEquals(2, $orders[1]['id']);
// }
}
}