Completed
Pull Request — master (#278)
by Asmir
05:07
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\TableMetadataStorageConfiguration;
13
use InvalidArgumentException;
14
use PHPUnit\Framework\TestCase;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Definition;
17
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
18
use function sys_get_temp_dir;
19
20
class DoctrineMigrationsExtensionTest extends TestCase
21
{
22
    public function testFullConfig() : void
23
    {
24
        $container = $this->getContainer();
25
        $extension = new DoctrineMigrationsExtension();
26
27
        $config = [
28
            'name' => 'Doctrine Sandbox Migrations',
29
            'storage' => [
30
                'table_storage' => [
31
                    'table_name'                 => 'doctrine_migration_versions_test',
32
                    'version_column_name'        => 'doctrine_migration_column_test',
33
                    'version_column_length'      => 2000,
34
                    'executed_at_column_name'    => 'doctrine_migration_executed_at_column_test',
35
                    'execution_time_column_name' => 'doctrine_migration_execution_time_column_test',
36
                ],
37
            ],
38
39
            'migrations_paths' => [
40
                'DoctrineMigrationsTest' => 'a',
41
                'DoctrineMigrationsTest2' => 'b',
42
            ],
43
44
            'migrations' => ['Foo', 'Bar'],
45
46
            'organize_migrations' => 'BY_YEAR_AND_MONTH',
47
48
            'all_or_nothing'            => true,
49
            'check_database_platform'   => true,
50
        ];
51
52
        $extension->load(['doctrine_migrations' => $config], $container);
53
54
        $container->getDefinition('doctrine.migrations.configuration')->setPublic(true);
55
        $container->compile();
56
        /**
57
         * @var $config Configuration
58
         */
59
        $config = $container->get('doctrine.migrations.configuration');
60
61
        self::isInstanceOf(Configuration::class, $config);
0 ignored issues
show
Unused Code introduced by
The call to PHPUnit\Framework\Assert::isInstanceOf() has too many arguments starting with $config. ( Ignorable by Annotation )

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

61
        self::/** @scrutinizer ignore-call */ 
62
              isInstanceOf(Configuration::class, $config);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
62
        self::assertSame('Doctrine Sandbox Migrations', $config->getName());
63
        self::assertSame([
64
            'DoctrineMigrationsTest' => 'a',
65
            'DoctrineMigrationsTest2' => 'b',
66
67
        ], $config->getMigrationDirectories());
68
69
        self::assertSame(['Foo', 'Bar'], $config->getMigrationClasses());
70
        self::assertSame(true, $config->isAllOrNothing());
71
        self::assertSame(true, $config->isDatabasePlatformChecked());
72
        self::assertSame(true, $config->areMigrationsOrganizedByYearAndMonth());
73
74
        $storage = $config->getMetadataStorageConfiguration();
75
        self::assertInstanceOf(TableMetadataStorageConfiguration::class, $storage);
76
77
        self::assertSame('doctrine_migration_versions_test', $storage->getTableName());
78
        self::assertSame('doctrine_migration_column_test', $storage->getVersionColumnName());
79
        self::assertSame(2000, $storage->getVersionColumnLength());
80
        self::assertSame('doctrine_migration_execution_time_column_test', $storage->getExecutionTimeColumnName());
81
        self::assertSame('doctrine_migration_executed_at_column_test', $storage->getExecutedAtColumnName());
82
    }
83
84
    public function testCustomSorter() : void
85
    {
86
        $container = $this->getContainer();
87
        $extension = new DoctrineMigrationsExtension();
88
89
        $config = [
90
            'migrations_paths' => ['DoctrineMigrationsTest' => 'a'],
91
92
            'sorter' => 'my_sorter',
93
94
        ];
95
96
        $extension->load(['doctrine_migrations' => $config], $container);
97
98
        $container->getDefinition('doctrine.migrations.di')->setPublic(true);
99
100
        $conn = $this->createMock(Connection::class);
101
        $container->set('doctrine.dbal.default_connection', $conn);
102
103
        $sorter = new class(){
104
            public function __invoke() : void
105
            {
106
            }
107
        };
108
        $container->set('my_sorter', $sorter);
109
110
        $container->compile();
111
        /**
112
         * @var $di DependencyFactory
113
         */
114
        $di = $container->get('doctrine.migrations.di');
115
116
        self::isInstanceOf(DependencyFactory::class, $di);
0 ignored issues
show
Unused Code introduced by
The call to PHPUnit\Framework\Assert::isInstanceOf() has too many arguments starting with $di. ( Ignorable by Annotation )

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

116
        self::/** @scrutinizer ignore-call */ 
117
              isInstanceOf(DependencyFactory::class, $di);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
117
        self::assertSame($sorter, $di->getSorter());
118
    }
119
120
    public function testCustomConnection() : void
121
    {
122
        $container = $this->getContainer();
123
        $extension = new DoctrineMigrationsExtension();
124
125
        $config = [
126
            'connection' => 'custom',
127
            'migrations_paths' => ['DoctrineMigrationsTest' => 'a'],
128
129
        ];
130
131
        $extension->load(['doctrine_migrations' => $config], $container);
132
133
        $container->getDefinition('doctrine.migrations.di')->setPublic(true);
134
135
        $conn = $this->createMock(Connection::class);
136
        $container->set('doctrine.dbal.custom_connection', $conn);
137
138
        $container->compile();
139
        /**
140
         * @var $di DependencyFactory
141
         */
142
        $di = $container->get('doctrine.migrations.di');
143
144
        self::isInstanceOf(DependencyFactory::class, $di);
0 ignored issues
show
Unused Code introduced by
The call to PHPUnit\Framework\Assert::isInstanceOf() has too many arguments starting with $di. ( Ignorable by Annotation )

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

144
        self::/** @scrutinizer ignore-call */ 
145
              isInstanceOf(DependencyFactory::class, $di);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
145
        self::assertSame($conn, $di->getConnection());
146
    }
147
148
    public function testCustomEntityManager() : void
149
    {
150
        $container = $this->getContainer();
151
        $extension = new DoctrineMigrationsExtension();
152
153
        $config = [
154
            'em' => 'custom',
155
            'migrations_paths' => ['DoctrineMigrationsTest' => 'a'],
156
        ];
157
158
        $em = new Definition(CustomEntityManager::class);
159
        $container->setDefinition('doctrine.orm.custom_entity_manager', $em);
160
161
        $extension->load(['doctrine_migrations' => $config], $container);
162
163
        $container->getDefinition('doctrine.migrations.di')->setPublic(true);
164
165
        $container->compile();
166
        /**
167
         * @var $di DependencyFactory
168
         */
169
        $di = $container->get('doctrine.migrations.di');
170
171
        self::isInstanceOf(DependencyFactory::class, $di);
0 ignored issues
show
Unused Code introduced by
The call to PHPUnit\Framework\Assert::isInstanceOf() has too many arguments starting with $di. ( Ignorable by Annotation )

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

171
        self::/** @scrutinizer ignore-call */ 
172
              isInstanceOf(DependencyFactory::class, $di);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
172
        $em = $di->getEntityManager();
173
        self::assertInstanceOf(CustomEntityManager::class, $em);
174
        self::assertInstanceOf(CustomEntityManager::class, $di->getConnection()->getEm());
0 ignored issues
show
Bug introduced by
The method getEm() does not exist on Doctrine\DBAL\Connection. It seems like you code against a sub-type of Doctrine\DBAL\Connection such as anonymous//Tests/Fixture...stomEntityManager.php$0. ( Ignorable by Annotation )

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

174
        self::assertInstanceOf(CustomEntityManager::class, $di->getConnection()->/** @scrutinizer ignore-call */ getEm());
Loading history...
175
        self::assertSame($em, $di->getConnection()->getEm());
176
    }
177
178
    public function testCanNotSpecifyBothEmAndConnection() : void
179
    {
180
        $this->expectExceptionMessage('You can not specify both "connection" and "em" in the DoctrineMigrationsBundle configurations');
181
        $this->expectException(InvalidArgumentException::class);
182
        $container = $this->getContainer();
183
        $extension = new DoctrineMigrationsExtension();
184
185
        $config = [
186
            'em' => 'custom',
187
            'connection' => 'custom',
188
        ];
189
190
        $extension->load(['doctrine_migrations' => $config], $container);
191
192
        $container->getDefinition('doctrine.migrations.di')->setPublic(true);
193
194
        $container->compile();
195
    }
196
197
    private function getContainer() : ContainerBuilder
198
    {
199
        return new ContainerBuilder(new ParameterBag([
200
            'kernel.debug' => false,
201
            'kernel.bundles' => [],
202
            'kernel.cache_dir' => sys_get_temp_dir(),
203
            'kernel.environment' => 'test',
204
            'kernel.root_dir' => __DIR__ . '/../../', // src dir
205
        ]));
206
    }
207
}
208