Passed
Push — master ( 594551...748657 )
by Wilmer
02:40
created

ActiveRecordTest::testToArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 18
rs 9.8666
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;
9
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerClosureField;
10
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Type;
11
use Yiisoft\ActiveRecord\Tests\Support\OracleHelper;
12
13
final class ActiveRecordTest extends \Yiisoft\ActiveRecord\Tests\ActiveRecordTest
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 testCastValues(): void
33
    {
34
        $this->markTestSkipped('Cant bind floats without support from a custom PDO driver.');
35
36
        $this->checkFixture($this->db, 'customer');
37
38
        $arClass = new Type($this->db);
39
        $arClass->int_col = 123;
40
        $arClass->int_col2 = 456;
41
        $arClass->smallint_col = 42;
42
        $arClass->char_col = '1337';
43
        $arClass->char_col2 = 'test';
44
        $arClass->char_col3 = 'test123';
45
        /** can't bind floats without support from a custom PDO driver */
46
        $arClass->float_col = 2;
47
        $arClass->float_col2 = 1;
48
        $arClass->bool_col = 1;
49
        $arClass->bool_col2 = 0;
50
        $arClass->save();
51
52
        $aqClass = new ActiveQuery(Type::class, $this->db);
53
        $query = $aqClass->onePopulate();
54
55
        $this->assertSame(123, $query->int_col);
56
        $this->assertSame(456, $query->int_col2);
57
        $this->assertSame(42, $query->smallint_col);
58
        $this->assertSame('1337', trim($query->char_col));
59
        $this->assertSame('test', $query->char_col2);
60
        $this->assertSame('test123', $query->char_col3);
61
        $this->assertSame(2.0, $query->float_col);
62
        $this->assertSame(1.0, $query->float_col2);
63
        $this->assertEquals('1', $query->bool_col);
64
        $this->assertEquals('0', $query->bool_col2);
65
    }
66
67
    public function testDefaultValues(): void
68
    {
69
        $this->checkFixture($this->db, 'customer');
70
71
        $arClass = new Type($this->db);
72
        $arClass->loadDefaultValues();
73
        $this->assertEquals(1, $arClass->int_col2);
74
        $this->assertEquals('something', $arClass->char_col2);
75
        $this->assertEquals(1.23, $arClass->float_col2);
76
        $this->assertEquals(33.22, $arClass->numeric_col);
77
        $this->assertEquals('1', $arClass->bool_col2);
78
79
        // not testing $arClass->time, because oci\Schema can't read default value
80
81
        $arClass = new Type($this->db);
82
        $arClass->char_col2 = 'not something';
83
84
        $arClass->loadDefaultValues();
85
        $this->assertEquals('not something', $arClass->char_col2);
86
87
        $arClass = new Type($this->db);
88
        $arClass->char_col2 = 'not something';
89
90
        $arClass->loadDefaultValues(false);
91
        $this->assertEquals('something', $arClass->char_col2);
92
    }
93
94
    /**
95
     * Some PDO implementations (e.g. cubrid) do not support boolean values.
96
     *
97
     * Make sure this does not affect AR layer.
98
     */
99
    public function testBooleanAttribute(): void
100
    {
101
        $this->checkFixture($this->db, 'customer', true);
102
103
        $customer = new Customer($this->db);
104
105
        $customer->name = 'boolean customer';
106
        $customer->email = '[email protected]';
107
        $customer->status = '1';
0 ignored issues
show
Documentation Bug introduced by
The property $status was declared of type integer, but '1' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
108
109
        $customer->save();
110
        $customer->refresh();
111
        $this->assertEquals('1', $customer->status);
112
113
        $customer->status = '0';
114
        $customer->save();
115
116
        $customer->refresh();
117
        $this->assertEquals('0', $customer->status);
118
119
        $customerQuery = new ActiveQuery(Customer::class, $this->db);
120
        $customers = $customerQuery->where(['status' => '1'])->all();
121
        $this->assertCount(2, $customers);
122
123
        $customerQuery = new ActiveQuery(Customer::class, $this->db);
124
        $customers = $customerQuery->where(['status' => '0'])->all();
125
        $this->assertCount(1, $customers);
126
    }
127
128
    public function testToArray(): void
129
    {
130
        $this->checkFixture($this->db, 'customer', true);
131
132
        $customerQuery = new ActiveQuery(Customer::class, $this->db);
133
        $customer = $customerQuery->findOne(1);
134
135
        $this->assertSame(
136
            [
137
                'id' => 1,
138
                'email' => '[email protected]',
139
                'name' => 'user1',
140
                'address' => 'address1',
141
                'status' => 1,
142
                'bool_status' => '1',
143
                'profile_id' => 1,
144
            ],
145
            $customer->toArray(),
146
        );
147
    }
148
149
    public function testToArrayWithClosure(): void
150
    {
151
        $this->checkFixture($this->db, 'customer', true);
152
153
        $customerQuery = new ActiveQuery(CustomerClosureField::class, $this->db);
154
        $customer = $customerQuery->findOne(1);
155
156
        $this->assertSame(
157
            [
158
                'id' => 1,
159
                'email' => '[email protected]',
160
                'name' => 'user1',
161
                'address' => 'address1',
162
                'status' => 'active',
163
                'bool_status' => '1',
164
                'profile_id' => 1,
165
            ],
166
            $customer->toArray(),
167
        );
168
    }
169
}
170