Completed
Pull Request — master (#278)
by Asmir
02:56
created

ensionTest.php$0   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 3
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Bundle\MigrationsBundle\Tests\DependencyInjection;
6
7
use Doctrine\Bundle\MigrationsBundle\DependencyInjection\DoctrineMigrationsExtension;
8
use Doctrine\Bundle\MigrationsBundle\Tests\Fixtures\CustomEntityManager;
9
use Doctrine\DBAL\Connection;
10
use Doctrine\Migrations\Configuration\Configuration;
11
use Doctrine\Migrations\DependencyFactory;
12
use Doctrine\Migrations\Metadata\Storage\MetadataStorage;
13
use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration;
14
use InvalidArgumentException;
15
use PHPUnit\Framework\TestCase;
16
use Symfony\Component\Config\FileLocator;
17
use Symfony\Component\DependencyInjection\Alias;
18
use Symfony\Component\DependencyInjection\ContainerBuilder;
19
use Symfony\Component\DependencyInjection\Definition;
20
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
21
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
22
use function assert;
23
use function method_exists;
24
use function sys_get_temp_dir;
25
26
class DoctrineMigrationsExtensionTest extends TestCase
27
{
28
    public function testXmlConfigs() : void
29
    {
30
        $container = $this->getContainer();
31
32
        $conn = $this->createMock(Connection::class);
33
        $container->set('doctrine.dbal.default_connection', $conn);
34
35
        $container->registerExtension(new DoctrineMigrationsExtension());
36
        $container->setAlias('doctrine.migrations.configuration.test', new Alias('doctrine.migrations.configuration', true));
37
38
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Fixtures'));
39
        $loader->load('conf.xml');
40
41
        $container->compile();
42
43
        $config = $container->get('doctrine.migrations.configuration.test');
44
        $this->assertConfigs($config);
45
    }
46
47
    public function testFullConfig() : void
48
    {
49
        $container = $this->getContainer();
50
        $extension = new DoctrineMigrationsExtension();
51
52
        $config = [
53
            'name' => 'Doctrine Sandbox Migrations',
54
            'storage' => [
55
                'table_storage' => [
56
                    'table_name'                 => 'doctrine_migration_versions_test',
57
                    'version_column_name'        => 'doctrine_migration_column_test',
58
                    'version_column_length'      => 2000,
59
                    'executed_at_column_name'    => 'doctrine_migration_executed_at_column_test',
60
                    'execution_time_column_name' => 'doctrine_migration_execution_time_column_test',
61
                ],
62
            ],
63
64
            'migrations_paths' => [
65
                'DoctrineMigrationsTest' => 'a',
66
                'DoctrineMigrationsTest2' => 'b',
67
            ],
68
69
            'migrations' => ['Foo', 'Bar'],
70
71
            'organize_migrations' => 'BY_YEAR_AND_MONTH',
72
73
            'all_or_nothing'            => true,
74
            'check_database_platform'   => true,
75
        ];
76
77
        $conn = $this->createMock(Connection::class);
78
        $container->set('doctrine.dbal.default_connection', $conn);
79
80
        $extension->load(['doctrine_migrations' => $config], $container);
81
82
        $container->getDefinition('doctrine.migrations.configuration')->setPublic(true);
83
        $container->compile();
84
85
        $config = $container->get('doctrine.migrations.configuration');
86
87
        $this->assertConfigs($config);
88
    }
89
90
    private function assertConfigs(?object $config) : void
91
    {
92
        self::assertInstanceOf(Configuration::class, $config);
93
        self::assertSame('Doctrine Sandbox Migrations', $config->getName());
0 ignored issues
show
Bug introduced by
The method getName() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

93
        self::assertSame('Doctrine Sandbox Migrations', $config->/** @scrutinizer ignore-call */ getName());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
94
        self::assertSame([
95
            'DoctrineMigrationsTest' => 'a',
96
            'DoctrineMigrationsTest2' => 'b',
97
98
        ], $config->getMigrationDirectories());
99
100
        self::assertSame(['Foo', 'Bar'], $config->getMigrationClasses());
101
        self::assertTrue($config->isAllOrNothing());
102
        self::assertTrue($config->isDatabasePlatformChecked());
103
        self::assertTrue($config->areMigrationsOrganizedByYearAndMonth());
104
105
        $storage = $config->getMetadataStorageConfiguration();
106
        self::assertInstanceOf(TableMetadataStorageConfiguration::class, $storage);
107
108
        self::assertSame('doctrine_migration_versions_test', $storage->getTableName());
109
        self::assertSame('doctrine_migration_column_test', $storage->getVersionColumnName());
110
        self::assertSame(2000, $storage->getVersionColumnLength());
111
        self::assertSame('doctrine_migration_execution_time_column_test', $storage->getExecutionTimeColumnName());
112
        self::assertSame('doctrine_migration_executed_at_column_test', $storage->getExecutedAtColumnName());
113
    }
114
115
    public function testCustomSorter() : void
116
    {
117
        $container = $this->getContainer();
118
        $extension = new DoctrineMigrationsExtension();
119
120
        $config = ['sorter' => 'my_sorter'];
121
122
        $extension->load(['doctrine_migrations' => $config], $container);
123
124
        $container->getDefinition('doctrine.migrations.dependency_factory')->setPublic(true);
125
126
        $conn = $this->createMock(Connection::class);
127
        $container->set('doctrine.dbal.default_connection', $conn);
128
129
        $sorter = new class(){
130
            public function __invoke() : void
131
            {
132
            }
133
        };
134
        $container->set('my_sorter', $sorter);
135
136
        $container->compile();
137
138
        $di = $container->get('doctrine.migrations.dependency_factory');
139
        self::assertInstanceOf(DependencyFactory::class, $di);
140
        self::assertSame($sorter, $di->getSorter());
141
    }
142
143
    public function testCustomConnection() : void
144
    {
145
        $container = $this->getContainer();
146
        $extension = new DoctrineMigrationsExtension();
147
148
        $config = ['connection' => 'custom'];
149
150
        $extension->load(['doctrine_migrations' => $config], $container);
151
152
        $container->getDefinition('doctrine.migrations.dependency_factory')->setPublic(true);
153
154
        $conn = $this->createMock(Connection::class);
155
        $container->set('doctrine.dbal.custom_connection', $conn);
156
157
        $container->compile();
158
159
        $di = $container->get('doctrine.migrations.dependency_factory');
160
        self::assertInstanceOf(DependencyFactory::class, $di);
161
        self::assertSame($conn, $di->getConnection());
162
    }
163
164
    public function testCustomEntityManager() : void
165
    {
166
        $container = $this->getContainer();
167
        $extension = new DoctrineMigrationsExtension();
168
169
        $config = ['em' => 'custom'];
170
171
        $em = new Definition(CustomEntityManager::class);
172
        $container->setDefinition('doctrine.orm.custom_entity_manager', $em);
173
174
        $extension->load(['doctrine_migrations' => $config], $container);
175
176
        $container->getDefinition('doctrine.migrations.dependency_factory')->setPublic(true);
177
178
        $container->compile();
179
180
        $di = $container->get('doctrine.migrations.dependency_factory');
181
        self::assertInstanceOf(DependencyFactory::class, $di);
182
183
        $em = $di->getEntityManager();
184
        self::assertInstanceOf(CustomEntityManager::class, $em);
185
186
        assert(method_exists($di->getConnection(), 'getEm'));
187
        self::assertInstanceOf(CustomEntityManager::class, $di->getConnection()->getEm());
188
        self::assertSame($em, $di->getConnection()->getEm());
189
    }
190
191
    public function testCustomMetadataStorage() : void
192
    {
193
        $container = $this->getContainer();
194
        $extension = new DoctrineMigrationsExtension();
195
196
        $config = [
197
            'storage' => ['id' => 'mock_storage_service'],
198
        ];
199
200
        $mockStorage = $this->createMock(MetadataStorage::class);
201
        $container->set('mock_storage_service', $mockStorage);
202
203
        $conn = $this->createMock(Connection::class);
204
        $container->set('doctrine.dbal.default_connection', $conn);
205
206
        $extension->load(['doctrine_migrations' => $config], $container);
207
208
        $container->getDefinition('doctrine.migrations.dependency_factory')->setPublic(true);
209
210
        $container->compile();
211
212
        $di = $container->get('doctrine.migrations.dependency_factory');
213
        self::assertInstanceOf(DependencyFactory::class, $di);
214
        self::assertSame($mockStorage, $di->getMetadataStorage());
215
    }
216
217
    public function testCanNotSpecifyBothEmAndConnection() : void
218
    {
219
        $this->expectExceptionMessage('You can not specify both "connection" and "em" in the DoctrineMigrationsBundle configurations');
220
        $this->expectException(InvalidArgumentException::class);
221
        $container = $this->getContainer();
222
        $extension = new DoctrineMigrationsExtension();
223
224
        $config = [
225
            'em' => 'custom',
226
            'connection' => 'custom',
227
        ];
228
229
        $extension->load(['doctrine_migrations' => $config], $container);
230
231
        $container->getDefinition('doctrine.migrations.dependency_factory')->setPublic(true);
232
233
        $container->compile();
234
    }
235
236
    private function getContainer() : ContainerBuilder
237
    {
238
        return new ContainerBuilder(new ParameterBag([
239
            'kernel.debug' => false,
240
            'kernel.bundles' => [],
241
            'kernel.cache_dir' => sys_get_temp_dir(),
242
            'kernel.environment' => 'test',
243
            'kernel.project_dir' => __DIR__ . '/../',
244
        ]));
245
    }
246
}
247