ColumnAssertions::assertLaravelTypeColumn()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Umbrellio\Postgres\Helpers;
6
7
use Illuminate\Support\Facades\DB;
8
use Illuminate\Support\Facades\Schema;
9
10
trait ColumnAssertions
11
{
12
    abstract public static function assertNull($actual, string $message = ''): void;
13
14
    abstract public static function assertSame($expected, $actual, string $message = ''): void;
15
16 1
    protected function assertCommentOnColumn(string $table, string $column, ?string $expected = null): void
17
    {
18 1
        $comment = $this->getCommentListing($table, $column);
19
20 1
        if ($expected === null) {
21 1
            $this->assertNull($comment);
22
        }
23
24 1
        $this->assertSame($expected, $comment);
25
    }
26
27 1
    protected function assertDefaultOnColumn(string $table, string $column, ?string $expected = null): void
28
    {
29 1
        $defaultValue = $this->getDefaultListing($table, $column);
30
31 1
        if ($expected === null) {
32 1
            $this->assertNull($defaultValue);
33
        }
34
35 1
        $this->assertSame($expected, $defaultValue);
36
    }
37
38 1
    protected function assertLaravelTypeColumn(string $table, string $column, string $expected): void
39
    {
40 1
        $this->assertSame($expected, Schema::getColumnType($table, $column));
0 ignored issues
show
Bug introduced by
The method getColumnType() does not exist on Illuminate\Support\Facades\Schema. ( Ignorable by Annotation )

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

40
        $this->assertSame($expected, Schema::/** @scrutinizer ignore-call */ getColumnType($table, $column));

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...
41
    }
42
43 1
    protected function assertPostgresTypeColumn(string $table, string $column, string $expected): void
44
    {
45 1
        $this->assertSame($expected, $this->getTypeListing($table, $column));
46
    }
47
48 1
    private function getCommentListing(string $table, string $column)
49
    {
50 1
        $definition = DB::selectOne(
51 1
            '
52
                SELECT pgd.description
53
                FROM pg_catalog.pg_statio_all_tables AS st
54
                INNER JOIN pg_catalog.pg_description pgd ON (pgd.objoid = st.relid)
55
                INNER JOIN information_schema.columns c ON pgd.objsubid = c.ordinal_position
56
                   AND c.table_schema = st.schemaname AND c.table_name = st.relname
57
                WHERE c.table_name = ? AND c.column_name = ?
58 1
            ',
59 1
            [$table, $column]
60 1
        );
61
62 1
        return $definition ? $definition->description : null;
63
    }
64
65 1
    private function getTypeListing(string $table, string $column): ?string
66
    {
67 1
        $definition = DB::selectOne(
68 1
            '
69
                SELECT data_type
70
                FROM information_schema.columns
71
                WHERE table_name = ? AND column_name = ?
72 1
            ',
73 1
            [$table, $column]
74 1
        );
75
76 1
        return $definition ? $definition->data_type : null;
77
    }
78
79 1
    private function getDefaultListing(string $table, string $column)
80
    {
81 1
        $definition = DB::selectOne(
82 1
            '
83
                SELECT column_default
84
                FROM information_schema.columns c
85
                WHERE c.table_name = ? and c.column_name = ?
86 1
            ',
87 1
            [$table, $column]
88 1
        );
89
90 1
        return $definition ? $definition->column_default : null;
91
    }
92
}
93