Passed
Pull Request — master (#368)
by Sergei
02:42
created

ActiveQueryFindTest::createConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
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\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
use Yiisoft\Db\Connection\ConnectionInterface;
13
14
final class ActiveQueryFindTest extends \Yiisoft\ActiveRecord\Tests\ActiveQueryFindTest
15
{
16
    protected function createConnection(): ConnectionInterface
17
    {
18
        return (new OracleHelper())->createConnection();
19
    }
20
21
    public function testFindLimit(): void
22
    {
23
        $this->checkFixture($this->db(), 'customer', true);
24
25
        /** one */
26
        $customerQuery = new ActiveQuery(CustomerWithRownumid::class);
27
        $customer = $customerQuery->orderBy('id')->onePopulate();
28
        $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

28
        $this->assertEquals('user1', $customer->/** @scrutinizer ignore-call */ getName());
Loading history...
29
30
        /** all */
31
        $customerQuery = new ActiveQuery(CustomerWithRownumid::class);
32
        $customers = $customerQuery->allPopulate();
33
        $this->assertCount(3, $customers);
34
35
        /** limit */
36
        $customerQuery = new ActiveQuery(CustomerWithRownumid::class);
37
        $customers = $customerQuery->orderBy('id')->limit(1)->allPopulate();
38
        $this->assertCount(1, $customers);
39
        $this->assertEquals('user1', $customers[0]->getName());
40
41
        $customers = $customerQuery->orderBy('id')->limit(1)->offset(1)->allPopulate();
42
        $this->assertCount(1, $customers);
43
        $this->assertEquals('user2', $customers[0]->getName());
44
45
        $customers = $customerQuery->orderBy('id')->limit(1)->offset(2)->allPopulate();
46
        $this->assertCount(1, $customers);
47
        $this->assertEquals('user3', $customers[0]->getName());
48
49
        $customers = $customerQuery->orderBy('id')->limit(2)->offset(1)->allPopulate();
50
        $this->assertCount(2, $customers);
51
        $this->assertEquals('user2', $customers[0]->getName());
52
        $this->assertEquals('user3', $customers[1]->getName());
53
54
        $customers = $customerQuery->limit(2)->offset(3)->allPopulate();
55
        $this->assertCount(0, $customers);
56
57
        /** offset */
58
        $customerQuery = new ActiveQuery(CustomerWithRownumid::class);
59
        $customer = $customerQuery->orderBy('id')->offset(0)->onePopulate();
60
        $this->assertEquals('user1', $customer->getName());
61
62
        $customer = $customerQuery->orderBy('id')->offset(1)->onePopulate();
63
        $this->assertEquals('user2', $customer->getName());
64
65
        $customer = $customerQuery->orderBy('id')->offset(2)->onePopulate();
66
        $this->assertEquals('user3', $customer->getName());
67
68
        $customer = $customerQuery->offset(3)->onePopulate();
69
        $this->assertNull($customer);
70
    }
71
72
    public function testFindEager(): void
73
    {
74
        $this->checkFixture($this->db(), 'customer', true);
75
76
        $customerQuery = new ActiveQuery(Customer::class);
77
        $customers = $customerQuery->with('orders')->indexBy('id')->all();
78
79
        ksort($customers);
80
        $this->assertCount(3, $customers);
81
        $this->assertTrue($customers[1]->isRelationPopulated('orders'));
82
        $this->assertTrue($customers[2]->isRelationPopulated('orders'));
83
        $this->assertTrue($customers[3]->isRelationPopulated('orders'));
84
        $this->assertCount(1, $customers[1]->getOrders());
85
        $this->assertCount(2, $customers[2]->getOrders());
86
        $this->assertCount(0, $customers[3]->getOrders());
87
88
        $customers[1]->resetRelation('orders');
89
        $this->assertFalse($customers[1]->isRelationPopulated('orders'));
90
91
        $customer = $customerQuery->where(['id' => 1])->with('orders')->onePopulate();
92
        $this->assertTrue($customer->isRelationPopulated('orders'));
93
        $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

93
        $this->assertCount(1, $customer->/** @scrutinizer ignore-call */ getOrders());
Loading history...
94
        $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

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