Completed
Pull Request — master (#947)
by Andreas
02:12
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 Doctrine\ORM\Version;
9
use PHPUnit\Framework\TestCase as BaseTestCase;
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
    protected function setUp()
19
    {
20
        if (class_exists(Version::class)) {
21
            return;
22
        }
23
24
        $this->markTestSkipped('Doctrine is not available.');
25
    }
26
27
    public function createYamlBundleTestContainer()
28
    {
29
        $container = new ContainerBuilder(new ParameterBag([
30
            'kernel.name' => 'app',
31
            'kernel.debug' => false,
32
            'kernel.bundles' => ['YamlBundle' => 'Fixtures\Bundles\YamlBundle\YamlBundle'],
33
            'kernel.cache_dir' => sys_get_temp_dir(),
34
            'kernel.environment' => 'test',
35
            'kernel.root_dir' => __DIR__ . '/../../../../', // src dir
36
        ]));
37
        $container->set('annotation_reader', new AnnotationReader());
38
        $extension = new DoctrineExtension();
39
        $container->registerExtension($extension);
40
        $extension->load([[
41
            'dbal' => [
42
                'connections' => [
43
                    'default' => [
44
                        'driver' => 'pdo_mysql',
45
                        'charset' => 'UTF8',
46
                        'platform-service' => 'my.platform',
47
                    ],
48
                ],
49
                'default_connection' => 'default',
50
                'types' => [
51
                    'test' => [
52
                        'class' => TestType::class,
53
                        'commented' => false,
54
                    ],
55
                ],
56
            ], 'orm' => [
57
                'default_entity_manager' => 'default',
58
                'entity_managers' => [
59
                    'default' => [
60
                        'mappings' => [
61
                            'YamlBundle' => [
62
                                'type' => 'yml',
63
                                'dir' => __DIR__ . '/DependencyInjection/Fixtures/Bundles/YamlBundle/Resources/config/doctrine',
64
                                'prefix' => 'Fixtures\Bundles\YamlBundle\Entity',
65
                            ],
66
                        ],
67
                    ],
68
                ],
69
                'resolve_target_entities' => ['Symfony\Component\Security\Core\User\UserInterface' => 'stdClass'],
70
            ],
71
        ],
72
        ], $container);
73
74
        $container->setDefinition('my.platform', new Definition('Doctrine\DBAL\Platforms\MySqlPlatform'))->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)
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