Passed
Pull Request — master (#234)
by Wilmer
02:38
created

ActiveRecordTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 69
dl 0
loc 117
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testBooleanAttribute() 0 29 1
A setUp() 0 5 1
A testCastValues() 0 33 1
A tearDown() 0 7 1
A testDefaultValues() 0 25 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\ActiveRecordTest as AbstractActiveRecordTest;
9
use Yiisoft\ActiveRecord\Tests\Driver\Oracle\Stubs\ActiveRecord\Customer;
0 ignored issues
show
Bug introduced by
The type Yiisoft\ActiveRecord\Tes...s\ActiveRecord\Customer 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...
10
use Yiisoft\ActiveRecord\Tests\Driver\Stubs\ActiveRecord\Type;
0 ignored issues
show
Bug introduced by
The type Yiisoft\ActiveRecord\Tes...Stubs\ActiveRecord\Type 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...
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';
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