UpdateBundleVersionMigrationTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
B testUp() 0 25 3
A upProvider() 0 49 1
A getMigration() 0 15 3
1
<?php
2
3
namespace RDV\Bundle\MigrationBundle\Tests\Unit\Migration;
4
5
use Doctrine\DBAL\Schema\Schema;
6
use RDV\Bundle\MigrationBundle\Migration\MigrationState;
7
use RDV\Bundle\MigrationBundle\Migration\QueryBag;
8
use RDV\Bundle\MigrationBundle\Migration\Tables;
9
use RDV\Bundle\MigrationBundle\Migration\UpdateBundleVersionMigration;
10
11
class UpdateBundleVersionMigrationTest extends \PHPUnit_Framework_TestCase
12
{
13
    /**
14
     * @dataProvider upProvider
15
     */
16
    public function testUp(array $migrations, array $expectedUpdates)
17
    {
18
        $queryBag        = new QueryBag();
19
        $updateMigration = new UpdateBundleVersionMigration($migrations);
20
        $updateMigration->up(new Schema(), $queryBag);
21
22
        $assertQueries = [];
23
        foreach ($expectedUpdates as $bundleName => $version) {
24
            $assertQueries[] = sprintf(
25
                "INSERT INTO %s (bundle, version, loaded_at) VALUES ('%s', '%s',",
26
                Tables::MIGRATION_TABLE,
27
                $bundleName,
28
                $version
29
            );
30
        }
31
32
        $this->assertEmpty($queryBag->getPreQueries());
33
        $postSqls = $queryBag->getPostQueries();
34
        foreach ($assertQueries as $index => $query) {
35
            $this->assertTrue(
36
                strpos($postSqls[$index], $query) === 0,
37
                sprintf('Query index: %d. Query: %s', $index, $query)
38
            );
39
        }
40
    }
41
42
    public function upProvider()
43
    {
44
        return [
45
            'all success'           => [
46
                'migrations'      => [
47
                    $this->getMigration('testBundle', 'v1_0'),
48
                    $this->getMigration('testBundle', 'v1_1'),
49
                    $this->getMigration('test1Bundle', 'v1_0'),
50
                ],
51
                'expectedUpdates' => [
52
                    'testBundle'  => 'v1_1',
53
                    'test1Bundle' => 'v1_0'
54
                ]
55
            ],
56
            'first version failed'  => [
57
                'migrations'      => [
58
                    $this->getMigration('testBundle', 'v1_0', false),
59
                    $this->getMigration('testBundle', 'v1_1', null),
60
                    $this->getMigration('test1Bundle', 'v1_0'),
61
                ],
62
                'expectedUpdates' => [
63
                    'test1Bundle' => 'v1_0'
64
                ]
65
            ],
66
            'last version failed'   => [
67
                'migrations'      => [
68
                    $this->getMigration('testBundle', 'v1_0'),
69
                    $this->getMigration('testBundle', 'v1_1', false),
70
                    $this->getMigration('test1Bundle', 'v1_0'),
71
                ],
72
                'expectedUpdates' => [
73
                    'testBundle'  => 'v1_0',
74
                    'test1Bundle' => 'v1_0'
75
                ]
76
            ],
77
            'middle version failed' => [
78
                'migrations'      => [
79
                    $this->getMigration('testBundle', 'v1_0'),
80
                    $this->getMigration('testBundle', 'v1_1', false),
81
                    $this->getMigration('testBundle', 'v1_2', null),
82
                    $this->getMigration('test1Bundle', 'v1_0'),
83
                ],
84
                'expectedUpdates' => [
85
                    'testBundle'  => 'v1_0',
86
                    'test1Bundle' => 'v1_0'
87
                ]
88
            ],
89
        ];
90
    }
91
92
    /**
93
     * @param string    $bundleName
94
     * @param string    $version
95
     * @param bool|null $state
96
     *
97
     * @return MigrationState
98
     */
99
    protected function getMigration($bundleName, $version, $state = true)
100
    {
101
        $migration = new MigrationState(
102
            $this->getMock('RDV\Bundle\MigrationBundle\Migration\Migration'),
103
            $bundleName,
104
            $version
105
        );
106
        if ($state === true) {
107
            $migration->setSuccessful();
108
        } elseif ($state === false) {
109
            $migration->setFailed();
110
        }
111
112
        return $migration;
113
    }
114
}
115