InitializedSymfonyExtensionEnvironment::getSuite()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the SymfonyExtension package.
7
 *
8
 * (c) Kamil Kokot <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace FriendsOfBehat\SymfonyExtension\Context\Environment;
15
16
use Behat\Behat\Context\Context;
17
use Behat\Behat\Context\Exception\ContextNotFoundException;
18
use Behat\Testwork\Call\Callee;
19
use Behat\Testwork\Suite\Suite;
20
use FriendsOfBehat\SymfonyExtension\Context\Environment\Handler\ContextServiceEnvironmentHandler;
21
22
/**
23
 * @see ContextServiceEnvironmentHandler
24
 */
25
final class InitializedSymfonyExtensionEnvironment implements SymfonyExtensionEnvironment
26
{
27
    /** @var Suite */
28
    private $suite;
29
30
    /** @var Context[] */
31
    private $contexts = [];
32
33
    public function __construct(Suite $suite)
34
    {
35
        $this->suite = $suite;
36
    }
37
38
    public function registerContext(Context $context): void
39
    {
40
        $this->contexts[get_class($context)] = $context;
41
    }
42
43
    public function getSuite(): Suite
44
    {
45
        return $this->suite;
46
    }
47
48
    public function bindCallee(Callee $callee): callable
49
    {
50
        $callable = $callee->getCallable();
51
52
        if (is_array($callable) && $callee->isAnInstanceMethod()) {
53
            return [$this->getContext($callable[0]), $callable[1]];
54
        }
55
56
        return $callable;
57
    }
58
59
    public function hasContexts(): bool
60
    {
61
        return count($this->contexts) > 0;
62
    }
63
64
    public function getContextClasses(): array
65
    {
66
        return array_keys($this->contexts);
67
    }
68
69
    public function hasContextClass($class): bool
70
    {
71
        return isset($this->contexts[$class]);
72
    }
73
74
    /**
75
     * @see http://behat.org/en/latest/cookbooks/accessing_contexts_from_each_other.html
76
     *
77
     * @throws ContextNotFoundException
78
     */
79
    public function getContext(string $class): Context
80
    {
81
        if (!isset($this->contexts[$class])) {
82
            throw new ContextNotFoundException(sprintf(
83
                '`%s` context is not found in the suite environment. Have you registered it?',
84
                $class
85
            ), $class);
86
        }
87
88
        return $this->contexts[$class];
89
    }
90
}
91