Passed
Pull Request — master (#380)
by Wilmer
04:47 queued 01:51
created

AbstractSchemaTest::testGetTableChecks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 12
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Tests;
6
7
use PDO;
8
use PHPUnit\Framework\TestCase;
9
use Yiisoft\Db\Exception\NotSupportedException;
10
use Yiisoft\Db\Schema\ColumnSchema;
11
use Yiisoft\Db\Schema\Schema;
12
use Yiisoft\Db\Tests\Support\TestTrait;
13
14
use function fclose;
15
use function fopen;
16
use function print_r;
17
18
abstract class AbstractSchemaTest extends TestCase
19
{
20
    use TestTrait;
21
22
    public function testColumnSchemaDbTypecastWithEmptyCharType(): void
23
    {
24
        $columnSchema = new ColumnSchema();
25
        $columnSchema->setType(Schema::TYPE_CHAR);
26
27
        $this->assertSame('', $columnSchema->dbTypecast(''));
28
    }
29
30
    public function testGetDefaultSchema(): void
31
    {
32
        $db = $this->getConnection();
33
34
        $schema = $db->getSchema();
35
36
        $this->assertNull($schema->getDefaultSchema());
37
    }
38
39
    public function testGetPDOType(): void
40
    {
41
        $values = [
42
            [null, PDO::PARAM_NULL],
43
            ['', PDO::PARAM_STR],
44
            ['hello', PDO::PARAM_STR],
45
            [0, PDO::PARAM_INT],
46
            [1, PDO::PARAM_INT],
47
            [1337, PDO::PARAM_INT],
48
            [true, PDO::PARAM_BOOL],
49
            [false, PDO::PARAM_BOOL],
50
            [$fp = fopen(__FILE__, 'rb'), PDO::PARAM_LOB],
51
        ];
52
53
        $db = $this->getConnection();
54
55
        $schema = $db->getSchema();
56
57
        foreach ($values as $value) {
58
            $this->assertSame(
59
                $value[1],
60
                $schema->getPdoType($value[0]),
61
                'type for value ' . print_r($value[0], true) . ' does not match.',
0 ignored issues
show
Bug introduced by
Are you sure print_r($value[0], true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

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

61
                'type for value ' . /** @scrutinizer ignore-type */ print_r($value[0], true) . ' does not match.',
Loading history...
62
            );
63
        }
64
65
        fclose($fp);
66
    }
67
68
    public function testGetSchemaChecks(): void
69
    {
70
        $db = $this->getConnection();
71
72
        $schema = $db->getSchema();
73
74
        $this->expectException(NotSupportedException::class);
75
        $this->expectExceptionMessage(
76
            'Yiisoft\Db\Tests\Support\Stubs\Schema does not support fetching all table names.'
77
        );
78
79
        $schema->getSchemaChecks();
80
    }
81
82
    public function testGetSchemaDefaultValues(): void
83
    {
84
        $db = $this->getConnection();
85
86
        $schema = $db->getSchema();
87
88
        $this->expectException(NotSupportedException::class);
89
        $this->expectExceptionMessage(
90
            'Yiisoft\Db\Tests\Support\Stubs\Schema does not support fetching all table names.'
91
        );
92
93
        $schema->getSchemaDefaultValues();
94
    }
95
96
    public function testGetSchemaForeignKeys(): void
97
    {
98
        $db = $this->getConnection();
99
100
        $schema = $db->getSchema();
101
102
        $this->expectException(NotSupportedException::class);
103
        $this->expectExceptionMessage(
104
            'Yiisoft\Db\Tests\Support\Stubs\Schema does not support fetching all table names.'
105
        );
106
107
        $schema->getSchemaForeignKeys();
108
    }
109
110
    public function testGetSchemaIndexes(): void
111
    {
112
        $db = $this->getConnection();
113
114
        $schema = $db->getSchema();
115
116
        $this->expectException(NotSupportedException::class);
117
        $this->expectExceptionMessage(
118
            'Yiisoft\Db\Tests\Support\Stubs\Schema does not support fetching all table names.'
119
        );
120
121
        $schema->getSchemaIndexes();
122
    }
123
124
    public function testGetSchemaNames(): void
125
    {
126
        $db = $this->getConnection();
127
128
        $schema = $db->getSchema();
129
130
        $this->expectException(NotSupportedException::class);
131
        $this->expectExceptionMessage(
132
            'Yiisoft\Db\Tests\Support\Stubs\Schema does not support fetching all schema names.'
133
        );
134
135
        $schema->getSchemaNames();
136
    }
137
138
    public function testGetSchemaPrimaryKeys(): void
139
    {
140
        $db = $this->getConnection();
141
142
        $schema = $db->getSchema();
143
144
        $this->expectException(NotSupportedException::class);
145
        $this->expectExceptionMessage(
146
            'Yiisoft\Db\Tests\Support\Stubs\Schema does not support fetching all table names.'
147
        );
148
149
        $schema->getSchemaPrimaryKeys();
150
    }
151
152
    public function testGetSchemaUniques(): void
153
    {
154
        $db = $this->getConnection();
155
156
        $schema = $db->getSchema();
157
158
        $this->expectException(NotSupportedException::class);
159
        $this->expectExceptionMessage(
160
            'Yiisoft\Db\Tests\Support\Stubs\Schema does not support fetching all table names.'
161
        );
162
163
        $schema->getSchemaUniques();
164
    }
165
166
    public function testGetTableChecks(): void
167
    {
168
        $db = $this->getConnection();
169
170
        $schema = $db->getSchema();
171
172
        $this->expectException(NotSupportedException::class);
173
        $this->expectExceptionMessage(
174
            'Yiisoft\Db\Tests\Support\Stubs\Schema::loadTableChecks() is not supported by core-db.'
175
        );
176
177
        $schema->getTableChecks('customer');
178
    }
179
180
    public function testGetTableDefaultValues(): void
181
    {
182
        $db = $this->getConnection();
183
184
        $schema = $db->getSchema();
185
186
        $this->expectException(NotSupportedException::class);
187
        $this->expectExceptionMessage(
188
            'Yiisoft\Db\Tests\Support\Stubs\Schema::loadTableDefaultValues() is not supported by core-db.'
189
        );
190
191
        $schema->getTableDefaultValues('customer');
192
    }
193
194
    public function testGetTableForeignKeys(): void
195
    {
196
        $db = $this->getConnection();
197
198
        $schema = $db->getSchema();
199
200
        $this->expectException(NotSupportedException::class);
201
        $this->expectExceptionMessage(
202
            'Yiisoft\Db\Tests\Support\Stubs\Schema::loadTableForeignKeys() is not supported by core-db.'
203
        );
204
205
        $schema->getTableForeignKeys('customer');
206
    }
207
}
208