Completed
Pull Request — master (#25)
by Yo
04:49 queued 02:28
created

KernelSubExtension::loadInitializer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

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