Failed Conditions
Pull Request — master (#33)
by Yo
02:17
created

clearBehatContextSubscriber()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 0
cts 6
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 6
1
<?php
2
namespace Yoanm\Behat3SymfonyExtension\Context\Initializer;
3
4
use Behat\Behat\Context\Context;
5
use Behat\Behat\Context\Initializer\ContextInitializer;
6
use Behat\Behat\EventDispatcher\Event\ExampleTested;
7
use Behat\Behat\EventDispatcher\Event\ScenarioTested;
8
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
9
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10
use Yoanm\Behat3SymfonyExtension\Context\BehatContextSubscriberInterface;
11
12
/**
13
 * Class BehatContextSubscriberInitializer
14
 * /!\ /!\ Contexts will be aware of behat event only from ScenarioTested::BEFORE to ScenarioTested::AFTER only /!\ /!\
15
 *
16
 * This is due to the fact that context are (re)-created for each scenario or example
17
 * So contexts will have access to the followings event workflow (depending of the type of the scenario) :
18
 *
19
 * - Basic scenario :
20
 *      - ScenarioTested
21
 *          - BEFORE
22
 *          - AFTER_SETUP
23
 *      - BackgroundTested
24
 *          - BEFORE
25
 *          - AFTER_SETUP
26
 *      - StepTested
27
 *          - BEFORE
28
 *          - AFTER_SETUP
29
 *          - BEFORE_TEARDOWN
30
 *          - AFTER
31
 *      - BackgroundTested
32
 *          - BEFORE_TEARDOWN
33
 *          - AFTER
34
 *      - ScenarioTested
35
 *          - BEFORE_TEARDOWN
36
 *          - AFTER
37
 *
38
 * - Scenario outline :
39
 *      - ExampleTested
40
 *          - BEFORE
41
 *          - AFTER_SETUP
42
 *      - BackgroundTested
43
 *          - BEFORE
44
 *          - AFTER_SETUP
45
 *      - StepTested
46
 *          - BEFORE
47
 *          - AFTER_SETUP
48
 *          - BEFORE_TEARDOWN
49
 *          - AFTER
50
 *      - BackgroundTested
51
 *          - BEFORE_TEARDOWN
52
 *          - AFTER
53
 *      - ExampleTested
54
 *          - BEFORE_TEARDOWN
55
 *          - AFTER
56
 */
57
class BehatContextSubscriberInitializer implements ContextInitializer, EventSubscriberInterface
58
{
59
    /** @var EventDispatcherInterface */
60
    private $behatEventDispatcher;
61
    /** @var BehatContextSubscriberInterface[] */
62
    private $registeredContextList = [];
63
64
65
    /**
66
     * @param EventDispatcherInterface $behatEventDispatcher
67
     */
68 2
    public function __construct(EventDispatcherInterface $behatEventDispatcher)
69
    {
70 2
        $this->behatEventDispatcher = $behatEventDispatcher;
71 2
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 2
    public function initializeContext(Context $context)
77
    {
78 2
        if (!$context instanceof BehatContextSubscriberInterface) {
79 1
            return;
80
        }
81
        // This method is called before each scenario/example, so context is probably already registered
82
        // To avoid any problem, keep a trace of registered context and remove it at feature end
83
        // (See clearBehatContextSubscriber method)
84 1
        $this->registeredContextList[] = $context;
85 1
        $this->behatEventDispatcher->addSubscriber($context);
86 1
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public static function getSubscribedEvents()
92
    {
93
        return [
94
            ScenarioTested::AFTER => 'clearBehatContextSubscriber',
95
            ExampleTested::AFTER => 'clearBehatContextSubscriber',
96
        ];
97
    }
98
99
    /**
100
     * Clear contexts subscriber after each scenario/example
101
     */
102
    public function clearBehatContextSubscriber()
103
    {
104
        foreach ($this->registeredContextList as $context) {
105
            $this->behatEventDispatcher->removeSubscriber($context);
106
        }
107
        $this->registeredContextList = [];
108
    }
109
}
110