BehatRetryExtension::loadRuntimeStepTester()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php namespace Chekote\BehatRetryExtension\ServiceContainer;
2
3
use Behat\Behat\Definition\ServiceContainer\DefinitionExtension;
4
use Behat\Testwork\Call\ServiceContainer\CallExtension;
5
use Behat\Testwork\ServiceContainer\Extension;
6
use Behat\Testwork\ServiceContainer\ExtensionManager;
7
use Chekote\BehatRetryExtension\Tester\RuntimeStepTester;
8
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
9
use Symfony\Component\DependencyInjection\ContainerBuilder;
10
use Symfony\Component\DependencyInjection\Definition;
11
use Symfony\Component\DependencyInjection\Reference;
12
13
/**
14
 * Extension for automatically retrying "Then" steps.
15
 */
16
class BehatRetryExtension implements Extension
17
{
18
    /** The service that our step tester needs to replace */
19
    const SERVICE_ID = 'tester.step.wrapper.hookable.inner';
20
21
    const CONFIG_KEY = 'spinner';
22
23
    const CONFIG_PARAM_ALL = 'parameters';
24
    const CONFIG_PARAM_INTERVAL = 'interval';
25
    const CONFIG_PARAM_TIMEOUT = 'timeout';
26
    const CONFIG_PARAM_STRICT_KEYWORDS = 'strictKeywords';
27
28
    const CONFIG_ALL = self::CONFIG_KEY . '.' . self::CONFIG_PARAM_ALL;
29
    const CONFIG_RETRY_INTERVAL = self::CONFIG_KEY . '.' . self::CONFIG_PARAM_INTERVAL;
30
    const CONFIG_TIMEOUT = self::CONFIG_KEY . '.' . self::CONFIG_PARAM_TIMEOUT;
31
    const CONFIG_STRICT_KEYWORDS = self::CONFIG_KEY . '.' . self::CONFIG_PARAM_STRICT_KEYWORDS;
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function process(ContainerBuilder $container)
37
    {
38
        $definition = new Definition(RuntimeStepTester::class, [
39
            new Reference(DefinitionExtension::FINDER_ID),
40
            new Reference(CallExtension::CALL_CENTER_ID),
41
        ]);
42
43
        $container->setDefinition(self::SERVICE_ID, $definition);
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getConfigKey()
50
    {
51
        return self::CONFIG_KEY;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function initialize(ExtensionManager $extensionManager)
58
    {
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function configure(ArrayNodeDefinition $builder)
65
    {
66
        /* @scrutinizer ignore-call Scrutinizer does not understand the context that determines the return types  */
67
        $builder
68
            ->children()
69
                ->floatNode(self::CONFIG_PARAM_TIMEOUT)->defaultValue(5)->end()
70
                ->integerNode(self::CONFIG_PARAM_INTERVAL)->defaultValue(100000000)->end()
71
                ->booleanNode(self::CONFIG_PARAM_STRICT_KEYWORDS)->defaultTrue()->end()
72
            ->end()
73
        ->end();
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function load(ContainerBuilder $container, array $config)
80
    {
81
        $container->setParameter(self::CONFIG_ALL, $config);
82
        $container->setParameter(self::CONFIG_TIMEOUT, $config[self::CONFIG_PARAM_TIMEOUT]);
83
        $container->setParameter(self::CONFIG_RETRY_INTERVAL, $config[self::CONFIG_PARAM_INTERVAL]);
84
        $container->setParameter(self::CONFIG_STRICT_KEYWORDS, $config[self::CONFIG_PARAM_STRICT_KEYWORDS]);
85
86
        $this->loadRuntimeStepTester($container);
87
    }
88
89
    /**
90
     * Sets up up the RuntimeStepTester.
91
     *
92
     * The specified container should have a self::CONFIG_TIMEOUT and a self::CONFIG_RETRY_INTERVAL parameter.
93
     *
94
     * @param ContainerBuilder $container the container with the parameters to use.
95
     */
96
    private function loadRuntimeStepTester(ContainerBuilder $container)
97
    {
98
        RuntimeStepTester::$timeout = $container->getParameter(self::CONFIG_TIMEOUT);
99
        RuntimeStepTester::$interval = $container->getParameter(self::CONFIG_RETRY_INTERVAL);
100
        RuntimeStepTester::$strictKeywords = $container->getParameter(self::CONFIG_STRICT_KEYWORDS);
101
    }
102
}
103