Passed
Pull Request — master (#368)
by Sergei
02:42
created

MagicActiveRecordTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A MagicActiveRecordTest::createConnection() 0 3 1
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
use Yiisoft\Db\Connection\ConnectionInterface;
13
14
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...
15
{
16
    protected function createConnection(): ConnectionInterface
17
    {
18
        return (new OracleHelper())->createConnection();
19
    }
20
21
    public function testCastValues(): void
22
    {
23
        $this->markTestSkipped('Cant bind floats without support from a custom PDO driver.');
24
25
        $this->checkFixture($this->db(), 'customer');
26
27
        $arClass = new Type();
28
        $arClass->int_col = 123;
29
        $arClass->int_col2 = 456;
30
        $arClass->smallint_col = 42;
31
        $arClass->char_col = '1337';
32
        $arClass->char_col2 = 'test';
33
        $arClass->char_col3 = 'test123';
34
        /** can't bind floats without support from a custom PDO driver */
35
        $arClass->float_col = 2;
36
        $arClass->float_col2 = 1;
37
        $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...
38
        $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...
39
        $arClass->save();
40
41
        $aqClass = new ActiveQuery(Type::class);
42
        $query = $aqClass->onePopulate();
43
44
        $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...
45
        $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...
46
        $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...
47
        $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...
48
        $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...
49
        $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...
50
        $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...
51
        $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...
52
        $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...
53
        $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...
54
    }
55
56
    public function testDefaultValues(): void
57
    {
58
        $this->checkFixture($this->db(), 'customer');
59
60
        $arClass = new Type();
61
        $arClass->loadDefaultValues();
62
        $this->assertEquals(1, $arClass->int_col2);
63
        $this->assertEquals('something', $arClass->char_col2);
64
        $this->assertEquals(1.23, $arClass->float_col2);
65
        $this->assertEquals(33.22, $arClass->numeric_col);
66
        $this->assertEquals('1', $arClass->bool_col2);
67
68
        // not testing $arClass->time, because oci\Schema can't read default value
69
70
        $arClass = new Type();
71
        $arClass->char_col2 = 'not something';
72
73
        $arClass->loadDefaultValues();
74
        $this->assertEquals('not something', $arClass->char_col2);
75
76
        $arClass = new Type();
77
        $arClass->char_col2 = 'not something';
78
79
        $arClass->loadDefaultValues(false);
80
        $this->assertEquals('something', $arClass->char_col2);
81
    }
82
83
    /**
84
     * Some PDO implementations (e.g. cubrid) do not support boolean values.
85
     *
86
     * Make sure this does not affect AR layer.
87
     */
88
    public function testBooleanAttribute(): void
89
    {
90
        $this->checkFixture($this->db(), 'customer', true);
91
92
        $customer = new Customer();
93
94
        $customer->name = 'boolean customer';
95
        $customer->email = '[email protected]';
96
        $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...
97
98
        $customer->save();
99
        $customer->refresh();
100
        $this->assertEquals('1', $customer->status);
101
102
        $customer->status = '0';
103
        $customer->save();
104
105
        $customer->refresh();
106
        $this->assertEquals('0', $customer->status);
107
108
        $customerQuery = new ActiveQuery(Customer::class);
109
        $customers = $customerQuery->where(['status' => '1'])->all();
110
        $this->assertCount(2, $customers);
111
112
        $customerQuery = new ActiveQuery(Customer::class);
113
        $customers = $customerQuery->where(['status' => '0'])->all();
114
        $this->assertCount(1, $customers);
115
    }
116
117
    public function testToArray(): void
118
    {
119
        $this->checkFixture($this->db(), 'customer', true);
120
121
        $customerQuery = new ActiveQuery(Customer::class);
122
        $customer = $customerQuery->findOne(1);
123
124
        $this->assertSame(
125
            [
126
                'id' => 1,
127
                'email' => '[email protected]',
128
                'name' => 'user1',
129
                'address' => 'address1',
130
                'status' => 1,
131
                'bool_status' => '1',
132
                'profile_id' => 1,
133
            ],
134
            $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

134
            $customer->/** @scrutinizer ignore-call */ 
135
                       toArray(),
Loading history...
135
        );
136
    }
137
138
    public function testToArrayWithClosure(): void
139
    {
140
        $this->checkFixture($this->db(), 'customer', true);
141
142
        $customerQuery = new ActiveQuery(CustomerClosureField::class);
143
        $customer = $customerQuery->findOne(1);
144
145
        $this->assertSame(
146
            [
147
                'id' => 1,
148
                'email' => '[email protected]',
149
                'name' => 'user1',
150
                'address' => 'address1',
151
                'status' => 'active',
152
                'bool_status' => '1',
153
                'profile_id' => 1,
154
            ],
155
            $customer->toArray(),
156
        );
157
    }
158
}
159