Completed
Push — master ( 28438d...1818d3 )
by Marco
05:22
created

DoctrineExtensionTest::testDbalWrapperClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 14
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Doctrine Bundle
5
 *
6
 * The code was originally distributed inside the Symfony framework.
7
 *
8
 * (c) Fabien Potencier <[email protected]>
9
 * (c) Doctrine Project, Benjamin Eberlei <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection;
16
17
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\DoctrineExtension;
18
use Doctrine\Bundle\DoctrineBundle\Tests\Builder\BundleConfigurationBuilder;
19
use Doctrine\Common\Proxy\AbstractProxyFactory;
20
use Doctrine\DBAL\Connection;
21
use Doctrine\ORM\Version;
22
use PHPUnit\Framework\TestCase;
23
use Symfony\Component\DependencyInjection\Compiler\ResolveDefinitionTemplatesPass;
24
use Symfony\Component\DependencyInjection\ContainerBuilder;
25
use Symfony\Component\DependencyInjection\Definition;
26
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
27
use Symfony\Component\DependencyInjection\Reference;
28
29
class DoctrineExtensionTest extends TestCase
30
{
31
32
    public function testDbalGenerateDefaultConnectionConfiguration()
33
    {
34
        $container = $this->getContainer();
35
        $extension = new DoctrineExtension();
36
37
        $container->registerExtension($extension);
38
39
        $extension->load(array(array('dbal' => array())), $container);
40
41
        // doctrine.dbal.default_connection
42
        $this->assertEquals('%doctrine.default_connection%', $container->getDefinition('doctrine')->getArgument(3));
43
        $this->assertEquals('default', $container->getParameter('doctrine.default_connection'));
44
        $this->assertEquals('root', $container->getDefinition('doctrine.dbal.default_connection')->getArgument(0)['user']);
45
        $this->assertNull($container->getDefinition('doctrine.dbal.default_connection')->getArgument(0)['password']);
46
        $this->assertEquals('localhost', $container->getDefinition('doctrine.dbal.default_connection')->getArgument(0)['host']);
47
        $this->assertNull($container->getDefinition('doctrine.dbal.default_connection')->getArgument(0)['port']);
48
        $this->assertEquals('pdo_mysql', $container->getDefinition('doctrine.dbal.default_connection')->getArgument(0)['driver']);
49
        $this->assertEquals(array(), $container->getDefinition('doctrine.dbal.default_connection')->getArgument(0)['driverOptions']);
50
    }
51
52
    public function testDbalOverrideDefaultConnection()
53
    {
54
        $container = $this->getContainer();
55
        $extension = new DoctrineExtension();
56
57
        $container->registerExtension($extension);
58
59
        $extension->load(array(array(), array('dbal' => array('default_connection' => 'foo')), array()), $container);
60
61
        // doctrine.dbal.default_connection
62
        $this->assertEquals('%doctrine.default_connection%', $container->getDefinition('doctrine')->getArgument(3), '->load() overrides existing configuration options');
63
        $this->assertEquals('foo', $container->getParameter('doctrine.default_connection'), '->load() overrides existing configuration options');
64
    }
65
66
    /**
67
     * @expectedException \LogicException
68
     * @expectedExceptionMessage Configuring the ORM layer requires to configure the DBAL layer as well.
69
     */
70
    public function testOrmRequiresDbal()
71
    {
72
        $extension = new DoctrineExtension();
73
74
        $extension->load(array(array('orm' => array('auto_mapping' => true))), $this->getContainer());
75
    }
76
77
    public function getAutomappingConfigurations()
78
    {
79
        return array(
80
            array(
81
                array(
82
                    'em1' => array(
83
                        'mappings' => array(
84
                            'YamlBundle' => null,
85
                        ),
86
                    ),
87
                    'em2' => array(
88
                        'mappings' => array(
89
                            'XmlBundle' => null,
90
                        ),
91
                    ),
92
                ),
93
            ),
94
            array(
95
                array(
96
                    'em1' => array(
97
                        'auto_mapping' => true,
98
                    ),
99
                    'em2' => array(
100
                        'mappings' => array(
101
                            'XmlBundle' => null,
102
                        ),
103
                    ),
104
                ),
105
            ),
106
            array(
107
                array(
108
                    'em1' => array(
109
                        'auto_mapping' => true,
110
                        'mappings' => array(
111
                            'YamlBundle' => null,
112
                        ),
113
                    ),
114
                    'em2' => array(
115
                        'mappings' => array(
116
                            'XmlBundle' => null,
117
                        ),
118
                    ),
119
                ),
120
            ),
121
        );
122
    }
123
124
    /**
125
     * @dataProvider getAutomappingConfigurations
126
     */
127
    public function testAutomapping(array $entityManagers)
128
    {
129
        $extension = new DoctrineExtension();
130
131
        if (!method_exists($extension, 'fixManagersAutoMappings')) {
132
            $this->markTestSkipped('Auto mapping with multiple managers available with Symfony ~2.6');
133
        }
134
135
        $container = $this->getContainer(array(
0 ignored issues
show
Documentation introduced by
array('YamlBundle', 'XmlBundle') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string.

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...
136
            'YamlBundle',
137
            'XmlBundle',
138
        ));
139
140
        $extension->load(
141
            array(
142
                array(
143
                    'dbal' => array(
144
                        'default_connection' => 'cn1',
145
                        'connections' => array(
146
                            'cn1' => array(),
147
                            'cn2' => array(),
148
                        ),
149
                    ),
150
                    'orm' => array(
151
                        'entity_managers' => $entityManagers,
152
                    ),
153
                ),
154
            ), $container);
155
156
        $configEm1 = $container->getDefinition('doctrine.orm.em1_configuration');
157
        $configEm2 = $container->getDefinition('doctrine.orm.em2_configuration');
158
159
        $this->assertContains(
160
            array(
161
                'setEntityNamespaces',
162
                array(
163
                    array(
164
                        'YamlBundle' => 'Fixtures\Bundles\YamlBundle\Entity',
165
                    ),
166
                ),
167
            ),
168
            $configEm1->getMethodCalls()
169
        );
170
171
        $this->assertContains(
172
            array(
173
                'setEntityNamespaces',
174
                array(
175
                    array(
176
                        'XmlBundle' => 'Fixtures\Bundles\XmlBundle\Entity',
177
                    ),
178
                ),
179
            ),
180
            $configEm2->getMethodCalls()
181
        );
182
    }
183
184
    public function testDbalLoad()
185
    {
186
        $container = $this->getContainer();
187
        $extension = new DoctrineExtension();
188
189
        $extension->load(array(
190
            array('dbal' => array('connections' => array('default' => array('password' => 'foo')))),
191
            array(),
192
            array('dbal' => array('default_connection' => 'foo')),
193
            array()), $container);
194
195
        $config = $container->getDefinition('doctrine.dbal.default_connection')->getArgument(0);
196
197
        $this->assertEquals('foo', $config['password']);
198
        $this->assertEquals('root', $config['user']);
199
    }
200
201
    public function testDbalWrapperClass()
202
    {
203
        $container = $this->getContainer();
204
        $extension = new DoctrineExtension();
205
206
        $extension->load(
207
            array(
208
                array('dbal' => array('connections' => array(
209
                    'default' => array('password' => 'foo', 'wrapper_class' => TestWrapperClass::class),
210
                    'second' => array('password' => 'boo'),
211
                ))),
212
                array(),
213
                array('dbal' => array('default_connection' => 'foo')),
214
                array()
215
            ),
216
            $container
217
        );
218
219
        $this->assertEquals(TestWrapperClass::class, $container->getDefinition('doctrine.dbal.default_connection')->getClass());
220
        $this->assertNull($container->getDefinition('doctrine.dbal.second_connection')->getClass());
221
    }
222
223
    public function testDependencyInjectionConfigurationDefaults()
224
    {
225
        $container = $this->getContainer();
226
        $extension = new DoctrineExtension();
227
        $config = BundleConfigurationBuilder::createBuilderWithBaseValues()->build();
228
229
        $extension->load(array($config), $container);
230
231
        $this->assertFalse($container->getParameter('doctrine.orm.auto_generate_proxy_classes'));
232
        $this->assertEquals('Doctrine\ORM\Configuration', $container->getParameter('doctrine.orm.configuration.class'));
233
        $this->assertEquals('Doctrine\ORM\EntityManager', $container->getParameter('doctrine.orm.entity_manager.class'));
234
        $this->assertEquals('Proxies', $container->getParameter('doctrine.orm.proxy_namespace'));
235
        $this->assertEquals('Doctrine\Common\Cache\ArrayCache', $container->getParameter('doctrine.orm.cache.array.class'));
236
        $this->assertEquals('Doctrine\Common\Cache\ApcCache', $container->getParameter('doctrine.orm.cache.apc.class'));
237
        $this->assertEquals('Doctrine\Common\Cache\MemcacheCache', $container->getParameter('doctrine.orm.cache.memcache.class'));
238
        $this->assertEquals('localhost', $container->getParameter('doctrine.orm.cache.memcache_host'));
239
        $this->assertEquals('11211', $container->getParameter('doctrine.orm.cache.memcache_port'));
240
        $this->assertEquals('Memcache', $container->getParameter('doctrine.orm.cache.memcache_instance.class'));
241
        $this->assertEquals('Doctrine\Common\Cache\XcacheCache', $container->getParameter('doctrine.orm.cache.xcache.class'));
242
        $this->assertEquals('Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain', $container->getParameter('doctrine.orm.metadata.driver_chain.class'));
243
        $this->assertEquals('Doctrine\ORM\Mapping\Driver\AnnotationDriver', $container->getParameter('doctrine.orm.metadata.annotation.class'));
244
        $this->assertEquals('Doctrine\ORM\Mapping\Driver\SimplifiedXmlDriver', $container->getParameter('doctrine.orm.metadata.xml.class'));
245
        $this->assertEquals('Doctrine\ORM\Mapping\Driver\SimplifiedYamlDriver', $container->getParameter('doctrine.orm.metadata.yml.class'));
246
247
        // second-level cache
248
        $this->assertEquals('Doctrine\ORM\Cache\DefaultCacheFactory', $container->getParameter('doctrine.orm.second_level_cache.default_cache_factory.class'));
249
        $this->assertEquals('Doctrine\ORM\Cache\Region\DefaultRegion', $container->getParameter('doctrine.orm.second_level_cache.default_region.class'));
250
        $this->assertEquals('Doctrine\ORM\Cache\Region\FileLockRegion', $container->getParameter('doctrine.orm.second_level_cache.filelock_region.class'));
251
        $this->assertEquals('Doctrine\ORM\Cache\Logging\CacheLoggerChain', $container->getParameter('doctrine.orm.second_level_cache.logger_chain.class'));
252
        $this->assertEquals('Doctrine\ORM\Cache\Logging\StatisticsCacheLogger', $container->getParameter('doctrine.orm.second_level_cache.logger_statistics.class'));
253
        $this->assertEquals('Doctrine\ORM\Cache\CacheConfiguration', $container->getParameter('doctrine.orm.second_level_cache.cache_configuration.class'));
254
        $this->assertEquals('Doctrine\ORM\Cache\RegionsConfiguration', $container->getParameter('doctrine.orm.second_level_cache.regions_configuration.class'));
255
256
257
        $config = BundleConfigurationBuilder::createBuilder()
258
            ->addBaseConnection()
259
            ->addEntityManager(array(
260
                'proxy_namespace' => 'MyProxies',
261
                'auto_generate_proxy_classes' => true,
262
                'default_entity_manager' => 'default',
263
                'entity_managers' => array(
264
                    'default' => array(
265
                        'mappings' => array('YamlBundle' => array()),
266
                    ),
267
                ),
268
            ))
269
            ->build();
270
271
        $container = $this->getContainer();
272
        $extension->load(array($config), $container);
273
        $this->compileContainer($container);
274
275
        $definition = $container->getDefinition('doctrine.dbal.default_connection');
276
277
        $args = $definition->getArguments();
278
        $this->assertEquals('pdo_mysql', $args[0]['driver']);
279
        $this->assertEquals('localhost', $args[0]['host']);
280
        $this->assertEquals('root', $args[0]['user']);
281
        $this->assertEquals('doctrine.dbal.default_connection.configuration', (string) $args[1]);
282
        $this->assertEquals('doctrine.dbal.default_connection.event_manager', (string) $args[2]);
283
        $this->assertCount(0, $definition->getMethodCalls());
284
285
        $definition = $container->getDefinition('doctrine.orm.default_entity_manager');
286
        $this->assertEquals('%doctrine.orm.entity_manager.class%', $definition->getClass());
287 View Code Duplication
        if (method_exists($definition, 'getFactory')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
288
            $this->assertEquals(array('%doctrine.orm.entity_manager.class%', 'create'), $definition->getFactory());
289
        } else {
290
            $this->assertEquals('%doctrine.orm.entity_manager.class%', $definition->getFactoryClass());
0 ignored issues
show
Bug introduced by
The method getFactoryClass() does not exist on Symfony\Component\DependencyInjection\Definition. Did you maybe mean getFactory()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
291
            $this->assertEquals('create', $definition->getFactoryMethod());
0 ignored issues
show
Bug introduced by
The method getFactoryMethod() does not exist on Symfony\Component\DependencyInjection\Definition. Did you maybe mean getFactory()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
292
        }
293
294
        $this->assertEquals(array('default' => 'doctrine.orm.default_entity_manager'), $container->getParameter('doctrine.entity_managers'), "Set of the existing EntityManagers names is incorrect.");
295
        $this->assertEquals('%doctrine.entity_managers%', $container->getDefinition('doctrine')->getArgument(2), "Set of the existing EntityManagers names is incorrect.");
296
297
        $arguments = $definition->getArguments();
298
        $this->assertInstanceOf('Symfony\Component\DependencyInjection\Reference', $arguments[0]);
299
        $this->assertEquals('doctrine.dbal.default_connection', (string) $arguments[0]);
300
        $this->assertInstanceOf('Symfony\Component\DependencyInjection\Reference', $arguments[1]);
301
        $this->assertEquals('doctrine.orm.default_configuration', (string) $arguments[1]);
302
303
        $definition = $container->getDefinition('doctrine.orm.default_configuration');
304
        $calls = array_values($definition->getMethodCalls());
305
        $this->assertEquals(array('YamlBundle' => 'Fixtures\Bundles\YamlBundle\Entity'), $calls[0][1][0]);
306
        $this->assertEquals('doctrine.orm.default_metadata_cache', (string) $calls[1][1][0]);
307
        $this->assertEquals('doctrine.orm.default_query_cache', (string) $calls[2][1][0]);
308
        $this->assertEquals('doctrine.orm.default_result_cache', (string) $calls[3][1][0]);
309
310
        if (version_compare(Version::VERSION, "2.3.0-DEV") >= 0) {
311
            $this->assertEquals('doctrine.orm.naming_strategy.default', (string) $calls[10][1][0]);
312
            $this->assertEquals('doctrine.orm.quote_strategy.default', (string) $calls[11][1][0]);
313
        }
314
        if (version_compare(Version::VERSION, "2.4.0-DEV") >= 0) {
315
            $this->assertEquals('doctrine.orm.default_entity_listener_resolver', (string) $calls[12][1][0]);
316
        }
317
318
        $definition = $container->getDefinition($container->getAlias('doctrine.orm.default_metadata_cache'));
319
        $this->assertEquals('%doctrine_cache.array.class%', $definition->getClass());
320
321
        $definition = $container->getDefinition($container->getAlias('doctrine.orm.default_query_cache'));
322
        $this->assertEquals('%doctrine_cache.array.class%', $definition->getClass());
323
324
        $definition = $container->getDefinition($container->getAlias('doctrine.orm.default_result_cache'));
325
        $this->assertEquals('%doctrine_cache.array.class%', $definition->getClass());
326
    }
327
328
    public function testUseSavePointsAddMethodCallToAddSavepointsToTheConnection()
329
    {
330
        $container = $this->getContainer();
331
        $extension = new DoctrineExtension();
332
333
        $extension->load(array(array('dbal' => array('connections' => array(
334
            'default' => array('password' => 'foo', 'use_savepoints' => true)
335
        )))), $container);
336
337
        $calls = $container->getDefinition('doctrine.dbal.default_connection')->getMethodCalls();
338
        $this->assertCount(1, $calls);
339
        $this->assertEquals('setNestTransactionsWithSavepoints', $calls[0][0]);
340
        $this->assertTrue($calls[0][1][0]);
341
    }
342
343
    public function testAutoGenerateProxyClasses()
344
    {
345
        $container = $this->getContainer();
346
        $extension = new DoctrineExtension();
347
348
        $config = BundleConfigurationBuilder::createBuilder()
349
            ->addBaseConnection()
350
            ->addEntityManager(array(
351
                'proxy_namespace' => 'MyProxies',
352
                'auto_generate_proxy_classes' => 'eval',
353
                'default_entity_manager' => 'default',
354
                'entity_managers' => array(
355
                    'default' => array(
356
                        'mappings' => array('YamlBundle' => array()),
357
                    ),
358
                ),
359
            ))
360
            ->build();
361
362
        $extension->load(array($config), $container);
363
364
        $this->assertEquals(AbstractProxyFactory::AUTOGENERATE_EVAL, $container->getParameter('doctrine.orm.auto_generate_proxy_classes'));
365
    }
366
367
    public function testSingleEntityManagerWithDefaultConfiguration()
368
    {
369
        $container = $this->getContainer();
370
        $extension = new DoctrineExtension();
371
372
        $configurationArray = BundleConfigurationBuilder::createBuilderWithBaseValues()->build();
373
374
        $extension->load(array($configurationArray), $container);
375
        $this->compileContainer($container);
376
377
        $definition = $container->getDefinition('doctrine.orm.default_entity_manager');
378
        $this->assertEquals('%doctrine.orm.entity_manager.class%', $definition->getClass());
379 View Code Duplication
        if (method_exists($definition, 'getFactory')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
380
            $this->assertEquals(array('%doctrine.orm.entity_manager.class%', 'create'), $definition->getFactory());
381
        } else {
382
            $this->assertEquals('%doctrine.orm.entity_manager.class%', $definition->getFactoryClass());
0 ignored issues
show
Bug introduced by
The method getFactoryClass() does not exist on Symfony\Component\DependencyInjection\Definition. Did you maybe mean getFactory()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
383
            $this->assertEquals('create', $definition->getFactoryMethod());
0 ignored issues
show
Bug introduced by
The method getFactoryMethod() does not exist on Symfony\Component\DependencyInjection\Definition. Did you maybe mean getFactory()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
384
        }
385
386
        $this->assertDICConstructorArguments($definition, array(
387
            new Reference('doctrine.dbal.default_connection'), new Reference('doctrine.orm.default_configuration'),
388
        ));
389
    }
390
391
    public function testSingleEntityManagerWithDefaultSecondLevelCacheConfiguration()
392
    {
393
        if (version_compare(Version::VERSION, "2.5.0-DEV") < 0) {
394
            $this->markTestSkipped(sprintf('Second Level cache not supported by this version of the ORM : %s', Version::VERSION));
395
        }
396
        $container = $this->getContainer();
397
        $extension = new DoctrineExtension();
398
399
        $configurationArray = BundleConfigurationBuilder::createBuilderWithBaseValues()
400
            ->addBaseSecondLevelCache()
401
            ->build();
402
403
        $extension->load(array($configurationArray), $container);
404
        $this->compileContainer($container);
405
406
        $definition = $container->getDefinition('doctrine.orm.default_entity_manager');
407
        $this->assertEquals('%doctrine.orm.entity_manager.class%', $definition->getClass());
408
        if (method_exists($definition, 'getFactory')) {
409
            $this->assertEquals(array('%doctrine.orm.entity_manager.class%', 'create'), $definition->getFactory());
410
        } else {
411
            $this->assertEquals('%doctrine.orm.entity_manager.class%', $definition->getFactoryClass());
0 ignored issues
show
Bug introduced by
The method getFactoryClass() does not exist on Symfony\Component\DependencyInjection\Definition. Did you maybe mean getFactory()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
412
            $this->assertEquals('create', $definition->getFactoryMethod());
0 ignored issues
show
Bug introduced by
The method getFactoryMethod() does not exist on Symfony\Component\DependencyInjection\Definition. Did you maybe mean getFactory()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
413
        }
414
415
        $this->assertDICConstructorArguments($definition, array(
416
            new Reference('doctrine.dbal.default_connection'), new Reference('doctrine.orm.default_configuration'),
417
        ));
418
419
        $slcDefinition = $container->getDefinition('doctrine.orm.default_second_level_cache.default_cache_factory');
420
        $this->assertEquals('%doctrine.orm.second_level_cache.default_cache_factory.class%', $slcDefinition->getClass());
421
    }
422
423
    public function testSingleEntityManagerWithCustomSecondLevelCacheConfiguration()
424
    {
425
        if (version_compare(Version::VERSION, "2.5.0-DEV") < 0) {
426
            $this->markTestSkipped(sprintf('Second Level cache not supported by this version of the ORM : %s', Version::VERSION));
427
        }
428
        $container = $this->getContainer();
429
        $extension = new DoctrineExtension();
430
431
        $configurationArray = BundleConfigurationBuilder::createBuilderWithBaseValues()
432
            ->addSecondLevelCache([
433
                'region_cache_driver' => [
434
                    'type' => 'memcache'],
435
                'regions' => [
436
                    'hour_region' => [
437
                        'lifetime' => 3600
438
                    ]
439
                ],
440
                'factory' => 'YamlBundle\Cache\MyCacheFactory',
441
            ])
442
            ->build();
443
444
        $extension->load(array($configurationArray), $container);
445
        $this->compileContainer($container);
446
447
        $definition = $container->getDefinition('doctrine.orm.default_entity_manager');
448
        $this->assertEquals('%doctrine.orm.entity_manager.class%', $definition->getClass());
449 View Code Duplication
        if (method_exists($definition, 'getFactory')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
450
            $this->assertEquals(array('%doctrine.orm.entity_manager.class%', 'create'), $definition->getFactory());
451
        } else {
452
            $this->assertEquals('%doctrine.orm.entity_manager.class%', $definition->getFactoryClass());
0 ignored issues
show
Bug introduced by
The method getFactoryClass() does not exist on Symfony\Component\DependencyInjection\Definition. Did you maybe mean getFactory()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
453
            $this->assertEquals('create', $definition->getFactoryMethod());
0 ignored issues
show
Bug introduced by
The method getFactoryMethod() does not exist on Symfony\Component\DependencyInjection\Definition. Did you maybe mean getFactory()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
454
        }
455
456
        $this->assertDICConstructorArguments($definition, array(
457
            new Reference('doctrine.dbal.default_connection'), new Reference('doctrine.orm.default_configuration'),
458
        ));
459
460
        $slcDefinition = $container->getDefinition('doctrine.orm.default_second_level_cache.default_cache_factory');
461
        $this->assertEquals('YamlBundle\Cache\MyCacheFactory', $slcDefinition->getClass());
462
    }
463
464 View Code Duplication
    public function testBundleEntityAliases()
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...
465
    {
466
        $container = $this->getContainer();
467
        $extension = new DoctrineExtension();
468
469
        $config = BundleConfigurationBuilder::createBuilder()
470
             ->addBaseConnection()
471
             ->build();
472
        $config['orm'] = array('default_entity_manager' => 'default', 'entity_managers' => array('default' => array('mappings' => array('YamlBundle' => array()))));
473
        $extension->load(array($config), $container);
474
475
        $definition = $container->getDefinition('doctrine.orm.default_configuration');
476
        $this->assertDICDefinitionMethodCallOnce($definition, 'setEntityNamespaces',
477
            array(array('YamlBundle' => 'Fixtures\Bundles\YamlBundle\Entity'))
478
        );
479
    }
480
481 View Code Duplication
    public function testOverwriteEntityAliases()
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...
482
    {
483
        $container = $this->getContainer();
484
        $extension = new DoctrineExtension();
485
486
        $config = BundleConfigurationBuilder::createBuilder()
487
             ->addBaseConnection()
488
             ->build();
489
        $config['orm'] = array('default_entity_manager' => 'default', 'entity_managers' => array('default' => array('mappings' => array('YamlBundle' => array('alias' => 'yml')))));
490
        $extension->load(array($config), $container);
491
492
        $definition = $container->getDefinition('doctrine.orm.default_configuration');
493
        $this->assertDICDefinitionMethodCallOnce($definition, 'setEntityNamespaces',
494
            array(array('yml' => 'Fixtures\Bundles\YamlBundle\Entity'))
495
        );
496
    }
497
498
    public function testYamlBundleMappingDetection()
499
    {
500
        $container = $this->getContainer('YamlBundle');
501
        $extension = new DoctrineExtension();
502
503
        $config = BundleConfigurationBuilder::createBuilder()
504
            ->addBaseConnection()
505
            ->addBaseEntityManager()
506
            ->build();
507
        $extension->load(array($config), $container);
508
509
        $definition = $container->getDefinition('doctrine.orm.default_metadata_driver');
510
        $this->assertDICDefinitionMethodCallOnce($definition, 'addDriver', array(
511
            new Reference('doctrine.orm.default_yml_metadata_driver'),
512
            'Fixtures\Bundles\YamlBundle\Entity',
513
        ));
514
    }
515
516 View Code Duplication
    public function testXmlBundleMappingDetection()
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...
517
    {
518
        $container = $this->getContainer('XmlBundle');
519
        $extension = new DoctrineExtension();
520
521
        $config = BundleConfigurationBuilder::createBuilder()
522
            ->addBaseConnection()
523
            ->addEntityManager(array(
524
                'default_entity_manager' => 'default',
525
                'entity_managers' => array(
526
                    'default' => array(
527
                        'mappings' => array(
528
                            'XmlBundle' => array()
529
                        )
530
                    )
531
                )
532
            ))
533
            ->build();
534
        $extension->load(array($config), $container);
535
536
        $definition = $container->getDefinition('doctrine.orm.default_metadata_driver');
537
        $this->assertDICDefinitionMethodCallOnce($definition, 'addDriver', array(
538
            new Reference('doctrine.orm.default_xml_metadata_driver'),
539
            'Fixtures\Bundles\XmlBundle\Entity',
540
        ));
541
    }
542
543 View Code Duplication
    public function testAnnotationsBundleMappingDetection()
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...
544
    {
545
        $container = $this->getContainer('AnnotationsBundle');
546
        $extension = new DoctrineExtension();
547
548
        $config = BundleConfigurationBuilder::createBuilder()
549
            ->addBaseConnection()
550
            ->addEntityManager(array(
551
                'default_entity_manager' => 'default',
552
                'entity_managers' => array(
553
                    'default' => array(
554
                        'mappings' => array(
555
                            'AnnotationsBundle' => array()
556
                        )
557
                    )
558
                )
559
            ))
560
            ->build();
561
        $extension->load(array($config), $container);
562
563
        $definition = $container->getDefinition('doctrine.orm.default_metadata_driver');
564
        $this->assertDICDefinitionMethodCallOnce($definition, 'addDriver', array(
565
            new Reference('doctrine.orm.default_annotation_metadata_driver'),
566
            'Fixtures\Bundles\AnnotationsBundle\Entity',
567
        ));
568
    }
569
570
    public function testOrmMergeConfigs()
571
    {
572
        $container = $this->getContainer(array('XmlBundle', 'AnnotationsBundle'));
0 ignored issues
show
Documentation introduced by
array('XmlBundle', 'AnnotationsBundle') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string.

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...
573
        $extension = new DoctrineExtension();
574
575
        $config1 = BundleConfigurationBuilder::createBuilder()
576
            ->addBaseConnection()
577
            ->addEntityManager(array(
578
                'auto_generate_proxy_classes' => true,
579
                'default_entity_manager' => 'default',
580
                'entity_managers' => array(
581
                    'default' => array(
582
                        'mappings' => array(
583
                            'AnnotationsBundle' => array()
584
                        )
585
                    ),
586
                ),
587
            ))
588
            ->build();
589
        $config2 = BundleConfigurationBuilder::createBuilder()
590
            ->addBaseConnection()
591
            ->addEntityManager(array(
592
                'auto_generate_proxy_classes' => false,
593
                'default_entity_manager' => 'default',
594
                'entity_managers' => array(
595
                    'default' => array(
596
                        'mappings' => array(
597
                            'XmlBundle' => array()
598
                        )
599
                    ),
600
                ),
601
            ))
602
            ->build();
603
        $extension->load(array($config1, $config2), $container);
604
605
        $definition = $container->getDefinition('doctrine.orm.default_metadata_driver');
606
        $this->assertDICDefinitionMethodCallAt(0, $definition, 'addDriver', array(
607
            new Reference('doctrine.orm.default_annotation_metadata_driver'),
608
            'Fixtures\Bundles\AnnotationsBundle\Entity',
609
        ));
610
        $this->assertDICDefinitionMethodCallAt(1, $definition, 'addDriver', array(
611
            new Reference('doctrine.orm.default_xml_metadata_driver'),
612
            'Fixtures\Bundles\XmlBundle\Entity',
613
        ));
614
615
        $configDef = $container->getDefinition('doctrine.orm.default_configuration');
616
        $this->assertDICDefinitionMethodCallOnce($configDef, 'setAutoGenerateProxyClasses');
617
618
        $calls = $configDef->getMethodCalls();
619
        foreach ($calls as $call) {
620
            if ($call[0] === 'setAutoGenerateProxyClasses') {
621
                $this->assertFalse($container->getParameterBag()->resolveValue($call[1][0]));
622
                break;
623
            }
624
        }
625
    }
626
627
    public function testAnnotationsBundleMappingDetectionWithVendorNamespace()
628
    {
629
        $container = $this->getContainer('AnnotationsBundle', 'Vendor');
630
        $extension = new DoctrineExtension();
631
632
        $config = BundleConfigurationBuilder::createBuilder()
633
            ->addBaseConnection()
634
            ->addEntityManager(array(
635
                'default_entity_manager' => 'default',
636
                'entity_managers' => array(
637
                    'default' => array(
638
                        'mappings' => array(
639
                            'AnnotationsBundle' => array()
640
                        )
641
                    )
642
                )
643
            ))
644
            ->build();
645
        $extension->load(array($config), $container);
646
647
        $calls = $container->getDefinition('doctrine.orm.default_metadata_driver')->getMethodCalls();
648
        $this->assertEquals('doctrine.orm.default_annotation_metadata_driver', (string) $calls[0][1][0]);
649
        $this->assertEquals('Fixtures\Bundles\Vendor\AnnotationsBundle\Entity', $calls[0][1][1]);
650
    }
651
652
    public function testCacheConfiguration()
653
    {
654
        $container = $this->getContainer();
655
        $extension = new DoctrineExtension();
656
657
        $config = BundleConfigurationBuilder::createBuilder()
658
             ->addBaseConnection()
659
             ->addEntityManager(array(
660
                 'metadata_cache_driver' => array(
661
                     'cache_provider' => 'metadata_cache',
662
                 ),
663
                 'query_cache_driver' => array(
664
                     'cache_provider' => 'query_cache',
665
                 ),
666
                 'result_cache_driver' => array(
667
                     'cache_provider' => 'result_cache',
668
                 ),
669
             ))
670
            ->build();
671
672
        $extension->load(array($config), $container);
673
674
        $this->assertTrue($container->hasAlias('doctrine.orm.default_metadata_cache'));
675
        $alias = $container->getAlias('doctrine.orm.default_metadata_cache');
676
        $this->assertEquals('doctrine_cache.providers.metadata_cache', (string) $alias);
677
678
        $this->assertTrue($container->hasAlias('doctrine.orm.default_query_cache'));
679
        $alias = $container->getAlias('doctrine.orm.default_query_cache');
680
        $this->assertEquals('doctrine_cache.providers.query_cache', (string) $alias);
681
682
        $this->assertTrue($container->hasAlias('doctrine.orm.default_result_cache'));
683
        $alias = $container->getAlias('doctrine.orm.default_result_cache');
684
        $this->assertEquals('doctrine_cache.providers.result_cache', (string) $alias);
685
    }
686
687
    public function testShardManager()
688
    {
689
        $container = $this->getContainer();
690
        $extension = new DoctrineExtension();
691
692
        $config = BundleConfigurationBuilder::createBuilder()
693
             ->addConnection(array(
694
                 'connections' => array(
695
                     'foo' => array(
696
                         'shards' => array(
697
                             'test' => array('id' => 1)
698
                         ),
699
                     ),
700
                     'bar' => array(),
701
                 ),
702
             ))
703
            ->build();
704
705
        $extension->load(array($config), $container);
706
707
        $this->assertTrue($container->hasDefinition('doctrine.dbal.foo_shard_manager'));
708
        $this->assertFalse($container->hasDefinition('doctrine.dbal.bar_shard_manager'));
709
    }
710
711
    private function getContainer($bundles = 'YamlBundle', $vendor = null)
712
    {
713
        $bundles = (array) $bundles;
714
715
        $map = array();
716
        foreach ($bundles as $bundle) {
717
            require_once __DIR__.'/Fixtures/Bundles/'.($vendor ? $vendor.'/' : '').$bundle.'/'.$bundle.'.php';
718
719
            $map[$bundle] = 'Fixtures\\Bundles\\'.($vendor ? $vendor.'\\' : '').$bundle.'\\'.$bundle;
720
        }
721
722
        return new ContainerBuilder(new ParameterBag(array(
723
            'kernel.debug' => false,
724
            'kernel.bundles' => $map,
725
            'kernel.cache_dir' => sys_get_temp_dir(),
726
            'kernel.environment' => 'test',
727
            'kernel.root_dir' => __DIR__.'/../../', // src dir
728
        )));
729
    }
730
731
    private function assertDICConstructorArguments(Definition $definition, array $args)
732
    {
733
        $this->assertEquals($args, $definition->getArguments(), "Expected and actual DIC Service constructor arguments of definition '".$definition->getClass()."' don't match.");
734
    }
735
736 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...
737
    {
738
        $calls = $definition->getMethodCalls();
739
        if (isset($calls[$pos][0])) {
740
            $this->assertEquals($methodName, $calls[$pos][0], "Method '".$methodName."' is expected to be called at position $pos.");
741
742
            if ($params !== null) {
743
                $this->assertEquals($params, $calls[$pos][1], "Expected parameters to methods '".$methodName."' do not match the actual parameters.");
744
            }
745
        }
746
    }
747
748
    /**
749
     * Assertion for the DI Container, check if the given definition contains a method call with the given parameters.
750
     *
751
     * @param Definition $definition
752
     * @param string     $methodName
753
     * @param array|null $params
754
     */
755 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...
756
    {
757
        $calls = $definition->getMethodCalls();
758
        $called = false;
759
        foreach ($calls as $call) {
760
            if ($call[0] === $methodName) {
761
                if ($called) {
762
                    $this->fail("Method '".$methodName."' is expected to be called only once, a second call was registered though.");
763
                } else {
764
                    $called = true;
765
                    if ($params !== null) {
766
                        $this->assertEquals($params, $call[1], "Expected parameters to methods '".$methodName."' do not match the actual parameters.");
767
                    }
768
                }
769
            }
770
        }
771
        if (!$called) {
772
            $this->fail("Method '".$methodName."' is expected to be called once, definition does not contain a call though.");
773
        }
774
    }
775
776
    private function compileContainer(ContainerBuilder $container)
777
    {
778
        $container->getCompilerPassConfig()->setOptimizationPasses(array(new ResolveDefinitionTemplatesPass()));
779
        $container->getCompilerPassConfig()->setRemovingPasses(array());
780
        $container->compile();
781
    }
782
783
}
784
785
class TestWrapperClass extends Connection {}
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...