1
|
|
|
<?php |
2
|
|
|
namespace Yoanm\Behat3SymfonyExtension\Subscriber; |
3
|
|
|
|
4
|
|
|
use Behat\Behat\EventDispatcher\Event\BackgroundTested; |
5
|
|
|
use Behat\Behat\EventDispatcher\Event\ExampleTested; |
6
|
|
|
use Behat\Behat\EventDispatcher\Event\FeatureTested; |
7
|
|
|
use Behat\Behat\EventDispatcher\Event\GherkinNodeTested; |
8
|
|
|
use Behat\Behat\EventDispatcher\Event\OutlineTested; |
9
|
|
|
use Behat\Behat\EventDispatcher\Event\ScenarioTested; |
10
|
|
|
use Behat\Behat\EventDispatcher\Event\StepTested; |
11
|
|
|
use Behat\Gherkin\Node\ExampleNode; |
12
|
|
|
use Behat\Gherkin\Node\StepContainerInterface; |
13
|
|
|
use Behat\Gherkin\Node\StepNode; |
14
|
|
|
use Behat\Testwork\EventDispatcher\Event\AfterTested; |
15
|
|
|
use Psr\Log\LoggerInterface; |
16
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class BehatStepLoggerSubscriber |
20
|
|
|
*/ |
21
|
|
|
class BehatStepLoggerSubscriber implements EventSubscriberInterface |
22
|
|
|
{ |
23
|
|
|
/** @var LoggerInterface */ |
24
|
|
|
private $logger; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param LoggerInterface $logger |
28
|
|
|
*/ |
29
|
|
|
public function __construct(LoggerInterface $logger) |
30
|
|
|
{ |
31
|
|
|
$this->logger = $logger; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
public static function getSubscribedEvents() |
38
|
|
|
{ |
39
|
|
|
// Set hight priority to log it at beginning |
40
|
|
|
$hightPriority = 9999999999999; |
41
|
|
|
return [ |
42
|
|
|
FeatureTested::BEFORE => ['featureEvents', $hightPriority], |
43
|
|
|
BackgroundTested::BEFORE => ['backgroundEvents', $hightPriority], |
44
|
|
|
ScenarioTested::BEFORE => ['scenarioEvents', $hightPriority], |
45
|
|
|
OutlineTested::BEFORE => ['outlineEvents', $hightPriority], |
46
|
|
|
ExampleTested::BEFORE => ['exampleEvents', $hightPriority], |
47
|
|
|
StepTested::BEFORE => ['stepEvents', $hightPriority], |
48
|
|
|
FeatureTested::AFTER => ['featureEvents', $hightPriority], |
49
|
|
|
BackgroundTested::AFTER => ['backgroundEvents', $hightPriority], |
50
|
|
|
ScenarioTested::AFTER => ['scenarioEvents', $hightPriority], |
51
|
|
|
OutlineTested::AFTER => ['outlineEvents', $hightPriority], |
52
|
|
|
ExampleTested::AFTER => ['exampleEvents', $hightPriority], |
53
|
|
|
StepTested::AFTER => ['stepEvents', $hightPriority], |
54
|
|
|
]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param FeatureTested $event |
59
|
|
|
*/ |
60
|
|
|
public function featureEvents(FeatureTested $event) |
61
|
|
|
{ |
62
|
|
|
list($header,) = $this->getNodeContext('FEATURE', $event); |
63
|
|
|
$this->logger->debug( |
64
|
|
|
$header, |
65
|
|
|
[ |
66
|
|
|
'title' => $event->getFeature()->getTitle(), |
67
|
|
|
'file' => $event->getFeature()->getFile(), |
68
|
|
|
] |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param BackgroundTested $event |
74
|
|
|
*/ |
75
|
|
|
public function backgroundEvents(BackgroundTested $event) |
76
|
|
|
{ |
77
|
|
|
list($header, $line) = $this->getNodeContext('BACKGROUND', $event); |
78
|
|
|
$this->logger->debug( |
79
|
|
|
$header, |
80
|
|
|
[ |
81
|
|
|
'title' => $event->getBackground()->getTitle(), |
82
|
|
|
'line' => $line, |
83
|
|
|
] |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param ScenarioTested $event |
89
|
|
|
*/ |
90
|
|
|
public function scenarioEvents(ScenarioTested $event) |
91
|
|
|
{ |
92
|
|
|
list($header, $line) = $this->getNodeContext('SCENARIO', $event); |
93
|
|
|
$this->logger->debug( |
94
|
|
|
$header, |
95
|
|
|
[ |
96
|
|
|
'title' => $event->getScenario()->getTitle(), |
97
|
|
|
'line' => $line, |
98
|
|
|
] |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param OutlineTested $event |
104
|
|
|
*/ |
105
|
|
|
public function outlineEvents(OutlineTested $event) |
106
|
|
|
{ |
107
|
|
|
list($header, $line) = $this->getNodeContext('SCENARIO OUTLINE', $event); |
108
|
|
|
$this->logger->debug( |
109
|
|
|
$header, |
110
|
|
|
[ |
111
|
|
|
'title' => $event->getOutline()->getTitle(), |
112
|
|
|
'line' => $line, |
113
|
|
|
] |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param ScenarioTested $event |
119
|
|
|
*/ |
120
|
|
|
public function exampleEvents(ScenarioTested $event) |
121
|
|
|
{ |
122
|
|
|
list($header, $line) = $this->getNodeContext('SCENARIO EXAMPLE', $event); |
123
|
|
|
$tokens = []; |
124
|
|
|
$scenario = $event->getScenario(); |
125
|
|
|
if ($scenario instanceof ExampleNode) { |
126
|
|
|
$tokens = $scenario->getTokens(); |
127
|
|
|
} |
128
|
|
|
$this->logger->debug( |
129
|
|
|
$header, |
130
|
|
|
[ |
131
|
|
|
'tokens' => $tokens, |
132
|
|
|
'line' => $line, |
133
|
|
|
] |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param StepTested $event |
139
|
|
|
*/ |
140
|
|
|
public function stepEvents(StepTested $event) |
141
|
|
|
{ |
142
|
|
|
list($header, $line) = $this->getNodeContext('STEP', $event); |
143
|
|
|
$this->logger->debug( |
144
|
|
|
$header, |
145
|
|
|
[ |
146
|
|
|
'text' => $event->getStep()->getText(), |
147
|
|
|
'line' => $line, |
148
|
|
|
] |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param string $eventId |
154
|
|
|
* @param GherkinNodeTested $event |
155
|
|
|
* |
156
|
|
|
* @return array the action text as first value and the node start line as second value |
157
|
|
|
*/ |
158
|
|
|
protected function getNodeContext($eventId, GherkinNodeTested $event) |
159
|
|
|
{ |
160
|
|
|
$action = 'IN'; |
161
|
|
|
$line = $event->getNode()->getLine(); |
162
|
|
|
if ($event instanceof AfterTested) { |
163
|
|
|
$action = 'OUT'; |
164
|
|
|
$node = $event->getNode(); |
165
|
|
|
if ($node instanceof StepContainerInterface) { |
166
|
|
|
$stepList = $node->getSteps(); |
167
|
|
|
$lastStep = array_pop($stepList); |
168
|
|
|
// Check if StepContainer is not empty |
169
|
|
|
if ($lastStep instanceof StepNode) { |
170
|
|
|
$line = $lastStep->getLine(); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
$header = sprintf('[%s][%s]', $eventId, $action); |
175
|
|
|
|
176
|
|
|
return [$header, $line]; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|