Completed
Pull Request — master (#1101)
by
unknown
16:28
created

TestCase::setUp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 10
cc 2
nc 2
nop 0
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()
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
            'dbal' => [
37
                'connections' => [
38
                    'default' => [
39
                        'driver' => 'pdo_mysql',
40
                        'charset' => 'UTF8',
41
                        'platform-service' => 'my.platform',
42
                    ],
43
                ],
44
                'default_connection' => 'default',
45
                'types' => [
46
                    'test' => [
47
                        'class' => TestType::class,
48
                    ],
49
                ],
50
            ], 'orm' => [
51
                'default_entity_manager' => 'default',
52
                'entity_managers' => [
53
                    'default' => [
54
                        'mappings' => [
55
                            'XmlBundle' => [
56
                                'type' => 'xml',
57
                                'dir' => __DIR__ . '/DependencyInjection/Fixtures/Bundles/XmlBundle/Resources/config/doctrine',
58
                                'prefix' => 'Fixtures\Bundles\XmlBundle\Entity',
59
                            ],
60
                        ],
61
                    ],
62
                ],
63
                'resolve_target_entities' => ['Symfony\Component\Security\Core\User\UserInterface' => 'stdClass'],
64
            ],
65
        ],
66
        ], $container);
67
68
        $container->setDefinition('my.platform', new Definition('Doctrine\DBAL\Platforms\MySqlPlatform'))->setPublic(true);
69
70
        // Register dummy cache services so we don't have to load the FrameworkExtension
71
        $container->setDefinition('cache.system', (new Definition(ArrayAdapter::class))->setPublic(true));
72
        $container->setDefinition('cache.app', (new Definition(ArrayAdapter::class))->setPublic(true));
73
74
        $container->getCompilerPassConfig()->setOptimizationPasses([new ResolveChildDefinitionsPass()]);
75
        $container->getCompilerPassConfig()->setRemovingPasses([]);
76
        // make all Doctrine services public, so we can fetch them in the test
77
        $container->getCompilerPassConfig()->addPass(new TestCaseAllPublicCompilerPass());
78
        $container->compile();
79
80
        return $container;
81
    }
82
}
83
84
class TestCaseAllPublicCompilerPass implements CompilerPassInterface
85
{
86
    public function process(ContainerBuilder $container)
87
    {
88
        foreach ($container->getDefinitions() as $id => $definition) {
89
            if (strpos($id, 'doctrine') === false) {
90
                continue;
91
            }
92
93
            $definition->setPublic(true);
94
        }
95
96
        foreach ($container->getAliases() as $id => $alias) {
97
            if (strpos($id, 'doctrine') === false) {
98
                continue;
99
            }
100
101
            $alias->setPublic(true);
102
        }
103
    }
104
}
105