Passed
Push — master ( 914087...c6011d )
by Alexander
11:22
created

TestBatchQueryResultTrait   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 86
c 1
b 0
f 0
dl 0
loc 169
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testBatchWithoutDbParameter() 0 14 1
A getAllRowsFromEach() 0 10 2
A getAllRowsFromBatch() 0 9 2
B testQuery() 0 112 7
A testBatchWithIndexBy() 0 14 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\TestSupport;
6
7
use Yiisoft\Db\Query\BatchQueryResultInterface;
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(BatchQueryResultInterface::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(BatchQueryResultInterface::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
        $step = 0;
38
        foreach ($batch as $rows) {
39
            $allRows = array_merge($allRows, $rows);
40
            $step++;
41
        }
42
43
        $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

43
        $this->/** @scrutinizer ignore-call */ 
44
               assertCount(3, $allRows);
Loading history...
44
        $this->assertEquals(2, $step);
45
        $this->assertEquals('user1', $allRows[0]['name']);
46
        $this->assertEquals('user2', $allRows[1]['name']);
47
        $this->assertEquals('user3', $allRows[2]['name']);
48
49
        /* rewind */
50
        $allRows = [];
51
52
        $step = 0;
53
        foreach ($batch as $rows) {
54
            $allRows = array_merge($allRows, $rows);
55
            $step++;
56
        }
57
58
        $this->assertCount(3, $allRows);
59
        $this->assertEquals(2, $step);
60
61
        /* reset */
62
        $batch->reset();
63
64
        /* empty query */
65
        $query = new Query($db);
66
67
        $query->from('customer')->where(['id' => 100]);
68
69
        $allRows = [];
70
71
        $batch = $query->batch(2);
72
73
        foreach ($batch as $rows) {
74
            $allRows = array_merge($allRows, $rows);
75
        }
76
77
        $this->assertCount(0, $allRows);
78
79
        /* query with index */
80
        $query = new Query($db);
81
82
        $query->from('customer')->indexBy('name');
83
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->assertEquals('address1', $allRows['user1']['address']);
92
        $this->assertEquals('address2', $allRows['user2']['address']);
93
        $this->assertEquals('address3', $allRows['user3']['address']);
94
95
        /* each */
96
        $query = new Query($db);
97
98
        $query->from('customer')->orderBy('id');
99
        $allRows = [];
100
101
        foreach ($query->each(2) as $index => $row) {
102
            /** @psalm-suppress PossiblyNullArrayOffset */
103
            $allRows[$index] = $row;
104
        }
105
        $this->assertCount(3, $allRows);
106
        $this->assertEquals('user1', $allRows[0]['name']);
107
        $this->assertEquals('user2', $allRows[1]['name']);
108
        $this->assertEquals('user3', $allRows[2]['name']);
109
110
        /* each with key */
111
        $query = new Query($db);
112
113
        $query->from('customer')->orderBy('id')->indexBy('name');
114
115
        $allRows = [];
116
117
        foreach ($query->each(100) as $key => $row) {
118
            /** @psalm-suppress PossiblyNullArrayOffset */
119
            $allRows[$key] = $row;
120
        }
121
122
        $this->assertCount(3, $allRows);
123
        $this->assertEquals('address1', $allRows['user1']['address']);
124
        $this->assertEquals('address2', $allRows['user2']['address']);
125
        $this->assertEquals('address3', $allRows['user3']['address']);
126
    }
127
128
    public function testBatchWithoutDbParameter(): void
129
    {
130
        $db = $this->getConnection();
131
132
        $query = new Query($db);
133
134
        $query = $query->from('customer')->orderBy('id')->limit(3);
135
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
    public function testBatchWithIndexBy(): void
145
    {
146
        $db = $this->getConnection();
147
148
        $query = new Query($db);
149
150
        $query->from('customer')->orderBy('id')->limit(3)->indexBy('id');
151
152
        $customers = $this->getAllRowsFromBatch($query->batch(2));
153
154
        $this->assertCount(3, $customers);
155
        $this->assertEquals('user1', $customers[0]['name']);
156
        $this->assertEquals('user2', $customers[1]['name']);
157
        $this->assertEquals('user3', $customers[2]['name']);
158
    }
159
160
    protected function getAllRowsFromBatch(BatchQueryResultInterface $batch): array
161
    {
162
        $allRows = [];
163
164
        foreach ($batch as $rows) {
165
            $allRows = array_merge($allRows, $rows);
166
        }
167
168
        return $allRows;
169
    }
170
171
    protected function getAllRowsFromEach(BatchQueryResultInterface $each): array
172
    {
173
        $allRows = [];
174
175
        foreach ($each as $index => $row) {
176
            /** @psalm-suppress PossiblyNullArrayOffset */
177
            $allRows[$index] = $row;
178
        }
179
180
        return $allRows;
181
    }
182
}
183