Passed
Pull Request — master (#234)
by Wilmer
04:01 queued 01:22
created

ActiveQueryFindTest::testFindAsArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 39
rs 9.392
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\ActiveQueryFindTest as AbstractActiveQueryFindTest;
9
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Customer;
10
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Order;
11
use Yiisoft\Db\Connection\ConnectionInterface;
12
13
/**
14
 * @group oci
15
 */
16
final class ActiveQueryFindTest extends AbstractActiveQueryFindTest
17
{
18
    protected string $driverName = 'oci';
19
    protected ConnectionInterface $db;
20
21
    public function setUp(): void
22
    {
23
        parent::setUp();
24
25
        $this->db = $this->ociConnection;
26
    }
27
28
    protected function tearDown(): void
29
    {
30
        parent::tearDown();
31
32
        $this->ociConnection->close();
33
34
        unset($this->ociConnection);
35
    }
36
37
    public function testFindEager(): void
38
    {
39
        $this->checkFixture($this->db, 'customer', true);
40
41
        $customerQuery = new ActiveQuery(Customer::class, $this->db);
42
        $customers = $customerQuery->with('orders')->indexBy('id')->all();
43
44
        ksort($customers);
45
        $this->assertCount(3, $customers);
46
        $this->assertTrue($customers[1]->isRelationPopulated('orders'));
47
        $this->assertTrue($customers[2]->isRelationPopulated('orders'));
48
        $this->assertTrue($customers[3]->isRelationPopulated('orders'));
49
        $this->assertCount(1, $customers[1]->orders);
50
        $this->assertCount(2, $customers[2]->orders);
51
        $this->assertCount(0, $customers[3]->orders);
52
53
        unset($customers[1]->orders);
54
        $this->assertFalse($customers[1]->isRelationPopulated('orders'));
55
56
        $customer = $customerQuery->where(['id' => 1])->with('orders')->one();
57
        $this->assertTrue($customer->isRelationPopulated('orders'));
58
        $this->assertCount(1, $customer->orders);
59
        $this->assertCount(1, $customer->relatedRecords);
60
61
        /** multiple with() calls */
62
        $orderQuery = new ActiveQuery(Order::class, $this->db);
63
        $orders = $orderQuery->with('customer', 'items')->all();
64
        $this->assertCount(3, $orders);
65
        $this->assertTrue($orders[0]->isRelationPopulated('customer'));
66
        $this->assertTrue($orders[0]->isRelationPopulated('items'));
67
68
        $orders = $orderQuery->with('customer')->with('items')->all();
69
        $this->assertCount(3, $orders);
70
        $this->assertTrue($orders[0]->isRelationPopulated('customer'));
71
        $this->assertTrue($orders[0]->isRelationPopulated('items'));
72
    }
73
74
    public function testFindAsArray(): void
75
    {
76
        $this->checkFixture($this->db, 'customer');
77
78
        /** asArray */
79
        $customerQuery = new ActiveQuery(Customer::class, $this->db);
80
        $customer = $customerQuery->where(['[[id]]' => 2])->asArray()->one();
81
        $this->assertEquals([
82
            'id' => 2,
83
            'email' => '[email protected]',
84
            'name' => 'user2',
85
            'address' => 'address2',
86
            'status' => 1,
87
            'profile_id' => null,
88
            'bool_status' => true,
89
        ], $customer);
90
91
        /** find all asArray */
92
        $customerQuery = new ActiveQuery(Customer::class, $this->db);
93
        $customers = $customerQuery->asArray()->all();
94
        $this->assertCount(3, $customers);
95
        $this->assertArrayHasKey('id', $customers[0]);
96
        $this->assertArrayHasKey('name', $customers[0]);
97
        $this->assertArrayHasKey('email', $customers[0]);
98
        $this->assertArrayHasKey('address', $customers[0]);
99
        $this->assertArrayHasKey('status', $customers[0]);
100
        $this->assertArrayHasKey('bool_status', $customers[0]);
101
        $this->assertArrayHasKey('id', $customers[1]);
102
        $this->assertArrayHasKey('name', $customers[1]);
103
        $this->assertArrayHasKey('email', $customers[1]);
104
        $this->assertArrayHasKey('address', $customers[1]);
105
        $this->assertArrayHasKey('status', $customers[1]);
106
        $this->assertArrayHasKey('bool_status', $customers[1]);
107
        $this->assertArrayHasKey('id', $customers[2]);
108
        $this->assertArrayHasKey('name', $customers[2]);
109
        $this->assertArrayHasKey('email', $customers[2]);
110
        $this->assertArrayHasKey('address', $customers[2]);
111
        $this->assertArrayHasKey('status', $customers[2]);
112
        $this->assertArrayHasKey('bool_status', $customers[2]);
113
    }
114
}
115