Completed
Push — master ( 928248...a75adc )
by Wilmer
14s queued 11s
created

BatchQueryResultTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\ActiveRecord\Tests;
6
7
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Customer;
8
use Yiisoft\Db\Query\BatchQueryResult;
9
use Yiisoft\Db\Query\Query;
10
11
abstract class BatchQueryResultTest extends TestCase
12
{
13
    protected ?string $driverName = 'mysql';
14
15
    public function testQuery(): void
16
    {
17
        $db = Customer::getConnection();
18
19
        $this->loadFixture($db);
20
21
        /** initialize property test */
22
        $query = new Query($db);
23
24
        $query->from('customer')->orderBy('id');
25
26
        $result = $query->batch(2);
27
28
        $this->assertInstanceOf(BatchQueryResult::class, $result);
29
        $this->assertEquals(2, $result->getBatchSize());
30
        $this->assertSame($result->getQuery(), $query);
31
32
        /** normal query */
33
        $query = new Query($db);
34
35
        $query->from('customer')->orderBy('id');
36
37
        $allRows = [];
38
39
        $batch = $query->batch(2);
40
41
        foreach ($batch as $rows) {
42
            $allRows = array_merge($allRows, $rows);
43
        }
44
45
        $this->assertCount(3, $allRows);
46
        $this->assertEquals('user1', $allRows[0]['name']);
47
        $this->assertEquals('user2', $allRows[1]['name']);
48
        $this->assertEquals('user3', $allRows[2]['name']);
49
50
        /** rewind */
51
        $allRows = [];
52
53
        foreach ($batch as $rows) {
54
            $allRows = array_merge($allRows, $rows);
55
        }
56
57
        $this->assertCount(3, $allRows);
58
59
        /** reset */
60
        $batch->reset();
61
62
        /** empty query */
63
        $query = new Query($db);
64
65
        $query->from('customer')->where(['id' => 100]);
66
67
        $allRows = [];
68
69
        $batch = $query->batch(2);
70
71
        foreach ($batch as $rows) {
72
            $allRows = array_merge($allRows, $rows);
73
        }
74
75
        $this->assertCount(0, $allRows);
76
77
        /** query with index */
78
        $query = new Query($db);
79
80
        $query->from('customer')->indexBy('name');
81
82
        $allRows = [];
83
84
        foreach ($query->batch(2) as $rows) {
85
            $allRows = array_merge($allRows, $rows);
86
        }
87
88
        $this->assertCount(3, $allRows);
89
        $this->assertEquals('address1', $allRows['user1']['address']);
90
        $this->assertEquals('address2', $allRows['user2']['address']);
91
        $this->assertEquals('address3', $allRows['user3']['address']);
92
93
        /** each */
94
        $query = new Query($db);
95
96
        $query->from('customer')->orderBy('id');
97
        $allRows = [];
98
99
        foreach ($query->each(2) as $index => $row) {
100
            $allRows[$index] = $row;
101
        }
102
        $this->assertCount(3, $allRows);
103
        $this->assertEquals('user1', $allRows[0]['name']);
104
        $this->assertEquals('user2', $allRows[1]['name']);
105
        $this->assertEquals('user3', $allRows[2]['name']);
106
107
        /** each with key */
108
        $query = new Query($db);
109
110
        $query->from('customer')->orderBy('id')->indexBy('name');
111
112
        $allRows = [];
113
114
        foreach ($query->each(100) as $key => $row) {
115
            $allRows[$key] = $row;
116
        }
117
118
        $this->assertCount(3, $allRows);
119
        $this->assertEquals('address1', $allRows['user1']['address']);
120
        $this->assertEquals('address2', $allRows['user2']['address']);
121
        $this->assertEquals('address3', $allRows['user3']['address']);
122
    }
123
124
    public function testActiveQuery(): void
125
    {
126
        $db = Customer::getConnection();
127
128
        /** batch with eager loading */
129
        $query = (new Customer($db))->find()->with('orders')->orderBy('id');
0 ignored issues
show
Unused Code introduced by
The call to Yiisoft\ActiveRecord\Tes...Customer::__construct() has too many arguments starting with $db. ( Ignorable by Annotation )

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

129
        $query = (/** @scrutinizer ignore-call */ new Customer($db))->find()->with('orders')->orderBy('id');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
130
131
        $customers = $this->getAllRowsFromBatch($query->batch(2));
132
133
        foreach ($customers as $customer) {
134
            $this->assertTrue($customer->isRelationPopulated('orders'));
135
        }
136
137
        $this->assertCount(3, $customers);
138
        $this->assertCount(1, $customers[0]->orders);
139
        $this->assertCount(2, $customers[1]->orders);
140
        $this->assertCount(0, $customers[2]->orders);
141
    }
142
143
    public function testBatchWithIndexBy(): void
144
    {
145
        $db = Customer::getConnection();
146
147
        $query = (new Customer($db))->find()->orderBy('id')->limit(3)->indexBy('id');
0 ignored issues
show
Unused Code introduced by
The call to Yiisoft\ActiveRecord\Tes...Customer::__construct() has too many arguments starting with $db. ( Ignorable by Annotation )

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

147
        $query = (/** @scrutinizer ignore-call */ new Customer($db))->find()->orderBy('id')->limit(3)->indexBy('id');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
148
149
        $customers = $this->getAllRowsFromBatch($query->batch(2), $db);
0 ignored issues
show
Unused Code introduced by
The call to Yiisoft\ActiveRecord\Tes...::getAllRowsFromBatch() has too many arguments starting with $db. ( Ignorable by Annotation )

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

149
        /** @scrutinizer ignore-call */ 
150
        $customers = $this->getAllRowsFromBatch($query->batch(2), $db);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
150
151
        $this->assertCount(3, $customers);
152
        $this->assertEquals('user1', $customers[0]->name);
153
        $this->assertEquals('user2', $customers[1]->name);
154
        $this->assertEquals('user3', $customers[2]->name);
155
    }
156
157
    protected function getAllRowsFromBatch(BatchQueryResult $batch): array
158
    {
159
        $allRows = [];
160
161
        foreach ($batch as $rows) {
162
            $allRows = array_merge($allRows, $rows);
163
        }
164
165
        return $allRows;
166
    }
167
168
    protected function getAllRowsFromEach(BatchQueryResult $each): array
169
    {
170
        $allRows = [];
171
172
        foreach ($each as $index => $row) {
173
            $allRows[$index] = $row;
174
        }
175
176
        return $allRows;
177
    }
178
}
179