Passed
Pull Request — master (#380)
by Wilmer
02:50
created

CommonBatchQueryResultTest::testBatchQueryResult()   B

Complexity

Conditions 7
Paths 64

Size

Total Lines 99
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 62
nc 64
nop 0
dl 0
loc 99
rs 7.8957
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 = [];
99
100
        foreach ($query->each(2) as $index => $row) {
101
            /** @psalm-suppress PossiblyNullArrayOffset */
102
            $allRows[$index] = $row;
103
        }
104
105
        $this->assertCount(3, $allRows);
106
        $this->assertSame('user1', $allRows[0]['name']);
107
        $this->assertSame('user2', $allRows[1]['name']);
108
        $this->assertSame('user3', $allRows[2]['name']);
109
110
        /* each with key */
111
        $query = $this->getQuery($db);
112
        $query->from('customer')->orderBy('id')->indexBy('name');
113
        $allRows = [];
114
115
        foreach ($query->each() as $key => $row) {
116
            $allRows[$key] = $row;
117
        }
118
119
        $this->assertCount(3, $allRows);
120
        $this->assertSame('address1', $allRows['user1']['address']);
121
        $this->assertSame('address2', $allRows['user2']['address']);
122
        $this->assertSame('address3', $allRows['user3']['address']);
123
    }
124
125
    public function testBatchWithoutDbParameter(): void
126
    {
127
        $db = $this->getConnectionWithData();
128
129
        $query = $this->getQuery($db);
130
        $query = $query->from('customer')->orderBy('id')->limit(3);
131
        $customers = $this->getAllRowsFromBatch($query->batch(2));
132
133
        $this->assertCount(3, $customers);
134
        $this->assertEquals('user1', $customers[0]['name']);
135
        $this->assertEquals('user2', $customers[1]['name']);
136
        $this->assertEquals('user3', $customers[2]['name']);
137
    }
138
139
    public function testBatchWithIndexBy(): void
140
    {
141
        $db = $this->getConnectionWithData();
142
143
        $query = $this->getQuery($db);
144
        $query->from('customer')->orderBy('id')->limit(3)->indexBy('id');
145
        $customers = $this->getAllRowsFromBatch($query->batch(2));
146
147
        $this->assertCount(3, $customers);
148
        $this->assertEquals('user1', $customers[0]['name']);
149
        $this->assertEquals('user2', $customers[1]['name']);
150
        $this->assertEquals('user3', $customers[2]['name']);
151
    }
152
153
    protected function getAllRowsFromBatch(BatchQueryResultInterface $batch): array
154
    {
155
        $allRows = [];
156
157
        foreach ($batch as $rows) {
158
            $allRows = array_merge($allRows, $rows);
159
        }
160
161
        return $allRows;
162
    }
163
164
    protected function getAllRowsFromEach(BatchQueryResultInterface $each): array
165
    {
166
        $allRows = [];
167
168
        foreach ($each as $index => $row) {
169
            $allRows[$index] = $row;
170
        }
171
172
        return $allRows;
173
    }
174
}
175