Passed
Pull Request — master (#372)
by Wilmer
16:16
created

AbstractBatchQueryResultTest::testQuery()   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
c 0
b 0
f 0
nc 64
nop 0
dl 0
loc 99
rs 7.8957

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;
6
7
use PHPUnit\Framework\TestCase;
8
use Yiisoft\Db\Query\BatchQueryResultInterface;
9
use Yiisoft\Db\Query\Query;
10
11
use function array_merge;
12
13
abstract class AbstractBatchQueryResultTest extends TestCase
14
{
15
    public function testBatchWithoutDbParameter(): void
16
    {
17
        $db = $this->getConnection(true);
0 ignored issues
show
Bug introduced by
The method getConnection() does not exist on Yiisoft\Db\Tests\AbstractBatchQueryResultTest. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

17
        /** @scrutinizer ignore-call */ 
18
        $db = $this->getConnection(true);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
18
19
        $query = new Query($db);
20
        $query = $query->from('customer')->orderBy('id')->limit(3);
21
        $customers = $this->getAllRowsFromBatch($query->batch(2));
22
23
        $this->assertCount(3, $customers);
24
        $this->assertSame('user1', $customers[0]['name']);
25
        $this->assertSame('user2', $customers[1]['name']);
26
        $this->assertSame('user3', $customers[2]['name']);
27
    }
28
29
    public function testBatchWithIndexBy(): void
30
    {
31
        $db = $this->getConnection();
32
33
        $query = new Query($db);
34
35
        $query->from('customer')->orderBy('id')->limit(3)->indexBy('id');
36
37
        $customers = $this->getAllRowsFromBatch($query->batch(2));
38
39
        $this->assertCount(3, $customers);
40
        $this->assertSame('user1', $customers[0]['name']);
41
        $this->assertSame('user2', $customers[1]['name']);
42
        $this->assertSame('user3', $customers[2]['name']);
43
    }
44
45
    public function testQuery(): void
46
    {
47
        /* initialize property test */
48
        $db = $this->getConnection();
49
50
        $query = new Query($db);
51
        $query->from('customer')->orderBy('id');
52
        $result = $query->batch(2);
53
54
        $this->assertInstanceOf(BatchQueryResultInterface::class, $result);
55
        $this->assertSame(2, $result->getBatchSize());
56
        $this->assertSame($result->getQuery(), $query);
57
58
        /* normal query */
59
        $query = new Query($db);
60
        $query->from('customer')->orderBy('id');
61
        $allRows = [];
62
        $batch = $query->batch(2);
63
        $step = 0;
64
65
        foreach ($batch as $rows) {
66
            $allRows = array_merge($allRows, $rows);
67
            $step++;
68
        }
69
70
        $this->assertCount(3, $allRows);
71
        $this->assertSame(2, $step);
72
        $this->assertSame('user1', $allRows[0]['name']);
73
        $this->assertSame('user2', $allRows[1]['name']);
74
        $this->assertSame('user3', $allRows[2]['name']);
75
76
        /* rewind */
77
        $allRows = [];
78
        $step = 0;
79
80
        foreach ($batch as $rows) {
81
            $allRows = array_merge($allRows, $rows);
82
            $step++;
83
        }
84
85
        $this->assertCount(3, $allRows);
86
        $this->assertSame(2, $step);
87
88
        /* reset */
89
        $batch->reset();
90
91
        /* empty query */
92
        $query = new Query($db);
93
        $query->from('customer')->where(['id' => 100]);
94
        $allRows = [];
95
        $batch = $query->batch(2);
96
97
        foreach ($batch as $rows) {
98
            $allRows = array_merge($allRows, $rows);
99
        }
100
101
        $this->assertCount(0, $allRows);
102
103
        /* query with index */
104
        $query = new Query($db);
105
        $query->from('customer')->indexBy('name');
106
        $allRows = [];
107
108
        foreach ($query->batch(2) as $rows) {
109
            $allRows = array_merge($allRows, $rows);
110
        }
111
112
        $this->assertCount(3, $allRows);
113
        $this->assertSame('address1', $allRows['user1']['address']);
114
        $this->assertSame('address2', $allRows['user2']['address']);
115
        $this->assertSame('address3', $allRows['user3']['address']);
116
117
        /* each */
118
        $query = new Query($db);
119
        $query->from('customer')->orderBy('id');
120
        $allRows = [];
121
122
        foreach ($query->each(2) as $index => $row) {
123
            $allRows[$index] = $row;
124
        }
125
126
        $this->assertCount(3, $allRows);
127
        $this->assertSame('user1', $allRows[0]['name']);
128
        $this->assertSame('user2', $allRows[1]['name']);
129
        $this->assertSame('user3', $allRows[2]['name']);
130
131
        /* each with key */
132
        $query = new Query($db);
133
        $query->from('customer')->orderBy('id')->indexBy('name');
134
        $allRows = [];
135
136
        foreach ($query->each(100) as $key => $row) {
137
            $allRows[$key] = $row;
138
        }
139
140
        $this->assertCount(3, $allRows);
141
        $this->assertSame('address1', $allRows['user1']['address']);
142
        $this->assertSame('address2', $allRows['user2']['address']);
143
        $this->assertSame('address3', $allRows['user3']['address']);
144
    }
145
146
    private function getAllRowsFromBatch(BatchQueryResultInterface $batch): array
147
    {
148
        $allRows = [];
149
150
        foreach ($batch as $rows) {
151
            $allRows = array_merge($allRows, $rows);
152
        }
153
154
        return $allRows;
155
    }
156
157
    private function getAllRowsFromEach(BatchQueryResultInterface $each): array
0 ignored issues
show
Unused Code introduced by
The method getAllRowsFromEach() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
158
    {
159
        $allRows = [];
160
161
        foreach ($each as $index => $row) {
162
            $allRows[$index] = $row;
163
        }
164
165
        return $allRows;
166
    }
167
}
168