Passed
Pull Request — master (#372)
by Wilmer
03:17 queued 44s
created

AbstractColumnSchemaTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 60
c 1
b 0
f 0
dl 0
loc 87
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testColumnSchemaDbTypecastWithEmptyCharType() 0 6 1
B testColumnSchema() 0 77 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Tests;
6
7
use PHPUnit\Framework\TestCase;
8
use Yiisoft\Db\Schema\ColumnSchema;
9
use Yiisoft\Db\Schema\Schema;
10
11
use function is_object;
12
13
abstract class AbstractColumnSchemaTest extends TestCase
14
{
15
    public function testColumnSchemaDbTypecastWithEmptyCharType(): void
16
    {
17
        $columnSchema = new ColumnSchema();
18
        $columnSchema->setType(Schema::TYPE_CHAR);
19
20
        $this->assertSame('', $columnSchema->dbTypecast(''));
21
    }
22
23
    public function testColumnSchema(): void
24
    {
25
        $columns = $this->getExpectedColumns();
0 ignored issues
show
Bug introduced by
The method getExpectedColumns() does not exist on Yiisoft\Db\Tests\AbstractColumnSchemaTest. Did you maybe mean getExpectedException()? ( Ignorable by Annotation )

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

25
        /** @scrutinizer ignore-call */ 
26
        $columns = $this->getExpectedColumns();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
26
27
        $db = $this->getConnection();
0 ignored issues
show
Bug introduced by
The method getConnection() does not exist on Yiisoft\Db\Tests\AbstractColumnSchemaTest. ( Ignorable by Annotation )

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

27
        /** @scrutinizer ignore-call */ 
28
        $db = $this->getConnection();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
28
29
        $schema = $db->getSchema();
30
        $table = $schema->getTableSchema('type', true);
31
32
        $this->assertNotNull($table);
33
34
        $expectedColNames = array_keys($columns);
35
        sort($expectedColNames);
36
        $colNames = $table->getColumnNames();
37
        sort($colNames);
38
39
        $this->assertSame($expectedColNames, $colNames);
40
41
        foreach ($table->getColumns() as $name => $column) {
42
            $expected = $columns[$name];
43
            $this->assertSame(
44
                $expected['dbType'],
45
                $column->getDbType(),
46
                "dbType of column $name does not match. type is {$column->getType()}, dbType is {$column->getDbType()}."
47
            );
48
            $this->assertSame(
49
                $expected['phpType'],
50
                $column->getPhpType(),
51
                "phpType of column $name does not match. type is {$column->getType()}, dbType is {$column->getDbType()}."
52
            );
53
            $this->assertSame($expected['type'], $column->getType(), "type of column $name does not match.");
54
            $this->assertSame(
55
                $expected['allowNull'],
56
                $column->isAllowNull(),
57
                "allowNull of column $name does not match."
58
            );
59
            $this->assertSame(
60
                $expected['autoIncrement'],
61
                $column->isAutoIncrement(),
62
                "autoIncrement of column $name does not match."
63
            );
64
            $this->assertSame(
65
                $expected['enumValues'],
66
                $column->getEnumValues(),
67
                "enumValues of column $name does not match."
68
            );
69
            $this->assertSame($expected['size'], $column->getSize(), "size of column $name does not match.");
70
            $this->assertSame(
71
                $expected['precision'],
72
                $column->getPrecision(),
73
                "precision of column $name does not match."
74
            );
75
            $this->assertSame($expected['scale'], $column->getScale(), "scale of column $name does not match.");
76
            if (is_object($expected['defaultValue'])) {
77
                $this->assertIsObject(
78
                    $column->getDefaultValue(),
79
                    "defaultValue of column $name is expected to be an object but it is not."
80
                );
81
                $this->assertEquals(
82
                    (string) $expected['defaultValue'],
83
                    (string) $column->getDefaultValue(),
84
                    "defaultValue of column $name does not match."
85
                );
86
            } else {
87
                $this->assertEquals(
88
                    $expected['defaultValue'],
89
                    $column->getDefaultValue(),
90
                    "defaultValue of column $name does not match."
91
                );
92
            }
93
            /* Pgsql only */
94
            if (isset($expected['dimension'])) {
95
                /** @psalm-suppress UndefinedMethod */
96
                $this->assertSame(
97
                    $expected['dimension'],
98
                    $column->getDimension(),
99
                    "dimension of column $name does not match"
100
                );
101
            }
102
        }
103
    }
104
}
105