Failed Conditions
Pull Request — master (#25)
by Yo
05:43 queued 02:51
created

KernelSubExtension::loadContainerParameter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 2
crap 2
1
<?php
2
namespace Yoanm\Behat3SymfonyExtension\ServiceContainer\SubExtension;
3
4
use Behat\MinkExtension\ServiceContainer\MinkExtension;
5
use Behat\Testwork\ServiceContainer\Exception\ProcessingException;
6
use Behat\Testwork\ServiceContainer\ExtensionManager;
7
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
8
use Symfony\Component\DependencyInjection\ContainerBuilder;
9
use Yoanm\Behat3SymfonyExtension\ServiceContainer\AbstractExtension;
10
use Yoanm\Behat3SymfonyExtension\ServiceContainer\DriverFactory\Behat3SymfonyFactory;
11
12
class KernelSubExtension extends AbstractExtension
13
{
14
    /**
15
     * @inheritDoc
16
     */
17 1
    public function getConfigKey()
18
    {
19 1
        return 'kernel';
20
    }
21
22
    // @codeCoverageIgnoreStart
23
    // Not possible to cover this because ExtensionManager is a final class
24
    // Will be covered by FT
25
    /**
26
     * @inheritDoc
27
     */
28
    public function initialize(ExtensionManager $extensionManager)
29
    {
30
        $minExtension = $extensionManager->getExtension('mink');
31
        if ($minExtension instanceof MinkExtension) {
32
            $minExtension->registerDriverFactory(new Behat3SymfonyFactory());
33
        }
34
    }
35
    // @codeCoverageIgnoreEnd
36
37
    // @codeCoverageIgnoreStart
38
    // Will be covered by FT
39
    /**
40
     * @inheritDoc
41
     */
42
    public function configure(ArrayNodeDefinition $builder)
43
    {
44
        $castToBool = function ($value) {
45
            $filtered = filter_var(
46
                $value,
47
                FILTER_VALIDATE_BOOLEAN,
48
                FILTER_NULL_ON_FAILURE
49
            );
50
51
            return (null === $filtered) ? (bool) $value : $filtered;
52
        };
53
        $builder
54
            ->addDefaultsIfNotSet()
55
            ->children()
56
                ->scalarNode('bootstrap')
57
                    ->defaultValue('app/autoload.php')
58
                ->end()
59
                ->scalarNode('path')
60
                    ->defaultValue('app/AppKernel.php')
61
                ->end()
62
                ->scalarNode('class')
63
                    ->defaultValue('AppKernel')
64
                ->end()
65
                ->scalarNode('env')
66
                    ->defaultValue('test')
67
                ->end()
68
                ->booleanNode('debug')
69
                    ->beforeNormalization()
70
                    ->always()
71
                        ->then($castToBool)
72
                    ->end()
73
                    ->defaultTrue()
74
                ->end()
75
                ->booleanNode('reboot')
76
                    ->info('If true symfony kernel will be rebooted after each scenario/example')
77
                    ->beforeNormalization()
78
                        ->always()
79
                        ->then($castToBool)
80
                    ->end()
81
                    ->defaultTrue()
82
                ->end()
83
            ->end();
84
    }
85
    // @codeCoverageIgnoreEnd
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function load(ContainerBuilder $container, array $config)
91
    {
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 3
    public function process(ContainerBuilder $container)
98
    {
99 3
        $basePath = $container->getParameter('paths.base');
100 3
        $bootstrapPath = $container->getParameter('behat3_symfony_extension.kernel.bootstrap');
101 3
        if ($bootstrapPath) {
102 2
            $bootstrapPathUnderBasePath = sprintf('%s/%s', $basePath, $bootstrapPath);
103 2
            if (file_exists($bootstrapPathUnderBasePath)) {
104 1
                $bootstrapPath = $bootstrapPathUnderBasePath;
105 1
            }
106 2
            if (file_exists($bootstrapPath)) {
107 1
                require_once($bootstrapPath);
108 1
            } else {
109 1
                throw new ProcessingException('Could not find bootstrap file !');
110
            }
111 1
        }
112
113
        // load kernel
114 2
        $kernelPath = $container->getParameter('behat3_symfony_extension.kernel.path');
115 2
        $kernelPathUnderBasePath = sprintf('%s/%s', $basePath, $kernelPath);
116 2
        if (file_exists($kernelPathUnderBasePath)) {
117
            $kernelPath = $kernelPathUnderBasePath;
118
        }
119
120 2
        $container->getDefinition(self::KERNEL_SERVICE_ID)
121 2
            ->setFile($kernelPath);
122 2
    }
123
}
124