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
|
|
|
|