Passed
Push — master ( cfe060...447357 )
by Alexander
03:18
created

SchemaTest::getExpectedColumns()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 134
Code Lines 125

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 125
nc 1
nop 0
dl 0
loc 134
rs 8
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Pgsql\Tests;
6
7
use Yiisoft\Db\Expression\Expression;
8
use Yiisoft\Db\Tests\SchemaTest as AbstractSchemaTest;
9
10
class SchemaTest extends AbstractSchemaTest
11
{
12
    protected ?string $driverName = 'pgsql';
13
14
    protected array $expectedSchemas = [
15
        'public',
16
    ];
17
18
    public function getExpectedColumns()
19
    {
20
        $columns = parent::getExpectedColumns();
21
        unset($columns['enum_col']);
22
        $columns['int_col']['dbType'] = 'int4';
23
        $columns['int_col']['size'] = null;
24
        $columns['int_col']['precision'] = 32;
25
        $columns['int_col']['scale'] = 0;
26
        $columns['int_col2']['dbType'] = 'int4';
27
        $columns['int_col2']['size'] = null;
28
        $columns['int_col2']['precision'] = 32;
29
        $columns['int_col2']['scale'] = 0;
30
        $columns['tinyint_col']['type'] = 'smallint';
31
        $columns['tinyint_col']['dbType'] = 'int2';
32
        $columns['tinyint_col']['size'] = null;
33
        $columns['tinyint_col']['precision'] = 16;
34
        $columns['tinyint_col']['scale'] = 0;
35
        $columns['smallint_col']['dbType'] = 'int2';
36
        $columns['smallint_col']['size'] = null;
37
        $columns['smallint_col']['precision'] = 16;
38
        $columns['smallint_col']['scale'] = 0;
39
        $columns['char_col']['dbType'] = 'bpchar';
40
        $columns['char_col']['precision'] = null;
41
        $columns['char_col2']['dbType'] = 'varchar';
42
        $columns['char_col2']['precision'] = null;
43
        $columns['float_col']['dbType'] = 'float8';
44
        $columns['float_col']['precision'] = 53;
45
        $columns['float_col']['scale'] = null;
46
        $columns['float_col']['size'] = null;
47
        $columns['float_col2']['dbType'] = 'float8';
48
        $columns['float_col2']['precision'] = 53;
49
        $columns['float_col2']['scale'] = null;
50
        $columns['float_col2']['size'] = null;
51
        $columns['blob_col']['dbType'] = 'bytea';
52
        $columns['blob_col']['phpType'] = 'resource';
53
        $columns['blob_col']['type'] = 'binary';
54
        $columns['numeric_col']['dbType'] = 'numeric';
55
        $columns['numeric_col']['size'] = null;
56
        $columns['bool_col']['type'] = 'boolean';
57
        $columns['bool_col']['phpType'] = 'boolean';
58
        $columns['bool_col']['dbType'] = 'bool';
59
        $columns['bool_col']['size'] = null;
60
        $columns['bool_col']['precision'] = null;
61
        $columns['bool_col']['scale'] = null;
62
        $columns['bool_col2']['type'] = 'boolean';
63
        $columns['bool_col2']['phpType'] = 'boolean';
64
        $columns['bool_col2']['dbType'] = 'bool';
65
        $columns['bool_col2']['size'] = null;
66
        $columns['bool_col2']['precision'] = null;
67
        $columns['bool_col2']['scale'] = null;
68
        $columns['bool_col2']['defaultValue'] = true;
69
        $columns['ts_default']['defaultValue'] = new Expression('now()');
70
        $columns['bit_col']['dbType'] = 'bit';
71
        $columns['bit_col']['size'] = 8;
72
        $columns['bit_col']['precision'] = null;
73
        $columns['bigint_col'] = [
74
            'type' => 'bigint',
75
            'dbType' => 'int8',
76
            'phpType' => 'integer',
77
            'allowNull' => true,
78
            'autoIncrement' => false,
79
            'enumValues' => null,
80
            'size' => null,
81
            'precision' => 64,
82
            'scale' => 0,
83
            'defaultValue' => null,
84
        ];
85
        $columns['intarray_col'] = [
86
            'type' => 'integer',
87
            'dbType' => 'int4',
88
            'phpType' => 'integer',
89
            'allowNull' => true,
90
            'autoIncrement' => false,
91
            'enumValues' => null,
92
            'size' => null,
93
            'precision' => null,
94
            'scale' => null,
95
            'defaultValue' => null,
96
            'dimension' => 1
97
        ];
98
        $columns['textarray2_col'] = [
99
            'type' => 'text',
100
            'dbType' => 'text',
101
            'phpType' => 'string',
102
            'allowNull' => true,
103
            'autoIncrement' => false,
104
            'enumValues' => null,
105
            'size' => null,
106
            'precision' => null,
107
            'scale' => null,
108
            'defaultValue' => null,
109
            'dimension' => 2
110
        ];
111
        $columns['json_col'] = [
112
            'type' => 'json',
113
            'dbType' => 'json',
114
            'phpType' => 'array',
115
            'allowNull' => true,
116
            'autoIncrement' => false,
117
            'enumValues' => null,
118
            'size' => null,
119
            'precision' => null,
120
            'scale' => null,
121
            'defaultValue' => ["a" => 1],
122
            'dimension' => 0
123
        ];
124
        $columns['jsonb_col'] = [
125
            'type' => 'json',
126
            'dbType' => 'jsonb',
127
            'phpType' => 'array',
128
            'allowNull' => true,
129
            'autoIncrement' => false,
130
            'enumValues' => null,
131
            'size' => null,
132
            'precision' => null,
133
            'scale' => null,
134
            'defaultValue' => null,
135
            'dimension' => 0
136
        ];
137
        $columns['jsonarray_col'] = [
138
            'type' => 'json',
139
            'dbType' => 'json',
140
            'phpType' => 'array',
141
            'allowNull' => true,
142
            'autoIncrement' => false,
143
            'enumValues' => null,
144
            'size' => null,
145
            'precision' => null,
146
            'scale' => null,
147
            'defaultValue' => null,
148
            'dimension' => 1
149
        ];
150
151
        return $columns;
152
    }
153
154
    public function testCompositeFk()
155
    {
156
        $schema = $this->getConnection()->getSchema();
157
158
        $table = $schema->getTableSchema('composite_fk');
159
160
        $fk = $table->getForeignKeys();
161
        $this->assertCount(1, $fk);
162
        $this->assertTrue(isset($fk['fk_composite_fk_order_item']));
163
        $this->assertEquals('order_item', $fk['fk_composite_fk_order_item'][0]);
164
        $this->assertEquals('order_id', $fk['fk_composite_fk_order_item']['order_id']);
165
        $this->assertEquals('item_id', $fk['fk_composite_fk_order_item']['item_id']);
166
    }
167
168
    public function testGetPDOType()
169
    {
170
        $values = [
171
            [null, \PDO::PARAM_NULL],
172
            ['', \PDO::PARAM_STR],
173
            ['hello', \PDO::PARAM_STR],
174
            [0, \PDO::PARAM_INT],
175
            [1, \PDO::PARAM_INT],
176
            [1337, \PDO::PARAM_INT],
177
            [true, \PDO::PARAM_BOOL],
178
            [false, \PDO::PARAM_BOOL],
179
            [$fp = fopen(__FILE__, 'rb'), \PDO::PARAM_LOB],
180
        ];
181
182
        $schema = $this->getConnection()->getSchema();
183
184
        foreach ($values as $value) {
185
            $this->assertEquals($value[1], $schema->getPdoType($value[0]));
186
        }
187
        fclose($fp);
188
    }
189
190
    public function testBooleanDefaultValues()
191
    {
192
        $schema = $this->getConnection()->getSchema();
193
194
        $table = $schema->getTableSchema('bool_values');
195
        $this->assertTrue($table->getColumn('default_true')->getDefaultValue());
196
        $this->assertFalse($table->getColumn('default_false')->getDefaultValue());
197
    }
198
199
    public function testSequenceName()
200
    {
201
        $connection = $this->getConnection();
202
203
        $sequenceName = $connection->getSchema()->getTableSchema('item')->getSequenceName();
204
205
        $connection->createCommand(
206
            'ALTER TABLE "item" ALTER COLUMN "id" SET DEFAULT nextval(\'item_id_seq_2\')'
207
        )->execute();
208
209
        $connection->getSchema()->refreshTableSchema('item');
210
        $this->assertEquals('item_id_seq_2', $connection->getSchema()->getTableSchema('item')->getSequenceName());
211
212
        $connection->createCommand(
213
            'ALTER TABLE "item" ALTER COLUMN "id" SET DEFAULT nextval(\'' . $sequenceName . '\')'
214
        )->execute();
215
216
        $connection->getSchema()->refreshTableSchema('item');
217
        $this->assertEquals($sequenceName, $connection->getSchema()->getTableSchema('item')->getSequenceName());
218
    }
219
220
    public function testGeneratedValues()
221
    {
222
        if (version_compare($this->getConnection(false)->getServerVersion(), '12.0', '<')) {
223
            $this->markTestSkipped('PostgreSQL < 12.0 does not support GENERATED AS IDENTITY columns.');
224
        }
225
226
        $config = $this->database;
227
        unset($config['fixture']);
228
        $this->prepareDatabase($config, \realpath(__DIR__ . '/../../../data') . '/postgres12.sql');
229
230
        $table = $this->getConnection(false)->getSchema()->getTableSchema('generated');
231
        $this->assertTrue($table->getColumn('id_always')->getAutoIncrement());
232
        $this->assertTrue($table->getColumn('id_primary')->getAutoIncrement());
233
        $this->assertTrue($table->getColumn('id_primary')->getAutoIncrement());
234
        $this->assertTrue($table->getColumn('id_default')->getAutoIncrement());
235
    }
236
237
    public function testPartitionedTable()
238
    {
239
        if (version_compare($this->getConnection(false)->getServerVersion(), '10.0', '<')) {
240
            $this->markTestSkipped('PostgreSQL < 10.0 does not support PARTITION BY clause.');
241
        }
242
243
        $config = $this->database;
244
        unset($config['fixture']);
245
        $this->prepareDatabase($config, realpath(__DIR__ . '/../../../data') . '/postgres10.sql');
246
247
        $this->assertNotNull($this->getConnection(false)->getSchema()->getTableSchema('partitioned'));
248
    }
249
250
    public function testFindSchemaNames()
251
    {
252
        $schema = $this->getConnection()->getSchema();
253
254
        $this->assertCount(3, $schema->getSchemaNames());
255
    }
256
257
    public function bigintValueProvider()
258
    {
259
        return [
260
            [8817806877],
261
            [3797444208],
262
            [3199585540],
263
            [1389831585],
264
            [922337203685477580],
265
            [9223372036854775807],
266
            [-9223372036854775808],
267
        ];
268
    }
269
270
    /**
271
     * @see https://github.com/yiisoft/yii2/issues/12483
272
     */
273
    public function testParenthesisDefaultValue()
274
    {
275
        $db = $this->getConnection(false);
276
        if ($db->getSchema()->getTableSchema('test_default_parenthesis') !== null) {
277
            $db->createCommand()->dropTable('test_default_parenthesis')->execute();
278
        }
279
280
        $db->createCommand()->createTable('test_default_parenthesis', [
281
            'id' => 'pk',
282
            'user_timezone' => 'numeric(5,2) DEFAULT (0)::numeric NOT NULL',
283
        ])->execute();
284
285
        $db->getSchema()->refreshTableSchema('test_default_parenthesis');
286
        $tableSchema = $db->getSchema()->getTableSchema('test_default_parenthesis');
287
        $this->assertNotNull($tableSchema);
288
        $column = $tableSchema->getColumn('user_timezone');
289
        $this->assertNotNull($column);
290
        $this->assertFalse($column->isAllowNull());
291
        $this->assertEquals('numeric', $column->getDbType());
292
        $this->assertEquals(0, $column->getDefaultValue());
293
    }
294
295
    /**
296
     * @see https://github.com/yiisoft/yii2/issues/14192
297
     */
298
    public function testTimestampNullDefaultValue()
299
    {
300
        $db = $this->getConnection(false);
301
        if ($db->getSchema()->getTableSchema('test_timestamp_default_null') !== null) {
302
            $db->createCommand()->dropTable('test_timestamp_default_null')->execute();
303
        }
304
305
        $db->createCommand()->createTable('test_timestamp_default_null', [
306
            'id' => 'pk',
307
            'timestamp' => 'timestamp DEFAULT NULL',
308
        ])->execute();
309
310
        $db->getSchema()->refreshTableSchema('test_timestamp_default_null');
311
        $tableSchema = $db->getSchema()->getTableSchema('test_timestamp_default_null');
312
        $this->assertNull($tableSchema->getColumn('timestamp')->getDefaultValue());
313
    }
314
315
    public function constraintsProvider(): array
316
    {
317
        $result = parent::constraintsProvider();
318
319
        $result['1: check'][2][0]->expression('CHECK ((("C_check")::text <> \'\'::text))');
320
        //$result['3: foreign key'][2][0]->setForeignSchemaName('public');
321
        $result['3: index'][2] = [];
322
323
        return $result;
324
    }
325
}
326