|
|
@ -123,9 +123,11 @@ class ActiveRecordTest extends RedisTestCase |
|
|
|
$this->assertEquals('user2', $customerName); |
|
|
|
$this->assertEquals('user2', $customerName); |
|
|
|
|
|
|
|
|
|
|
|
// find by column values |
|
|
|
// find by column values |
|
|
|
$customer = Customer::find(array('id' => 2)); |
|
|
|
$customer = Customer::find(array('id' => 2, 'name' => 'user2')); |
|
|
|
$this->assertTrue($customer instanceof Customer); |
|
|
|
$this->assertTrue($customer instanceof Customer); |
|
|
|
$this->assertEquals('user2', $customer->name); |
|
|
|
$this->assertEquals('user2', $customer->name); |
|
|
|
|
|
|
|
$customer = Customer::find(array('id' => 2, 'name' => 'user1')); |
|
|
|
|
|
|
|
$this->assertNull($customer); |
|
|
|
$customer = Customer::find(array('id' => 5)); |
|
|
|
$customer = Customer::find(array('id' => 5)); |
|
|
|
$this->assertNull($customer); |
|
|
|
$this->assertNull($customer); |
|
|
|
|
|
|
|
|
|
|
@ -135,13 +137,14 @@ class ActiveRecordTest extends RedisTestCase |
|
|
|
$this->assertEquals(2, $customer->id); |
|
|
|
$this->assertEquals(2, $customer->id); |
|
|
|
|
|
|
|
|
|
|
|
// find count, sum, average, min, max, scalar |
|
|
|
// find count, sum, average, min, max, scalar |
|
|
|
|
|
|
|
$this->assertEquals(3, Customer::find()->count()); |
|
|
|
$this->assertEquals(6, Customer::find()->sum('id')); |
|
|
|
$this->assertEquals(6, Customer::find()->sum('id')); |
|
|
|
$this->assertEquals(2, Customer::find()->average('id')); |
|
|
|
$this->assertEquals(2, Customer::find()->average('id')); |
|
|
|
$this->assertEquals(1, Customer::find()->min('id')); |
|
|
|
$this->assertEquals(1, Customer::find()->min('id')); |
|
|
|
$this->assertEquals(3, Customer::find()->max('id')); |
|
|
|
$this->assertEquals(3, Customer::find()->max('id')); |
|
|
|
|
|
|
|
|
|
|
|
// scope |
|
|
|
// scope |
|
|
|
// $this->assertEquals(2, Customer::find()->active()->count()); |
|
|
|
$this->assertEquals(2, Customer::find()->active()->count()); |
|
|
|
|
|
|
|
|
|
|
|
// asArray |
|
|
|
// asArray |
|
|
|
$customer = Customer::find()->where(array('id' => 2))->asArray()->one(); |
|
|
|
$customer = Customer::find()->where(array('id' => 2))->asArray()->one(); |
|
|
@ -261,125 +264,78 @@ class ActiveRecordTest extends RedisTestCase |
|
|
|
$this->assertEquals(3, $orders[0]->id); |
|
|
|
$this->assertEquals(3, $orders[0]->id); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// public function testFindEager() |
|
|
|
public function testFindEager() |
|
|
|
// { |
|
|
|
{ |
|
|
|
// $customers = Customer::find()->with('orders')->all(); |
|
|
|
$customers = Customer::find()->with('orders')->all(); |
|
|
|
// $this->assertEquals(3, count($customers)); |
|
|
|
$this->assertEquals(3, count($customers)); |
|
|
|
// $this->assertEquals(1, count($customers[0]->orders)); |
|
|
|
$this->assertEquals(1, count($customers[0]->orders)); |
|
|
|
// $this->assertEquals(2, count($customers[1]->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')->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() |
|
|
|
public function testFindLazyVia() |
|
|
|
{ |
|
|
|
{ |
|
|
|
/** @var $order Order * / |
|
|
|
/** @var $order Order */ |
|
|
|
$order = Order::find(1); |
|
|
|
$order = Order::find(1); |
|
|
|
$this->assertEquals(1, $order->id); |
|
|
|
$this->assertEquals(1, $order->id); |
|
|
|
$this->assertEquals(2, count($order->books)); |
|
|
|
$this->assertEquals(2, count($order->items)); |
|
|
|
$this->assertEquals(1, $order->items[0]->id); |
|
|
|
$this->assertEquals(1, $order->items[0]->id); |
|
|
|
$this->assertEquals(2, $order->items[1]->id); |
|
|
|
$this->assertEquals(2, $order->items[1]->id); |
|
|
|
|
|
|
|
|
|
|
|
$order = Order::find(2); |
|
|
|
$order = Order::find(1); |
|
|
|
$this->assertEquals(2, $order->id); |
|
|
|
$order->id = 100; |
|
|
|
$this->assertEquals(0, count($order->books)); |
|
|
|
$this->assertEquals(array(), $order->items); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function testFindEagerViaTable() |
|
|
|
public function testFindEagerViaRelation() |
|
|
|
{ |
|
|
|
{ |
|
|
|
$orders = Order::find()->with('books')->all(); |
|
|
|
$orders = Order::find()->with('items')->all(); |
|
|
|
$this->assertEquals(3, count($orders)); |
|
|
|
$this->assertEquals(3, count($orders)); |
|
|
|
|
|
|
|
|
|
|
|
$order = $orders[0]; |
|
|
|
$order = $orders[0]; |
|
|
|
$this->assertEquals(1, $order->id); |
|
|
|
$this->assertEquals(1, $order->id); |
|
|
|
$this->assertEquals(2, count($order->books)); |
|
|
|
$this->assertEquals(2, count($order->items)); |
|
|
|
$this->assertEquals(1, $order->books[0]->id); |
|
|
|
$this->assertEquals(1, $order->items[0]->id); |
|
|
|
$this->assertEquals(2, $order->books[1]->id); |
|
|
|
$this->assertEquals(2, $order->items[1]->id); |
|
|
|
|
|
|
|
} |
|
|
|
$order = $orders[1]; |
|
|
|
|
|
|
|
$this->assertEquals(2, $order->id); |
|
|
|
public function testFindNestedRelation() |
|
|
|
$this->assertEquals(0, count($order->books)); |
|
|
|
{ |
|
|
|
|
|
|
|
$customers = Customer::find()->with('orders', 'orders.items')->all(); |
|
|
|
$order = $orders[2]; |
|
|
|
$this->assertEquals(3, count($customers)); |
|
|
|
$this->assertEquals(3, $order->id); |
|
|
|
$this->assertEquals(1, count($customers[0]->orders)); |
|
|
|
$this->assertEquals(1, count($order->books)); |
|
|
|
$this->assertEquals(2, count($customers[1]->orders)); |
|
|
|
$this->assertEquals(2, $order->books[0]->id); |
|
|
|
$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)); |
|
|
|
// public function testFindNestedRelation() |
|
|
|
$this->assertEquals(1, count($customers[1]->orders[1]->items)); |
|
|
|
// { |
|
|
|
} |
|
|
|
// $customers = Customer::find()->with('orders', 'orders.items')->all(); |
|
|
|
|
|
|
|
// $this->assertEquals(3, count($customers)); |
|
|
|
public function testLink() |
|
|
|
// $this->assertEquals(1, count($customers[0]->orders)); |
|
|
|
{ |
|
|
|
// $this->assertEquals(2, count($customers[1]->orders)); |
|
|
|
$customer = Customer::find(2); |
|
|
|
// $this->assertEquals(0, count($customers[2]->orders)); |
|
|
|
$this->assertEquals(2, count($customer->orders)); |
|
|
|
// $this->assertEquals(2, count($customers[0]->orders[0]->items)); |
|
|
|
|
|
|
|
// $this->assertEquals(3, count($customers[1]->orders[0]->items)); |
|
|
|
// has many |
|
|
|
// $this->assertEquals(1, count($customers[1]->orders[1]->items)); |
|
|
|
$order = new Order; |
|
|
|
// } |
|
|
|
$order->total = 100; |
|
|
|
|
|
|
|
$this->assertTrue($order->isNewRecord); |
|
|
|
// public function testLink() |
|
|
|
$customer->link('orders', $order); |
|
|
|
// { |
|
|
|
$this->assertEquals(3, count($customer->orders)); |
|
|
|
// $customer = Customer::find(2); |
|
|
|
$this->assertFalse($order->isNewRecord); |
|
|
|
// $this->assertEquals(2, count($customer->orders)); |
|
|
|
$this->assertEquals(3, count($customer->getOrders()->all())); |
|
|
|
// |
|
|
|
$this->assertEquals(2, $order->customer_id); |
|
|
|
// // has many |
|
|
|
|
|
|
|
// $order = new Order; |
|
|
|
// belongs to |
|
|
|
// $order->total = 100; |
|
|
|
$order = new Order; |
|
|
|
// $this->assertTrue($order->isNewRecord); |
|
|
|
$order->total = 100; |
|
|
|
// $customer->link('orders', $order); |
|
|
|
$this->assertTrue($order->isNewRecord); |
|
|
|
// $this->assertEquals(3, count($customer->orders)); |
|
|
|
$customer = Customer::find(1); |
|
|
|
// $this->assertFalse($order->isNewRecord); |
|
|
|
$this->assertNull($order->customer); |
|
|
|
// $this->assertEquals(3, count($customer->getOrders()->all())); |
|
|
|
$order->link('customer', $customer); |
|
|
|
// $this->assertEquals(2, $order->customer_id); |
|
|
|
$this->assertFalse($order->isNewRecord); |
|
|
|
// |
|
|
|
$this->assertEquals(1, $order->customer_id); |
|
|
|
// // belongs to |
|
|
|
$this->assertEquals(1, $order->customer->id); |
|
|
|
// $order = new Order; |
|
|
|
|
|
|
|
// $order->total = 100; |
|
|
|
// TODO support via |
|
|
|
// $this->assertTrue($order->isNewRecord); |
|
|
|
|
|
|
|
// $customer = Customer::find(1); |
|
|
|
|
|
|
|
// $this->assertNull($order->customer); |
|
|
|
|
|
|
|
// $order->link('customer', $customer); |
|
|
|
|
|
|
|
// $this->assertFalse($order->isNewRecord); |
|
|
|
|
|
|
|
// $this->assertEquals(1, $order->customer_id); |
|
|
|
|
|
|
|
// $this->assertEquals(1, $order->customer->id); |
|
|
|
|
|
|
|
// |
|
|
|
|
|
|
|
// // via table |
|
|
|
|
|
|
|
// $order = Order::find(2); |
|
|
|
|
|
|
|
// $this->assertEquals(0, count($order->books)); |
|
|
|
|
|
|
|
// $orderItem = OrderItem::find(array('order_id' => 2, 'item_id' => 1)); |
|
|
|
|
|
|
|
// $this->assertNull($orderItem); |
|
|
|
|
|
|
|
// $item = Item::find(1); |
|
|
|
|
|
|
|
// $order->link('books', $item, array('quantity' => 10, 'subtotal' => 100)); |
|
|
|
|
|
|
|
// $this->assertEquals(1, count($order->books)); |
|
|
|
|
|
|
|
// $orderItem = OrderItem::find(array('order_id' => 2, 'item_id' => 1)); |
|
|
|
|
|
|
|
// $this->assertTrue($orderItem instanceof OrderItem); |
|
|
|
|
|
|
|
// $this->assertEquals(10, $orderItem->quantity); |
|
|
|
|
|
|
|
// $this->assertEquals(100, $orderItem->subtotal); |
|
|
|
|
|
|
|
// |
|
|
|
|
|
|
|
// // via model |
|
|
|
// // via model |
|
|
|
// $order = Order::find(1); |
|
|
|
// $order = Order::find(1); |
|
|
|
// $this->assertEquals(2, count($order->items)); |
|
|
|
// $this->assertEquals(2, count($order->items)); |
|
|
@ -394,17 +350,18 @@ class ActiveRecordTest extends RedisTestCase |
|
|
|
// $this->assertTrue($orderItem instanceof OrderItem); |
|
|
|
// $this->assertTrue($orderItem instanceof OrderItem); |
|
|
|
// $this->assertEquals(10, $orderItem->quantity); |
|
|
|
// $this->assertEquals(10, $orderItem->quantity); |
|
|
|
// $this->assertEquals(100, $orderItem->subtotal); |
|
|
|
// $this->assertEquals(100, $orderItem->subtotal); |
|
|
|
// } |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// public function testUnlink() |
|
|
|
public function testUnlink() |
|
|
|
// { |
|
|
|
{ |
|
|
|
// // has many |
|
|
|
// has many |
|
|
|
// $customer = Customer::find(2); |
|
|
|
$customer = Customer::find(2); |
|
|
|
// $this->assertEquals(2, count($customer->orders)); |
|
|
|
$this->assertEquals(2, count($customer->orders)); |
|
|
|
// $customer->unlink('orders', $customer->orders[1], true); |
|
|
|
$customer->unlink('orders', $customer->orders[1], true); |
|
|
|
// $this->assertEquals(1, count($customer->orders)); |
|
|
|
$this->assertEquals(1, count($customer->orders)); |
|
|
|
// $this->assertNull(Order::find(3)); |
|
|
|
$this->assertNull(Order::find(3)); |
|
|
|
// |
|
|
|
|
|
|
|
|
|
|
|
// TODO support via |
|
|
|
// // via model |
|
|
|
// // via model |
|
|
|
// $order = Order::find(2); |
|
|
|
// $order = Order::find(2); |
|
|
|
// $this->assertEquals(3, count($order->items)); |
|
|
|
// $this->assertEquals(3, count($order->items)); |
|
|
@ -412,14 +369,7 @@ class ActiveRecordTest extends RedisTestCase |
|
|
|
// $order->unlink('items', $order->items[2], true); |
|
|
|
// $order->unlink('items', $order->items[2], true); |
|
|
|
// $this->assertEquals(2, count($order->items)); |
|
|
|
// $this->assertEquals(2, count($order->items)); |
|
|
|
// $this->assertEquals(2, count($order->orderItems)); |
|
|
|
// $this->assertEquals(2, count($order->orderItems)); |
|
|
|
// |
|
|
|
} |
|
|
|
// // via table |
|
|
|
|
|
|
|
// $order = Order::find(1); |
|
|
|
|
|
|
|
// $this->assertEquals(2, count($order->books)); |
|
|
|
|
|
|
|
// $order->unlink('books', $order->books[1], true); |
|
|
|
|
|
|
|
// $this->assertEquals(1, count($order->books)); |
|
|
|
|
|
|
|
// $this->assertEquals(1, count($order->orderItems)); |
|
|
|
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function testInsert() |
|
|
|
public function testInsert() |
|
|
|
{ |
|
|
|
{ |
|
|
@ -453,16 +403,6 @@ class ActiveRecordTest extends RedisTestCase |
|
|
|
$customer2 = Customer::find(2); |
|
|
|
$customer2 = Customer::find(2); |
|
|
|
$this->assertEquals('user2x', $customer2->name); |
|
|
|
$this->assertEquals('user2x', $customer2->name); |
|
|
|
|
|
|
|
|
|
|
|
// updateCounters |
|
|
|
|
|
|
|
$pk = array('order_id' => 2, 'item_id' => 4); |
|
|
|
|
|
|
|
$orderItem = OrderItem::find($pk); |
|
|
|
|
|
|
|
$this->assertEquals(1, $orderItem->quantity); |
|
|
|
|
|
|
|
$ret = $orderItem->updateCounters(array('quantity' => -1)); |
|
|
|
|
|
|
|
$this->assertTrue($ret); |
|
|
|
|
|
|
|
$this->assertEquals(0, $orderItem->quantity); |
|
|
|
|
|
|
|
$orderItem = OrderItem::find($pk); |
|
|
|
|
|
|
|
$this->assertEquals(0, $orderItem->quantity); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// updateAll |
|
|
|
// updateAll |
|
|
|
$customer = Customer::find(3); |
|
|
|
$customer = Customer::find(3); |
|
|
|
$this->assertEquals('user3', $customer->name); |
|
|
|
$this->assertEquals('user3', $customer->name); |
|
|
@ -472,8 +412,21 @@ class ActiveRecordTest extends RedisTestCase |
|
|
|
$this->assertEquals(1, $ret); |
|
|
|
$this->assertEquals(1, $ret); |
|
|
|
$customer = Customer::find(3); |
|
|
|
$customer = Customer::find(3); |
|
|
|
$this->assertEquals('temp', $customer->name); |
|
|
|
$this->assertEquals('temp', $customer->name); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function testUpdateCounters() |
|
|
|
|
|
|
|
{ |
|
|
|
// updateCounters |
|
|
|
// updateCounters |
|
|
|
|
|
|
|
$pk = array('order_id' => 2, 'item_id' => 4); |
|
|
|
|
|
|
|
$orderItem = OrderItem::find($pk); |
|
|
|
|
|
|
|
$this->assertEquals(1, $orderItem->quantity); |
|
|
|
|
|
|
|
$ret = $orderItem->updateCounters(array('quantity' => -1)); |
|
|
|
|
|
|
|
$this->assertTrue($ret); |
|
|
|
|
|
|
|
$this->assertEquals(0, $orderItem->quantity); |
|
|
|
|
|
|
|
$orderItem = OrderItem::find($pk); |
|
|
|
|
|
|
|
$this->assertEquals(0, $orderItem->quantity); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// updateAllCounters |
|
|
|
$pk = array('order_id' => 1, 'item_id' => 2); |
|
|
|
$pk = array('order_id' => 1, 'item_id' => 2); |
|
|
|
$orderItem = OrderItem::find($pk); |
|
|
|
$orderItem = OrderItem::find($pk); |
|
|
|
$this->assertEquals(2, $orderItem->quantity); |
|
|
|
$this->assertEquals(2, $orderItem->quantity); |
|
|
@ -487,6 +440,22 @@ class ActiveRecordTest extends RedisTestCase |
|
|
|
$this->assertEquals(30, $orderItem->subtotal); |
|
|
|
$this->assertEquals(30, $orderItem->subtotal); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function testUpdatePk() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// updateCounters |
|
|
|
|
|
|
|
$pk = array('order_id' => 2, 'item_id' => 4); |
|
|
|
|
|
|
|
$orderItem = OrderItem::find($pk); |
|
|
|
|
|
|
|
$this->assertEquals(2, $orderItem->order_id); |
|
|
|
|
|
|
|
$this->assertEquals(4, $orderItem->item_id); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$orderItem->order_id = 2; |
|
|
|
|
|
|
|
$orderItem->item_id = 10; |
|
|
|
|
|
|
|
$orderItem->save(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$this->assertNull(OrderItem::find($pk)); |
|
|
|
|
|
|
|
$this->assertNotNull(OrderItem::find(array('order_id' => 2, 'item_id' => 10))); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function testDelete() |
|
|
|
public function testDelete() |
|
|
|
{ |
|
|
|
{ |
|
|
|
// delete |
|
|
|
// delete |
|
|
|