BatchQueryResultTest.php 3.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yiiunit\framework\db;

use Yii;
use yiiunit\data\ar\ActiveRecord;
use yii\db\Query;
use yii\db\BatchQueryResult;
use yiiunit\data\ar\Customer;

/**
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class BatchQueryResultTest extends DatabaseTestCase
{
	public function setUp()
	{
		parent::setUp();
		ActiveRecord::$db = $this->getConnection();
	}

	public function testQuery()
	{
		$db = $this->getConnection();

		// initialize property test
		$query = new Query();
		$query->from('tbl_customer')->orderBy('id');
		$result = $query->batch(2, $db);
		$this->assertTrue($result instanceof BatchQueryResult);
		$this->assertEquals(2, $result->batchSize);
		$this->assertTrue($result->query === $query);

		// normal query
		$query = new Query();
		$query->from('tbl_customer')->orderBy('id');
		$allRows = [];
		$batch = $query->batch(2, $db);
		foreach ($batch as $rows) {
			$allRows = array_merge($allRows, $rows);
		}
		$this->assertEquals(3, count($allRows));
		$this->assertEquals('user1', $allRows[0]['name']);
		$this->assertEquals('user2', $allRows[1]['name']);
		$this->assertEquals('user3', $allRows[2]['name']);
		// rewind
		$allRows = [];
		foreach ($batch as $rows) {
			$allRows = array_merge($allRows, $rows);
		}
		$this->assertEquals(3, count($allRows));
		// reset
		$batch->reset();
Qiang Xue committed
60 61 62 63 64 65 66 67 68 69

		// empty query
		$query = new Query();
		$query->from('tbl_customer')->where(['id' => 100]);
		$allRows = [];
		$batch = $query->batch(2, $db);
		foreach ($batch as $rows) {
			$allRows = array_merge($allRows, $rows);
		}
		$this->assertEquals(0, count($allRows));
70 71 72 73 74 75 76 77 78 79 80 81 82

		// query with index
		$query = new Query();
		$query->from('tbl_customer')->indexBy('name');
		$allRows = [];
		foreach ($query->batch(2, $db) as $rows) {
			$allRows = array_merge($allRows, $rows);
		}
		$this->assertEquals(3, count($allRows));
		$this->assertEquals('address1', $allRows['user1']['address']);
		$this->assertEquals('address2', $allRows['user2']['address']);
		$this->assertEquals('address3', $allRows['user3']['address']);

83
		// each
84 85 86
		$query = new Query();
		$query->from('tbl_customer')->orderBy('id');
		$allRows = [];
Qiang Xue committed
87
		foreach ($query->each(100, $db) as $rows) {
88 89 90 91 92 93
			$allRows[] = $rows;
		}
		$this->assertEquals(3, count($allRows));
		$this->assertEquals('user1', $allRows[0]['name']);
		$this->assertEquals('user2', $allRows[1]['name']);
		$this->assertEquals('user3', $allRows[2]['name']);
94 95 96 97 98

		// each with key
		$query = new Query();
		$query->from('tbl_customer')->orderBy('id')->indexBy('name');
		$allRows = [];
Qiang Xue committed
99
		foreach ($query->each(100, $db) as $key => $row) {
100 101 102 103 104 105
			$allRows[$key] = $row;
		}
		$this->assertEquals(3, count($allRows));
		$this->assertEquals('address1', $allRows['user1']['address']);
		$this->assertEquals('address2', $allRows['user2']['address']);
		$this->assertEquals('address3', $allRows['user3']['address']);
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
	}

	public function testActiveQuery()
	{
		$db = $this->getConnection();

		$query = Customer::find()->orderBy('id');
		$customers = [];
		foreach ($query->batch(2, $db) as $models) {
			$customers = array_merge($customers, $models);
		}
		$this->assertEquals(3, count($customers));
		$this->assertEquals('user1', $customers[0]->name);
		$this->assertEquals('user2', $customers[1]->name);
		$this->assertEquals('user3', $customers[2]->name);

		// batch with eager loading
		$query = Customer::find()->with('orders')->orderBy('id');
		$customers = [];
		foreach ($query->batch(2, $db) as $models) {
			$customers = array_merge($customers, $models);
			foreach ($models as $model) {
				$this->assertTrue($model->isRelationPopulated('orders'));
			}
		}
		$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));
	}
}