MigrationExecutorWithNameGeneratorTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 136
Duplicated Lines 37.5 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 11
dl 51
loc 136
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 10 1
B testExecuteUp() 0 36 1
B testExecuteUpWithDryRun() 0 26 1
B testWrongTableNameQuery() 25 25 1
B testWrongColumnNameQuery() 26 26 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;
4
5
use RDV\Bundle\MigrationBundle\Migration\MigrationState;
6
use RDV\Bundle\MigrationBundle\Tests\Unit\Fixture\TestPackage\Test1Bundle\Migrations\Schema\v1_0\Test1BundleMigration10;
7
use RDV\Bundle\MigrationBundle\Tests\Unit\Fixture\TestPackage\Test1Bundle\Migrations\Schema\v1_1\Test1BundleMigration11;
8
9
use RDV\Bundle\MigrationBundle\Migration\MigrationExecutorWithNameGenerator;
10
use RDV\Bundle\MigrationBundle\Tools\DbIdentifierNameGenerator;
11
use RDV\Bundle\MigrationBundle\Tests\Unit\Fixture\TestPackage\WrongTableNameMigration;
12
use RDV\Bundle\MigrationBundle\Tests\Unit\Fixture\TestPackage\WrongColumnNameMigration;
13
14
class MigrationExecutorWithNameGeneratorTest extends AbstractTestMigrationExecutor
15
{
16
    /** @var MigrationExecutorWithNameGenerator */
17
    protected $executor;
18
19
    /** @var DbIdentifierNameGenerator */
20
    protected $nameGenerator;
21
22
    protected function setUp()
23
    {
24
        parent::setUp();
25
26
        $this->nameGenerator = new DbIdentifierNameGenerator();
27
28
        $this->executor = new MigrationExecutorWithNameGenerator($this->queryExecutor);
29
        $this->executor->setLogger($this->logger);
30
        $this->executor->setNameGenerator($this->nameGenerator);
31
    }
32
33
    public function testExecuteUp()
34
    {
35
        $migration10 = new Test1BundleMigration10();
36
        $migration11 = new Test1BundleMigration11();
37
        $migrations = [
38
            new MigrationState($migration10),
39
            new MigrationState($migration11)
40
        ];
41
42
        $this->connection->expects($this->at(2))
43
            ->method('executeQuery')
44
            ->with('CREATE TABLE TEST (id INT AUTO_INCREMENT NOT NULL)');
45
        $this->connection->expects($this->at(3))
46
            ->method('executeQuery')
47
            ->with(
48
                'CREATE TABLE test1table (id INT NOT NULL) DEFAULT CHARACTER SET utf8 '
49
                . 'COLLATE utf8_unicode_ci ENGINE = InnoDB'
50
            );
51
        $this->connection->expects($this->at(4))
52
            ->method('executeQuery')
53
            ->with('ALTER TABLE TEST ADD COLUMN test_column INT NOT NULL');
54
55
        $this->executor->executeUp($migrations);
56
        $messages = $this->logger->getMessages();
57
        $this->assertEquals(
58
            [
59
                '> ' . get_class($migration10),
60
                'CREATE TABLE TEST (id INT AUTO_INCREMENT NOT NULL)',
61
                '> ' . get_class($migration11),
62
                'CREATE TABLE test1table (id INT NOT NULL) DEFAULT CHARACTER SET utf8 '
63
                . 'COLLATE utf8_unicode_ci ENGINE = InnoDB',
64
                'ALTER TABLE TEST ADD COLUMN test_column INT NOT NULL',
65
            ],
66
            $messages
67
        );
68
    }
69
70
    public function testExecuteUpWithDryRun()
71
    {
72
        $migration10 = new Test1BundleMigration10();
73
        $migration11 = new Test1BundleMigration11();
74
        $migrations = [
75
            new MigrationState($migration10),
76
            new MigrationState($migration11)
77
        ];
78
79
        $this->connection->expects($this->never())
80
            ->method('executeQuery');
81
82
        $this->executor->executeUp($migrations, true);
83
        $messages = $this->logger->getMessages();
84
        $this->assertEquals(
85
            [
86
                '> ' . get_class($migration10),
87
                'CREATE TABLE TEST (id INT AUTO_INCREMENT NOT NULL)',
88
                '> ' . get_class($migration11),
89
                'CREATE TABLE test1table (id INT NOT NULL) DEFAULT CHARACTER SET utf8 '
90
                . 'COLLATE utf8_unicode_ci ENGINE = InnoDB',
91
                'ALTER TABLE TEST ADD COLUMN test_column INT NOT NULL',
92
            ],
93
            $messages
94
        );
95
    }
96
97 View Code Duplication
    public function testWrongTableNameQuery()
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...
98
    {
99
        $migration = new WrongTableNameMigration();
100
        $migrations = [
101
            new MigrationState($migration)
102
        ];
103
        $this->setExpectedException(
104
            '\RuntimeException',
105
            'Failed migrations: RDV\Bundle\MigrationBundle\Tests\Unit\Fixture\TestPackage\WrongTableNameMigration.'
106
        );
107
        $this->executor->executeUp($migrations);
108
        $this->assertEquals(
109
            '> RDV\Bundle\MigrationBundle\Tests\Unit\Fixture\TestPackage\WrongTableNameMigration',
110
            $this->logger->getMessages()[0]
111
        );
112
        $this->assertEquals(
113
            sprintf(
114
                '  ERROR: Max table name length is %s. Please correct "%s" table in "%s" migration',
115
                $this->nameGenerator->getMaxIdentifierSize(),
116
                'extra_long_table_name_bigger_than_30_chars',
117
                get_class($migration)
118
            ),
119
            $this->logger->getMessages()[1]
120
        );
121
    }
122
123 View Code Duplication
    public function testWrongColumnNameQuery()
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...
124
    {
125
        $migration = new WrongColumnNameMigration();
126
        $migrations = [
127
            new MigrationState($migration)
128
        ];
129
        $this->setExpectedException(
130
            '\RuntimeException',
131
            'Failed migrations: RDV\Bundle\MigrationBundle\Tests\Unit\Fixture\TestPackage\WrongColumnNameMigration.'
132
        );
133
        $this->executor->executeUp($migrations);
134
        $this->assertEquals(
135
            '> RDV\Bundle\MigrationBundle\Tests\Unit\Fixture\TestPackage\WrongColumnNameMigration',
136
            $this->logger->getMessages()[0]
137
        );
138
        $this->assertEquals(
139
            sprintf(
140
                '  ERROR: Max column name length is %s. Please correct "%s:%s" column in "%s" migration',
141
                $this->nameGenerator->getMaxIdentifierSize(),
142
                'wrong_table',
143
                'extra_long_column_bigger_30_chars',
144
                get_class($migration)
145
            ),
146
            $this->logger->getMessages()[1]
147
        );
148
    }
149
}
150