Passed
Push — master ( 32f4fa...9a9d10 )
by Pavel
02:45
created

ContainerTestTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 0
loc 79
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Bankiru\Doctrine\DiType\Test;
4
5
use Symfony\Component\DependencyInjection\ContainerBuilder;
6
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
7
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
8
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
9
10
trait ContainerTestTrait
11
{
12
    /**
13
     * @param BundleInterface[] $bundles
14
     * @param array             $configs
15
     * @param bool              $compile
16
     *
17
     * @return ContainerBuilder
18
     */
19
    protected function buildContainer(array $bundles = [], array $configs = [], $compile = true)
20
    {
21
        $container = new ContainerBuilder(
22
            new ParameterBag(
23
                [
24
                    'kernel.debug'       => false,
25
                    'kernel.bundles'     => $bundles,
26
                    'kernel.cache_dir'   => sys_get_temp_dir(),
27
                    'kernel.environment' => 'test',
28
                    'kernel.root_dir'    => __DIR__,
29
                ]
30
            )
31
        );
32
        foreach ($bundles as $bundle) {
33
            $bundle->build($container);
34
            $this->loadExtension($bundle, $container, $configs);
35
        }
36
37
        if ($compile) {
38
            $this->compile($container);
39
        }
40
41
        return $container;
42
    }
43
44
    /**
45
     * @param BundleInterface  $bundle
46
     * @param ContainerBuilder $container
47
     * @param array            $configs
48
     */
49
    protected function loadExtension(BundleInterface $bundle, ContainerBuilder $container, array $configs)
50
    {
51
        $extension = $bundle->getContainerExtension();
52
        if (!$extension) {
53
            return;
54
        }
55
56
        $config = [];
57
58
        if (array_key_exists($extension->getAlias(), $configs)) {
59
            $config = [$configs[$extension->getAlias()]];
60
        }
61
62
        $extension->load($config, $container);
63
    }
64
65
    /**
66
     * @param ContainerBuilder $container
67
     */
68
    protected function compile(ContainerBuilder $container)
69
    {
70
        $container->compile();
71
72
        // Check that container can be dumped
73
        $dumper = new PhpDumper($container);
74
        $dumper->dump();
75
76
        $this->boot($container);
77
    }
78
79
    protected function boot(ContainerBuilder $container)
80
    {
81
        /** @var BundleInterface[] $bundles */
82
        $bundles = $container->getParameter('kernel.bundles');
83
        foreach ($bundles as $bundle) {
84
            $bundle->setContainer($container);
85
            $bundle->boot();
86
        }
87
    }
88
}
89