Passed
Push — master ( 9f7d35...3f1c7e )
by Wilmer
08:50 queued 06:32
created

testBatchWithoutDbParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 14
ccs 9
cts 9
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\TestUtility;
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 4
    public function testQuery(): void
15
    {
16
        /* initialize property test */
17 4
        $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 4
        $query = new Query($db);
20
21 4
        $query->from('customer')->orderBy('id');
22
23 4
        $result = $query->batch(2);
24
25 4
        $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 4
        $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 4
        $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 4
        $query = new Query($db);
31
32 4
        $query->from('customer')->orderBy('id');
33
34 4
        $allRows = [];
35
36 4
        $batch = $query->batch(2);
37
38 4
        foreach ($batch as $rows) {
39 4
            $allRows = array_merge($allRows, $rows);
40
        }
41
42 4
        $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 4
        $this->assertEquals('user1', $allRows[0]['name']);
44 4
        $this->assertEquals('user2', $allRows[1]['name']);
45 4
        $this->assertEquals('user3', $allRows[2]['name']);
46
47
        /* rewind */
48 4
        $allRows = [];
49
50 4
        foreach ($batch as $rows) {
51 4
            $allRows = array_merge($allRows, $rows);
52
        }
53
54 4
        $this->assertCount(3, $allRows);
55
56
        /* reset */
57 4
        $batch->reset();
58
59
        /* empty query */
60 4
        $query = new Query($db);
61
62 4
        $query->from('customer')->where(['id' => 100]);
63
64 4
        $allRows = [];
65
66 4
        $batch = $query->batch(2);
67
68 4
        foreach ($batch as $rows) {
69
            $allRows = array_merge($allRows, $rows);
70
        }
71
72 4
        $this->assertCount(0, $allRows);
73
74
        /* query with index */
75 4
        $query = new Query($db);
76
77 4
        $query->from('customer')->indexBy('name');
78
79 4
        $allRows = [];
80
81 4
        foreach ($query->batch(2) as $rows) {
82 4
            $allRows = array_merge($allRows, $rows);
83
        }
84
85 4
        $this->assertCount(3, $allRows);
86 4
        $this->assertEquals('address1', $allRows['user1']['address']);
87 4
        $this->assertEquals('address2', $allRows['user2']['address']);
88 4
        $this->assertEquals('address3', $allRows['user3']['address']);
89
90
        /* each */
91 4
        $query = new Query($db);
92
93 4
        $query->from('customer')->orderBy('id');
94 4
        $allRows = [];
95
96 4
        foreach ($query->each(2) as $index => $row) {
97 4
            $allRows[$index] = $row;
98
        }
99 4
        $this->assertCount(3, $allRows);
100 4
        $this->assertEquals('user1', $allRows[0]['name']);
101 4
        $this->assertEquals('user2', $allRows[1]['name']);
102 4
        $this->assertEquals('user3', $allRows[2]['name']);
103
104
        /* each with key */
105 4
        $query = new Query($db);
106
107 4
        $query->from('customer')->orderBy('id')->indexBy('name');
108
109 4
        $allRows = [];
110
111 4
        foreach ($query->each(100) as $key => $row) {
112 4
            $allRows[$key] = $row;
113
        }
114
115 4
        $this->assertCount(3, $allRows);
116 4
        $this->assertEquals('address1', $allRows['user1']['address']);
117 4
        $this->assertEquals('address2', $allRows['user2']['address']);
118 4
        $this->assertEquals('address3', $allRows['user3']['address']);
119 4
    }
120
121 4
    public function testBatchWithoutDbParameter(): void
122
    {
123 4
        $db = $this->getConnection();
124
125 4
        $query = new Query($db);
126
127 4
        $query = $query->from('customer')->orderBy('id')->limit(3);
128
129 4
        $customers = $this->getAllRowsFromBatch($query->batch(2));
130
131 4
        $this->assertCount(3, $customers);
132 4
        $this->assertEquals('user1', $customers[0]['name']);
133 4
        $this->assertEquals('user2', $customers[1]['name']);
134 4
        $this->assertEquals('user3', $customers[2]['name']);
135 4
    }
136
137 4
    public function testBatchWithIndexBy(): void
138
    {
139 4
        $db = $this->getConnection();
140
141 4
        $query = new Query($db);
142
143 4
        $query->from('customer')->orderBy('id')->limit(3)->indexBy('id');
144
145 4
        $customers = $this->getAllRowsFromBatch($query->batch(2));
146
147 4
        $this->assertCount(3, $customers);
148 4
        $this->assertEquals('user1', $customers[0]['name']);
149 4
        $this->assertEquals('user2', $customers[1]['name']);
150 4
        $this->assertEquals('user3', $customers[2]['name']);
151 4
    }
152
153 8
    protected function getAllRowsFromBatch(BatchQueryResult $batch): array
154
    {
155 8
        $allRows = [];
156
157 8
        foreach ($batch as $rows) {
158 8
            $allRows = array_merge($allRows, $rows);
159
        }
160
161 8
        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