Passed
Branch add-more-test-schema (1ebac6)
by Wilmer
02:54
created

CommonQueryBuilderTest::testCreateTableWithGetColumnTypes()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 17
nc 6
nop 0
dl 0
loc 31
rs 8.4444
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Tests\Common;
6
7
use Yiisoft\Db\Schema\Schema;
8
use Yiisoft\Db\Tests\AbstractQueryBuilderTest;
9
use Yiisoft\Db\Tests\Provider\ColumnTypes;
10
11
use function str_replace;
12
use function str_starts_with;
13
use function strncmp;
14
use function substr;
15
16
abstract class CommonQueryBuilderTest extends AbstractQueryBuilderTest
17
{
18
    public function testCreateTableWithGetColumnTypes(): void
19
    {
20
        $db = $this->getConnection(true);
21
22
        $qb = $db->getQueryBuilder();
23
24
        if ($db->getTableSchema('column_type_table', true) !== null) {
25
            $db->createCommand($qb->dropTable('column_type_table'))->execute();
26
        }
27
28
        $columnTypes = (new ColumnTypes($db))->getColumnTypes();
29
        $columns = [];
30
        $i = 0;
31
32
        foreach ($columnTypes as [$column, $builder, $expected]) {
33
            if (
34
                !(
35
                    strncmp($column, Schema::TYPE_PK, 2) === 0 ||
36
                    strncmp($column, Schema::TYPE_UPK, 3) === 0 ||
37
                    strncmp($column, Schema::TYPE_BIGPK, 5) === 0 ||
38
                    strncmp($column, Schema::TYPE_UBIGPK, 6) === 0 ||
39
                    str_starts_with(substr($column, -5), 'FIRST')
40
                )
41
            ) {
42
                $columns['col' . ++$i] = str_replace('CHECK (value', 'CHECK ([[col' . $i . ']]', $column);
43
            }
44
        }
45
46
        $db->createCommand($qb->createTable('column_type_table', $columns))->execute();
47
48
        $this->assertNotEmpty($db->getTableSchema('column_type_table', true));
49
    }
50
51
    public function testGetColumnType(): void
52
    {
53
        $db = $this->getConnection();
54
55
        $qb = $db->getQueryBuilder();
56
        $columnTypes = (new ColumnTypes($db))->getColumnTypes();
57
58
        foreach ($columnTypes as $item) {
59
            [$column, $builder, $expected] = $item;
60
61
            $driverName = $db->getDriver()->getDriverName();
62
            if (isset($item[3][$driverName])) {
63
                $expectedColumnSchemaBuilder = $item[3][$driverName];
64
            } elseif (isset($item[3]) && !is_array($item[3])) {
65
                $expectedColumnSchemaBuilder = $item[3];
66
            } else {
67
                $expectedColumnSchemaBuilder = $column;
68
            }
69
70
            $this->assertEquals($expectedColumnSchemaBuilder, $builder->__toString());
71
            $this->assertEquals($expected, $qb->getColumnType($column));
72
            $this->assertEquals($expected, $qb->getColumnType($builder));
73
        }
74
    }
75
}
76