Failed Conditions
Push — master ( 24dbc4...1eba78 )
by Sergei
31:31 queued 31:22
created

ColumnCommentTest::testColumnComment()   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
nc 1
nop 3
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Tests\Functional\Schema;
6
7
use Doctrine\DBAL\Schema\Comparator;
8
use Doctrine\DBAL\Schema\Table;
9
use Doctrine\DBAL\Tests\FunctionalTestCase;
10
use function array_merge;
11
use function sprintf;
12
13
class ColumnCommentTest extends FunctionalTestCase
14
{
15
    /** @var bool */
16
    private static $initialized = false;
17
18
    protected function setUp() : void
19
    {
20
        parent::setUp();
21
22
        if (self::$initialized) {
23
            return;
24
        }
25
26
        self::$initialized = true;
27
28
        $table = new Table('column_comments');
29
        $table->addColumn('id', 'integer');
30
31
        foreach (self::columnProvider() as [$name, $type, $options]) {
32
            $table->addColumn($name, $type, $options);
33
        }
34
35
        $this->connection->getSchemaManager()
36
            ->dropAndCreateTable($table);
37
    }
38
39
    /**
40
     * @param array<string,mixed> $options
41
     *
42
     * @dataProvider columnProvider
43
     */
44
    public function testColumnComment(string $name, string $type, array $options) : void
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

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

44
    public function testColumnComment(string $name, /** @scrutinizer ignore-unused */ string $type, array $options) : void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
45
    {
46
        $this->assertColumnComment('column_comments', $name, $options['comment'] ?? '');
47
    }
48
49
    /**
50
     * @return mixed[][]
51
     */
52
    public static function columnProvider() : iterable
53
    {
54
        foreach ([
55
            'commented' => [
56
                'string',
57
                ['length' => 16],
58
            ],
59
            'not_commented' => [
60
                'array',
61
                [],
62
            ],
63
        ] as $typeName => [$type, $typeOptions]) {
64
            foreach ([
65
                'no_comment' => [],
66
                'with_comment' => ['comment' => 'Some comment'],
67
                'zero_comment' => ['comment' => '0'],
68
                'empty_comment' => ['comment' => ''],
69
                'quoted_comment' => ['comment' => "O'Reilly"],
70
            ] as $caseName => $caseOptions) {
71
                $name = sprintf('%s_%s', $typeName, $caseName);
72
73
                yield $name => [
0 ignored issues
show
Bug Best Practice introduced by
The expression yield $name => array($na...Options, $caseOptions)) returns the type Generator which is incompatible with the documented return type array<mixed,array<mixed,mixed>>.
Loading history...
74
                    $name,
75
                    $type,
76
                    array_merge($typeOptions, $caseOptions),
77
                ];
78
            }
79
        }
80
    }
81
82
    /**
83
     * @dataProvider alterColumnCommentProvider
84
     */
85
    public function testAlterColumnComment(string $comment1, string $comment2) : void
86
    {
87
        $table1 = new Table('column_comments');
88
        $table1->addColumn('id', 'integer', ['comment' => $comment1]);
89
90
        $this->connection->getSchemaManager()
91
            ->dropAndCreateTable($table1);
92
93
        $table2 = clone $table1;
94
        $table2->getColumn('id')->setComment($comment2);
95
96
        $diff = (new Comparator())->diffTable($table1, $table2);
97
        self::assertNotNull($diff);
98
99
        $sm = $this->connection->getSchemaManager();
100
        $sm->alterTable($diff);
101
102
        $this->assertColumnComment('column_comments', 'id', $comment2);
103
    }
104
105
    /**
106
     * @return mixed[][]
107
     */
108
    public static function alterColumnCommentProvider() : iterable
109
    {
110
        return [
111
            'Empty to non-empty' => ['', 'foo'],
112
            'Non-empty to empty' => ['foo', ''],
113
            'Empty to zero' => ['', '0'],
114
            'Zero to empty' => ['0', ''],
115
            'Non-empty to non-empty' => ['foo', 'bar'],
116
        ];
117
    }
118
119
    private function assertColumnComment(string $table, string $column, string $expectedComment) : void
120
    {
121
        self::assertSame(
122
            $expectedComment,
123
            $this->connection->getSchemaManager()
124
                ->listTableDetails($table)
125
                ->getColumn($column)
126
                ->getComment()
127
        );
128
    }
129
}
130