Completed
Pull Request — master (#663)
by
unknown
02:49
created

testAnnotationsBundleMappingDetectionWithVendorNamespace()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 1
eloc 16
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 Symfony\Component\DependencyInjection\Compiler\ResolveDefinitionTemplatesPass;
23
use Symfony\Component\DependencyInjection\ContainerBuilder;
24
use Symfony\Component\DependencyInjection\Definition;
25
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
26
use Symfony\Component\DependencyInjection\Reference;
27
28
class DoctrineExtensionTest extends \PHPUnit_Framework_TestCase
29
{
30
31
    public function testDbalGenerateDefaultConnectionConfiguration()
32
    {
33
        $container = $this->getContainer();
34
        $extension = new DoctrineExtension();
35
36
        $container->registerExtension($extension);
37
38
        $extension->load(array(array('dbal' => array())), $container);
39
40
        // doctrine.dbal.default_connection
41
        $this->assertEquals('%doctrine.default_connection%', $container->getDefinition('doctrine')->getArgument(3));
42
        $this->assertEquals('default', $container->getParameter('doctrine.default_connection'));
43
        $this->assertEquals('root', $container->getDefinition('doctrine.dbal.default_connection')->getArgument(0)['user']);
44
        $this->assertNull($container->getDefinition('doctrine.dbal.default_connection')->getArgument(0)['password']);
45
        $this->assertEquals('localhost', $container->getDefinition('doctrine.dbal.default_connection')->getArgument(0)['host']);
46
        $this->assertNull($container->getDefinition('doctrine.dbal.default_connection')->getArgument(0)['port']);
47
        $this->assertEquals('pdo_mysql', $container->getDefinition('doctrine.dbal.default_connection')->getArgument(0)['driver']);
48
        $this->assertEquals(array(), $container->getDefinition('doctrine.dbal.default_connection')->getArgument(0)['driverOptions']);
49
    }
50
51
    public function testDbalOverrideDefaultConnection()
52
    {
53
        $container = $this->getContainer();
54
        $extension = new DoctrineExtension();
55
56
        $container->registerExtension($extension);
57
58
        $extension->load(array(array(), array('dbal' => array('default_connection' => 'foo')), array()), $container);
59
60
        // doctrine.dbal.default_connection
61
        $this->assertEquals('%doctrine.default_connection%', $container->getDefinition('doctrine')->getArgument(3), '->load() overrides existing configuration options');
62
        $this->assertEquals('foo', $container->getParameter('doctrine.default_connection'), '->load() overrides existing configuration options');
63
    }
64
65
    /**
66
     * @expectedException \LogicException
67
     * @expectedExceptionMessage Configuring the ORM layer requires to configure the DBAL layer as well.
68
     */
69
    public function testOrmRequiresDbal()
70
    {
71
        $extension = new DoctrineExtension();
72
73
        $extension->load(array(array('orm' => array('auto_mapping' => true))), $this->getContainer());
74
    }
75
76
    public function getAutomappingConfigurations()
77
    {
78
        return array(
79
            array(
80
                array(
81
                    'em1' => array(
82
                        'mappings' => array(
83
                            'YamlBundle' => null,
84
                        ),
85
                    ),
86
                    'em2' => array(
87
                        'mappings' => array(
88
                            'XmlBundle' => null,
89
                        ),
90
                    ),
91
                ),
92
            ),
93
            array(
94
                array(
95
                    'em1' => array(
96
                        'auto_mapping' => true,
97
                    ),
98
                    'em2' => array(
99
                        'mappings' => array(
100
                            'XmlBundle' => null,
101
                        ),
102
                    ),
103
                ),
104
            ),
105
            array(
106
                array(
107
                    'em1' => array(
108
                        'auto_mapping' => true,
109
                        'mappings' => array(
110
                            'YamlBundle' => null,
111
                        ),
112
                    ),
113
                    'em2' => array(
114
                        'mappings' => array(
115
                            'XmlBundle' => null,
116
                        ),
117
                    ),
118
                ),
119
            ),
120
        );
121
    }
122
123
    /**
124
     * @dataProvider getAutomappingConfigurations
125
     */
126
    public function testAutomapping(array $entityManagers)
127
    {
128
        $extension = new DoctrineExtension();
129
130
        if (!method_exists($extension, 'fixManagersAutoMappings')) {
131
            $this->markTestSkipped('Auto mapping with multiple managers available with Symfony ~2.6');
132
        }
133
134
        $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...
135
            'YamlBundle',
136
            'XmlBundle',
137
        ));
138
139
        $extension->load(
140
            array(
141
                array(
142
                    'dbal' => array(
143
                        'default_connection' => 'cn1',
144
                        'connections' => array(
145
                            'cn1' => array(),
146
                            'cn2' => array(),
147
                        ),
148
                    ),
149
                    'orm' => array(
150
                        'entity_managers' => $entityManagers,
151
                    ),
152
                ),
153
            ), $container);
154
155
        $configEm1 = $container->getDefinition('doctrine.orm.em1_configuration');
156
        $configEm2 = $container->getDefinition('doctrine.orm.em2_configuration');
157
158
        $this->assertContains(
159
            array(
160
                'setEntityNamespaces',
161
                array(
162
                    array(
163
                        'YamlBundle' => 'Fixtures\Bundles\YamlBundle\Entity',
164
                    ),
165
                ),
166
            ),
167
            $configEm1->getMethodCalls()
168
        );
169
170
        $this->assertContains(
171
            array(
172
                'setEntityNamespaces',
173
                array(
174
                    array(
175
                        'XmlBundle' => 'Fixtures\Bundles\XmlBundle\Entity',
176
                    ),
177
                ),
178
            ),
179
            $configEm2->getMethodCalls()
180
        );
181
    }
182
183
    public function testDbalLoad()
184
    {
185
        $container = $this->getContainer();
186
        $extension = new DoctrineExtension();
187
188
        $extension->load(array(
189
            array('dbal' => array('connections' => array('default' => array('password' => 'foo')))),
190
            array(),
191
            array('dbal' => array('default_connection' => 'foo')),
192
            array()), $container);
193
194
        $config = $container->getDefinition('doctrine.dbal.default_connection')->getArgument(0);
195
196
        $this->assertEquals('foo', $config['password']);
197
        $this->assertEquals('root', $config['user']);
198
    }
199
200
    public function testDbalWrapperClass()
201
    {
202
        $container = $this->getContainer();
203
        $extension = new DoctrineExtension();
204
205
        $extension->load(
206
            array(
207
                array('dbal' => array('connections' => array(
208
                    'default' => array('password' => 'foo', 'wrapper_class' => TestWrapperClass::class),
209
                    'second' => array('password' => 'boo'),
210
                ))),
211
                array(),
212
                array('dbal' => array('default_connection' => 'foo')),
213
                array()
214
            ),
215
            $container
216
        );
217
218
        $this->assertEquals(TestWrapperClass::class, $container->getDefinition('doctrine.dbal.default_connection')->getClass());
219
        $this->assertEquals(null, $container->getDefinition('doctrine.dbal.second_connection')->getClass());
220
    }
221
222
    public function testDependencyInjectionConfigurationDefaults()
223
    {
224
        $container = $this->getContainer();
225
        $extension = new DoctrineExtension();
226
        $config = BundleConfigurationBuilder::createBuilderWithBaseValues()->build();
227
228
        $extension->load(array($config), $container);
229
230
        $this->assertFalse($container->getParameter('doctrine.orm.auto_generate_proxy_classes'));
231
        $this->assertEquals('Doctrine\ORM\Configuration', $container->getParameter('doctrine.orm.configuration.class'));
232
        $this->assertEquals('Doctrine\ORM\EntityManager', $container->getParameter('doctrine.orm.entity_manager.class'));
233
        $this->assertEquals('Proxies', $container->getParameter('doctrine.orm.proxy_namespace'));
234
        $this->assertEquals('Doctrine\Common\Cache\ArrayCache', $container->getParameter('doctrine.orm.cache.array.class'));
235
        $this->assertEquals('Doctrine\Common\Cache\ApcCache', $container->getParameter('doctrine.orm.cache.apc.class'));
236
        $this->assertEquals('Doctrine\Common\Cache\MemcacheCache', $container->getParameter('doctrine.orm.cache.memcache.class'));
237
        $this->assertEquals('localhost', $container->getParameter('doctrine.orm.cache.memcache_host'));
238
        $this->assertEquals('11211', $container->getParameter('doctrine.orm.cache.memcache_port'));
239
        $this->assertEquals('Memcache', $container->getParameter('doctrine.orm.cache.memcache_instance.class'));
240
        $this->assertEquals('Doctrine\Common\Cache\XcacheCache', $container->getParameter('doctrine.orm.cache.xcache.class'));
241
        $this->assertEquals('Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain', $container->getParameter('doctrine.orm.metadata.driver_chain.class'));
242
        $this->assertEquals('Doctrine\ORM\Mapping\Driver\AnnotationDriver', $container->getParameter('doctrine.orm.metadata.annotation.class'));
243
        $this->assertEquals('Doctrine\ORM\Mapping\Driver\SimplifiedXmlDriver', $container->getParameter('doctrine.orm.metadata.xml.class'));
244
        $this->assertEquals('Doctrine\ORM\Mapping\Driver\SimplifiedYamlDriver', $container->getParameter('doctrine.orm.metadata.yml.class'));
245
246
        // second-level cache
247
        $this->assertEquals('Doctrine\ORM\Cache\DefaultCacheFactory', $container->getParameter('doctrine.orm.second_level_cache.default_cache_factory.class'));
248
        $this->assertEquals('Doctrine\ORM\Cache\Region\DefaultRegion', $container->getParameter('doctrine.orm.second_level_cache.default_region.class'));
249
        $this->assertEquals('Doctrine\ORM\Cache\Region\FileLockRegion', $container->getParameter('doctrine.orm.second_level_cache.filelock_region.class'));
250
        $this->assertEquals('Doctrine\ORM\Cache\Logging\CacheLoggerChain', $container->getParameter('doctrine.orm.second_level_cache.logger_chain.class'));
251
        $this->assertEquals('Doctrine\ORM\Cache\Logging\StatisticsCacheLogger', $container->getParameter('doctrine.orm.second_level_cache.logger_statistics.class'));
252
        $this->assertEquals('Doctrine\ORM\Cache\CacheConfiguration', $container->getParameter('doctrine.orm.second_level_cache.cache_configuration.class'));
253
        $this->assertEquals('Doctrine\ORM\Cache\RegionsConfiguration', $container->getParameter('doctrine.orm.second_level_cache.regions_configuration.class'));
254
255
256
        $config = BundleConfigurationBuilder::createBuilder()
257
            ->addBaseConnection()
258
            ->addEntityManager(array(
259
                'proxy_namespace' => 'MyProxies',
260
                'auto_generate_proxy_classes' => true,
261
                'default_entity_manager' => 'default',
262
                'entity_managers' => array(
263
                    'default' => array(
264
                        'mappings' => array('YamlBundle' => array()),
265
                    ),
266
                ),
267
            ))
268
            ->build();
269
270
        $container = $this->getContainer();
271
        $extension->load(array($config), $container);
272
        $this->compileContainer($container);
273
274
        $definition = $container->getDefinition('doctrine.dbal.default_connection');
275
276
        $args = $definition->getArguments();
277
        $this->assertEquals('pdo_mysql', $args[0]['driver']);
278
        $this->assertEquals('localhost', $args[0]['host']);
279
        $this->assertEquals('root', $args[0]['user']);
280
        $this->assertEquals('doctrine.dbal.default_connection.configuration', (string) $args[1]);
281
        $this->assertEquals('doctrine.dbal.default_connection.event_manager', (string) $args[2]);
282
        $this->assertCount(0, $definition->getMethodCalls());
283
284
        $definition = $container->getDefinition('doctrine.orm.default_entity_manager');
285
        $this->assertEquals('%doctrine.orm.entity_manager.class%', $definition->getClass());
286 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...
287
            $this->assertEquals(array('%doctrine.orm.entity_manager.class%', 'create'), $definition->getFactory());
288
        } else {
289
            $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...
290
            $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...
291
        }
292
293
        $this->assertEquals(array('default' => 'doctrine.orm.default_entity_manager'), $container->getParameter('doctrine.entity_managers'), "Set of the existing EntityManagers names is incorrect.");
294
        $this->assertEquals('%doctrine.entity_managers%', $container->getDefinition('doctrine')->getArgument(2), "Set of the existing EntityManagers names is incorrect.");
295
296
        $arguments = $definition->getArguments();
297
        $this->assertInstanceOf('Symfony\Component\DependencyInjection\Reference', $arguments[0]);
298
        $this->assertEquals('doctrine.dbal.default_connection', (string) $arguments[0]);
299
        $this->assertInstanceOf('Symfony\Component\DependencyInjection\Reference', $arguments[1]);
300
        $this->assertEquals('doctrine.orm.default_configuration', (string) $arguments[1]);
301
302
        $definition = $container->getDefinition('doctrine.orm.default_configuration');
303
        $calls = array_values($definition->getMethodCalls());
304
        $this->assertEquals(array('YamlBundle' => 'Fixtures\Bundles\YamlBundle\Entity'), $calls[0][1][0]);
305
        $this->assertEquals('doctrine.orm.default_metadata_cache', (string) $calls[1][1][0]);
306
        $this->assertEquals('doctrine.orm.default_query_cache', (string) $calls[2][1][0]);
307
        $this->assertEquals('doctrine.orm.default_result_cache', (string) $calls[3][1][0]);
308
309
        if (version_compare(Version::VERSION, "2.3.0-DEV") >= 0) {
310
            $this->assertEquals('doctrine.orm.naming_strategy.default', (string) $calls[10][1][0]);
311
            $this->assertEquals('doctrine.orm.quote_strategy.default', (string) $calls[11][1][0]);
312
        }
313
        if (version_compare(Version::VERSION, "2.4.0-DEV") >= 0) {
314
            $this->assertEquals('doctrine.orm.default_entity_listener_resolver', (string) $calls[12][1][0]);
315
        }
316
317
        $definition = $container->getDefinition($container->getAlias('doctrine.orm.default_metadata_cache'));
318
        $this->assertEquals('%doctrine_cache.array.class%', $definition->getClass());
319
320
        $definition = $container->getDefinition($container->getAlias('doctrine.orm.default_query_cache'));
321
        $this->assertEquals('%doctrine_cache.array.class%', $definition->getClass());
322
323
        $definition = $container->getDefinition($container->getAlias('doctrine.orm.default_result_cache'));
324
        $this->assertEquals('%doctrine_cache.array.class%', $definition->getClass());
325
    }
326
327
    public function testUseSavePointsAddMethodCallToAddSavepointsToTheConnection()
328
    {
329
        $container = $this->getContainer();
330
        $extension = new DoctrineExtension();
331
332
        $extension->load(array(array('dbal' => array('connections' => array(
333
            'default' => array('password' => 'foo', 'use_savepoints' => true)
334
        )))), $container);
335
336
        $calls = $container->getDefinition('doctrine.dbal.default_connection')->getMethodCalls();
337
        $this->assertCount(1, $calls);
338
        $this->assertEquals('setNestTransactionsWithSavepoints', $calls[0][0]);
339
        $this->assertTrue($calls[0][1][0]);
340
    }
341
342
    public function testAutoGenerateProxyClasses()
343
    {
344
        $container = $this->getContainer();
345
        $extension = new DoctrineExtension();
346
347
        $config = BundleConfigurationBuilder::createBuilder()
348
            ->addBaseConnection()
349
            ->addEntityManager(array(
350
                'proxy_namespace' => 'MyProxies',
351
                'auto_generate_proxy_classes' => 'eval',
352
                'default_entity_manager' => 'default',
353
                'entity_managers' => array(
354
                    'default' => array(
355
                        'mappings' => array('YamlBundle' => array()),
356
                    ),
357
                ),
358
            ))
359
            ->build();
360
361
        $extension->load(array($config), $container);
362
363
        $this->assertEquals(AbstractProxyFactory::AUTOGENERATE_EVAL, $container->getParameter('doctrine.orm.auto_generate_proxy_classes'));
364
    }
365
366
    public function testSingleEntityManagerWithDefaultConfiguration()
367
    {
368
        $container = $this->getContainer();
369
        $extension = new DoctrineExtension();
370
371
        $configurationArray = BundleConfigurationBuilder::createBuilderWithBaseValues()->build();
372
373
        $extension->load(array($configurationArray), $container);
374
        $this->compileContainer($container);
375
376
        $definition = $container->getDefinition('doctrine.orm.default_entity_manager');
377
        $this->assertEquals('%doctrine.orm.entity_manager.class%', $definition->getClass());
378 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...
379
            $this->assertEquals(array('%doctrine.orm.entity_manager.class%', 'create'), $definition->getFactory());
380
        } else {
381
            $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...
382
            $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...
383
        }
384
385
        $this->assertDICConstructorArguments($definition, array(
386
            new Reference('doctrine.dbal.default_connection'), new Reference('doctrine.orm.default_configuration'),
387
        ));
388
    }
389
390
    public function testSingleEntityManagerWithDefaultSecondLevelCacheConfiguration()
391
    {
392
        if (version_compare(Version::VERSION, "2.5.0-DEV") < 0) {
393
            $this->markTestSkipped(sprintf('Second Level cache not supported by this version of the ORM : %s', Version::VERSION));
394
        }
395
        $container = $this->getContainer();
396
        $extension = new DoctrineExtension();
397
398
        $configurationArray = BundleConfigurationBuilder::createBuilderWithBaseValues()
399
            ->addBaseSecondLevelCache()
400
            ->build();
401
402
        $extension->load(array($configurationArray), $container);
403
        $this->compileContainer($container);
404
405
        $definition = $container->getDefinition('doctrine.orm.default_entity_manager');
406
        $this->assertEquals('%doctrine.orm.entity_manager.class%', $definition->getClass());
407
        if (method_exists($definition, 'getFactory')) {
408
            $this->assertEquals(array('%doctrine.orm.entity_manager.class%', 'create'), $definition->getFactory());
409
        } else {
410
            $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...
411
            $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...
412
        }
413
414
        $this->assertDICConstructorArguments($definition, array(
415
            new Reference('doctrine.dbal.default_connection'), new Reference('doctrine.orm.default_configuration'),
416
        ));
417
418
        $slcDefinition = $container->getDefinition('doctrine.orm.default_second_level_cache.default_cache_factory');
419
        $this->assertEquals('%doctrine.orm.second_level_cache.default_cache_factory.class%', $slcDefinition->getClass());
420
    }
421
422
    public function testSingleEntityManagerWithCustomSecondLevelCacheConfiguration()
423
    {
424
        if (version_compare(Version::VERSION, "2.5.0-DEV") < 0) {
425
            $this->markTestSkipped(sprintf('Second Level cache not supported by this version of the ORM : %s', Version::VERSION));
426
        }
427
        $container = $this->getContainer();
428
        $extension = new DoctrineExtension();
429
430
        $configurationArray = BundleConfigurationBuilder::createBuilderWithBaseValues()
431
            ->addSecondLevelCache([
432
                'region_cache_driver' => [
433
                    'type' => 'memcache'],
434
                'regions' => [
435
                    'hour_region' => [
436
                        'lifetime' => 3600
437
                    ]
438
                ],
439
                'factory' => 'YamlBundle\Cache\MyCacheFactory',
440
            ])
441
            ->build();
442
443
        $extension->load(array($configurationArray), $container);
444
        $this->compileContainer($container);
445
446
        $definition = $container->getDefinition('doctrine.orm.default_entity_manager');
447
        $this->assertEquals('%doctrine.orm.entity_manager.class%', $definition->getClass());
448 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...
449
            $this->assertEquals(array('%doctrine.orm.entity_manager.class%', 'create'), $definition->getFactory());
450
        } else {
451
            $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...
452
            $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...
453
        }
454
455
        $this->assertDICConstructorArguments($definition, array(
456
            new Reference('doctrine.dbal.default_connection'), new Reference('doctrine.orm.default_configuration'),
457
        ));
458
459
        $slcDefinition = $container->getDefinition('doctrine.orm.default_second_level_cache.default_cache_factory');
460
        $this->assertEquals('YamlBundle\Cache\MyCacheFactory', $slcDefinition->getClass());
461
    }
462
463 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...
464
    {
465
        $container = $this->getContainer();
466
        $extension = new DoctrineExtension();
467
468
        $config = BundleConfigurationBuilder::createBuilder()
469
             ->addBaseConnection()
470
             ->build();
471
        $config['orm'] = array('default_entity_manager' => 'default', 'entity_managers' => array('default' => array('mappings' => array('YamlBundle' => array()))));
472
        $extension->load(array($config), $container);
473
474
        $definition = $container->getDefinition('doctrine.orm.default_configuration');
475
        $this->assertDICDefinitionMethodCallOnce($definition, 'setEntityNamespaces',
476
            array(array('YamlBundle' => 'Fixtures\Bundles\YamlBundle\Entity'))
477
        );
478
    }
479
480 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...
481
    {
482
        $container = $this->getContainer();
483
        $extension = new DoctrineExtension();
484
485
        $config = BundleConfigurationBuilder::createBuilder()
486
             ->addBaseConnection()
487
             ->build();
488
        $config['orm'] = array('default_entity_manager' => 'default', 'entity_managers' => array('default' => array('mappings' => array('YamlBundle' => array('alias' => 'yml')))));
489
        $extension->load(array($config), $container);
490
491
        $definition = $container->getDefinition('doctrine.orm.default_configuration');
492
        $this->assertDICDefinitionMethodCallOnce($definition, 'setEntityNamespaces',
493
            array(array('yml' => 'Fixtures\Bundles\YamlBundle\Entity'))
494
        );
495
    }
496
497
    public function testYamlBundleMappingDetection()
498
    {
499
        $container = $this->getContainer('YamlBundle');
500
        $extension = new DoctrineExtension();
501
502
        $config = BundleConfigurationBuilder::createBuilder()
503
            ->addBaseConnection()
504
            ->addBaseEntityManager()
505
            ->build();
506
        $extension->load(array($config), $container);
507
508
        $definition = $container->getDefinition('doctrine.orm.default_metadata_driver');
509
        $this->assertDICDefinitionMethodCallOnce($definition, 'addDriver', array(
510
            new Reference('doctrine.orm.default_yml_metadata_driver'),
511
            'Fixtures\Bundles\YamlBundle\Entity',
512
        ));
513
    }
514
515 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...
516
    {
517
        $container = $this->getContainer('XmlBundle');
518
        $extension = new DoctrineExtension();
519
520
        $config = BundleConfigurationBuilder::createBuilder()
521
            ->addBaseConnection()
522
            ->addEntityManager(array(
523
                'default_entity_manager' => 'default',
524
                'entity_managers' => array(
525
                    'default' => array(
526
                        'mappings' => array(
527
                            'XmlBundle' => array()
528
                        )
529
                    )
530
                )
531
            ))
532
            ->build();
533
        $extension->load(array($config), $container);
534
535
        $definition = $container->getDefinition('doctrine.orm.default_metadata_driver');
536
        $this->assertDICDefinitionMethodCallOnce($definition, 'addDriver', array(
537
            new Reference('doctrine.orm.default_xml_metadata_driver'),
538
            'Fixtures\Bundles\XmlBundle\Entity',
539
        ));
540
    }
541
542 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...
543
    {
544
        $container = $this->getContainer('AnnotationsBundle');
545
        $extension = new DoctrineExtension();
546
547
        $config = BundleConfigurationBuilder::createBuilder()
548
            ->addBaseConnection()
549
            ->addEntityManager(array(
550
                'default_entity_manager' => 'default',
551
                'entity_managers' => array(
552
                    'default' => array(
553
                        'mappings' => array(
554
                            'AnnotationsBundle' => array()
555
                        )
556
                    )
557
                )
558
            ))
559
            ->build();
560
        $extension->load(array($config), $container);
561
562
        $definition = $container->getDefinition('doctrine.orm.default_metadata_driver');
563
        $this->assertDICDefinitionMethodCallOnce($definition, 'addDriver', array(
564
            new Reference('doctrine.orm.default_annotation_metadata_driver'),
565
            'Fixtures\Bundles\AnnotationsBundle\Entity',
566
        ));
567
    }
568
569
    public function testOrmMergeConfigs()
570
    {
571
        $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...
572
        $extension = new DoctrineExtension();
573
574
        $config1 = BundleConfigurationBuilder::createBuilder()
575
            ->addBaseConnection()
576
            ->addEntityManager(array(
577
                'auto_generate_proxy_classes' => true,
578
                'default_entity_manager' => 'default',
579
                'entity_managers' => array(
580
                    'default' => array(
581
                        'mappings' => array(
582
                            'AnnotationsBundle' => array()
583
                        )
584
                    ),
585
                ),
586
            ))
587
            ->build();
588
        $config2 = BundleConfigurationBuilder::createBuilder()
589
            ->addBaseConnection()
590
            ->addEntityManager(array(
591
                'auto_generate_proxy_classes' => false,
592
                'default_entity_manager' => 'default',
593
                'entity_managers' => array(
594
                    'default' => array(
595
                        'mappings' => array(
596
                            'XmlBundle' => array()
597
                        )
598
                    ),
599
                ),
600
            ))
601
            ->build();
602
        $extension->load(array($config1, $config2), $container);
603
604
        $definition = $container->getDefinition('doctrine.orm.default_metadata_driver');
605
        $this->assertDICDefinitionMethodCallAt(0, $definition, 'addDriver', array(
606
            new Reference('doctrine.orm.default_annotation_metadata_driver'),
607
            'Fixtures\Bundles\AnnotationsBundle\Entity',
608
        ));
609
        $this->assertDICDefinitionMethodCallAt(1, $definition, 'addDriver', array(
610
            new Reference('doctrine.orm.default_xml_metadata_driver'),
611
            'Fixtures\Bundles\XmlBundle\Entity',
612
        ));
613
614
        $configDef = $container->getDefinition('doctrine.orm.default_configuration');
615
        $this->assertDICDefinitionMethodCallOnce($configDef, 'setAutoGenerateProxyClasses');
616
617
        $calls = $configDef->getMethodCalls();
618
        foreach ($calls as $call) {
619
            if ($call[0] === 'setAutoGenerateProxyClasses') {
620
                $this->assertFalse($container->getParameterBag()->resolveValue($call[1][0]));
621
                break;
622
            }
623
        }
624
    }
625
626
    public function testAnnotationsBundleMappingDetectionWithVendorNamespace()
627
    {
628
        $container = $this->getContainer('AnnotationsBundle', 'Vendor');
629
        $extension = new DoctrineExtension();
630
631
        $config = BundleConfigurationBuilder::createBuilder()
632
            ->addBaseConnection()
633
            ->addEntityManager(array(
634
                'default_entity_manager' => 'default',
635
                'entity_managers' => array(
636
                    'default' => array(
637
                        'mappings' => array(
638
                            'AnnotationsBundle' => array()
639
                        )
640
                    )
641
                )
642
            ))
643
            ->build();
644
        $extension->load(array($config), $container);
645
646
        $calls = $container->getDefinition('doctrine.orm.default_metadata_driver')->getMethodCalls();
647
        $this->assertEquals('doctrine.orm.default_annotation_metadata_driver', (string) $calls[0][1][0]);
648
        $this->assertEquals('Fixtures\Bundles\Vendor\AnnotationsBundle\Entity', $calls[0][1][1]);
649
    }
650
651
    public function testCacheConfiguration()
652
    {
653
        $container = $this->getContainer();
654
        $extension = new DoctrineExtension();
655
656
        $config = BundleConfigurationBuilder::createBuilder()
657
             ->addBaseConnection()
658
             ->addEntityManager(array(
659
                 'metadata_cache_driver' => array(
660
                     'cache_provider' => 'metadata_cache',
661
                 ),
662
                 'query_cache_driver' => array(
663
                     'cache_provider' => 'query_cache',
664
                 ),
665
                 'result_cache_driver' => array(
666
                     'cache_provider' => 'result_cache',
667
                 ),
668
             ))
669
            ->build();
670
671
        $extension->load(array($config), $container);
672
673
        $this->assertTrue($container->hasAlias('doctrine.orm.default_metadata_cache'));
674
        $alias = $container->getAlias('doctrine.orm.default_metadata_cache');
675
        $this->assertEquals('doctrine_cache.providers.metadata_cache', (string) $alias);
676
677
        $this->assertTrue($container->hasAlias('doctrine.orm.default_query_cache'));
678
        $alias = $container->getAlias('doctrine.orm.default_query_cache');
679
        $this->assertEquals('doctrine_cache.providers.query_cache', (string) $alias);
680
681
        $this->assertTrue($container->hasAlias('doctrine.orm.default_result_cache'));
682
        $alias = $container->getAlias('doctrine.orm.default_result_cache');
683
        $this->assertEquals('doctrine_cache.providers.result_cache', (string) $alias);
684
    }
685
686
    public function testShardManager()
687
    {
688
        $container = $this->getContainer();
689
        $extension = new DoctrineExtension();
690
691
        $config = BundleConfigurationBuilder::createBuilder()
692
             ->addConnection(array(
693
                 'connections' => array(
694
                     'foo' => array(
695
                         'shards' => array(
696
                             'test' => array('id' => 1)
697
                         ),
698
                     ),
699
                     'bar' => array(),
700
                 ),
701
             ))
702
            ->build();
703
704
        $extension->load(array($config), $container);
705
706
        $this->assertTrue($container->hasDefinition('doctrine.dbal.foo_shard_manager'));
707
        $this->assertFalse($container->hasDefinition('doctrine.dbal.bar_shard_manager'));
708
    }
709
710
    private function getContainer($bundles = 'YamlBundle', $vendor = null)
711
    {
712
        $bundles = (array) $bundles;
713
714
        $map = array();
715
        foreach ($bundles as $bundle) {
716
            require_once __DIR__.'/Fixtures/Bundles/'.($vendor ? $vendor.'/' : '').$bundle.'/'.$bundle.'.php';
717
718
            $map[$bundle] = 'Fixtures\\Bundles\\'.($vendor ? $vendor.'\\' : '').$bundle.'\\'.$bundle;
719
        }
720
721
        return new ContainerBuilder(new ParameterBag(array(
722
            'kernel.debug' => false,
723
            'kernel.bundles' => $map,
724
            'kernel.cache_dir' => sys_get_temp_dir(),
725
            'kernel.environment' => 'test',
726
            'kernel.root_dir' => __DIR__.'/../../', // src dir
727
        )));
728
    }
729
730
    private function assertDICConstructorArguments(Definition $definition, array $args)
731
    {
732
        $this->assertEquals($args, $definition->getArguments(), "Expected and actual DIC Service constructor arguments of definition '".$definition->getClass()."' don't match.");
733
    }
734
735 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...
736
    {
737
        $calls = $definition->getMethodCalls();
738
        if (isset($calls[$pos][0])) {
739
            $this->assertEquals($methodName, $calls[$pos][0], "Method '".$methodName."' is expected to be called at position $pos.");
740
741
            if ($params !== null) {
742
                $this->assertEquals($params, $calls[$pos][1], "Expected parameters to methods '".$methodName."' do not match the actual parameters.");
743
            }
744
        }
745
    }
746
747
    /**
748
     * Assertion for the DI Container, check if the given definition contains a method call with the given parameters.
749
     *
750
     * @param Definition $definition
751
     * @param string     $methodName
752
     * @param array|null $params
753
     */
754 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...
755
    {
756
        $calls = $definition->getMethodCalls();
757
        $called = false;
758
        foreach ($calls as $call) {
759
            if ($call[0] === $methodName) {
760
                if ($called) {
761
                    $this->fail("Method '".$methodName."' is expected to be called only once, a second call was registered though.");
762
                } else {
763
                    $called = true;
764
                    if ($params !== null) {
765
                        $this->assertEquals($params, $call[1], "Expected parameters to methods '".$methodName."' do not match the actual parameters.");
766
                    }
767
                }
768
            }
769
        }
770
        if (!$called) {
771
            $this->fail("Method '".$methodName."' is expected to be called once, definition does not contain a call though.");
772
        }
773
    }
774
775
    private function compileContainer(ContainerBuilder $container)
776
    {
777
        $container->getCompilerPassConfig()->setOptimizationPasses(array(new ResolveDefinitionTemplatesPass()));
778
        $container->getCompilerPassConfig()->setRemovingPasses(array());
779
        $container->compile();
780
    }
781
782
}
783
784
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...