MigrationExecutorTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 9
dl 0
loc 101
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A testIndexesSuccessful() 0 8 1
B testIndexFailed() 0 25 2
B testUpdatedColumnIndexFailed() 0 31 2
A getTables() 0 17 1
1
<?php
2
3
namespace RDV\Bundle\MigrationBundle\Tests\Unit\Migration;
4
5
use Doctrine\DBAL\Schema\Table;
6
use Doctrine\DBAL\Schema\Column;
7
use Doctrine\DBAL\Types\Type;
8
9
use RDV\Bundle\MigrationBundle\Migration\MigrationExecutor;
10
use RDV\Bundle\MigrationBundle\Migration\MigrationState;
11
use RDV\Bundle\MigrationBundle\Tests\Unit\Fixture\TestPackage\IndexMigration;
12
use RDV\Bundle\MigrationBundle\Tests\Unit\Fixture\TestPackage\Test1Bundle\Migrations\Schema\Test1BundleInstallation;
13
14
class MigrationExecutorTest extends AbstractTestMigrationExecutor
15
{
16
    /** @var MigrationExecutor */
17
    protected $executor;
18
19
    protected function setUp()
20
    {
21
        parent::setUp();
22
23
        $this->executor = new MigrationExecutor($this->queryExecutor);
24
        $this->executor->setLogger($this->logger);
25
    }
26
27
    public function testIndexesSuccessful()
28
    {
29
        $migrations = [
30
            new MigrationState(new IndexMigration()),
31
        ];
32
33
        $this->executor->executeUp($migrations);
34
    }
35
36
    public function testIndexFailed()
37
    {
38
        $migrations = ['InvalidIndexMigration'];
39
        $migrationsToExecute = [];
40
        foreach ($migrations as $migration) {
41
            $migrationClass = 'RDV\\Bundle\\MigrationBundle\\Tests\\Unit\\Fixture\\TestPackage\\' . $migration;
42
            $migrationsToExecute[] = new MigrationState(new $migrationClass());
43
        }
44
45
        $this->setExpectedException(
46
            '\RuntimeException',
47
            'Failed migrations: RDV\Bundle\MigrationBundle\Tests\Unit\Fixture\TestPackage\InvalidIndexMigration.'
48
        );
49
        $this->executor->executeUp($migrationsToExecute);
50
        $this->assertEquals(
51
            '> RDV\Bundle\MigrationBundle\Tests\Unit\Fixture\TestPackage\InvalidIndexMigration',
52
            $this->logger->getMessages()[0]
53
        );
54
        $this->assertEquals(
55
            '  ERROR: Could not create index for column with length more than 255.'
56
            . ' Please correct "key" column length "index_table" in table in'
57
            . ' "RDV\Bundle\MigrationBundle\Tests\Unit\Fixture\TestPackage\InvalidIndexMigration" migration',
58
            $this->logger->getMessages()[1]
59
        );
60
    }
61
62
    public function testUpdatedColumnIndexFailed()
63
    {
64
        $migrations = ['IndexMigration', 'UpdatedColumnIndexMigration'];
65
        $migrationsToExecute = [];
66
        foreach ($migrations as $migration) {
67
            $migrationClass = 'RDV\\Bundle\\MigrationBundle\\Tests\\Unit\\Fixture\\TestPackage\\' . $migration;
68
            $migrationsToExecute[] = new MigrationState(new $migrationClass());
69
        }
70
        $migrationsToExecute[] = new MigrationState(new Test1BundleInstallation());
71
72
        $this->setExpectedException(
73
            '\RuntimeException',
74
            'Failed migrations: RDV\Bundle\MigrationBundle\Tests\Unit\Fixture\TestPackage\UpdatedColumnIndexMigration.'
75
        );
76
        $this->executor->executeUp($migrationsToExecute);
77
        $this->assertEquals(
78
            '> RDV\Bundle\MigrationBundle\Tests\Unit\Fixture\TestPackage\UpdatedColumnIndexMigration',
79
            $this->logger->getMessages()[2]
80
        );
81
        $this->assertEquals(
82
            '  ERROR: Could not create index for column with length more than 255.'
83
            . ' Please correct "key" column length "index_table2" in table in'
84
            . ' "RDV\Bundle\MigrationBundle\Tests\Unit\Fixture\TestPackage\UpdatedColumnIndexMigration" migration',
85
            $this->logger->getMessages()[3]
86
        );
87
        $this->assertEquals(
88
            '> RDV\Bundle\MigrationBundle\Tests\Unit\Fixture\TestPackage\Test1Bundle\Migrations\Schema'
89
            . '\Test1BundleInstallation - skipped',
90
            $this->logger->getMessages()[4]
91
        );
92
    }
93
94
    /**
95
     * @return Table[]
96
     */
97
    protected function getTables()
98
    {
99
        return [
100
            new Table(
101
                'index_table2',
102
                [
103
                    new Column(
104
                        'key',
105
                        Type::getType('string'),
106
                        [
107
                            'length' => 255
108
                        ]
109
                    )
110
                ]
111
            )
112
        ];
113
    }
114
}
115