Passed
Push — master ( bc8b6f...7a9b1f )
by Sergei
02:57
created

MagicActiveRecordTest::testCastValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 33
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\Driver\Oracle\Stubs\MagicCustomer as Customer;
9
use Yiisoft\ActiveRecord\Tests\Stubs\MagicActiveRecord\CustomerClosureField;
10
use Yiisoft\ActiveRecord\Tests\Stubs\MagicActiveRecord\Type;
11
use Yiisoft\ActiveRecord\Tests\Support\OracleHelper;
12
13
final class MagicActiveRecordTest extends \Yiisoft\ActiveRecord\Tests\MagicActiveRecordTest
0 ignored issues
show
Bug introduced by
The type Yiisoft\ActiveRecord\Tests\MagicActiveRecordTest 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...
14
{
15
    public function setUp(): void
16
    {
17
        parent::setUp();
18
19
        $oracleHelper = new OracleHelper();
20
        $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...
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;
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...
49
        $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...
50
        $arClass->save();
51
52
        $aqClass = new ActiveQuery(Type::class, $this->db);
53
        $query = $aqClass->onePopulate();
54
55
        $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...
56
        $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...
57
        $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...
58
        $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...
59
        $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...
60
        $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...
61
        $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...
62
        $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...
63
        $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...
64
        $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...
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(),
0 ignored issues
show
Bug introduced by
The method toArray() does not exist on Yiisoft\ActiveRecord\ActiveRecordInterface. It seems like you code against a sub-type of Yiisoft\ActiveRecord\ActiveRecordInterface such as Yiisoft\ActiveRecord\Tests\Stubs\MagicActiveRecord or Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

145
            $customer->/** @scrutinizer ignore-call */ 
146
                       toArray(),
Loading history...
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