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

php$0 ➔ testCustomConnection()   A

Complexity

Conditions 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
rs 9.6333
cc 1
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
        $container->registerExtension(new DoctrineMigrationsExtension());
33
        $container->setAlias('doctrine.migrations.configuration.test', new Alias('doctrine.migrations.configuration', true));
34
35
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Fixtures'));
36
        $loader->load('conf.xml');
37
38
        $container->compile();
39
40
        $config = $container->get('doctrine.migrations.configuration.test');
41
        $this->assertConfigs($config);
42
    }
43
44
    public function testFullConfig() : void
45
    {
46
        $container = $this->getContainer();
47
        $extension = new DoctrineMigrationsExtension();
48
49
        $config = [
50
            'name' => 'Doctrine Sandbox Migrations',
51
            'storage' => [
52
                'table_storage' => [
53
                    'table_name'                 => 'doctrine_migration_versions_test',
54
                    'version_column_name'        => 'doctrine_migration_column_test',
55
                    'version_column_length'      => 2000,
56
                    'executed_at_column_name'    => 'doctrine_migration_executed_at_column_test',
57
                    'execution_time_column_name' => 'doctrine_migration_execution_time_column_test',
58
                ],
59
            ],
60
61
            'migrations_paths' => [
62
                'DoctrineMigrationsTest' => 'a',
63
                'DoctrineMigrationsTest2' => 'b',
64
            ],
65
66
            'migrations' => ['Foo', 'Bar'],
67
68
            'organize_migrations' => 'BY_YEAR_AND_MONTH',
69
70
            'all_or_nothing'            => true,
71
            'check_database_platform'   => true,
72
        ];
73
74
        $conn = $this->createMock(Connection::class);
75
        $container->set('doctrine.dbal.default_connection', $conn);
76
77
        $extension->load(['doctrine_migrations' => $config], $container);
78
79
        $container->getDefinition('doctrine.migrations.configuration')->setPublic(true);
80
        $container->compile();
81
82
        $config = $container->get('doctrine.migrations.configuration');
83
84
        $this->assertConfigs($config);
85
    }
86
87
    private function assertConfigs(?object $config) : void
88
    {
89
        self::assertInstanceOf(Configuration::class, $config);
90
        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

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