Completed
Push — master ( f4262c...828262 )
by Andreas
02:14 queued 11s
created

TestCase::createYamlBundleTestContainer()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 64
rs 8.7853
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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