ActiveDataProviderTest.php 2.71 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?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
use yii\db\Query;
12 13 14 15 16 17 18
use yiiunit\data\ar\ActiveRecord;
use yiiunit\framework\db\DatabaseTestCase;
use yiiunit\data\ar\Order;

/**
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
19 20
 *
 * @group data
21 22 23 24 25 26 27 28 29 30 31
 */
class ActiveDataProviderTest extends DatabaseTestCase
{
	protected function setUp()
	{
		parent::setUp();
		ActiveRecord::$db = $this->getConnection();
	}

	public function testActiveQuery()
	{
Alexander Makarov committed
32
		$provider = new ActiveDataProvider([
33
			'query' => Order::find()->orderBy('id'),
Alexander Makarov committed
34
		]);
35
		$orders = $provider->getModels();
36
		$this->assertEquals(3, count($orders));
37
		$this->assertTrue($orders[0] instanceof Order);
Alexander Makarov committed
38
		$this->assertEquals([1, 2, 3], $provider->getKeys());
39

Alexander Makarov committed
40
		$provider = new ActiveDataProvider([
41
			'query' => Order::find(),
Alexander Makarov committed
42
			'pagination' => [
43
				'pageSize' => 2,
Alexander Makarov committed
44 45
			]
		]);
46
		$orders = $provider->getModels();
47 48 49 50 51
		$this->assertEquals(2, count($orders));
	}

	public function testQuery()
	{
52
		$query = new Query;
Alexander Makarov committed
53
		$provider = new ActiveDataProvider([
Qiang Xue committed
54
			'db' => $this->getConnection(),
55
			'query' => $query->from('tbl_order')->orderBy('id'),
Alexander Makarov committed
56
		]);
57
		$orders = $provider->getModels();
58 59
		$this->assertEquals(3, count($orders));
		$this->assertTrue(is_array($orders[0]));
Alexander Makarov committed
60
		$this->assertEquals([0, 1, 2], $provider->getKeys());
61

62
		$query = new Query;
Alexander Makarov committed
63
		$provider = new ActiveDataProvider([
Qiang Xue committed
64
			'db' => $this->getConnection(),
65
			'query' => $query->from('tbl_order'),
Alexander Makarov committed
66
			'pagination' => [
67
				'pageSize' => 2,
Alexander Makarov committed
68 69
			]
		]);
70
		$orders = $provider->getModels();
71
		$this->assertEquals(2, count($orders));
72
	}
73 74 75 76

	public function testRefresh()
	{
		$query = new Query;
Alexander Makarov committed
77
		$provider = new ActiveDataProvider([
78 79
			'db' => $this->getConnection(),
			'query' => $query->from('tbl_order')->orderBy('id'),
Alexander Makarov committed
80
		]);
81 82 83 84 85 86 87
		$this->assertEquals(3, count($provider->getModels()));

		$provider->getPagination()->pageSize = 2;
		$this->assertEquals(3, count($provider->getModels()));
		$provider->refresh();
		$this->assertEquals(2, count($provider->getModels()));
	}
88 89 90 91
	
	public function testPaginationBeforeModels() 
	{
		$query = new Query;
Alexander Makarov committed
92
		$provider = new ActiveDataProvider([
93 94
			'db' => $this->getConnection(),
			'query' => $query->from('tbl_order')->orderBy('id'),
Alexander Makarov committed
95
		]);
96
		$pagination = $provider->getPagination();
Qiang Xue committed
97
		$this->assertEquals(0, $pagination->getPageCount());
98
		$this->assertCount(3, $provider->getModels());
Qiang Xue committed
99 100
		$this->assertEquals(1, $pagination->getPageCount());

101 102 103 104 105
		$provider->getPagination()->pageSize = 2;
		$this->assertEquals(3, count($provider->getModels()));
		$provider->refresh();
		$this->assertEquals(2, count($provider->getModels()));
	}
106
}