TestCaseAllPublicCompilerPass   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 3
dl 0
loc 21
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A process() 0 18 5
1
<?php
2
3
namespace Doctrine\Bundle\DoctrineBundle\Tests;
4
5
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\DoctrineExtension;
6
use Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\TestType;
7
use Doctrine\Common\Annotations\AnnotationReader;
8
use PHPUnit\Framework\TestCase as BaseTestCase;
9
use Symfony\Component\Cache\Adapter\ArrayAdapter;
10
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
11
use Symfony\Component\DependencyInjection\Compiler\ResolveChildDefinitionsPass;
12
use Symfony\Component\DependencyInjection\ContainerBuilder;
13
use Symfony\Component\DependencyInjection\Definition;
14
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
15
16
class TestCase extends BaseTestCase
17
{
18
    public function createXmlBundleTestContainer() : ContainerBuilder
19
    {
20
        $container = new ContainerBuilder(new ParameterBag([
21
            'kernel.name' => 'app',
22
            'kernel.debug' => false,
23
            'kernel.bundles' => ['XmlBundle' => 'Fixtures\Bundles\XmlBundle\XmlBundle'],
24
            'kernel.cache_dir' => sys_get_temp_dir(),
25
            'kernel.environment' => 'test',
26
            'kernel.root_dir' => __DIR__ . '/../../../../', // src dir
27
            'kernel.project_dir' => __DIR__ . '/../../../../', // src dir
28
            'kernel.bundles_metadata' => [],
29
            'container.build_id' => uniqid(),
30
        ]));
31
        $container->set('annotation_reader', new AnnotationReader());
32
33
        $extension = new DoctrineExtension();
34
        $container->registerExtension($extension);
35
        $extension->load([
36
            [
37
                'dbal' => [
38
                    'connections' => [
39
                        'default' => [
40
                            'driver' => 'pdo_mysql',
41
                            'charset' => 'UTF8',
42
                            'platform-service' => 'my.platform',
43
                        ],
44
                    ],
45
                    'default_connection' => 'default',
46
                    'types' => [
47
                        'test' => [
48
                            'class' => TestType::class,
49
                        ],
50
                    ],
51
                ],
52
                'orm' => [
53
                    'default_entity_manager' => 'default',
54
                    'entity_managers' => [
55
                        'default' => [
56
                            'mappings' => [
57
                                'XmlBundle' => [
58
                                    'type' => 'xml',
59
                                    'dir' => __DIR__ . '/DependencyInjection/Fixtures/Bundles/XmlBundle/Resources/config/doctrine',
60
                                    'prefix' => 'Fixtures\Bundles\XmlBundle\Entity',
61
                                ],
62
                            ],
63
                        ],
64
                    ],
65
                    'resolve_target_entities' => ['Symfony\Component\Security\Core\User\UserInterface' => 'stdClass'],
66
                ],
67
            ],
68
        ], $container);
69
70
        $container->setDefinition('my.platform', new Definition('Doctrine\DBAL\Platforms\MySqlPlatform'))->setPublic(true);
71
72
        // Register dummy cache services so we don't have to load the FrameworkExtension
73
        $container->setDefinition('cache.system', (new Definition(ArrayAdapter::class))->setPublic(true));
74
        $container->setDefinition('cache.app', (new Definition(ArrayAdapter::class))->setPublic(true));
75
76
        $container->getCompilerPassConfig()->setOptimizationPasses([new ResolveChildDefinitionsPass()]);
77
        $container->getCompilerPassConfig()->setRemovingPasses([]);
78
        // make all Doctrine services public, so we can fetch them in the test
79
        $container->getCompilerPassConfig()->addPass(new TestCaseAllPublicCompilerPass());
80
        $container->compile();
81
82
        return $container;
83
    }
84
}
85
86
class TestCaseAllPublicCompilerPass implements CompilerPassInterface
87
{
88
    public function process(ContainerBuilder $container) : void
89
    {
90
        foreach ($container->getDefinitions() as $id => $definition) {
91
            if (strpos($id, 'doctrine') === false) {
92
                continue;
93
            }
94
95
            $definition->setPublic(true);
96
        }
97
98
        foreach ($container->getAliases() as $id => $alias) {
99
            if (strpos($id, 'doctrine') === false) {
100
                continue;
101
            }
102
103
            $alias->setPublic(true);
104
        }
105
    }
106
}
107