Passed
Branch dev (f56f10)
by Wilmer
04:41 queued 01:34
created

TestBatchQueryResultTrait::testQuery()   B

Complexity

Conditions 7
Paths 64

Size

Total Lines 105
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 56
nc 64
nop 0
dl 0
loc 105
rs 8.0266
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\TestSupport;
6
7
use Yiisoft\Db\Query\BatchQueryResult;
8
use Yiisoft\Db\Query\Query;
9
10
use function array_merge;
11
12
trait TestBatchQueryResultTrait
13
{
14
    public function testQuery(): void
15
    {
16
        /* initialize property test */
17
        $db = $this->getConnection(true);
0 ignored issues
show
Bug introduced by
It seems like getConnection() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( 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);
Loading history...
18
19
        $query = new Query($db);
20
21
        $query->from('customer')->orderBy('id');
22
23
        $result = $query->batch(2);
24
25
        $this->assertInstanceOf(BatchQueryResult::class, $result);
0 ignored issues
show
Bug introduced by
It seems like assertInstanceOf() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

25
        $this->/** @scrutinizer ignore-call */ 
26
               assertInstanceOf(BatchQueryResult::class, $result);
Loading history...
26
        $this->assertEquals(2, $result->getBatchSize());
0 ignored issues
show
Bug introduced by
It seems like assertEquals() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

26
        $this->/** @scrutinizer ignore-call */ 
27
               assertEquals(2, $result->getBatchSize());
Loading history...
27
        $this->assertSame($result->getQuery(), $query);
0 ignored issues
show
Bug introduced by
It seems like assertSame() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

27
        $this->/** @scrutinizer ignore-call */ 
28
               assertSame($result->getQuery(), $query);
Loading history...
28
29
        /* normal query */
30
        $query = new Query($db);
31
32
        $query->from('customer')->orderBy('id');
33
34
        $allRows = [];
35
36
        $batch = $query->batch(2);
37
38
        foreach ($batch as $rows) {
39
            $allRows = array_merge($allRows, $rows);
40
        }
41
42
        $this->assertCount(3, $allRows);
0 ignored issues
show
Bug introduced by
It seems like assertCount() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

42
        $this->/** @scrutinizer ignore-call */ 
43
               assertCount(3, $allRows);
Loading history...
43
        $this->assertEquals('user1', $allRows[0]['name']);
44
        $this->assertEquals('user2', $allRows[1]['name']);
45
        $this->assertEquals('user3', $allRows[2]['name']);
46
47
        /* rewind */
48
        $allRows = [];
49
50
        foreach ($batch as $rows) {
51
            $allRows = array_merge($allRows, $rows);
52
        }
53
54
        $this->assertCount(3, $allRows);
55
56
        /* reset */
57
        $batch->reset();
58
59
        /* empty query */
60
        $query = new Query($db);
61
62
        $query->from('customer')->where(['id' => 100]);
63
64
        $allRows = [];
65
66
        $batch = $query->batch(2);
67
68
        foreach ($batch as $rows) {
69
            $allRows = array_merge($allRows, $rows);
0 ignored issues
show
Bug introduced by
$rows of type null is incompatible with the type array expected by parameter $arrays of array_merge(). ( Ignorable by Annotation )

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

69
            $allRows = array_merge($allRows, /** @scrutinizer ignore-type */ $rows);
Loading history...
70
        }
71
72
        $this->assertCount(0, $allRows);
73
74
        /* query with index */
75
        $query = new Query($db);
76
77
        $query->from('customer')->indexBy('name');
78
79
        $allRows = [];
80
81
        foreach ($query->batch(2) as $rows) {
82
            $allRows = array_merge($allRows, $rows);
83
        }
84
85
        $this->assertCount(3, $allRows);
86
        $this->assertEquals('address1', $allRows['user1']['address']);
87
        $this->assertEquals('address2', $allRows['user2']['address']);
88
        $this->assertEquals('address3', $allRows['user3']['address']);
89
90
        /* each */
91
        $query = new Query($db);
92
93
        $query->from('customer')->orderBy('id');
94
        $allRows = [];
95
96
        foreach ($query->each(2) as $index => $row) {
97
            $allRows[$index] = $row;
98
        }
99
        $this->assertCount(3, $allRows);
100
        $this->assertEquals('user1', $allRows[0]['name']);
101
        $this->assertEquals('user2', $allRows[1]['name']);
102
        $this->assertEquals('user3', $allRows[2]['name']);
103
104
        /* each with key */
105
        $query = new Query($db);
106
107
        $query->from('customer')->orderBy('id')->indexBy('name');
108
109
        $allRows = [];
110
111
        foreach ($query->each(100) as $key => $row) {
112
            $allRows[$key] = $row;
113
        }
114
115
        $this->assertCount(3, $allRows);
116
        $this->assertEquals('address1', $allRows['user1']['address']);
117
        $this->assertEquals('address2', $allRows['user2']['address']);
118
        $this->assertEquals('address3', $allRows['user3']['address']);
119
    }
120
121
    public function testBatchWithoutDbParameter(): void
122
    {
123
        $db = $this->getConnection();
124
125
        $query = new Query($db);
126
127
        $query = $query->from('customer')->orderBy('id')->limit(3);
128
129
        $customers = $this->getAllRowsFromBatch($query->batch(2));
130
131
        $this->assertCount(3, $customers);
132
        $this->assertEquals('user1', $customers[0]['name']);
133
        $this->assertEquals('user2', $customers[1]['name']);
134
        $this->assertEquals('user3', $customers[2]['name']);
135
    }
136
137
    public function testBatchWithIndexBy(): void
138
    {
139
        $db = $this->getConnection();
140
141
        $query = new Query($db);
142
143
        $query->from('customer')->orderBy('id')->limit(3)->indexBy('id');
144
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(BatchQueryResult $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(BatchQueryResult $each): array
165
    {
166
        $allRows = [];
167
168
        foreach ($each as $index => $row) {
169
            $allRows[$index] = $row;
170
        }
171
172
        return $allRows;
173
    }
174
}
175