Passed
Branch add-rector-actions (5668b1)
by Wilmer
03:26
created

BatchQueryResultTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 82
dl 0
loc 164
rs 10
c 2
b 0
f 1
wmc 14

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testActiveQuery() 0 19 2
A testBatchWithIndexBy() 0 14 1
A getAllRowsFromBatch() 0 9 2
A getAllRowsFromEach() 0 9 2
B testQuery() 0 103 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\ActiveRecord\Tests;
6
7
use Yiisoft\ActiveRecord\ActiveQuery;
8
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Customer;
9
use Yiisoft\Db\Query\BatchQueryResultInterface;
10
11
abstract class BatchQueryResultTest extends TestCase
12
{
13
    public function testQuery(): void
14
    {
15
        $this->checkFixture($this->db, 'customer', true);
16
17
        $customerQuery = new ActiveQuery(Customer::class, $this->db);
18
19
        $query = $customerQuery->orderBy('id');
20
21
        $result = $query->batch(2);
22
23
        $this->assertInstanceOf(BatchQueryResultInterface::class, $result);
24
        $this->assertEquals(2, $result->getBatchSize());
25
        $this->assertSame($result->getQuery(), $query);
26
27
        /** normal query */
28
        $customerQuery = new ActiveQuery(Customer::class, $this->db);
29
30
        $query = $customerQuery->orderBy('id');
31
32
        $allRows = [];
33
34
        $batch = $query->batch(2);
35
36
        foreach ($batch as $rows) {
37
            $allRows = array_merge($allRows, $rows);
38
        }
39
40
        $this->assertCount(3, $allRows);
41
        $this->assertEquals('user1', $allRows[0]['name']);
42
        $this->assertEquals('user2', $allRows[1]['name']);
43
        $this->assertEquals('user3', $allRows[2]['name']);
44
45
        /** rewind */
46
        $allRows = [];
47
48
        foreach ($batch as $rows) {
49
            $allRows = array_merge($allRows, $rows);
50
        }
51
52
        $this->assertCount(3, $allRows);
53
54
        /** reset */
55
        $batch->reset();
56
57
        /** empty query */
58
        $query = $customerQuery->where(['id' => 100]);
59
60
        $allRows = [];
61
62
        $batch = $query->batch(2);
63
64
        foreach ($batch as $rows) {
65
            $allRows = array_merge($allRows, $rows);
66
        }
67
68
        $this->assertCount(0, $allRows);
69
70
        /** query with index */
71
        $customerQuery = new ActiveQuery(Customer::class, $this->db);
72
73
        $query = $customerQuery->indexBy('name');
74
75
        $allRows = [];
76
77
        foreach ($query->batch(2) as $rows) {
78
            $allRows = array_merge($allRows, $rows);
79
        }
80
81
        $this->assertCount(3, $allRows);
82
        $this->assertEquals('address1', $allRows['user1']['address']);
83
        $this->assertEquals('address2', $allRows['user2']['address']);
84
        $this->assertEquals('address3', $allRows['user3']['address']);
85
86
        /** each */
87
        $customerQuery = new ActiveQuery(Customer::class, $this->db);
88
89
        $query = $customerQuery->orderBy('id');
90
91
        $allRows = [];
92
93
        foreach ($query->each(2) as $index => $row) {
94
            $allRows[$index] = $row;
95
        }
96
        $this->assertCount(3, $allRows);
97
        $this->assertEquals('user1', $allRows[0]['name']);
98
        $this->assertEquals('user2', $allRows[1]['name']);
99
        $this->assertEquals('user3', $allRows[2]['name']);
100
101
        /** each with key */
102
        $customerQuery = new ActiveQuery(Customer::class, $this->db);
103
104
        $query = $customerQuery->orderBy('id')->indexBy('name');
105
106
        $allRows = [];
107
108
        foreach ($query->each(100) as $key => $row) {
109
            $allRows[$key] = $row;
110
        }
111
112
        $this->assertCount(3, $allRows);
113
        $this->assertEquals('address1', $allRows['user1']['address']);
114
        $this->assertEquals('address2', $allRows['user2']['address']);
115
        $this->assertEquals('address3', $allRows['user3']['address']);
116
    }
117
118
    public function testActiveQuery(): void
119
    {
120
        $this->checkFixture($this->db, 'customer');
121
122
        /** batch with eager loading */
123
        $customerQuery = new ActiveQuery(Customer::class, $this->db);
124
125
        $query = $customerQuery->with('orders')->orderBy('id');
126
127
        $customers = $this->getAllRowsFromBatch($query->batch(2));
128
129
        foreach ($customers as $customer) {
130
            $this->assertTrue($customer->isRelationPopulated('orders'));
131
        }
132
133
        $this->assertCount(3, $customers);
134
        $this->assertCount(1, $customers[0]->orders);
135
        $this->assertCount(2, $customers[1]->orders);
136
        $this->assertCount(0, $customers[2]->orders);
137
    }
138
139
    public function testBatchWithIndexBy(): void
140
    {
141
        $this->checkFixture($this->db, 'customer');
142
143
        $customerQuery = new ActiveQuery(Customer::class, $this->db);
144
145
        $query = $customerQuery->orderBy('id')->limit(3)->indexBy('id');
146
147
        $customers = $this->getAllRowsFromBatch($query->batch(2));
148
149
        $this->assertCount(3, $customers);
150
        $this->assertEquals('user1', $customers[0]->name);
151
        $this->assertEquals('user2', $customers[1]->name);
152
        $this->assertEquals('user3', $customers[2]->name);
153
    }
154
155
    protected function getAllRowsFromBatch(BatchQueryResultInterface $batch): array
156
    {
157
        $allRows = [];
158
159
        foreach ($batch as $rows) {
160
            $allRows = array_merge($allRows, $rows);
161
        }
162
163
        return $allRows;
164
    }
165
166
    protected function getAllRowsFromEach(BatchQueryResultInterface $each): array
167
    {
168
        $allRows = [];
169
170
        foreach ($each as $index => $row) {
171
            $allRows[$index] = $row;
172
        }
173
174
        return $allRows;
175
    }
176
}
177