Passed
Pull Request — master (#380)
by Wilmer
16:46 queued 13:55
created

CommonBatchQueryResultTest::getAllRowsFromEach()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Tests\Common;
6
7
use PHPUnit\Framework\TestCase;
8
use Yiisoft\Db\Query\BatchQueryResultInterface;
9
use Yiisoft\Db\Tests\Support\TestTrait;
10
11
use function array_merge;
12
13
/**
14
 * @group mssql
15
 * @group mysql
16
 * @group pgsql
17
 * @group oracle
18
 * @group sqlite
19
 */
20
abstract class CommonBatchQueryResultTest extends TestCase
21
{
22
    use TestTrait;
23
24
    public function testBatchQueryResult(): void
25
    {
26
        // initialize property test
27
        $db = $this->getConnectionWithData();
28
29
        $query = $this->getQuery($db);
30
        $query->from('customer')->orderBy('id');
31
        $result = $query->batch(2);
32
33
        $this->assertInstanceOf(BatchQueryResultInterface::class, $result);
34
        $this->assertSame(2, $result->getBatchSize());
35
        $this->assertSame($result->getQuery(), $query);
36
37
        // normal query
38
        $query = $this->getQuery($db);
39
        $query->from('customer')->orderBy('id');
40
        $allRows = [];
41
        $batch = $query->batch(2);
42
        $step = 0;
43
44
        foreach ($batch as $rows) {
45
            $allRows = array_merge($allRows, $rows);
46
            $step++;
47
        }
48
49
        $this->assertCount(3, $allRows);
50
        $this->assertSame(2, $step);
51
        $this->assertSame('user1', $allRows[0]['name']);
52
        $this->assertSame('user2', $allRows[1]['name']);
53
        $this->assertSame('user3', $allRows[2]['name']);
54
55
        // rewind
56
        $allRows = [];
57
        $step = 0;
58
59
        foreach ($batch as $rows) {
60
            $allRows = array_merge($allRows, $rows);
61
            $step++;
62
        }
63
64
        $this->assertCount(3, $allRows);
65
        $this->assertSame(2, $step);
66
67
        $batch->reset();
68
69
        // empty query
70
        $query = $this->getQuery($db);
71
        $query->from('customer')->where(['id' => 100]);
72
        $allRows = [];
73
        $batch = $query->batch(2);
74
75
        foreach ($batch as $rows) {
76
            $allRows = array_merge($allRows, $rows);
77
        }
78
79
        $this->assertCount(0, $allRows);
80
81
        // query with index
82
        $query = $this->getQuery($db);
83
        $query->from('customer')->indexBy('name');
84
        $allRows = [];
85
86
        foreach ($query->batch(2) as $rows) {
87
            $allRows = array_merge($allRows, $rows);
88
        }
89
90
        $this->assertCount(3, $allRows);
91
        $this->assertSame('address1', $allRows['user1']['address']);
92
        $this->assertSame('address2', $allRows['user2']['address']);
93
        $this->assertSame('address3', $allRows['user3']['address']);
94
95
        // each
96
        $query = $this->getQuery($db);
97
        $query->from('customer')->orderBy('id');
98
        $allRows = $this->getAllRowsFromEach($query->each(2));
99
100
        $this->assertCount(3, $allRows);
101
        $this->assertSame('user1', $allRows[0]['name']);
102
        $this->assertSame('user2', $allRows[1]['name']);
103
        $this->assertSame('user3', $allRows[2]['name']);
104
105
        // each with key
106
        $query = $this->getQuery($db);
107
        $query->from('customer')->orderBy('id')->indexBy('name');
108
        $allRows = $this->getAllRowsFromEach($query->each(100));
109
110
        $this->assertCount(3, $allRows);
111
        $this->assertSame('address1', $allRows['user1']['address']);
112
        $this->assertSame('address2', $allRows['user2']['address']);
113
        $this->assertSame('address3', $allRows['user3']['address']);
114
    }
115
116
    public function testBatchWithoutDbParameter(): void
117
    {
118
        $db = $this->getConnectionWithData();
119
120
        $query = $this->getQuery($db);
121
        $query = $query->from('customer')->orderBy('id')->limit(3);
122
        $customers = $this->getAllRowsFromBatch($query->batch(2));
123
124
        $this->assertCount(3, $customers);
125
        $this->assertEquals('user1', $customers[0]['name']);
126
        $this->assertEquals('user2', $customers[1]['name']);
127
        $this->assertEquals('user3', $customers[2]['name']);
128
    }
129
130
    public function testBatchWithIndexBy(): void
131
    {
132
        $db = $this->getConnectionWithData();
133
134
        $query = $this->getQuery($db);
135
        $query->from('customer')->orderBy('id')->limit(3)->indexBy('id');
136
        $customers = $this->getAllRowsFromBatch($query->batch(2));
137
138
        $this->assertCount(3, $customers);
139
        $this->assertEquals('user1', $customers[0]['name']);
140
        $this->assertEquals('user2', $customers[1]['name']);
141
        $this->assertEquals('user3', $customers[2]['name']);
142
    }
143
144
    protected function getAllRowsFromBatch(BatchQueryResultInterface $batch): array
145
    {
146
        $allRows = [];
147
148
        foreach ($batch as $rows) {
149
            $allRows = array_merge($allRows, $rows);
150
        }
151
152
        return $allRows;
153
    }
154
155
    protected function getAllRowsFromEach(BatchQueryResultInterface $each): array
156
    {
157
        $allRows = [];
158
159
        foreach ($each as $index => $row) {
160
            $allRows[$index] = $row;
161
        }
162
163
        return $allRows;
164
    }
165
}
166