Completed
Push — master ( bce990...a5f4ec )
by Andreas
02:13 queued 11s
created

testLoadSimpleSingleConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 9.424
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection;
4
5
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DbalSchemaFilterPass;
6
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\EntityListenerPass;
7
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\WellKnownSchemaFilterPass;
8
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\DoctrineExtension;
9
use Doctrine\DBAL\Configuration;
10
use Doctrine\DBAL\Schema\AbstractAsset;
11
use Doctrine\ORM\EntityManager;
12
use PHPUnit\Framework\TestCase;
13
use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\RegisterEventListenersAndSubscribersPass;
14
use Symfony\Component\Cache\Adapter\ArrayAdapter;
15
use Symfony\Component\Cache\DoctrineProvider;
16
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17
use Symfony\Component\DependencyInjection\Compiler\ResolveChildDefinitionsPass;
18
use Symfony\Component\DependencyInjection\ContainerBuilder;
19
use Symfony\Component\DependencyInjection\Definition;
20
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
21
use Symfony\Component\DependencyInjection\Reference;
22
use Symfony\Component\DependencyInjection\ServiceLocator;
23
24
abstract class AbstractDoctrineExtensionTest extends TestCase
25
{
26
    abstract protected function loadFromFile(ContainerBuilder $container, $file);
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
27
28
    public function testDbalLoadFromXmlMultipleConnections()
29
    {
30
        $container = $this->loadContainer('dbal_service_multiple_connections');
31
32
        // doctrine.dbal.mysql_connection
33
        $config = $container->getDefinition('doctrine.dbal.mysql_connection')->getArgument(0);
34
35
        $this->assertEquals('mysql_s3cr3t', $config['password']);
36
        $this->assertEquals('mysql_user', $config['user']);
37
        $this->assertEquals('mysql_db', $config['dbname']);
38
        $this->assertEquals('/path/to/mysqld.sock', $config['unix_socket']);
39
40
        // doctrine.dbal.sqlite_connection
41
        $config = $container->getDefinition('doctrine.dbal.sqlite_connection')->getArgument(0);
42
        $this->assertSame('pdo_sqlite', $config['driver']);
43
        $this->assertSame('sqlite_db', $config['dbname']);
44
        $this->assertSame('sqlite_user', $config['user']);
45
        $this->assertSame('sqlite_s3cr3t', $config['password']);
46
        $this->assertSame('/tmp/db.sqlite', $config['path']);
47
        $this->assertTrue($config['memory']);
48
49
        // doctrine.dbal.oci8_connection
50
        $config = $container->getDefinition('doctrine.dbal.oci_connection')->getArgument(0);
51
        $this->assertSame('oci8', $config['driver']);
52
        $this->assertSame('oracle_db', $config['dbname']);
53
        $this->assertSame('oracle_user', $config['user']);
54
        $this->assertSame('oracle_s3cr3t', $config['password']);
55
        $this->assertSame('oracle_service', $config['servicename']);
56
        $this->assertTrue($config['service']);
57
        $this->assertTrue($config['pooled']);
58
        $this->assertSame('utf8', $config['charset']);
59
60
        // doctrine.dbal.ibmdb2_connection
61
        $config = $container->getDefinition('doctrine.dbal.ibmdb2_connection')->getArgument(0);
62
        $this->assertSame('ibm_db2', $config['driver']);
63
        $this->assertSame('ibmdb2_db', $config['dbname']);
64
        $this->assertSame('ibmdb2_user', $config['user']);
65
        $this->assertSame('ibmdb2_s3cr3t', $config['password']);
66
        $this->assertSame('TCPIP', $config['protocol']);
67
68
        // doctrine.dbal.pgsql_connection
69
        $config = $container->getDefinition('doctrine.dbal.pgsql_connection')->getArgument(0);
70
        $this->assertSame('pdo_pgsql', $config['driver']);
71
        $this->assertSame('pgsql_schema', $config['dbname']);
72
        $this->assertSame('pgsql_user', $config['user']);
73
        $this->assertSame('pgsql_s3cr3t', $config['password']);
74
        $this->assertSame('pgsql_db', $config['default_dbname']);
75
        $this->assertSame('require', $config['sslmode']);
76
        $this->assertSame('postgresql-ca.pem', $config['sslrootcert']);
77
        $this->assertSame('postgresql-cert.pem', $config['sslcert']);
78
        $this->assertSame('postgresql-key.pem', $config['sslkey']);
79
        $this->assertSame('postgresql.crl', $config['sslcrl']);
80
        $this->assertSame('utf8', $config['charset']);
81
82
        // doctrine.dbal.sqlanywhere_connection
83
        $config = $container->getDefinition('doctrine.dbal.sqlanywhere_connection')->getArgument(0);
84
        $this->assertSame('sqlanywhere', $config['driver']);
85
        $this->assertSame('localhost', $config['host']);
86
        $this->assertSame(2683, $config['port']);
87
        $this->assertSame('sqlanywhere_server', $config['server']);
88
        $this->assertSame('sqlanywhere_db', $config['dbname']);
89
        $this->assertSame('sqlanywhere_user', $config['user']);
90
        $this->assertSame('sqlanywhere_s3cr3t', $config['password']);
91
        $this->assertTrue($config['persistent']);
92
        $this->assertSame('utf8', $config['charset']);
93
    }
94
95
    public function testDbalLoadFromXmlSingleConnections()
96
    {
97
        $container = $this->loadContainer('dbal_service_single_connection');
98
99
        // doctrine.dbal.mysql_connection
100
        $config = $container->getDefinition('doctrine.dbal.default_connection')->getArgument(0);
101
102
        $this->assertEquals('mysql_s3cr3t', $config['password']);
103
        $this->assertEquals('mysql_user', $config['user']);
104
        $this->assertEquals('mysql_db', $config['dbname']);
105
        $this->assertEquals('/path/to/mysqld.sock', $config['unix_socket']);
106
        $this->assertEquals('5.6.20', $config['serverVersion']);
107
    }
108
109
    public function testDbalLoadSingleMasterSlaveConnection()
110
    {
111
        $container = $this->loadContainer('dbal_service_single_master_slave_connection');
112
113
        // doctrine.dbal.mysql_connection
114
        $param = $container->getDefinition('doctrine.dbal.default_connection')->getArgument(0);
115
116
        $this->assertEquals('Doctrine\\DBAL\\Connections\\MasterSlaveConnection', $param['wrapperClass']);
117
        $this->assertTrue($param['keepSlave']);
118
        $this->assertEquals(
119
            [
120
                'user' => 'mysql_user',
121
                'password' => 'mysql_s3cr3t',
122
                'port' => null,
123
                'dbname' => 'mysql_db',
124
                'host' => 'localhost',
125
                'unix_socket' => '/path/to/mysqld.sock',
126
            ],
127
            $param['master']
128
        );
129
        $this->assertEquals(
130
            [
131
                'user' => 'slave_user',
132
                'password' => 'slave_s3cr3t',
133
                'port' => null,
134
                'dbname' => 'slave_db',
135
                'host' => 'localhost',
136
                'unix_socket' => '/path/to/mysqld_slave.sock',
137
            ],
138
            $param['slaves']['slave1']
139
        );
140
        $this->assertEquals(['engine' => 'InnoDB'], $param['defaultTableOptions']);
141
    }
142
143
    public function testDbalLoadPoolShardingConnection()
144
    {
145
        $container = $this->loadContainer('dbal_service_pool_sharding_connection');
146
147
        // doctrine.dbal.mysql_connection
148
        $param = $container->getDefinition('doctrine.dbal.default_connection')->getArgument(0);
149
150
        $this->assertEquals('Doctrine\\DBAL\\Sharding\\PoolingShardConnection', $param['wrapperClass']);
151
        $this->assertEquals(new Reference('foo.shard_choser'), $param['shardChoser']);
152
        $this->assertEquals(
153
            [
154
                'user' => 'mysql_user',
155
                'password' => 'mysql_s3cr3t',
156
                'port' => null,
157
                'dbname' => 'mysql_db',
158
                'host' => 'localhost',
159
                'unix_socket' => '/path/to/mysqld.sock',
160
            ],
161
            $param['global']
162
        );
163
        $this->assertEquals(
164
            [
165
                'user' => 'shard_user',
166
                'password' => 'shard_s3cr3t',
167
                'port' => null,
168
                'dbname' => 'shard_db',
169
                'host' => 'localhost',
170
                'unix_socket' => '/path/to/mysqld_shard.sock',
171
                'id' => 1,
172
            ],
173
            $param['shards'][0]
174
        );
175
        $this->assertEquals(['engine' => 'InnoDB'], $param['defaultTableOptions']);
176
    }
177
178
    public function testDbalLoadSavepointsForNestedTransactions()
179
    {
180
        $container = $this->loadContainer('dbal_savepoints');
181
182
        $calls = $container->getDefinition('doctrine.dbal.savepoints_connection')->getMethodCalls();
183
        $this->assertCount(1, $calls);
0 ignored issues
show
Documentation introduced by
$calls is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
184
        $this->assertEquals('setNestTransactionsWithSavepoints', $calls[0][0]);
185
        $this->assertTrue($calls[0][1][0]);
186
187
        $calls = $container->getDefinition('doctrine.dbal.nosavepoints_connection')->getMethodCalls();
188
        $this->assertCount(0, $calls);
0 ignored issues
show
Documentation introduced by
$calls is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
189
190
        $calls = $container->getDefinition('doctrine.dbal.notset_connection')->getMethodCalls();
191
        $this->assertCount(0, $calls);
0 ignored issues
show
Documentation introduced by
$calls is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
192
    }
193
194
    public function testLoadSimpleSingleConnection()
195
    {
196
        $container = $this->loadContainer('orm_service_simple_single_entity_manager');
197
198
        $definition = $container->getDefinition('doctrine.dbal.default_connection');
199
200
        $this->assertDICConstructorArguments($definition, [
201
            [
202
                'dbname' => 'db',
203
                'host' => 'localhost',
204
                'port' => null,
205
                'user' => 'root',
206
                'password' => null,
207
                'driver' => 'pdo_mysql',
208
                'driverOptions' => [],
209
                'defaultTableOptions' => [],
210
            ],
211
            new Reference('doctrine.dbal.default_connection.configuration'),
212
            new Reference('doctrine.dbal.default_connection.event_manager'),
213
            [],
214
        ]);
215
216
        $definition = $container->getDefinition('doctrine.orm.default_entity_manager');
217
        $this->assertEquals('%doctrine.orm.entity_manager.class%', $definition->getClass());
218
        $this->assertEquals(['%doctrine.orm.entity_manager.class%', 'create'], $definition->getFactory());
219
220
        $this->assertDICConstructorArguments($definition, [
221
            new Reference('doctrine.dbal.default_connection'),
222
            new Reference('doctrine.orm.default_configuration'),
223
        ]);
224
    }
225
226
    /**
227
     * The PDO driver doesn't require a database name to be to set when connecting to a database server
228
     */
229
    public function testLoadSimpleSingleConnectionWithoutDbName()
230
    {
231
        $container = $this->loadContainer('orm_service_simple_single_entity_manager_without_dbname');
232
233
        /** @var Definition $definition */
234
        $definition = $container->getDefinition('doctrine.dbal.default_connection');
235
236
        $this->assertDICConstructorArguments($definition, [
237
            [
238
                'host' => 'localhost',
239
                'port' => null,
240
                'user' => 'root',
241
                'password' => null,
242
                'driver' => 'pdo_mysql',
243
                'driverOptions' => [],
244
                'defaultTableOptions' => [],
245
            ],
246
            new Reference('doctrine.dbal.default_connection.configuration'),
247
            new Reference('doctrine.dbal.default_connection.event_manager'),
248
            [],
249
        ]);
250
251
        $definition = $container->getDefinition('doctrine.orm.default_entity_manager');
252
        $this->assertEquals('%doctrine.orm.entity_manager.class%', $definition->getClass());
253
        $factory = $definition->getFactory();
254
255
        $this->assertEquals('%doctrine.orm.entity_manager.class%', $factory[0]);
256
        $this->assertEquals('create', $factory[1]);
257
258
        $this->assertDICConstructorArguments($definition, [
259
            new Reference('doctrine.dbal.default_connection'),
260
            new Reference('doctrine.orm.default_configuration'),
261
        ]);
262
    }
263
264
    public function testLoadSingleConnection()
265
    {
266
        $container = $this->loadContainer('orm_service_single_entity_manager');
267
268
        $definition = $container->getDefinition('doctrine.dbal.default_connection');
269
270
        $this->assertDICConstructorArguments($definition, [
271
            [
272
                'host' => 'localhost',
273
                'driver' => 'pdo_sqlite',
274
                'driverOptions' => [],
275
                'user' => 'sqlite_user',
276
                'port' => null,
277
                'password' => 'sqlite_s3cr3t',
278
                'dbname' => 'sqlite_db',
279
                'memory' => true,
280
                'defaultTableOptions' => [],
281
            ],
282
            new Reference('doctrine.dbal.default_connection.configuration'),
283
            new Reference('doctrine.dbal.default_connection.event_manager'),
284
            [],
285
        ]);
286
287
        $definition = $container->getDefinition('doctrine.orm.default_entity_manager');
288
        $this->assertEquals('%doctrine.orm.entity_manager.class%', $definition->getClass());
289
        $this->assertEquals(['%doctrine.orm.entity_manager.class%', 'create'], $definition->getFactory());
290
291
        $this->assertDICConstructorArguments($definition, [
292
            new Reference('doctrine.dbal.default_connection'),
293
            new Reference('doctrine.orm.default_configuration'),
294
        ]);
295
296
        $configDef = $container->getDefinition('doctrine.orm.default_configuration');
297
        $this->assertDICDefinitionMethodCallOnce($configDef, 'setDefaultRepositoryClassName', ['Acme\Doctrine\Repository']);
298
    }
299
300
    public function testLoadMultipleConnections()
301
    {
302
        $container = $this->loadContainer('orm_service_multiple_entity_managers');
303
304
        $definition = $container->getDefinition('doctrine.dbal.conn1_connection');
305
306
        $args = $definition->getArguments();
307
        $this->assertEquals('pdo_sqlite', $args[0]['driver']);
308
        $this->assertEquals('localhost', $args[0]['host']);
309
        $this->assertEquals('sqlite_user', $args[0]['user']);
310
        $this->assertEquals('doctrine.dbal.conn1_connection.configuration', (string) $args[1]);
311
        $this->assertEquals('doctrine.dbal.conn1_connection.event_manager', (string) $args[2]);
312
313
        $this->assertEquals('doctrine.orm.em2_entity_manager', (string) $container->getAlias('doctrine.orm.entity_manager'));
314
315
        $definition = $container->getDefinition('doctrine.orm.em1_entity_manager');
316
        $this->assertEquals('%doctrine.orm.entity_manager.class%', $definition->getClass());
317
        $this->assertEquals(['%doctrine.orm.entity_manager.class%', 'create'], $definition->getFactory());
318
319
        $arguments = $definition->getArguments();
320
        $this->assertInstanceOf('Symfony\Component\DependencyInjection\Reference', $arguments[0]);
321
        $this->assertEquals('doctrine.dbal.conn1_connection', (string) $arguments[0]);
322
        $this->assertInstanceOf('Symfony\Component\DependencyInjection\Reference', $arguments[1]);
323
        $this->assertEquals('doctrine.orm.em1_configuration', (string) $arguments[1]);
324
325
        $definition = $container->getDefinition('doctrine.dbal.conn2_connection');
326
327
        $args = $definition->getArguments();
328
        $this->assertEquals('pdo_sqlite', $args[0]['driver']);
329
        $this->assertEquals('localhost', $args[0]['host']);
330
        $this->assertEquals('sqlite_user', $args[0]['user']);
331
        $this->assertEquals('doctrine.dbal.conn2_connection.configuration', (string) $args[1]);
332
        $this->assertEquals('doctrine.dbal.conn2_connection.event_manager', (string) $args[2]);
333
334
        $definition = $container->getDefinition('doctrine.orm.em2_entity_manager');
335
        $this->assertEquals('%doctrine.orm.entity_manager.class%', $definition->getClass());
336
        $this->assertEquals(['%doctrine.orm.entity_manager.class%', 'create'], $definition->getFactory());
337
338
        $arguments = $definition->getArguments();
339
        $this->assertInstanceOf('Symfony\Component\DependencyInjection\Reference', $arguments[0]);
340
        $this->assertEquals('doctrine.dbal.conn2_connection', (string) $arguments[0]);
341
        $this->assertInstanceOf('Symfony\Component\DependencyInjection\Reference', $arguments[1]);
342
        $this->assertEquals('doctrine.orm.em2_configuration', (string) $arguments[1]);
343
344
        $definition = $container->getDefinition((string) $container->getAlias('doctrine.orm.em1_metadata_cache'));
345
        $this->assertEquals(DoctrineProvider::class, $definition->getClass());
346
347
        $definition = $container->getDefinition((string) $container->getAlias('doctrine.orm.em1_query_cache'));
348
        $this->assertEquals(DoctrineProvider::class, $definition->getClass());
349
        $arguments = $definition->getArguments();
350
        $this->assertInstanceOf(Reference::class, $arguments[0]);
351
        $this->assertEquals('cache.app', (string) $arguments[0]);
352
353
        $definition = $container->getDefinition((string) $container->getAlias('doctrine.orm.em1_result_cache'));
354
        $this->assertEquals(DoctrineProvider::class, $definition->getClass());
355
        $arguments = $definition->getArguments();
356
        $this->assertInstanceOf(Reference::class, $arguments[0]);
357
        $this->assertEquals('cache.app', (string) $arguments[0]);
358
    }
359
360
    public function testLoadLogging()
361
    {
362
        $container = $this->loadContainer('dbal_logging');
363
364
        $definition = $container->getDefinition('doctrine.dbal.log_connection.configuration');
365
        $this->assertDICDefinitionMethodCallOnce($definition, 'setSQLLogger', [new Reference('doctrine.dbal.logger')]);
366
367
        $definition = $container->getDefinition('doctrine.dbal.profile_connection.configuration');
368
        $this->assertDICDefinitionMethodCallOnce($definition, 'setSQLLogger', [new Reference('doctrine.dbal.logger.profiling.profile')]);
369
370
        $definition = $container->getDefinition('doctrine.dbal.profile_with_backtrace_connection.configuration');
371
        $this->assertDICDefinitionMethodCallOnce($definition, 'setSQLLogger', [new Reference('doctrine.dbal.logger.backtrace.profile_with_backtrace')]);
372
373
        $definition = $container->getDefinition('doctrine.dbal.backtrace_without_profile_connection.configuration');
374
        $this->assertDICDefinitionNoMethodCall($definition, 'setSQLLogger');
375
376
        $definition = $container->getDefinition('doctrine.dbal.both_connection.configuration');
377
        $this->assertDICDefinitionMethodCallOnce($definition, 'setSQLLogger', [new Reference('doctrine.dbal.logger.chain.both')]);
378
    }
379
380
    public function testEntityManagerMetadataCacheDriverConfiguration()
381
    {
382
        $container = $this->loadContainer('orm_service_multiple_entity_managers');
383
384
        $definition = $container->getDefinition((string) $container->getAlias('doctrine.orm.em1_metadata_cache'));
385
        $this->assertDICDefinitionClass($definition, DoctrineProvider::class);
386
387
        $definition = $container->getDefinition((string) $container->getAlias('doctrine.orm.em2_metadata_cache'));
388
        $this->assertDICDefinitionClass($definition, DoctrineProvider::class);
389
    }
390
391
    public function testDependencyInjectionImportsOverrideDefaults()
392
    {
393
        $container = $this->loadContainer('orm_imports');
394
395
        $cacheDefinition = $container->getDefinition((string) $container->getAlias('doctrine.orm.default_metadata_cache'));
396
        $this->assertEquals(DoctrineProvider::class, $cacheDefinition->getClass());
397
398
        $configDefinition = $container->getDefinition('doctrine.orm.default_configuration');
399
        $this->assertDICDefinitionMethodCallOnce($configDefinition, 'setAutoGenerateProxyClasses', ['%doctrine.orm.auto_generate_proxy_classes%']);
400
    }
401
402
    public function testSingleEntityManagerMultipleMappingBundleDefinitions()
403
    {
404
        $container = $this->loadContainer('orm_single_em_bundle_mappings', ['YamlBundle', 'AnnotationsBundle', 'XmlBundle']);
405
406
        $definition = $container->getDefinition('doctrine.orm.default_metadata_driver');
407
408
        $this->assertDICDefinitionMethodCallAt(0, $definition, 'addDriver', [
409
            new Reference('doctrine.orm.default_annotation_metadata_driver'),
410
            'Fixtures\Bundles\AnnotationsBundle\Entity',
411
        ]);
412
413
        $this->assertDICDefinitionMethodCallAt(1, $definition, 'addDriver', [
414
            new Reference('doctrine.orm.default_yml_metadata_driver'),
415
            'Fixtures\Bundles\YamlBundle\Entity',
416
        ]);
417
418
        $this->assertDICDefinitionMethodCallAt(2, $definition, 'addDriver', [
419
            new Reference('doctrine.orm.default_xml_metadata_driver'),
420
            'Fixtures\Bundles\XmlBundle',
421
        ]);
422
423
        $annDef = $container->getDefinition('doctrine.orm.default_annotation_metadata_driver');
424
        $this->assertDICConstructorArguments($annDef, [
425
            new Reference('doctrine.orm.metadata.annotation_reader'),
426
            [__DIR__ . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR . 'Bundles' . DIRECTORY_SEPARATOR . 'AnnotationsBundle' . DIRECTORY_SEPARATOR . 'Entity'],
427
        ]);
428
429
        $ymlDef = $container->getDefinition('doctrine.orm.default_yml_metadata_driver');
430
        $this->assertDICConstructorArguments($ymlDef, [
431
            [__DIR__ . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR . 'Bundles' . DIRECTORY_SEPARATOR . 'YamlBundle' . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'doctrine' => 'Fixtures\Bundles\YamlBundle\Entity'],
432
        ]);
433
434
        $xmlDef = $container->getDefinition('doctrine.orm.default_xml_metadata_driver');
435
        $this->assertDICConstructorArguments($xmlDef, [
436
            [__DIR__ . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR . 'Bundles' . DIRECTORY_SEPARATOR . 'XmlBundle' . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'doctrine' => 'Fixtures\Bundles\XmlBundle'],
437
        ]);
438
    }
439
440
    public function testMultipleEntityManagersMappingBundleDefinitions()
441
    {
442
        $container = $this->loadContainer('orm_multiple_em_bundle_mappings', ['YamlBundle', 'AnnotationsBundle', 'XmlBundle']);
443
444
        $this->assertEquals(['em1' => 'doctrine.orm.em1_entity_manager', 'em2' => 'doctrine.orm.em2_entity_manager'], $container->getParameter('doctrine.entity_managers'), 'Set of the existing EntityManagers names is incorrect.');
445
        $this->assertEquals('%doctrine.entity_managers%', $container->getDefinition('doctrine')->getArgument(2), 'Set of the existing EntityManagers names is incorrect.');
446
447
        $def1 = $container->getDefinition('doctrine.orm.em1_metadata_driver');
448
        $def2 = $container->getDefinition('doctrine.orm.em2_metadata_driver');
449
450
        $this->assertDICDefinitionMethodCallAt(0, $def1, 'addDriver', [
451
            new Reference('doctrine.orm.em1_annotation_metadata_driver'),
452
            'Fixtures\Bundles\AnnotationsBundle\Entity',
453
        ]);
454
455
        $this->assertDICDefinitionMethodCallAt(0, $def2, 'addDriver', [
456
            new Reference('doctrine.orm.em2_yml_metadata_driver'),
457
            'Fixtures\Bundles\YamlBundle\Entity',
458
        ]);
459
460
        $this->assertDICDefinitionMethodCallAt(1, $def2, 'addDriver', [
461
            new Reference('doctrine.orm.em2_xml_metadata_driver'),
462
            'Fixtures\Bundles\XmlBundle',
463
        ]);
464
465
        $annDef = $container->getDefinition('doctrine.orm.em1_annotation_metadata_driver');
466
        $this->assertDICConstructorArguments($annDef, [
467
            new Reference('doctrine.orm.metadata.annotation_reader'),
468
            [__DIR__ . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR . 'Bundles' . DIRECTORY_SEPARATOR . 'AnnotationsBundle' . DIRECTORY_SEPARATOR . 'Entity'],
469
        ]);
470
471
        $ymlDef = $container->getDefinition('doctrine.orm.em2_yml_metadata_driver');
472
        $this->assertDICConstructorArguments($ymlDef, [
473
            [__DIR__ . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR . 'Bundles' . DIRECTORY_SEPARATOR . 'YamlBundle' . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'doctrine' => 'Fixtures\Bundles\YamlBundle\Entity'],
474
        ]);
475
476
        $xmlDef = $container->getDefinition('doctrine.orm.em2_xml_metadata_driver');
477
        $this->assertDICConstructorArguments($xmlDef, [
478
            [__DIR__ . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR . 'Bundles' . DIRECTORY_SEPARATOR . 'XmlBundle' . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'doctrine' => 'Fixtures\Bundles\XmlBundle'],
479
        ]);
480
    }
481
482
    public function testSingleEntityManagerDefaultTableOptions()
483
    {
484
        $container = $this->loadContainer('orm_single_em_default_table_options', ['YamlBundle', 'AnnotationsBundle', 'XmlBundle']);
485
486
        $param = $container->getDefinition('doctrine.dbal.default_connection')->getArgument(0);
487
488
        $this->assertArrayHasKey('defaultTableOptions', $param);
489
490
        $defaults = $param['defaultTableOptions'];
491
492
        $this->assertArrayHasKey('charset', $defaults);
493
        $this->assertArrayHasKey('collate', $defaults);
494
        $this->assertArrayHasKey('engine', $defaults);
495
496
        $this->assertEquals('utf8mb4', $defaults['charset']);
497
        $this->assertEquals('utf8mb4_unicode_ci', $defaults['collate']);
498
        $this->assertEquals('InnoDB', $defaults['engine']);
499
    }
500
501
    public function testSetTypes()
502
    {
503
        $container = $this->loadContainer('dbal_types');
504
505
        $this->assertEquals(
506
            ['test' => ['class' => TestType::class]],
507
            $container->getParameter('doctrine.dbal.connection_factory.types')
508
        );
509
        $this->assertEquals('%doctrine.dbal.connection_factory.types%', $container->getDefinition('doctrine.dbal.connection_factory')->getArgument(0));
510
    }
511
512
    public function testSetCustomFunctions()
513
    {
514
        $container = $this->loadContainer('orm_functions');
515
516
        $definition = $container->getDefinition('doctrine.orm.default_configuration');
517
        $this->assertDICDefinitionMethodCallOnce($definition, 'addCustomStringFunction', ['test_string', 'Symfony\Bundle\DoctrineBundle\Tests\DependencyInjection\TestStringFunction']);
518
        $this->assertDICDefinitionMethodCallOnce($definition, 'addCustomNumericFunction', ['test_numeric', 'Symfony\Bundle\DoctrineBundle\Tests\DependencyInjection\TestNumericFunction']);
519
        $this->assertDICDefinitionMethodCallOnce($definition, 'addCustomDatetimeFunction', ['test_datetime', 'Symfony\Bundle\DoctrineBundle\Tests\DependencyInjection\TestDatetimeFunction']);
520
    }
521
522 View Code Duplication
    public function testSetNamingStrategy()
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...
523
    {
524
        $container = $this->loadContainer('orm_namingstrategy');
525
526
        $def1 = $container->getDefinition('doctrine.orm.em1_configuration');
527
        $def2 = $container->getDefinition('doctrine.orm.em2_configuration');
528
529
        $this->assertDICDefinitionMethodCallOnce($def1, 'setNamingStrategy', [0 => new Reference('doctrine.orm.naming_strategy.default')]);
530
        $this->assertDICDefinitionMethodCallOnce($def2, 'setNamingStrategy', [0 => new Reference('doctrine.orm.naming_strategy.underscore')]);
531
    }
532
533 View Code Duplication
    public function testSetQuoteStrategy()
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...
534
    {
535
        $container = $this->loadContainer('orm_quotestrategy');
536
537
        $def1 = $container->getDefinition('doctrine.orm.em1_configuration');
538
        $def2 = $container->getDefinition('doctrine.orm.em2_configuration');
539
540
        $this->assertDICDefinitionMethodCallOnce($def1, 'setQuoteStrategy', [0 => new Reference('doctrine.orm.quote_strategy.default')]);
541
        $this->assertDICDefinitionMethodCallOnce($def2, 'setQuoteStrategy', [0 => new Reference('doctrine.orm.quote_strategy.ansi')]);
542
    }
543
544
    public function testSecondLevelCache()
545
    {
546
        $container = $this->loadContainer('orm_second_level_cache');
547
548
        $this->assertTrue($container->has('doctrine.orm.default_configuration'));
549
        $this->assertTrue($container->has('doctrine.orm.default_second_level_cache.cache_configuration'));
550
        $this->assertTrue($container->has('doctrine.orm.default_second_level_cache.region_cache_driver'));
551
        $this->assertTrue($container->has('doctrine.orm.default_second_level_cache.regions_configuration'));
552
        $this->assertTrue($container->has('doctrine.orm.default_second_level_cache.default_cache_factory'));
553
554
        $this->assertTrue($container->has('doctrine.orm.default_second_level_cache.logger_chain'));
555
        $this->assertTrue($container->has('doctrine.orm.default_second_level_cache.logger_statistics'));
556
        $this->assertTrue($container->has('doctrine.orm.default_second_level_cache.logger.my_service_logger1'));
557
        $this->assertTrue($container->has('doctrine.orm.default_second_level_cache.logger.my_service_logger2'));
558
559
        $this->assertTrue($container->has('doctrine.orm.default_second_level_cache.region.my_entity_region'));
560
        $this->assertTrue($container->has('doctrine.orm.default_second_level_cache.region.my_service_region'));
561
        $this->assertTrue($container->has('doctrine.orm.default_second_level_cache.region.my_query_region_filelock'));
562
563
        $slcFactoryDef       = $container->getDefinition('doctrine.orm.default_second_level_cache.default_cache_factory');
564
        $myEntityRegionDef   = $container->getDefinition('doctrine.orm.default_second_level_cache.region.my_entity_region');
565
        $loggerChainDef      = $container->getDefinition('doctrine.orm.default_second_level_cache.logger_chain');
566
        $loggerStatisticsDef = $container->getDefinition('doctrine.orm.default_second_level_cache.logger_statistics');
567
        $myQueryRegionDef    = $container->getDefinition('doctrine.orm.default_second_level_cache.region.my_query_region_filelock');
568
        $cacheDriverDef      = $container->getDefinition((string) $container->getAlias('doctrine.orm.default_second_level_cache.region_cache_driver'));
569
        $configDef           = $container->getDefinition('doctrine.orm.default_configuration');
570
        $myEntityRegionArgs  = $myEntityRegionDef->getArguments();
571
        $myQueryRegionArgs   = $myQueryRegionDef->getArguments();
572
        $slcFactoryArgs      = $slcFactoryDef->getArguments();
573
574
        $this->assertDICDefinitionClass($slcFactoryDef, '%doctrine.orm.second_level_cache.default_cache_factory.class%');
575
        $this->assertDICDefinitionClass($myQueryRegionDef, '%doctrine.orm.second_level_cache.filelock_region.class%');
576
        $this->assertDICDefinitionClass($myEntityRegionDef, '%doctrine.orm.second_level_cache.default_region.class%');
577
        $this->assertDICDefinitionClass($loggerChainDef, '%doctrine.orm.second_level_cache.logger_chain.class%');
578
        $this->assertDICDefinitionClass($loggerStatisticsDef, '%doctrine.orm.second_level_cache.logger_statistics.class%');
579
        $this->assertDICDefinitionClass($cacheDriverDef, DoctrineProvider::class);
580
        $this->assertDICDefinitionMethodCallOnce($configDef, 'setSecondLevelCacheConfiguration');
581
        $this->assertDICDefinitionMethodCallCount($slcFactoryDef, 'setRegion', [], 3);
582
        $this->assertDICDefinitionMethodCallCount($loggerChainDef, 'setLogger', [], 3);
583
584
        $this->assertInstanceOf('Symfony\Component\DependencyInjection\Reference', $slcFactoryArgs[0]);
585
        $this->assertInstanceOf('Symfony\Component\DependencyInjection\Reference', $slcFactoryArgs[1]);
586
587
        $this->assertInstanceOf('Symfony\Component\DependencyInjection\Reference', $myEntityRegionArgs[1]);
588
        $this->assertInstanceOf('Symfony\Component\DependencyInjection\Reference', $myQueryRegionArgs[0]);
589
590
        $this->assertEquals('my_entity_region', $myEntityRegionArgs[0]);
591
        $this->assertEquals('doctrine.orm.default_second_level_cache.region.my_entity_region_driver', $myEntityRegionArgs[1]);
592
        $this->assertEquals(600, $myEntityRegionArgs[2]);
593
594
        $this->assertEquals('doctrine.orm.default_second_level_cache.region.my_query_region', $myQueryRegionArgs[0]);
595
        $this->assertContains('/doctrine/orm/slc/filelock', $myQueryRegionArgs[1]);
596
        $this->assertEquals(60, $myQueryRegionArgs[2]);
597
598
        $this->assertEquals('doctrine.orm.default_second_level_cache.regions_configuration', $slcFactoryArgs[0]);
599
        $this->assertEquals('doctrine.orm.default_second_level_cache.region_cache_driver', $slcFactoryArgs[1]);
600
    }
601
602
    public function testSingleEMSetCustomFunctions()
603
    {
604
        $container = $this->loadContainer('orm_single_em_dql_functions');
605
606
        $definition = $container->getDefinition('doctrine.orm.default_configuration');
607
        $this->assertDICDefinitionMethodCallOnce($definition, 'addCustomStringFunction', ['test_string', 'Symfony\Bundle\DoctrineBundle\Tests\DependencyInjection\TestStringFunction']);
608
    }
609
610
    public function testAddCustomHydrationMode()
611
    {
612
        $container = $this->loadContainer('orm_hydration_mode');
613
614
        $definition = $container->getDefinition('doctrine.orm.default_configuration');
615
        $this->assertDICDefinitionMethodCallOnce($definition, 'addCustomHydrationMode', ['test_hydrator', 'Symfony\Bundle\DoctrineBundle\Tests\DependencyInjection\TestHydrator']);
616
    }
617
618
    public function testAddFilter()
619
    {
620
        $container = $this->loadContainer('orm_filters');
621
622
        $definition = $container->getDefinition('doctrine.orm.default_configuration');
623
        $args       = [
624
            ['soft_delete', 'Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\TestFilter'],
625
            ['myFilter', 'Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\TestFilter'],
626
        ];
627
        $this->assertDICDefinitionMethodCallCount($definition, 'addFilter', $args, 2);
628
629
        $definition = $container->getDefinition('doctrine.orm.default_manager_configurator');
630
        $this->assertDICConstructorArguments($definition, [['soft_delete', 'myFilter'], ['myFilter' => ['myParameter' => 'myValue', 'mySecondParameter' => 'mySecondValue']]]);
631
632
        // Let's create the instance to check the configurator work.
633
        /** @var EntityManager $entityManager */
634
        $entityManager = $container->get('doctrine.orm.entity_manager');
635
        $this->assertCount(2, $entityManager->getFilters()->getEnabledFilters());
0 ignored issues
show
Documentation introduced by
$entityManager->getFilters()->getEnabledFilters() is of type array<integer,object<Doc...uery\Filter\SQLFilter>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
636
    }
637
638
    public function testResolveTargetEntity()
639
    {
640
        $container = $this->loadContainer('orm_resolve_target_entity');
641
642
        $definition = $container->getDefinition('doctrine.orm.listeners.resolve_target_entity');
643
        $this->assertDICDefinitionMethodCallOnce($definition, 'addResolveTargetEntity', ['Symfony\Component\Security\Core\User\UserInterface', 'MyUserClass', []]);
644
645
        $this->assertEquals(['doctrine.event_subscriber' => [[]]], $definition->getTags());
646
    }
647
648
    public function testAttachEntityListeners()
649
    {
650
        $container = $this->loadContainer('orm_attach_entity_listener');
651
652
        $definition  = $container->getDefinition('doctrine.orm.default_listeners.attach_entity_listeners');
653
        $methodCalls = $definition->getMethodCalls();
654
655
        $this->assertDICDefinitionMethodCallCount($definition, 'addEntityListener', [], 6);
656
        $this->assertEquals(['doctrine.event_listener' => [ ['event' => 'loadClassMetadata'] ] ], $definition->getTags());
657
658
        $this->assertEquals($methodCalls[0], [
659
            'addEntityListener',
660
            [
661
                'ExternalBundles\Entities\FooEntity',
662
                'MyBundles\Listeners\FooEntityListener',
663
                'prePersist',
664
                null,
665
            ],
666
        ]);
667
668
        $this->assertEquals($methodCalls[1], [
669
            'addEntityListener',
670
            [
671
                'ExternalBundles\Entities\FooEntity',
672
                'MyBundles\Listeners\FooEntityListener',
673
                'postPersist',
674
                'postPersist',
675
            ],
676
        ]);
677
678
        $this->assertEquals($methodCalls[2], [
679
            'addEntityListener',
680
            [
681
                'ExternalBundles\Entities\FooEntity',
682
                'MyBundles\Listeners\FooEntityListener',
683
                'postLoad',
684
                'postLoadHandler',
685
            ],
686
        ]);
687
688
        $this->assertEquals($methodCalls[3], [
689
            'addEntityListener',
690
            [
691
                'ExternalBundles\Entities\BarEntity',
692
                'MyBundles\Listeners\BarEntityListener',
693
                'prePersist',
694
                'prePersist',
695
            ],
696
        ]);
697
698
        $this->assertEquals($methodCalls[4], [
699
            'addEntityListener',
700
            [
701
                'ExternalBundles\Entities\BarEntity',
702
                'MyBundles\Listeners\BarEntityListener',
703
                'prePersist',
704
                'prePersistHandler',
705
            ],
706
        ]);
707
708
        $this->assertEquals($methodCalls[5], [
709
            'addEntityListener',
710
            [
711
                'ExternalBundles\Entities\BarEntity',
712
                'MyBundles\Listeners\LogDeleteEntityListener',
713
                'postDelete',
714
                'postDelete',
715
            ],
716
        ]);
717
    }
718
719
    public function testDbalAutoCommit()
720
    {
721
        $container = $this->loadContainer('dbal_auto_commit');
722
723
        $definition = $container->getDefinition('doctrine.dbal.default_connection.configuration');
724
        $this->assertDICDefinitionMethodCallOnce($definition, 'setAutoCommit', [false]);
725
    }
726
727
    public function testDbalOracleConnectstring()
728
    {
729
        $container = $this->loadContainer('dbal_oracle_connectstring');
730
731
        $config = $container->getDefinition('doctrine.dbal.default_connection')->getArgument(0);
732
        $this->assertSame('scott@sales-server:1521/sales.us.example.com', $config['connectstring']);
733
    }
734
735
    public function testDbalOracleInstancename()
736
    {
737
        $container = $this->loadContainer('dbal_oracle_instancename');
738
739
        $config = $container->getDefinition('doctrine.dbal.default_connection')->getArgument(0);
740
        $this->assertSame('mySuperInstance', $config['instancename']);
741
    }
742
743
    public function testDbalSchemaFilter()
744
    {
745
        if (method_exists(Configuration::class, 'setSchemaAssetsFilter')) {
746
            $this->markTestSkipped('Test only applies to doctrine/dbal 2.8 or lower');
747
        }
748
749
        $container = $this->loadContainer('dbal_schema_filter');
750
751
        $definition = $container->getDefinition('doctrine.dbal.connection1_connection.configuration');
752
        $this->assertDICDefinitionMethodCallOnce($definition, 'setFilterSchemaAssetsExpression', ['~^(?!t_)~']);
753
    }
754
755
    public function testDbalSchemaFilterNewConfig()
756
    {
757
        if (! method_exists(Configuration::class, 'setSchemaAssetsFilter')) {
758
            $this->markTestSkipped('Test requires doctrine/dbal 2.9 or higher');
759
        }
760
761
        $container = $this->getContainer([]);
762
        $loader    = new DoctrineExtension();
763
        $container->registerExtension($loader);
764
        $container->addCompilerPass(new WellKnownSchemaFilterPass());
765
        $container->addCompilerPass(new DbalSchemaFilterPass());
766
767
        // ignore table1 table on "default" connection
768
        $container->register('dummy_filter1', DummySchemaAssetsFilter::class)
769
            ->setArguments(['table1'])
770
            ->addTag('doctrine.dbal.schema_filter');
771
772
        // ignore table2 table on "connection2" connection
773
        $container->register('dummy_filter2', DummySchemaAssetsFilter::class)
774
            ->setArguments(['table2'])
775
            ->addTag('doctrine.dbal.schema_filter', ['connection' => 'connection2']);
776
777
        $this->loadFromFile($container, 'dbal_schema_filter');
778
779
        $assetNames               = ['table1', 'table2', 'table3', 't_ignored'];
780
        $expectedConnectionAssets = [
781
            // ignores table1 + schema_filter applies
782
            'connection1' => ['table2', 'table3'],
783
            // ignores table2, no schema_filter applies
784
            'connection2' => ['table1', 'table3', 't_ignored'],
785
            // connection3 has no ignores, handled separately
786
        ];
787
788
        $this->compileContainer($container);
789
790
        $getConfiguration = static function (string $connectionName) use ($container) : Configuration {
791
            return $container->get(sprintf('doctrine.dbal.%s_connection', $connectionName))->getConfiguration();
792
        };
793
794
        foreach ($expectedConnectionAssets as $connectionName => $expectedTables) {
795
            $connConfig = $getConfiguration($connectionName);
796
            $this->assertSame($expectedTables, array_values(array_filter($assetNames, $connConfig->getSchemaAssetsFilter())), sprintf('Filtering for connection "%s"', $connectionName));
797
        }
798
799
        $this->assertNull($connConfig = $getConfiguration('connection3')->getSchemaAssetsFilter());
800
    }
801
802
    public function testWellKnownSchemaFilterDefaultTables()
803
    {
804
        if (! method_exists(Configuration::class, 'setSchemaAssetsFilter')) {
805
            $this->markTestSkipped('Test requires doctrine/dbal 2.9 or higher');
806
        }
807
808
        $container = $this->getContainer([]);
809
        $loader    = new DoctrineExtension();
810
        $container->registerExtension($loader);
811
        $container->addCompilerPass(new WellKnownSchemaFilterPass());
812
        $container->addCompilerPass(new DbalSchemaFilterPass());
813
814
        $this->loadFromFile($container, 'well_known_schema_filter_default_tables');
815
816
        $this->compileContainer($container);
817
818
        $definition = $container->getDefinition('doctrine.dbal.well_known_schema_asset_filter');
819
820
        $this->assertSame([['cache_items', 'lock_keys', 'sessions', 'messenger_messages']], $definition->getArguments());
821
        $this->assertSame([['connection' => 'connection1'], ['connection' => 'connection2'], ['connection' => 'connection3']], $definition->getTag('doctrine.dbal.schema_filter'));
822
823
        $definition = $container->getDefinition('doctrine.dbal.connection1_schema_asset_filter_manager');
824
825
        $this->assertEquals([new Reference('doctrine.dbal.well_known_schema_asset_filter'), new Reference('doctrine.dbal.connection1_regex_schema_filter')], $definition->getArgument(0));
826
827
        $filter = $container->get('well_known_filter');
828
829
        $this->assertFalse($filter('sessions'));
830
        $this->assertFalse($filter('cache_items'));
831
        $this->assertFalse($filter('lock_keys'));
832
        $this->assertFalse($filter('messenger_messages'));
833
        $this->assertTrue($filter('anything_else'));
834
    }
835
836
    public function testWellKnownSchemaFilterOverriddenTables()
837
    {
838
        if (! method_exists(Configuration::class, 'setSchemaAssetsFilter')) {
839
            $this->markTestSkipped('Test requires doctrine/dbal 2.9 or higher');
840
        }
841
842
        $container = $this->getContainer([]);
843
        $loader    = new DoctrineExtension();
844
        $container->registerExtension($loader);
845
        $container->addCompilerPass(new WellKnownSchemaFilterPass());
846
        $container->addCompilerPass(new DbalSchemaFilterPass());
847
848
        $this->loadFromFile($container, 'well_known_schema_filter_overridden_tables');
849
850
        $this->compileContainer($container);
851
852
        $filter = $container->get('well_known_filter');
853
854
        $this->assertFalse($filter('app_session'));
855
        $this->assertFalse($filter('app_cache'));
856
        $this->assertFalse($filter('app_locks'));
857
        $this->assertFalse($filter('app_messages'));
858
        $this->assertTrue($filter('sessions'));
859
        $this->assertTrue($filter('cache_items'));
860
        $this->assertTrue($filter('lock_keys'));
861
        $this->assertTrue($filter('messenger_messages'));
862
    }
863
864
    public function testEntityListenerResolver()
865
    {
866
        $container = $this->loadContainer('orm_entity_listener_resolver', ['YamlBundle'], new EntityListenerPass());
867
868
        $definition = $container->getDefinition('doctrine.orm.em1_configuration');
869
        $this->assertDICDefinitionMethodCallOnce($definition, 'setEntityListenerResolver', [new Reference('doctrine.orm.em1_entity_listener_resolver')]);
870
871
        $definition = $container->getDefinition('doctrine.orm.em2_configuration');
872
        $this->assertDICDefinitionMethodCallOnce($definition, 'setEntityListenerResolver', [new Reference('doctrine.orm.em2_entity_listener_resolver')]);
873
874
        $listener = $container->getDefinition('doctrine.orm.em1_entity_listener_resolver');
875
        $this->assertDICDefinitionMethodCallOnce($listener, 'registerService', ['EntityListener', 'entity_listener1']);
876
877
        $listener = $container->getDefinition('entity_listener_resolver');
878
        $this->assertDICDefinitionMethodCallOnce($listener, 'register', [new Reference('entity_listener2')]);
879
    }
880
881
    public function testAttachEntityListenerTag()
882
    {
883
        $container = $this->getContainer([]);
884
        $loader    = new DoctrineExtension();
885
        $container->registerExtension($loader);
886
        $container->addCompilerPass(new EntityListenerPass());
887
888
        $this->loadFromFile($container, 'orm_attach_entity_listener_tag');
889
890
        $this->compileContainer($container);
891
892
        $listener = $container->getDefinition('doctrine.orm.em1_entity_listener_resolver');
893
        $this->assertDICDefinitionMethodCallCount($listener, 'registerService', [
894
            ['EntityListener1', 'entity_listener1'],
895
            ['Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Fixtures\InvokableEntityListener', 'invokable_entity_listener'],
896
            ['Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Fixtures\InvokableEntityListener', 'invokable_entity_listener'],
897
            ['ParentEntityListener', 'children_entity_listener'],
898
        ], 4);
899
900
        $listener = $container->getDefinition('doctrine.orm.em2_entity_listener_resolver');
901
        $this->assertDICDefinitionMethodCallOnce($listener, 'registerService', ['EntityListener2', 'entity_listener2']);
902
903
        $attachListener = $container->getDefinition('doctrine.orm.em1_listeners.attach_entity_listeners');
904
        $this->assertDICDefinitionMethodCallAt(0, $attachListener, 'addEntityListener', ['My/Entity1', 'EntityListener1', 'postLoad']);
905
        $this->assertDICDefinitionMethodCallAt(1, $attachListener, 'addEntityListener', ['My/Entity1', 'Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Fixtures\InvokableEntityListener', 'loadClassMetadata', '__invoke']);
906
        $this->assertDICDefinitionMethodCallAt(2, $attachListener, 'addEntityListener', ['My/Entity1', 'Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Fixtures\InvokableEntityListener', 'postPersist']);
907
        $this->assertDICDefinitionMethodCallAt(3, $attachListener, 'addEntityListener', ['My/Entity3', 'ParentEntityListener', 'postLoad']);
908
909
        $attachListener = $container->getDefinition('doctrine.orm.em2_listeners.attach_entity_listeners');
910
        $this->assertDICDefinitionMethodCallOnce($attachListener, 'addEntityListener', ['My/Entity2', 'EntityListener2', 'preFlush', 'preFlushHandler']);
911
    }
912
913
    public function testAttachEntityListenersTwoConnections()
914
    {
915
        $container = $this->getContainer(['YamlBundle']);
916
        $loader    = new DoctrineExtension();
917
        $container->registerExtension($loader);
918
        $container->addCompilerPass(new RegisterEventListenersAndSubscribersPass('doctrine.connections', 'doctrine.dbal.%s_connection.event_manager', 'doctrine'));
919
920
        $this->loadFromFile($container, 'orm_attach_entity_listeners_two_connections');
921
922
        $this->compileContainer($container);
923
924
        $defaultEventManager = $container->getDefinition('doctrine.dbal.default_connection.event_manager');
925
        $this->assertDICDefinitionNoMethodCall($defaultEventManager, 'addEventListener', [['loadClassMetadata'], new Reference('doctrine.orm.em2_listeners.attach_entity_listeners')]);
926
        $this->assertDICDefinitionMethodCallOnce($defaultEventManager, 'addEventListener', [['loadClassMetadata'], new Reference('doctrine.orm.em1_listeners.attach_entity_listeners')]);
927
928
        $foobarEventManager = $container->getDefinition('doctrine.dbal.foobar_connection.event_manager');
929
        $this->assertDICDefinitionNoMethodCall($foobarEventManager, 'addEventListener', [['loadClassMetadata'], new Reference('doctrine.orm.em1_listeners.attach_entity_listeners')]);
930
        $this->assertDICDefinitionMethodCallOnce($foobarEventManager, 'addEventListener', [['loadClassMetadata'], new Reference('doctrine.orm.em2_listeners.attach_entity_listeners')]);
931
    }
932
933
    public function testAttachLazyEntityListener()
934
    {
935
        $container = $this->getContainer([]);
936
        $loader    = new DoctrineExtension();
937
        $container->registerExtension($loader);
938
        $container->addCompilerPass(new EntityListenerPass());
939
940
        $this->loadFromFile($container, 'orm_attach_lazy_entity_listener');
941
942
        $this->compileContainer($container);
943
944
        $resolver1 = $container->getDefinition('doctrine.orm.em1_entity_listener_resolver');
945
        $this->assertDICDefinitionMethodCallAt(0, $resolver1, 'registerService', ['EntityListener1', 'entity_listener1']);
946
        $this->assertDICDefinitionMethodCallAt(1, $resolver1, 'register', [new Reference('entity_listener3')]);
947
        $this->assertDICDefinitionMethodCallAt(2, $resolver1, 'registerService', ['EntityListener4', 'entity_listener4']);
948
949
        $serviceLocatorReference = $resolver1->getArgument(0);
950
        $this->assertInstanceOf(Reference::class, $serviceLocatorReference);
951
        $serviceLocatorDefinition = $container->getDefinition((string) $serviceLocatorReference);
952
        $this->assertSame(ServiceLocator::class, $serviceLocatorDefinition->getClass());
953
        $serviceLocatorMap = $serviceLocatorDefinition->getArgument(0);
954
        $this->assertSame(['entity_listener1', 'entity_listener4'], array_keys($serviceLocatorMap));
955
956
        $resolver2 = $container->findDefinition('custom_entity_listener_resolver');
957
        $this->assertDICDefinitionMethodCallOnce($resolver2, 'registerService', ['EntityListener2', 'entity_listener2']);
958
    }
959
960
    public function testAttachLazyEntityListenerForCustomResolver()
961
    {
962
        $container = $this->getContainer([]);
963
        $loader    = new DoctrineExtension();
964
        $container->registerExtension($loader);
965
        $container->addCompilerPass(new EntityListenerPass());
966
967
        $this->loadFromFile($container, 'orm_entity_listener_custom_resolver');
968
969
        $this->compileContainer($container);
970
971
        $resolver = $container->getDefinition('custom_entity_listener_resolver');
972
        $this->assertTrue($resolver->isPublic());
973
        $this->assertEmpty($resolver->getArguments(), 'We must not change the arguments for custom services.');
974
        $this->assertDICDefinitionMethodCallOnce($resolver, 'registerService', ['EntityListener', 'entity_listener']);
975
        $this->assertTrue($container->getDefinition('entity_listener')->isPublic());
976
    }
977
978
    /**
979
     * @expectedException \InvalidArgumentException
980
     * @expectedExceptionMessage EntityListenerServiceResolver
981
     */
982 View Code Duplication
    public function testLazyEntityListenerResolverWithoutCorrectInterface()
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...
983
    {
984
        $container = $this->getContainer([]);
985
        $loader    = new DoctrineExtension();
986
        $container->registerExtension($loader);
987
        $container->addCompilerPass(new EntityListenerPass());
988
989
        $this->loadFromFile($container, 'orm_entity_listener_lazy_resolver_without_interface');
990
991
        $this->compileContainer($container);
992
    }
993
994
    public function testPrivateLazyEntityListener()
995
    {
996
        $container = $this->getContainer([]);
997
        $loader    = new DoctrineExtension();
998
        $container->registerExtension($loader);
999
        $container->addCompilerPass(new EntityListenerPass());
1000
1001
        $this->loadFromFile($container, 'orm_entity_listener_lazy_private');
1002
1003
        $this->compileContainer($container);
1004
1005
        $this->assertTrue($container->getDefinition('doctrine.orm.em1_entity_listener_resolver')->isPublic());
1006
    }
1007
1008
    /**
1009
     * @expectedException \InvalidArgumentException
1010
     * @expectedExceptionMessageRegExp /The service ".*" must not be abstract as this entity listener is lazy-loaded/
1011
     */
1012 View Code Duplication
    public function testAbstractLazyEntityListener()
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...
1013
    {
1014
        $container = $this->getContainer([]);
1015
        $loader    = new DoctrineExtension();
1016
        $container->registerExtension($loader);
1017
        $container->addCompilerPass(new EntityListenerPass());
1018
1019
        $this->loadFromFile($container, 'orm_entity_listener_lazy_abstract');
1020
1021
        $this->compileContainer($container);
1022
    }
1023
1024
    public function testRepositoryFactory()
1025
    {
1026
        $container = $this->loadContainer('orm_repository_factory');
1027
1028
        $definition = $container->getDefinition('doctrine.orm.default_configuration');
1029
        $this->assertDICDefinitionMethodCallOnce($definition, 'setRepositoryFactory', ['repository_factory']);
1030
    }
1031
1032
    private function loadContainer($fixture, array $bundles = ['YamlBundle'], CompilerPassInterface $compilerPass = null)
1033
    {
1034
        $container = $this->getContainer($bundles);
1035
        $container->registerExtension(new DoctrineExtension());
1036
1037
        $this->loadFromFile($container, $fixture);
1038
1039
        if ($compilerPass !== null) {
1040
            $container->addCompilerPass($compilerPass);
1041
        }
1042
1043
        $this->compileContainer($container);
1044
1045
        return $container;
1046
    }
1047
1048
    private function getContainer(array $bundles)
1049
    {
1050
        $map = [];
1051
        foreach ($bundles as $bundle) {
1052
            require_once __DIR__ . '/Fixtures/Bundles/' . $bundle . '/' . $bundle . '.php';
1053
1054
            $map[$bundle] = 'Fixtures\\Bundles\\' . $bundle . '\\' . $bundle;
1055
        }
1056
1057
        $container = new ContainerBuilder(new ParameterBag([
1058
            'kernel.name' => 'app',
1059
            'kernel.debug' => false,
1060
            'kernel.bundles' => $map,
1061
            'kernel.cache_dir' => sys_get_temp_dir(),
1062
            'kernel.environment' => 'test',
1063
            'kernel.root_dir' => __DIR__ . '/../../', // src dir
1064
            'kernel.project_dir' => __DIR__ . '/../../', // src dir
1065
            'kernel.bundles_metadata' => [],
1066
            'container.build_id' => uniqid(),
1067
        ]));
1068
1069
        // Register dummy cache services so we don't have to load the FrameworkExtension
1070
        $container->setDefinition('cache.system', (new Definition(ArrayAdapter::class))->setPublic(true));
1071
        $container->setDefinition('cache.app', (new Definition(ArrayAdapter::class))->setPublic(true));
1072
1073
        return $container;
1074
    }
1075
1076
    /**
1077
     * Assertion on the Class of a DIC Service Definition.
1078
     *
1079
     * @param string $expectedClass
1080
     */
1081
    private function assertDICDefinitionClass(Definition $definition, $expectedClass)
1082
    {
1083
        $this->assertEquals($expectedClass, $definition->getClass(), 'Expected Class of the DIC Container Service Definition is wrong.');
1084
    }
1085
1086
    private function assertDICConstructorArguments(Definition $definition, $args)
1087
    {
1088
        $this->assertEquals($args, $definition->getArguments(), "Expected and actual DIC Service constructor arguments of definition '" . $definition->getClass() . "' don't match.");
1089
    }
1090
1091 View Code Duplication
    private function assertDICDefinitionMethodCallAt($pos, Definition $definition, $methodName, array $params = null)
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...
1092
    {
1093
        $calls = $definition->getMethodCalls();
1094
        if (! isset($calls[$pos][0])) {
1095
            $this->fail(sprintf('Method call at position %s not found!', $pos));
1096
1097
            return;
1098
        }
1099
1100
        $this->assertEquals($methodName, $calls[$pos][0], "Method '" . $methodName . "' is expected to be called at position " . $pos . '.');
1101
1102
        if ($params === null) {
1103
            return;
1104
        }
1105
1106
        $this->assertEquals($params, $calls[$pos][1], "Expected parameters to methods '" . $methodName . "' do not match the actual parameters.");
1107
    }
1108
1109
    /**
1110
     * Assertion for the DI Container, check if the given definition contains a method call with the given parameters.
1111
     *
1112
     * @param string $methodName
1113
     * @param array  $params
0 ignored issues
show
Documentation introduced by
Should the type for parameter $params not be null|array?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
1114
     */
1115 View Code Duplication
    private function assertDICDefinitionMethodCallOnce(Definition $definition, $methodName, array $params = null)
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...
1116
    {
1117
        $calls  = $definition->getMethodCalls();
1118
        $called = false;
1119
        foreach ($calls as $call) {
1120
            if ($call[0] !== $methodName) {
1121
                continue;
1122
            }
1123
1124
            if ($called) {
1125
                $this->fail("Method '" . $methodName . "' is expected to be called only once, a second call was registered though.");
1126
            } else {
1127
                $called = true;
1128
                if ($params !== null) {
1129
                    $this->assertEquals($params, $call[1], "Expected parameters to methods '" . $methodName . "' do not match the actual parameters.");
1130
                }
1131
            }
1132
        }
1133
        if ($called) {
1134
            return;
1135
        }
1136
1137
        $this->fail("Method '" . $methodName . "' is expected to be called once, definition does not contain a call though.");
1138
    }
1139
1140
    private function assertDICDefinitionMethodCallCount(Definition $definition, $methodName, array $params = [], $nbCalls = 1)
1141
    {
1142
        $calls  = $definition->getMethodCalls();
1143
        $called = 0;
1144
        foreach ($calls as $call) {
1145
            if ($call[0] !== $methodName) {
1146
                continue;
1147
            }
1148
1149
            if ($called > $nbCalls) {
1150
                break;
1151
            }
1152
1153
            if (isset($params[$called])) {
1154
                $this->assertEquals($params[$called], $call[1], "Expected parameters to methods '" . $methodName . "' do not match the actual parameters.");
1155
            }
1156
            $called++;
1157
        }
1158
1159
        $this->assertEquals($nbCalls, $called, sprintf('The method "%s" should be called %d times', $methodName, $nbCalls));
1160
    }
1161
1162
    /**
1163
     * Assertion for the DI Container, check if the given definition does not contain a method call with the given parameters.
1164
     *
1165
     * @param string $methodName
1166
     * @param array  $params
0 ignored issues
show
Documentation introduced by
Should the type for parameter $params not be null|array?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
1167
     */
1168
    private function assertDICDefinitionNoMethodCall(Definition $definition, $methodName, array $params = null)
1169
    {
1170
        $calls = $definition->getMethodCalls();
1171
        foreach ($calls as $call) {
1172
            if ($call[0] !== $methodName) {
1173
                continue;
1174
            }
1175
1176
            if ($params !== null) {
1177
                $this->assertNotEquals($params, $call[1], "Method '" . $methodName . "' is not expected to be called with the given parameters.");
1178
            } else {
1179
                $this->fail("Method '" . $methodName . "' is not expected to be called");
1180
            }
1181
        }
1182
    }
1183
1184
    private function compileContainer(ContainerBuilder $container)
1185
    {
1186
        $container->getCompilerPassConfig()->setOptimizationPasses([new ResolveChildDefinitionsPass()]);
1187
        $container->getCompilerPassConfig()->setRemovingPasses([]);
1188
        $container->compile();
1189
    }
1190
}
1191
1192
class DummySchemaAssetsFilter
1193
{
1194
    /** @var string */
1195
    private $tableToIgnore;
1196
1197
    public function __construct(string $tableToIgnore)
1198
    {
1199
        $this->tableToIgnore = $tableToIgnore;
1200
    }
1201
1202
    public function __invoke($assetName) : bool
1203
    {
1204
        if ($assetName instanceof AbstractAsset) {
1205
            $assetName = $assetName->getName();
1206
        }
1207
1208
        return $assetName !== $this->tableToIgnore;
1209
    }
1210
}
1211