Passed
Pull Request — master (#234)
by Wilmer
03:08
created

ActiveQueryFindTest::testFindEager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

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