Passed
Pull Request — master (#163)
by Wilmer
10:25
created

getColumnSchemaBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\TestUtility;
6
7
use Yiisoft\Db\Expression\Expression;
8
use Yiisoft\Db\Schema\ColumnSchemaBuilder;
9
use Yiisoft\Db\Schema\Schema;
10
11
use function array_shift;
12
use function call_user_func_array;
13
14
trait TestColumnSchemaBuilderTrait
15
{
16
    /**
17
     * @param string $type
18
     * @param int|null $length
19
     *
20
     * @return ColumnSchemaBuilder
21
     */
22
    public function getColumnSchemaBuilder(string $type, ?int $length = null): ColumnSchemaBuilder
23
    {
24
        return new ColumnSchemaBuilder($type, $length, $this->getConnection());
0 ignored issues
show
Bug introduced by
It seems like getConnection() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

24
        return new ColumnSchemaBuilder($type, $length, $this->/** @scrutinizer ignore-call */ getConnection());
Loading history...
25
    }
26
27
    public function typesProvider(): array
28
    {
29
        return [
30
            ['integer NULL DEFAULT NULL', Schema::TYPE_INTEGER, null, [
31
                ['unsigned'], ['null'],
32
            ]],
33
            ['integer(10)', Schema::TYPE_INTEGER, 10, [
34
                ['unsigned'],
35
            ]],
36
            ['timestamp() WITH TIME ZONE NOT NULL', 'timestamp() WITH TIME ZONE', null, [
37
                ['notNull'],
38
            ]],
39
            ['timestamp() WITH TIME ZONE DEFAULT NOW()', 'timestamp() WITH TIME ZONE', null, [
40
                ['defaultValue', new Expression('NOW()')],
41
            ]],
42
            ['integer(10)', Schema::TYPE_INTEGER, 10, [
43
                ['comment', 'test'],
44
            ]],
45
        ];
46
    }
47
48
    /**
49
     * @dataProvider typesProvider
50
     *
51
     * @param string $expected
52
     * @param string $type
53
     * @param int|null $length
54
     * @param mixed $calls
55
     */
56
    public function testCustomTypes(string $expected, string $type, ?int $length, $calls): void
57
    {
58
        $this->checkBuildString($expected, $type, $length, $calls);
59
    }
60
61
    /**
62
     * @param string $expected
63
     * @param string $type
64
     * @param int|null $length
65
     * @param array $calls
66
     */
67
    public function checkBuildString(string $expected, string $type, ?int $length, $calls): void
68
    {
69
        $builder = $this->getColumnSchemaBuilder($type, $length);
70
71
        foreach ($calls as $call) {
72
            $method = array_shift($call);
73
            call_user_func_array([$builder, $method], $call);
74
        }
75
76
        self::assertEquals($expected, $builder->__toString());
77
    }
78
}
79