Completed
Pull Request — master (#599)
by Mike
07:03
created

testAutoGenerateProxyClasses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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