Passed
Push — master ( bc8b6f...7a9b1f )
by Sergei
02:57
created

ActiveQueryFindTest::testFindLimit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 49
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 49
rs 9.408
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\ActiveRecord\Tests\Driver\Oracle;
6
7
use Yiisoft\ActiveRecord\ActiveQuery;
8
use Yiisoft\ActiveRecord\Tests\Driver\Oracle\Stubs\Customer as CustomerWithRownumid;
9
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Customer;
10
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Order;
11
use Yiisoft\ActiveRecord\Tests\Support\OracleHelper;
12
13
final class ActiveQueryFindTest extends \Yiisoft\ActiveRecord\Tests\ActiveQueryFindTest
14
{
15
    public function setUp(): void
16
    {
17
        parent::setUp();
18
19
        $oracleHelper = new OracleHelper();
20
        $this->db = $oracleHelper->createConnection();
21
    }
22
23
    protected function tearDown(): void
24
    {
25
        parent::tearDown();
26
27
        $this->db->close();
28
29
        unset($this->db);
30
    }
31
32
    public function testFindLimit(): void
33
    {
34
        $this->checkFixture($this->db, 'customer', true);
35
36
        /** one */
37
        $customerQuery = new ActiveQuery(CustomerWithRownumid::class, $this->db);
38
        $customer = $customerQuery->orderBy('id')->onePopulate();
39
        $this->assertEquals('user1', $customer->getName());
0 ignored issues
show
Bug introduced by
The method getName() does not exist on Yiisoft\ActiveRecord\ActiveRecordInterface. It seems like you code against a sub-type of Yiisoft\ActiveRecord\ActiveRecordInterface such as Yiisoft\ActiveRecord\Tes...\CustomerWithProperties or Yiisoft\ActiveRecord\Tes...icActiveRecord\Customer or Yiisoft\ActiveRecord\Tes...s\ActiveRecord\Customer or Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Item or Yiisoft\ActiveRecord\Tes...s\ActiveRecord\Category. ( Ignorable by Annotation )

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

39
        $this->assertEquals('user1', $customer->/** @scrutinizer ignore-call */ getName());
Loading history...
40
41
        /** all */
42
        $customerQuery = new ActiveQuery(CustomerWithRownumid::class, $this->db);
43
        $customers = $customerQuery->allPopulate();
44
        $this->assertCount(3, $customers);
45
46
        /** limit */
47
        $customerQuery = new ActiveQuery(CustomerWithRownumid::class, $this->db);
48
        $customers = $customerQuery->orderBy('id')->limit(1)->allPopulate();
49
        $this->assertCount(1, $customers);
50
        $this->assertEquals('user1', $customers[0]->getName());
51
52
        $customers = $customerQuery->orderBy('id')->limit(1)->offset(1)->allPopulate();
53
        $this->assertCount(1, $customers);
54
        $this->assertEquals('user2', $customers[0]->getName());
55
56
        $customers = $customerQuery->orderBy('id')->limit(1)->offset(2)->allPopulate();
57
        $this->assertCount(1, $customers);
58
        $this->assertEquals('user3', $customers[0]->getName());
59
60
        $customers = $customerQuery->orderBy('id')->limit(2)->offset(1)->allPopulate();
61
        $this->assertCount(2, $customers);
62
        $this->assertEquals('user2', $customers[0]->getName());
63
        $this->assertEquals('user3', $customers[1]->getName());
64
65
        $customers = $customerQuery->limit(2)->offset(3)->allPopulate();
66
        $this->assertCount(0, $customers);
67
68
        /** offset */
69
        $customerQuery = new ActiveQuery(CustomerWithRownumid::class, $this->db);
70
        $customer = $customerQuery->orderBy('id')->offset(0)->onePopulate();
71
        $this->assertEquals('user1', $customer->getName());
72
73
        $customer = $customerQuery->orderBy('id')->offset(1)->onePopulate();
74
        $this->assertEquals('user2', $customer->getName());
75
76
        $customer = $customerQuery->orderBy('id')->offset(2)->onePopulate();
77
        $this->assertEquals('user3', $customer->getName());
78
79
        $customer = $customerQuery->offset(3)->onePopulate();
80
        $this->assertNull($customer);
81
    }
82
83
    public function testFindEager(): void
84
    {
85
        $this->checkFixture($this->db, 'customer', true);
86
87
        $customerQuery = new ActiveQuery(Customer::class, $this->db);
88
        $customers = $customerQuery->with('orders')->indexBy('id')->all();
89
90
        ksort($customers);
91
        $this->assertCount(3, $customers);
92
        $this->assertTrue($customers[1]->isRelationPopulated('orders'));
93
        $this->assertTrue($customers[2]->isRelationPopulated('orders'));
94
        $this->assertTrue($customers[3]->isRelationPopulated('orders'));
95
        $this->assertCount(1, $customers[1]->getOrders());
96
        $this->assertCount(2, $customers[2]->getOrders());
97
        $this->assertCount(0, $customers[3]->getOrders());
98
99
        $customers[1]->resetRelation('orders');
100
        $this->assertFalse($customers[1]->isRelationPopulated('orders'));
101
102
        $customer = $customerQuery->where(['id' => 1])->with('orders')->onePopulate();
103
        $this->assertTrue($customer->isRelationPopulated('orders'));
104
        $this->assertCount(1, $customer->getOrders());
0 ignored issues
show
Bug introduced by
The method getOrders() does not exist on Yiisoft\ActiveRecord\ActiveRecordInterface. It seems like you code against a sub-type of Yiisoft\ActiveRecord\ActiveRecordInterface such as Yiisoft\ActiveRecord\Tes...s\ActiveRecord\Customer or Yiisoft\ActiveRecord\Tes...s\ActiveRecord\Category. ( Ignorable by Annotation )

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

104
        $this->assertCount(1, $customer->/** @scrutinizer ignore-call */ getOrders());
Loading history...
105
        $this->assertCount(1, $customer->getRelatedRecords());
0 ignored issues
show
Bug introduced by
The method getRelatedRecords() does not exist on Yiisoft\ActiveRecord\ActiveRecordInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Yiisoft\ActiveRecord\ActiveRecordInterface. ( Ignorable by Annotation )

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

105
        $this->assertCount(1, $customer->/** @scrutinizer ignore-call */ getRelatedRecords());
Loading history...
106
107
        /** multiple with() calls */
108
        $orderQuery = new ActiveQuery(Order::class, $this->db);
109
        $orders = $orderQuery->with('customer', 'items')->all();
110
        $this->assertCount(3, $orders);
111
        $this->assertTrue($orders[0]->isRelationPopulated('customer'));
112
        $this->assertTrue($orders[0]->isRelationPopulated('items'));
113
114
        $orders = $orderQuery->with('customer')->with('items')->all();
115
        $this->assertCount(3, $orders);
116
        $this->assertTrue($orders[0]->isRelationPopulated('customer'));
117
        $this->assertTrue($orders[0]->isRelationPopulated('items'));
118
    }
119
120
    public function testFindAsArray(): void
121
    {
122
        $this->checkFixture($this->db, 'customer');
123
124
        /** asArray */
125
        $customerQuery = new ActiveQuery(Customer::class, $this->db);
126
        $customer = $customerQuery->where(['[[id]]' => 2])->asArray()->onePopulate();
127
        $this->assertEquals([
128
            'id' => 2,
129
            'email' => '[email protected]',
130
            'name' => 'user2',
131
            'address' => 'address2',
132
            'status' => 1,
133
            'profile_id' => null,
134
            'bool_status' => true,
135
        ], $customer);
136
137
        /** find all asArray */
138
        $customerQuery = new ActiveQuery(Customer::class, $this->db);
139
        $customers = $customerQuery->asArray()->all();
140
        $this->assertCount(3, $customers);
141
        $this->assertArrayHasKey('id', $customers[0]);
142
        $this->assertArrayHasKey('name', $customers[0]);
143
        $this->assertArrayHasKey('email', $customers[0]);
144
        $this->assertArrayHasKey('address', $customers[0]);
145
        $this->assertArrayHasKey('status', $customers[0]);
146
        $this->assertArrayHasKey('bool_status', $customers[0]);
147
        $this->assertArrayHasKey('id', $customers[1]);
148
        $this->assertArrayHasKey('name', $customers[1]);
149
        $this->assertArrayHasKey('email', $customers[1]);
150
        $this->assertArrayHasKey('address', $customers[1]);
151
        $this->assertArrayHasKey('status', $customers[1]);
152
        $this->assertArrayHasKey('bool_status', $customers[1]);
153
        $this->assertArrayHasKey('id', $customers[2]);
154
        $this->assertArrayHasKey('name', $customers[2]);
155
        $this->assertArrayHasKey('email', $customers[2]);
156
        $this->assertArrayHasKey('address', $customers[2]);
157
        $this->assertArrayHasKey('status', $customers[2]);
158
        $this->assertArrayHasKey('bool_status', $customers[2]);
159
    }
160
}
161