Completed
Pull Request — master (#4)
by Pavel
10:02
created

ContainerTestTrait::buildContainer()   C

Complexity

Conditions 7
Paths 48

Size

Total Lines 48
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 34
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 48
ccs 34
cts 34
cp 1
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 27
nc 48
nop 3
crap 7
1
<?php
2
3
namespace Bankiru\Api\Tests;
4
5
use Doctrine\Common\Annotations\AnnotationReader;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
8
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
9
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
10
use Symfony\Component\HttpKernel\DependencyInjection\MergeExtensionConfigurationPass;
11
12
trait ContainerTestTrait
13
{
14
    /**
15
     * Returns Cache directory location
16
     *
17
     * @return string
18
     */
19
    abstract protected function getCacheDir();
20
21
    /**
22
     * @param BundleInterface[] $bundles
23
     * @param array             $configs
24
     * @param bool              $compile
25
     *
26
     * @return ContainerBuilder
27
     */
28 5
    protected function buildContainer(array $bundles = [], array $configs = [], $compile = true)
29
    {
30 5
        $container = new ContainerBuilder(
31 5
            new ParameterBag(
32
                [
33 5
                    'kernel.debug'       => false,
34 5
                    'kernel.bundles'     => array_map('get_class', $bundles),
35 5
                    'kernel.cache_dir'   => $this->getCacheDir() . 'test',
36 5
                    'kernel.environment' => 'test',
37 5
                    'kernel.root_dir'    => __DIR__,
38
                ]
39 5
            )
40 5
        );
41 5
        $container->set('annotation_reader', new AnnotationReader());
42
43 5
        $container->addObjectResource($container);
44 5
        $extensions = [];
45 5
        foreach ($bundles as $bundle) {
46 5
            if ($extension = $bundle->getContainerExtension()) {
47 5
                $container->registerExtension($extension);
48 5
                $extensions[] = $extension->getAlias();
49 5
            }
50
51 5
            $container->addObjectResource($bundle);
52 5
        }
53
54 5
        foreach ($configs as $alias => $config) {
55 2
            $container->prependExtensionConfig($alias, $config);
56 5
        }
57
58 5
        foreach ($bundles as $bundle) {
59 5
            $bundle->build($container);
60 5
        }
61
62
        // ensure these extensions are implicitly loaded
63 5
        $container->getCompilerPassConfig()->setMergePass(new MergeExtensionConfigurationPass($extensions));
64
65 5
        foreach ($bundles as $bundle) {
66 5
            $bundle->setContainer($container);
67 5
            $bundle->boot();
68 5
        }
69
70 5
        if ($compile) {
71 3
            return $this->compile($container);
72
        }
73
74 2
        return $container;
75
    }
76
77 5
    protected function compile(ContainerBuilder $container)
78
    {
79 5
        $container->compile();
80 5
        $dumper = new PhpDumper($container);
81 5
        $dumper->dump();
82
83 5
        return $container;
84
    }
85
}
86