BatchQueryResultTest.php 4.45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?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;

/**
Carsten Brandt committed
17 18
 * @group db
 * @group mysql
19 20 21
 */
class BatchQueryResultTest extends DatabaseTestCase
{
22 23 24 25 26
    public function setUp()
    {
        parent::setUp();
        ActiveRecord::$db = $this->getConnection();
    }
27

28 29 30
    public function testQuery()
    {
        $db = $this->getConnection();
31

32 33
        // initialize property test
        $query = new Query();
34
        $query->from('customer')->orderBy('id');
35 36 37 38
        $result = $query->batch(2, $db);
        $this->assertTrue($result instanceof BatchQueryResult);
        $this->assertEquals(2, $result->batchSize);
        $this->assertTrue($result->query === $query);
39

40 41
        // normal query
        $query = new Query();
42
        $query->from('customer')->orderBy('id');
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
        $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
        // empty query
        $query = new Query();
63
        $query->from('customer')->where(['id' => 100]);
64 65 66 67 68 69
        $allRows = [];
        $batch = $query->batch(2, $db);
        foreach ($batch as $rows) {
            $allRows = array_merge($allRows, $rows);
        }
        $this->assertEquals(0, count($allRows));
70

71 72
        // query with index
        $query = new Query();
73
        $query->from('customer')->indexBy('name');
74 75 76 77 78 79 80 81
        $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']);
82

83 84
        // each
        $query = new Query();
85
        $query->from('customer')->orderBy('id');
86 87 88 89 90 91 92 93
        $allRows = [];
        foreach ($query->each(100, $db) as $rows) {
            $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
        // each with key
        $query = new Query();
97
        $query->from('customer')->orderBy('id')->indexBy('name');
98 99 100 101 102 103 104 105 106
        $allRows = [];
        foreach ($query->each(100, $db) as $key => $row) {
            $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']);
    }
107

108 109 110
    public function testActiveQuery()
    {
        $db = $this->getConnection();
111

112 113 114 115 116 117 118 119 120
        $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);
121

122 123 124 125 126 127 128 129 130 131 132 133 134 135
        // 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));
    }
136
}