Passed
Pull Request — master (#234)
by Wilmer
04:01 queued 01:22
created

ActiveRecordTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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