MigrationsLoaderTest::getMigrationClasses()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 9
rs 9.6666
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace RDV\Bundle\MigrationBundle\Tests\Unit\Migration\Loader;
4
5
use RDV\Bundle\MigrationBundle\Event\MigrationEvents;
6
use RDV\Bundle\MigrationBundle\Event\PreMigrationEvent;
7
use RDV\Bundle\MigrationBundle\Migration\MigrationState;
8
use RDV\Bundle\MigrationBundle\Tests\Unit\Fixture\TestPackage\Test1Bundle\TestPackageTest1Bundle;
9
use RDV\Bundle\MigrationBundle\Tests\Unit\Fixture\TestPackage\Test2Bundle\TestPackageTest2Bundle;
10
11
use RDV\Bundle\MigrationBundle\Migration\Loader\MigrationsLoader;
12
13
class MigrationsLoaderTest extends \PHPUnit_Framework_TestCase
14
{
15
    /** @var MigrationsLoader */
16
    protected $loader;
17
18
    /** @var \PHPUnit_Framework_MockObject_MockObject */
19
    protected $kernel;
20
21
    /** @var \PHPUnit_Framework_MockObject_MockObject */
22
    protected $container;
23
24
    /** @var \PHPUnit_Framework_MockObject_MockObject */
25
    protected $eventDispatcher;
26
27
    /** @var \PHPUnit_Framework_MockObject_MockObject */
28
    protected $connection;
29
30 View Code Duplication
    protected function setUp()
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...
31
    {
32
        $this->kernel          = $this->getMockBuilder('Symfony\Component\HttpKernel\Kernel')
33
            ->disableOriginalConstructor()
34
            ->getMock();
35
        $this->container       = $this->getMockForAbstractClass(
36
            'Symfony\Component\DependencyInjection\ContainerInterface'
37
        );
38
        $this->eventDispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher')
39
            ->disableOriginalConstructor()
40
            ->getMock();
41
42
        $this->connection = $this->getMockBuilder('Doctrine\DBAL\Connection')
43
            ->disableOriginalConstructor()
44
            ->getMock();
45
46
        $this->loader = new MigrationsLoader(
47
            $this->kernel,
48
            $this->connection,
49
            $this->container,
50
            $this->eventDispatcher
51
        );
52
    }
53
54
    /**
55
     * @dataProvider getMigrationsProvider
56
     */
57
    public function testGetMigrations($bundles, $installed, $expectedMigrationClasses)
58
    {
59
        $bundlesList = [];
60
        /** @var \Symfony\Component\HttpKernel\Bundle\Bundle $bundle */
61
        foreach ($bundles as $bundle) {
62
            $bundlesList[$bundle->getName()] = $bundle;
63
        }
64
65
        $this->kernel->expects($this->any())
66
            ->method('getBundles')
67
            ->will($this->returnValue($bundlesList));
68
69
        $this->eventDispatcher->expects($this->exactly(2))
70
            ->method('dispatch')
71
            ->will(
72
                $this->returnCallback(
73
                    function ($eventName, $event) use (&$installed) {
74
                        if ($eventName === MigrationEvents::PRE_UP) {
75
                            if (null !== $installed) {
76
                                foreach ($installed as $val) {
77
                                    /** @var PreMigrationEvent $event */
78
                                    $event->setLoadedVersion($val['bundle'], $val['version']);
79
                                }
80
                            }
81
                        }
82
                    }
83
                )
84
            );
85
86
        $migrations       = $this->loader->getMigrations();
87
        $migrationClasses = $this->getMigrationClasses($migrations);
88
        $this->assertEquals($expectedMigrationClasses, $migrationClasses);
89
    }
90
91
    /**
92
     * @param MigrationState[] $migrations
93
     *
94
     * @return string[]
95
     */
96
    protected function getMigrationClasses(array $migrations)
97
    {
98
        return array_map(
99
            function ($migration) {
100
                return get_class($migration->getMigration());
101
            },
102
            $migrations
103
        );
104
    }
105
106
    /**
107
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
108
     */
109
    public function getMigrationsProvider()
110
    {
111
        $testPackage = 'RDV\\Bundle\\MigrationBundle\\Tests\\Unit\\Fixture\\TestPackage\\';
112
        $test1Bundle = $testPackage . 'Test1Bundle\\Migrations\\Schema';
113
        $test2Bundle = $testPackage . 'Test2Bundle\\Migrations\\Schema';
114
115
        return [
116
            [
117
                [new TestPackageTest1Bundle(), new TestPackageTest2Bundle()],
118
                null,
119
                [
120
                    $test1Bundle . '\Test1BundleInstallation',
121
                    $test1Bundle . '\v1_1\Test1BundleMigration11',
122
                    $test2Bundle . '\v1_0\Test2BundleMigration10',
123
                    $test2Bundle . '\v1_0\Test2BundleMigration11',
124
                    $test2Bundle . '\v1_1\Test2BundleMigration12',
125
                    $test2Bundle . '\v1_1\Test2BundleMigration11',
126
                    'RDV\Bundle\MigrationBundle\Migration\UpdateBundleVersionMigration',
127
                ]
128
            ],
129
            [
130
                [new TestPackageTest2Bundle(), new TestPackageTest1Bundle()],
131
                null,
132
                [
133
                    $test2Bundle . '\v1_0\Test2BundleMigration10',
134
                    $test2Bundle . '\v1_0\Test2BundleMigration11',
135
                    $test2Bundle . '\v1_1\Test2BundleMigration12',
136
                    $test2Bundle . '\v1_1\Test2BundleMigration11',
137
                    $test1Bundle . '\Test1BundleInstallation',
138
                    $test1Bundle . '\v1_1\Test1BundleMigration11',
139
                    'RDV\Bundle\MigrationBundle\Migration\UpdateBundleVersionMigration',
140
                ]
141
            ],
142
            [
143
                [new TestPackageTest1Bundle(), new TestPackageTest2Bundle()],
144
                [],
145
                [
146
                    $test1Bundle . '\Test1BundleInstallation',
147
                    $test1Bundle . '\v1_1\Test1BundleMigration11',
148
                    $test2Bundle . '\v1_0\Test2BundleMigration10',
149
                    $test2Bundle . '\v1_0\Test2BundleMigration11',
150
                    $test2Bundle . '\v1_1\Test2BundleMigration12',
151
                    $test2Bundle . '\v1_1\Test2BundleMigration11',
152
                    'RDV\Bundle\MigrationBundle\Migration\UpdateBundleVersionMigration',
153
                ]
154
            ],
155
            [
156
                [new TestPackageTest1Bundle(), new TestPackageTest2Bundle()],
157
                [
158
                    ['bundle' => 'TestPackageTest1Bundle', 'version' => null],
159
                ],
160
                [
161
                    $test1Bundle . '\Test1BundleInstallation',
162
                    $test1Bundle . '\v1_1\Test1BundleMigration11',
163
                    $test2Bundle . '\v1_0\Test2BundleMigration10',
164
                    $test2Bundle . '\v1_0\Test2BundleMigration11',
165
                    $test2Bundle . '\v1_1\Test2BundleMigration12',
166
                    $test2Bundle . '\v1_1\Test2BundleMigration11',
167
                    'RDV\Bundle\MigrationBundle\Migration\UpdateBundleVersionMigration',
168
                ]
169
            ],
170
            [
171
                [new TestPackageTest1Bundle(), new TestPackageTest2Bundle()],
172
                [
173
                    ['bundle' => 'TestPackageTest1Bundle', 'version' => 'v1_0'],
174
                ],
175
                [
176
                    $test1Bundle . '\v1_1\Test1BundleMigration11',
177
                    $test2Bundle . '\v1_0\Test2BundleMigration10',
178
                    $test2Bundle . '\v1_0\Test2BundleMigration11',
179
                    $test2Bundle . '\v1_1\Test2BundleMigration12',
180
                    $test2Bundle . '\v1_1\Test2BundleMigration11',
181
                    'RDV\Bundle\MigrationBundle\Migration\UpdateBundleVersionMigration',
182
                ]
183
            ],
184
            [
185
                [new TestPackageTest1Bundle(), new TestPackageTest2Bundle()],
186
                [
187
                    ['bundle' => 'TestPackageTest1Bundle', 'version' => 'v1_0'],
188
                    ['bundle' => 'TestPackageTest2Bundle', 'version' => 'v1_0'],
189
                ],
190
                [
191
                    $test1Bundle . '\v1_1\Test1BundleMigration11',
192
                    $test2Bundle . '\v1_1\Test2BundleMigration12',
193
                    $test2Bundle . '\v1_1\Test2BundleMigration11',
194
                    'RDV\Bundle\MigrationBundle\Migration\UpdateBundleVersionMigration',
195
                ]
196
            ],
197
            [
198
                [new TestPackageTest1Bundle(), new TestPackageTest2Bundle()],
199
                [
200
                    ['bundle' => 'TestPackageTest1Bundle', 'version' => 'v1_1'],
201
                    ['bundle' => 'TestPackageTest2Bundle', 'version' => 'v1_1'],
202
                ],
203
                [
204
                    'RDV\Bundle\MigrationBundle\Migration\UpdateBundleVersionMigration',
205
                ]
206
            ],
207
        ];
208
    }
209
}
210