|
1
|
|
|
<?php |
|
2
|
|
|
namespace Yoanm\BehatUtilsExtension\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\BehatUtilsExtension\Context\BehatContextSubscriberInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class BehatContextSubscriberInitializer |
|
14
|
|
|
* /!\ /!\ Contexts will be aware of Behat events 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
|
4 |
|
public function __construct(EventDispatcherInterface $behatEventDispatcher) |
|
69
|
|
|
{ |
|
70
|
4 |
|
$this->behatEventDispatcher = $behatEventDispatcher; |
|
71
|
4 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritdoc} |
|
75
|
|
|
*/ |
|
76
|
3 |
|
public function initializeContext(Context $context) |
|
77
|
|
|
{ |
|
78
|
3 |
|
if (!$context instanceof BehatContextSubscriberInterface) { |
|
79
|
1 |
|
return; |
|
80
|
|
|
} |
|
81
|
|
|
// This method is called before each scenario/example, so (an instance of) context |
|
82
|
|
|
// is probably already registered |
|
83
|
|
|
// To avoid any problem, keep a trace of registered contexts instance |
|
84
|
|
|
// and remove it at scenario or example end |
|
85
|
|
|
// (See clearBehatContextSubscriber method) |
|
86
|
2 |
|
$this->behatEventDispatcher->addSubscriber($context); |
|
87
|
2 |
|
$this->registeredContextList[] = $context; |
|
88
|
2 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritdoc} |
|
92
|
|
|
*/ |
|
93
|
1 |
|
public static function getSubscribedEvents() |
|
94
|
|
|
{ |
|
95
|
|
|
return [ |
|
96
|
1 |
|
ScenarioTested::AFTER => 'clearBehatContextSubscriber', |
|
97
|
1 |
|
ExampleTested::AFTER => 'clearBehatContextSubscriber', |
|
98
|
1 |
|
]; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Clear contexts subscriber after each scenario/example |
|
103
|
|
|
*/ |
|
104
|
1 |
|
public function clearBehatContextSubscriber() |
|
105
|
|
|
{ |
|
106
|
1 |
|
foreach ($this->registeredContextList as $context) { |
|
107
|
1 |
|
$this->behatEventDispatcher->removeSubscriber($context); |
|
108
|
1 |
|
} |
|
109
|
1 |
|
$this->registeredContextList = []; |
|
110
|
1 |
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|