You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
181 lines
5.5 KiB
181 lines
5.5 KiB
11 years ago
|
<?php
|
||
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
|
namespace yiiunit\framework\data;
|
||
|
|
||
|
use yii\data\ActiveDataProvider;
|
||
11 years ago
|
use yii\db\Query;
|
||
11 years ago
|
use yiiunit\data\ar\ActiveRecord;
|
||
11 years ago
|
use yiiunit\data\ar\Customer;
|
||
|
use yiiunit\data\ar\Item;
|
||
11 years ago
|
use yiiunit\framework\db\DatabaseTestCase;
|
||
|
use yiiunit\data\ar\Order;
|
||
|
|
||
|
/**
|
||
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||
|
* @since 2.0
|
||
11 years ago
|
*
|
||
|
* @group data
|
||
11 years ago
|
* @group db
|
||
11 years ago
|
*/
|
||
|
class ActiveDataProviderTest extends DatabaseTestCase
|
||
|
{
|
||
11 years ago
|
protected function setUp()
|
||
|
{
|
||
|
parent::setUp();
|
||
|
ActiveRecord::$db = $this->getConnection();
|
||
|
}
|
||
|
|
||
|
public function testActiveQuery()
|
||
|
{
|
||
|
$provider = new ActiveDataProvider([
|
||
|
'query' => Order::find()->orderBy('id'),
|
||
|
]);
|
||
|
$orders = $provider->getModels();
|
||
|
$this->assertEquals(3, count($orders));
|
||
|
$this->assertTrue($orders[0] instanceof Order);
|
||
|
$this->assertTrue($orders[1] instanceof Order);
|
||
|
$this->assertTrue($orders[2] instanceof Order);
|
||
|
$this->assertEquals([1, 2, 3], $provider->getKeys());
|
||
|
|
||
|
$provider = new ActiveDataProvider([
|
||
|
'query' => Order::find(),
|
||
|
'pagination' => [
|
||
|
'pageSize' => 2,
|
||
|
]
|
||
|
]);
|
||
|
$orders = $provider->getModels();
|
||
|
$this->assertEquals(2, count($orders));
|
||
|
}
|
||
|
|
||
|
public function testActiveRelation()
|
||
|
{
|
||
10 years ago
|
/* @var $customer Customer */
|
||
11 years ago
|
$customer = Customer::findOne(2);
|
||
11 years ago
|
$provider = new ActiveDataProvider([
|
||
|
'query' => $customer->getOrders(),
|
||
|
]);
|
||
|
$orders = $provider->getModels();
|
||
|
$this->assertEquals(2, count($orders));
|
||
|
$this->assertTrue($orders[0] instanceof Order);
|
||
|
$this->assertTrue($orders[1] instanceof Order);
|
||
|
$this->assertEquals([2, 3], $provider->getKeys());
|
||
|
|
||
|
$provider = new ActiveDataProvider([
|
||
|
'query' => $customer->getOrders(),
|
||
|
'pagination' => [
|
||
|
'pageSize' => 1,
|
||
|
]
|
||
|
]);
|
||
|
$orders = $provider->getModels();
|
||
|
$this->assertEquals(1, count($orders));
|
||
|
}
|
||
|
|
||
|
public function testActiveRelationVia()
|
||
|
{
|
||
10 years ago
|
/* @var $order Order */
|
||
11 years ago
|
$order = Order::findOne(2);
|
||
11 years ago
|
$provider = new ActiveDataProvider([
|
||
|
'query' => $order->getItems(),
|
||
|
]);
|
||
|
$items = $provider->getModels();
|
||
|
$this->assertEquals(3, count($items));
|
||
|
$this->assertTrue($items[0] instanceof Item);
|
||
|
$this->assertTrue($items[1] instanceof Item);
|
||
|
$this->assertTrue($items[2] instanceof Item);
|
||
|
$this->assertEquals([3, 4, 5], $provider->getKeys());
|
||
|
|
||
|
$provider = new ActiveDataProvider([
|
||
|
'query' => $order->getItems(),
|
||
|
'pagination' => [
|
||
|
'pageSize' => 2,
|
||
|
]
|
||
|
]);
|
||
|
$items = $provider->getModels();
|
||
|
$this->assertEquals(2, count($items));
|
||
|
}
|
||
|
|
||
|
public function testActiveRelationViaTable()
|
||
|
{
|
||
10 years ago
|
/* @var $order Order */
|
||
11 years ago
|
$order = Order::findOne(1);
|
||
11 years ago
|
$provider = new ActiveDataProvider([
|
||
|
'query' => $order->getBooks(),
|
||
|
]);
|
||
|
$items = $provider->getModels();
|
||
|
$this->assertEquals(2, count($items));
|
||
|
$this->assertTrue($items[0] instanceof Item);
|
||
|
$this->assertTrue($items[1] instanceof Item);
|
||
|
|
||
|
$provider = new ActiveDataProvider([
|
||
|
'query' => $order->getBooks(),
|
||
|
'pagination' => [
|
||
|
'pageSize' => 1,
|
||
|
]
|
||
|
]);
|
||
|
$items = $provider->getModels();
|
||
|
$this->assertEquals(1, count($items));
|
||
|
}
|
||
|
|
||
|
public function testQuery()
|
||
|
{
|
||
|
$query = new Query;
|
||
|
$provider = new ActiveDataProvider([
|
||
|
'db' => $this->getConnection(),
|
||
11 years ago
|
'query' => $query->from('order')->orderBy('id'),
|
||
11 years ago
|
]);
|
||
|
$orders = $provider->getModels();
|
||
|
$this->assertEquals(3, count($orders));
|
||
|
$this->assertTrue(is_array($orders[0]));
|
||
|
$this->assertEquals([0, 1, 2], $provider->getKeys());
|
||
|
|
||
|
$query = new Query;
|
||
|
$provider = new ActiveDataProvider([
|
||
|
'db' => $this->getConnection(),
|
||
11 years ago
|
'query' => $query->from('order'),
|
||
11 years ago
|
'pagination' => [
|
||
|
'pageSize' => 2,
|
||
|
]
|
||
|
]);
|
||
|
$orders = $provider->getModels();
|
||
|
$this->assertEquals(2, count($orders));
|
||
|
}
|
||
|
|
||
|
public function testRefresh()
|
||
|
{
|
||
|
$query = new Query;
|
||
|
$provider = new ActiveDataProvider([
|
||
|
'db' => $this->getConnection(),
|
||
11 years ago
|
'query' => $query->from('order')->orderBy('id'),
|
||
11 years ago
|
]);
|
||
|
$this->assertEquals(3, count($provider->getModels()));
|
||
|
|
||
|
$provider->getPagination()->pageSize = 2;
|
||
|
$this->assertEquals(3, count($provider->getModels()));
|
||
|
$provider->refresh();
|
||
|
$this->assertEquals(2, count($provider->getModels()));
|
||
|
}
|
||
|
|
||
|
public function testPaginationBeforeModels()
|
||
|
{
|
||
|
$query = new Query;
|
||
|
$provider = new ActiveDataProvider([
|
||
|
'db' => $this->getConnection(),
|
||
11 years ago
|
'query' => $query->from('order')->orderBy('id'),
|
||
11 years ago
|
]);
|
||
|
$pagination = $provider->getPagination();
|
||
|
$this->assertEquals(0, $pagination->getPageCount());
|
||
|
$this->assertCount(3, $provider->getModels());
|
||
|
$this->assertEquals(1, $pagination->getPageCount());
|
||
|
|
||
|
$provider->getPagination()->pageSize = 2;
|
||
|
$this->assertEquals(3, count($provider->getModels()));
|
||
|
$provider->refresh();
|
||
|
$this->assertEquals(2, count($provider->getModels()));
|
||
|
}
|
||
11 years ago
|
}
|