generateIdentifierNameProvider()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 54
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 54
rs 9.6716
cc 1
eloc 37
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace RDV\Bundle\MigrationBundle\Tests\Unit\Tools;
4
5
use Doctrine\DBAL\Schema\Column;
6
use Doctrine\DBAL\Schema\Table;
7
use Doctrine\DBAL\Types\Type;
8
use RDV\Bundle\MigrationBundle\Tools\DbIdentifierNameGenerator;
9
10
class DbIdentifierNameGeneratorTest extends \PHPUnit_Framework_TestCase
11
{
12
    /**
13
     * @dataProvider generateIndexNameProvider
14
     */
15
    public function testGenerateIndexName($tableName, $columnNames, $uniqueIndex, $expectedName)
16
    {
17
        $generator = new DbIdentifierNameGenerator();
18
        $result = $generator->generateIndexName($tableName, $columnNames, $uniqueIndex);
19
        $this->assertEquals($expectedName, $result);
20
    }
21
22 View Code Duplication
    public function testEncodedIndexNameIsTheSameAsDoctrineDefault()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
    {
24
        $tableName  = 'tbl123456789012345';
25
        $columnName = 'clmn1234567890';
26
27
        $table = new Table($tableName, [new Column($columnName, Type::getType('string'))]);
28
        $table->addIndex([$columnName]);
29
        $indices = $table->getIndexes();
30
        $doctrineResult = array_pop($indices)->getName();
31
32
        $generator = new DbIdentifierNameGenerator();
33
        $result = $generator->generateIndexName($tableName, [$columnName]);
34
35
        $this->assertEquals($doctrineResult, $result);
36
    }
37
38 View Code Duplication
    public function testEncodedUniqueIndexNameIsTheSameAsDoctrineDefault()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
    {
40
        $tableName  = 'tbl123456789012345';
41
        $columnName = 'clmn1234567890';
42
43
        $table = new Table($tableName, [new Column($columnName, Type::getType('string'))]);
44
        $table->addUniqueIndex([$columnName]);
45
        $indices = $table->getIndexes();
46
        $doctrineResult = array_pop($indices)->getName();
47
48
        $generator = new DbIdentifierNameGenerator();
49
        $result = $generator->generateIndexName($tableName, [$columnName], true);
50
51
        $this->assertEquals($doctrineResult, $result);
52
    }
53
54
    /**
55
     * @dataProvider generateForeignKeyConstraintNameProvider
56
     */
57
    public function testGenerateForeignKeyConstraintName(
58
        $tableName,
59
        $columnNames,
60
        $expectedName
61
    ) {
62
        $generator = new DbIdentifierNameGenerator();
63
        $result = $generator->generateForeignKeyConstraintName($tableName, $columnNames);
64
        $this->assertEquals($expectedName, $result);
65
    }
66
67
    public function testEncodedForeignKeyConstraintNameIsTheSameAsDoctrineDefault()
68
    {
69
        $tableName1  = 'tbl123456789012345';
70
        $columnName1 = 'clmn1234567890';
71
72
        $tableName2  = 'tbl1234567890';
73
        $columnName2 = 'clmn12345';
74
75
        $table1 = new Table($tableName1, [new Column($columnName1, Type::getType('integer'))]);
76
        $table2 = new Table($tableName2, [new Column($columnName2, Type::getType('integer'))]);
77
        $table2->setPrimaryKey([$columnName2]);
78
79
        $table1->addForeignKeyConstraint($table2, [$columnName1], [$columnName2]);
80
81
        $foreignKeys = $table1->getForeignKeys();
82
        $doctrineResult = array_pop($foreignKeys)->getName();
83
84
        $generator = new DbIdentifierNameGenerator();
85
        $result = $generator->generateForeignKeyConstraintName($tableName1, [$columnName1]);
86
87
        $this->assertEquals($doctrineResult, $result);
88
    }
89
90
    /**
91
     * @dataProvider generateIdentifierNameProvider
92
     */
93
    public function testGenerateIdentifierName($tableNames, $columnNames, $prefix, $expectedName, $upperCase)
94
    {
95
        $generator = new DbIdentifierNameGenerator();
96
        $result = $generator->generateIdentifierName($tableNames, $columnNames, $prefix, $upperCase);
97
        $this->assertEquals($expectedName, $result);
98
    }
99
100
    /**
101
     * @dataProvider emptyTableNameProvider
102
     * @expectedException \InvalidArgumentException
103
     * @expectedExceptionMessage A table name must not be empty.
104
     */
105
    public function testGenerateIdentifierNameWithEmptyTableName($tableNames)
106
    {
107
        $generator = new DbIdentifierNameGenerator();
108
        $generator->generateIdentifierName($tableNames, ['test'], 'test');
109
    }
110
111
    public function generateIndexNameProvider()
112
    {
113
        return [
114
            ['table1', ['column1'], false, 'idx_table1_column1'],
115
            ['table1', ['column1'], true, 'uniq_table1_column1'],
116
            ['table1', ['column1', 'column2'], false, 'idx_table1_column1_column2'],
117
            ['table1', ['column1', 'column2'], true, 'uniq_table1_column1_column2'],
118
            ['table1', ['column1', 'column2', 'column3'], false, 'IDX_1C95229D341CE00BAD15B1B1DA'],
119
            ['table1', ['column1', 'column2', 'column3'], true, 'UNIQ_1C95229D341CE00BAD15B1B1D'],
120
        ];
121
    }
122
123
    public function generateForeignKeyConstraintNameProvider()
124
    {
125
        return [
126
            ['table1', ['clmn1'], 'fk_table1_clmn1'],
127
            ['table1', ['column123456789012346'], 'FK_1C95229DCB68A266'],
128
            ['table1', ['c1', 'c2'], 'fk_table1_c1_c2'],
129
            ['table1', ['column1', 'column2', 'column3'], 'FK_1C95229D341CE00BAD15B1B1DA1'],
130
        ];
131
    }
132
133
    public function generateIdentifierNameProvider()
134
    {
135
        return [
136
            [
137
                ['table1'],
138
                ['column1'],
139
                'IDX',
140
                'idx_table1_column1',
141
                null
142
            ],
143
            [
144
                ['table1', 'table2'],
145
                ['column1'],
146
                'FK',
147
                'fk_table1_table2_column1',
148
                null
149
            ],
150
            [
151
                ['table1', 'table2'],
152
                ['column1'],
153
                'p1234567',
154
                'p1234567_table1_table2_column1',
155
                null
156
            ],
157
            [
158
                ['table1', 'table2'],
159
                ['column1'],
160
                'p12345678',
161
                'P12345678_1C95229D859C7327341C',
162
                null
163
            ],
164
            [
165
                ['table1'],
166
                ['column1', 'column2'],
167
                'IDX',
168
                'idx_table1_column1_column2',
169
                null
170
            ],
171
            [
172
                ['table1', 'table2'],
173
                ['column1', 'column2'],
174
                'system',
175
                'system_1c95229d859c7327341ce00',
176
                false
177
            ],
178
            [
179
                ['table1'],
180
                ['column1', 'column2', 'column3', 'column3'],
181
                'IDX',
182
                'IDX_1C95229D341CE00BAD15B1B1DA',
183
                null
184
            ],
185
        ];
186
    }
187
188
    public function emptyTableNameProvider()
189
    {
190
        return [
191
            [null],
192
            [''],
193
            [[]],
194
            [['']],
195
        ];
196
    }
197
}
198