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