Passed
Pull Request — master (#273)
by
unknown
02:59
created

ActiveRecordTest::testToArrayForArrayable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 50
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 35
c 1
b 0
f 1
dl 0
loc 50
rs 9.36
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\CustomerForArrayable;
11
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Type;
12
use Yiisoft\ActiveRecord\Tests\Support\OracleHelper;
13
14
final class ActiveRecordTest extends \Yiisoft\ActiveRecord\Tests\ActiveRecordTest
0 ignored issues
show
Bug introduced by
The type Yiisoft\ActiveRecord\Tests\ActiveRecordTest was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
{
16
    public function setUp(): void
17
    {
18
        parent::setUp();
19
20
        $oracleHelper = new OracleHelper();
21
        $this->db = $oracleHelper->createConnection();
0 ignored issues
show
Bug Best Practice introduced by
The property db does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
22
    }
23
24
    protected function tearDown(): void
25
    {
26
        parent::tearDown();
27
28
        $this->db->close();
29
30
        unset($this->db);
31
    }
32
33
    public function testCastValues(): void
34
    {
35
        $this->markTestSkipped('Cant bind floats without support from a custom PDO driver.');
36
37
        $this->checkFixture($this->db, 'customer');
38
39
        $arClass = new Type($this->db);
40
        $arClass->int_col = 123;
41
        $arClass->int_col2 = 456;
42
        $arClass->smallint_col = 42;
43
        $arClass->char_col = '1337';
44
        $arClass->char_col2 = 'test';
45
        $arClass->char_col3 = 'test123';
46
        /** can't bind floats without support from a custom PDO driver */
47
        $arClass->float_col = 2;
48
        $arClass->float_col2 = 1;
49
        $arClass->bool_col = 1;
0 ignored issues
show
Documentation Bug introduced by
The property $bool_col was declared of type boolean, but 1 is of type integer. 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...
50
        $arClass->bool_col2 = 0;
0 ignored issues
show
Documentation Bug introduced by
The property $bool_col2 was declared of type boolean, but 0 is of type integer. 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...
51
        $arClass->save();
52
53
        $aqClass = new ActiveQuery(Type::class, $this->db);
54
        $query = $aqClass->onePopulate();
55
56
        $this->assertSame(123, $query->int_col);
0 ignored issues
show
Bug introduced by
Accessing int_col on the interface Yiisoft\ActiveRecord\ActiveRecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
57
        $this->assertSame(456, $query->int_col2);
0 ignored issues
show
Bug introduced by
Accessing int_col2 on the interface Yiisoft\ActiveRecord\ActiveRecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
58
        $this->assertSame(42, $query->smallint_col);
0 ignored issues
show
Bug introduced by
Accessing smallint_col on the interface Yiisoft\ActiveRecord\ActiveRecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
59
        $this->assertSame('1337', trim($query->char_col));
0 ignored issues
show
Bug introduced by
Accessing char_col on the interface Yiisoft\ActiveRecord\ActiveRecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
60
        $this->assertSame('test', $query->char_col2);
0 ignored issues
show
Bug introduced by
Accessing char_col2 on the interface Yiisoft\ActiveRecord\ActiveRecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
61
        $this->assertSame('test123', $query->char_col3);
0 ignored issues
show
Bug introduced by
Accessing char_col3 on the interface Yiisoft\ActiveRecord\ActiveRecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
62
        $this->assertSame(2.0, $query->float_col);
0 ignored issues
show
Bug introduced by
Accessing float_col on the interface Yiisoft\ActiveRecord\ActiveRecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
63
        $this->assertSame(1.0, $query->float_col2);
0 ignored issues
show
Bug introduced by
Accessing float_col2 on the interface Yiisoft\ActiveRecord\ActiveRecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
64
        $this->assertEquals('1', $query->bool_col);
0 ignored issues
show
Bug introduced by
Accessing bool_col on the interface Yiisoft\ActiveRecord\ActiveRecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
65
        $this->assertEquals('0', $query->bool_col2);
0 ignored issues
show
Bug introduced by
Accessing bool_col2 on the interface Yiisoft\ActiveRecord\ActiveRecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
66
    }
67
68
    public function testDefaultValues(): void
69
    {
70
        $this->checkFixture($this->db, 'customer');
71
72
        $arClass = new Type($this->db);
73
        $arClass->loadDefaultValues();
74
        $this->assertEquals(1, $arClass->int_col2);
75
        $this->assertEquals('something', $arClass->char_col2);
76
        $this->assertEquals(1.23, $arClass->float_col2);
77
        $this->assertEquals(33.22, $arClass->numeric_col);
78
        $this->assertEquals('1', $arClass->bool_col2);
79
80
        // not testing $arClass->time, because oci\Schema can't read default value
81
82
        $arClass = new Type($this->db);
83
        $arClass->char_col2 = 'not something';
84
85
        $arClass->loadDefaultValues();
86
        $this->assertEquals('not something', $arClass->char_col2);
87
88
        $arClass = new Type($this->db);
89
        $arClass->char_col2 = 'not something';
90
91
        $arClass->loadDefaultValues(false);
92
        $this->assertEquals('something', $arClass->char_col2);
93
    }
94
95
    /**
96
     * Some PDO implementations (e.g. cubrid) do not support boolean values.
97
     *
98
     * Make sure this does not affect AR layer.
99
     */
100
    public function testBooleanAttribute(): void
101
    {
102
        $this->checkFixture($this->db, 'customer', true);
103
104
        $customer = new Customer($this->db);
105
106
        $customer->name = 'boolean customer';
107
        $customer->email = '[email protected]';
108
        $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...
109
110
        $customer->save();
111
        $customer->refresh();
112
        $this->assertEquals('1', $customer->status);
113
114
        $customer->status = '0';
115
        $customer->save();
116
117
        $customer->refresh();
118
        $this->assertEquals('0', $customer->status);
119
120
        $customerQuery = new ActiveQuery(Customer::class, $this->db);
121
        $customers = $customerQuery->where(['status' => '1'])->all();
122
        $this->assertCount(2, $customers);
123
124
        $customerQuery = new ActiveQuery(Customer::class, $this->db);
125
        $customers = $customerQuery->where(['status' => '0'])->all();
126
        $this->assertCount(1, $customers);
127
    }
128
129
    public function testToArray(): void
130
    {
131
        $this->checkFixture($this->db, 'customer', true);
132
133
        $customerQuery = new ActiveQuery(Customer::class, $this->db);
134
        $customer = $customerQuery->findOne(1);
135
136
        $this->assertSame(
137
            [
138
                'id' => 1,
139
                'email' => '[email protected]',
140
                'name' => 'user1',
141
                'address' => 'address1',
142
                'status' => 1,
143
                'bool_status' => '1',
144
                'profile_id' => 1,
145
            ],
146
            $customer->toArray(),
147
        );
148
    }
149
150
    public function testToArrayWithClosure(): void
151
    {
152
        $this->checkFixture($this->db, 'customer', true);
153
154
        $customerQuery = new ActiveQuery(CustomerClosureField::class, $this->db);
155
        $customer = $customerQuery->findOne(1);
156
157
        $this->assertSame(
158
            [
159
                'id' => 1,
160
                'email' => '[email protected]',
161
                'name' => 'user1',
162
                'address' => 'address1',
163
                'status' => 'active',
164
                'bool_status' => '1',
165
                'profile_id' => 1,
166
            ],
167
            $customer->toArray(),
168
        );
169
    }
170
171
    public function testToArrayForArrayable(): void
172
    {
173
        $this->checkFixture($this->db, 'customer', true);
174
175
        $customerQuery = new ActiveQuery(CustomerForArrayable::class, $this->db);
176
177
        /** @var CustomerForArrayable $customer */
178
        $customer = $customerQuery->findOne(1);
179
        /** @var CustomerForArrayable $customer2 */
180
        $customer2 = $customerQuery->findOne(2);
181
        /** @var CustomerForArrayable $customer3 */
182
        $customer3 = $customerQuery->findOne(3);
183
184
        $customer->setItem($customer2);
185
        $customer->setItems($customer3);
186
187
        $this->assertSame(
188
            [
189
                'id' => 1,
190
                'email' => '[email protected]',
191
                'name' => 'user1',
192
                'address' => 'address1',
193
                'status' => 'active',
194
                'item' => [
195
                    'id' => 2,
196
                    'email' => '[email protected]',
197
                    'name' => 'user2',
198
                    'status' => 'active',
199
                ],
200
                'items' => [
201
                    [
202
                        'id' => 3,
203
                        'email' => '[email protected]',
204
                        'name' => 'user3',
205
                        'status' => 'inactive',
206
                    ],
207
                ],
208
            ],
209
            $customer->toArray([
210
                'id',
211
                'name',
212
                'email',
213
                'address',
214
                'status',
215
                'item.id',
216
                'item.name',
217
                'item.email',
218
                'items.0.id',
219
                'items.0.name',
220
                'items.0.email',
221
            ]),
222
        );
223
    }
224
}
225