RenameExtensionTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 199
Duplicated Lines 60.3 %

Coupling/Cohesion

Components 1
Dependencies 13

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 10
c 1
b 1
f 0
lcom 1
cbo 13
dl 120
loc 199
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A testRenameTable() 14 14 1
A testAddIndex() 15 15 1
A testUniqueAddIndex() 15 15 1
B testAddForeignKeyConstraint() 0 24 1
A renameTableProvider() 20 20 1
A renameColumnProvider() 9 9 1
A addIndexProvider() 9 9 1
A addUniqueIndexProvider() 12 12 1
B addForeignKeyConstraintProvider() 26 26 1
B testRenameColumn() 0 28 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace RDV\Bundle\MigrationBundle\Tests\Unit\Migration\Extension;
4
5
use Doctrine\DBAL\Platforms\MySqlPlatform;
6
use Doctrine\DBAL\Platforms\OraclePlatform;
7
use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
8
use Doctrine\DBAL\Platforms\SQLServer2005Platform;
9
use Doctrine\DBAL\Schema\Column;
10
use Doctrine\DBAL\Schema\Schema;
11
use Doctrine\DBAL\Schema\Table;
12
use Doctrine\DBAL\Types\Type;
13
use RDV\Bundle\MigrationBundle\Migration\Extension\RenameExtension;
14
use RDV\Bundle\MigrationBundle\Migration\MigrationQuery;
15
use RDV\Bundle\MigrationBundle\Migration\QueryBag;
16
use RDV\Bundle\MigrationBundle\Tools\DbIdentifierNameGenerator;
17
18
class RenameExtensionTest extends \PHPUnit_Framework_TestCase
19
{
20
    /**
21
     * @dataProvider renameTableProvider
22
     */
23 View Code Duplication
    public function testRenameTable($platform, $expectedSql)
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...
24
    {
25
        $extension = new RenameExtension();
26
        $extension->setDatabasePlatform($platform);
27
28
        $schema  = new Schema([new Table('old_table')]);
29
        $queries = new QueryBag();
30
31
        $extension->renameTable($schema, $queries, 'old_table', 'new_table');
32
        /** @var MigrationQuery $query */
33
        $query = $queries->getPostQueries()[0];
34
35
        $this->assertEquals($expectedSql, $query->getDescription());
36
    }
37
38
    /**
39
     * @dataProvider renameColumnProvider
40
     */
41
    public function testRenameColumn($platform, $expectedSql)
42
    {
43
        $extension = new RenameExtension();
44
        $extension->setDatabasePlatform($platform);
45
46
        $schema = new Schema(
47
            [
48
                new Table(
49
                    'test_table',
50
                    [
51
                        new Column(
52
                            'old_column',
53
                            Type::getType('string'),
54
                            ['length' => 100]
55
                        )
56
                    ]
57
                )
58
            ]
59
        );
60
        $queries = new QueryBag();
61
        $table = $schema->getTable('test_table');
62
63
        $extension->renameColumn($schema, $queries, $table, 'old_column', 'new_column');
64
        /** @var MigrationQuery $query */
65
        $query = $queries->getPostQueries()[0];
66
67
        $this->assertEquals($expectedSql, $query->getDescription());
68
    }
69
70
    /**
71
     * @dataProvider addIndexProvider
72
     */
73 View Code Duplication
    public function testAddIndex($platform, $expectedSql)
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...
74
    {
75
        $extension = new RenameExtension();
76
        $extension->setDatabasePlatform($platform);
77
        $extension->setNameGenerator(new DbIdentifierNameGenerator());
78
79
        $schema  = new Schema();
80
        $queries = new QueryBag();
81
82
        $extension->addIndex($schema, $queries, 'test_table', ['new_column']);
83
        /** @var MigrationQuery $query */
84
        $query = $queries->getPostQueries()[0];
85
86
        $this->assertEquals($expectedSql, $query->getDescription());
87
    }
88
89
    /**
90
     * @dataProvider addUniqueIndexProvider
91
     */
92 View Code Duplication
    public function testUniqueAddIndex($platform, $expectedSql)
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...
93
    {
94
        $extension = new RenameExtension();
95
        $extension->setDatabasePlatform($platform);
96
        $extension->setNameGenerator(new DbIdentifierNameGenerator());
97
98
        $schema  = new Schema();
99
        $queries = new QueryBag();
100
101
        $extension->addUniqueIndex($schema, $queries, 'test_table', ['new_column']);
102
        /** @var MigrationQuery $query */
103
        $query = $queries->getPostQueries()[0];
104
105
        $this->assertEquals($expectedSql, $query->getDescription());
106
    }
107
108
    /**
109
     * @dataProvider addForeignKeyConstraintProvider
110
     */
111
    public function testAddForeignKeyConstraint($platform, $expectedSql)
112
    {
113
        $extension = new RenameExtension();
114
        $extension->setDatabasePlatform($platform);
115
        $extension->setNameGenerator(new DbIdentifierNameGenerator());
116
117
        $schema  = new Schema();
118
        $queries = new QueryBag();
119
120
        $extension->addForeignKeyConstraint(
121
            $schema,
122
            $queries,
123
            'test_table',
124
            'foreign_table',
125
            ['local_column'],
126
            ['foreign_column'],
127
            ['onDelete' => 'CASCADE']
128
        );
129
130
        /** @var MigrationQuery $query */
131
        $query = $queries->getPostQueries()[0];
132
133
        $this->assertEquals($expectedSql, $query->getDescription());
134
    }
135
136 View Code Duplication
    public function renameTableProvider()
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...
137
    {
138
        return [
139
            [new MySqlPlatform(), 'ALTER TABLE old_table RENAME TO new_table'],
140
            [new PostgreSqlPlatform(), 'ALTER TABLE old_table RENAME TO new_table'],
141
            [new OraclePlatform(), 'ALTER TABLE old_table RENAME TO new_table'],
142
            [
143
                new SQLServer2005Platform(),
144
                [
145
                    "sp_RENAME 'old_table', 'new_table'",
146
                    "DECLARE @sql NVARCHAR(MAX) = N''; "
147
                    . "SELECT @sql += N'EXEC sp_rename N''' + dc.name + ''', N'''"
148
                    . " + REPLACE(dc.name, '50BD45A0', 'EBFCC9B') + ''', ''OBJECT'';' "
149
                    . "FROM sys.default_constraints dc JOIN sys.tables tbl ON dc.parent_object_id = tbl.object_id "
150
                    . "WHERE tbl.name = 'new_table';"
151
                    . "EXEC sp_executesql @sql"
152
                ]
153
            ],
154
        ];
155
    }
156
157 View Code Duplication
    public function renameColumnProvider()
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...
158
    {
159
        return [
160
            [new MySqlPlatform(), 'ALTER TABLE test_table CHANGE old_column new_column VARCHAR(100) NOT NULL'],
161
            [new PostgreSqlPlatform(), 'ALTER TABLE test_table RENAME COLUMN old_column TO new_column'],
162
            [new OraclePlatform(), 'ALTER TABLE test_table RENAME COLUMN old_column TO new_column'],
163
            [new SQLServer2005Platform(), "sp_RENAME 'test_table.old_column', 'new_column', 'COLUMN'",],
164
        ];
165
    }
166
167 View Code Duplication
    public function addIndexProvider()
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...
168
    {
169
        return [
170
            [new MySqlPlatform(), 'CREATE INDEX idx_test_table_new_column ON test_table (new_column)'],
171
            [new PostgreSqlPlatform(), 'CREATE INDEX idx_test_table_new_column ON test_table (new_column)'],
172
            [new OraclePlatform(), 'CREATE INDEX idx_test_table_new_column ON test_table (new_column)'],
173
            [new SQLServer2005Platform(), 'CREATE INDEX idx_test_table_new_column ON test_table (new_column)'],
174
        ];
175
    }
176
177 View Code Duplication
    public function addUniqueIndexProvider()
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...
178
    {
179
        return [
180
            [new MySqlPlatform(), 'CREATE UNIQUE INDEX uniq_test_table_new_column ON test_table (new_column)'],
181
            [new PostgreSqlPlatform(), 'CREATE UNIQUE INDEX uniq_test_table_new_column ON test_table (new_column)'],
182
            [new OraclePlatform(), 'CREATE UNIQUE INDEX uniq_test_table_new_column ON test_table (new_column)'],
183
            [
184
                new SQLServer2005Platform(),
185
                'CREATE UNIQUE INDEX uniq_test_table_new_column ON test_table (new_column) WHERE new_column IS NOT NULL'
186
            ],
187
        ];
188
    }
189
190 View Code Duplication
    public function addForeignKeyConstraintProvider()
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...
191
    {
192
        return [
193
            [
194
                new MySqlPlatform(),
195
                'ALTER TABLE test_table ADD CONSTRAINT fk_test_table_local_column '
196
                . 'FOREIGN KEY (local_column) REFERENCES foreign_table (foreign_column) ON DELETE CASCADE'
197
            ],
198
            [
199
                new PostgreSqlPlatform(),
200
                'ALTER TABLE test_table ADD CONSTRAINT fk_test_table_local_column '
201
                . 'FOREIGN KEY (local_column) REFERENCES foreign_table (foreign_column) ON DELETE CASCADE '
202
                . 'NOT DEFERRABLE INITIALLY IMMEDIATE'
203
            ],
204
            [
205
                new OraclePlatform(),
206
                'ALTER TABLE test_table ADD CONSTRAINT fk_test_table_local_column '
207
                . 'FOREIGN KEY (local_column) REFERENCES foreign_table (foreign_column) ON DELETE CASCADE'
208
            ],
209
            [
210
                new SQLServer2005Platform(),
211
                'ALTER TABLE test_table ADD CONSTRAINT fk_test_table_local_column '
212
                . 'FOREIGN KEY (local_column) REFERENCES foreign_table (foreign_column) ON DELETE CASCADE'
213
            ],
214
        ];
215
    }
216
}
217