Completed
Push — master ( 873526...76d715 )
by Yo
02:14
created

Behat3SymfonyExtension::process()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 21
cts 21
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 17
nc 8
nop 1
crap 5
1
<?php
2
namespace Yoanm\Behat3SymfonyExtension\ServiceContainer;
3
4
use Behat\MinkExtension\ServiceContainer\MinkExtension;
5
use Behat\Testwork\ServiceContainer\Exception\ProcessingException;
6
use Behat\Testwork\ServiceContainer\Extension;
7
use Behat\Testwork\ServiceContainer\ExtensionManager;
8
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
9
use Symfony\Component\Config\FileLocator;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
12
use Yoanm\Behat3SymfonyExtension\ServiceContainer\Configuration\KernelConfiguration;
13
use Yoanm\Behat3SymfonyExtension\ServiceContainer\Configuration\LoggerConfiguration;
14
use Yoanm\Behat3SymfonyExtension\ServiceContainer\DriverFactory\Behat3SymfonyFactory;
15
16
class Behat3SymfonyExtension implements Extension
17
{
18
    const TEST_CLIENT_SERVICE_ID = 'behat3_symfony_extension.test.client';
19
    const KERNEL_SERVICE_ID = 'behat3_symfony_extension.kernel';
20
21
    /**
22
     * {@inheritdoc}
23
     */
24 1
    public function getConfigKey()
25
    {
26 1
        return 'behat3_symfony';
27
    }
28
29
    // @codeCoverageIgnoreStart
30
    /**
31
     * (Not possible to cover this because ExtensionManager is a final class)
32
     *
33
     * {@inheritdoc}
34
     */
35
    public function initialize(ExtensionManager $extensionManager)
36
    {
37
        $minExtension = $extensionManager->getExtension('mink');
38
        if ($minExtension instanceof MinkExtension) {
39
            $minExtension->registerDriverFactory(new Behat3SymfonyFactory());
40
        }
41
    }
42
43
    /**
44
     * (Will be covered by Functional tests)
45
     * {@inheritdoc}
46
     */
47
    public function configure(ArrayNodeDefinition $builder)
48
    {
49
        $builder->append((new KernelConfiguration())->getConfigTreeBuilder());
50
        $builder->append((new LoggerConfiguration())->getConfigTreeBuilder());
51
    }
52
    // @codeCoverageIgnoreEnd
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 4
    public function load(ContainerBuilder $container, array $config)
58
    {
59 4
        foreach ($config['kernel'] as $key => $value) {
60 4
            $container->setParameter(sprintf('behat3_symfony_extension.kernel.%s', $key), $value);
61 4
        }
62 4
        foreach ($config['logger'] as $key => $value) {
63 4
            $container->setParameter(sprintf('behat3_symfony_extension.logger.%s', $key), $value);
64 4
        }
65 4
        $loader = new XmlFileLoader(
66 4
            $container,
67 4
            new FileLocator(__DIR__.'/../Resources/config')
68 4
        );
69
70 4
        $loader->load('client.xml');
71 4
        $loader->load('kernel.xml');
72 4
        $loader->load('initializer.xml');
73 4
        $loader->load('logger.xml');
74 4
        if (true === $config['kernel']['reboot']) {
75 2
            $loader->load('kernel_auto_reboot.xml');
76 2
        }
77 4
        if (true === $config['kernel']['debug']) {
78 2
            $loader->load('kernel_debug_mode.xml');
79 2
        }
80 4
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 4
    public function process(ContainerBuilder $container)
86
    {
87 4
        $basePath = $container->getParameter('paths.base');
88 4
        $bootstrapPath = $container->getParameter('behat3_symfony_extension.kernel.bootstrap');
89 4
        if ($bootstrapPath) {
90 3
            $bootstrapPathUnderBasePath = sprintf('%s/%s', $basePath, $bootstrapPath);
91 3
            if (file_exists($bootstrapPathUnderBasePath)) {
92 2
                $bootstrapPath = $bootstrapPathUnderBasePath;
93 2
            }
94 3
            if (file_exists($bootstrapPath)) {
95 2
                require_once($bootstrapPath);
96 2
            } else {
97 1
                throw new ProcessingException('Could not find bootstrap file !');
98
            }
99 2
        }
100
101
        // load kernel
102 3
        $kernelPath = $container->getParameter('behat3_symfony_extension.kernel.path');
103 3
        $kernelPathUnderBasePath = sprintf('%s/%s', $basePath, $kernelPath);
104 3
        if (file_exists($kernelPathUnderBasePath)) {
105 1
            $kernelPath = $kernelPathUnderBasePath;
106 1
        }
107
108 3
        $container->getDefinition(self::KERNEL_SERVICE_ID)
109 3
            ->setFile($kernelPath);
110 3
    }
111
}
112