Failed Conditions
Push — feature/stepLogger ( ed93ac...7567ff )
by Yo
02:50 queued 02:13
created

BehatStepLoggerSubscriber::stepEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
ccs 0
cts 11
cp 0
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
crap 2
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
        return [
40
            FeatureTested::BEFORE => 'featureEvents',
41
            FeatureTested::AFTER => 'featureEvents',
42
            BackgroundTested::BEFORE => 'backgroundEvents',
43
            BackgroundTested::AFTER => 'backgroundEvents',
44
            ScenarioTested::BEFORE => 'scenarioEvents',
45
            ScenarioTested::AFTER => 'scenarioEvents',
46
            OutlineTested::BEFORE => 'outlineEvents',
47
            OutlineTested::AFTER => 'outlineEvents',
48
            ExampleTested::BEFORE => 'exampleEvents',
49
            ExampleTested::AFTER => 'exampleEvents',
50
            StepTested::BEFORE => 'stepEvents',
51
            StepTested::AFTER => 'stepEvents',
52
        ];
53
    }
54
55
    /**
56
     * @param FeatureTested $event
57
     */
58
    public function featureEvents(FeatureTested $event)
59
    {
60
        list($header,) = $this->getNodeContext('FEATURE', $event);
61
        $this->logger->debug(
62
            $header,
63
            [
64
                'title' => $event->getFeature()->getTitle(),
65
                'file' => $event->getFeature()->getFile(),
66
            ]
67
        );
68
    }
69
70
    /**
71
     * @param BackgroundTested $event
72
     */
73
    public function backgroundEvents(BackgroundTested $event)
74
    {
75
        list($header, $line) = $this->getNodeContext('BACKGROUND', $event);
76
        $this->logger->debug(
77
            $header,
78
            [
79
                'title' => $event->getBackground()->getTitle(),
80
                'line' => $line,
81
            ]
82
        );
83
    }
84
85
    /**
86
     * @param ScenarioTested $event
87
     */
88
    public function scenarioEvents(ScenarioTested $event)
89
    {
90
        list($header, $line) = $this->getNodeContext('SCENARIO', $event);
91
        $this->logger->debug(
92
            $header,
93
            [
94
                'title' => $event->getScenario()->getTitle(),
95
                'line' => $line,
96
            ]
97
        );
98
    }
99
100
    /**
101
     * @param OutlineTested $event
102
     */
103
    public function outlineEvents(OutlineTested $event)
104
    {
105
        list($header, $line) = $this->getNodeContext('SCENARIO OUTLINE', $event);
106
        $this->logger->debug(
107
            $header,
108
            [
109
                'title' => $event->getOutline()->getTitle(),
110
                'line' => $line,
111
            ]
112
        );
113
    }
114
115
    /**
116
     * @param ScenarioTested $event
117
     */
118
    public function exampleEvents(ScenarioTested $event)
119
    {
120
        list($header, $line) = $this->getNodeContext('SCENARIO EXAMPLE', $event);
121
        $tokens = [];
122
        $scenario = $event->getScenario();
123
        if ($scenario instanceof ExampleNode) {
124
            $tokens = $scenario->getTokens();
125
        }
126
        $this->logger->debug(
127
            $header,
128
            [
129
                'tokens' => $tokens,
130
                'line' => $line,
131
            ]
132
        );
133
    }
134
135
    /**
136
     * @param StepTested $event
137
     */
138
    public function stepEvents(StepTested $event)
139
    {
140
        list($header, $line) = $this->getNodeContext('STEP', $event);
141
        $this->logger->debug(
142
            $header,
143
            [
144
                'text' => $event->getStep()->getText(),
145
                'line' => $line,
146
            ]
147
        );
148
    }
149
150
    /**
151
     * @param string            $eventId
152
     * @param GherkinNodeTested $event
153
     *
154
     * @return array the action text as first value and the node start line as second value
155
     */
156
    protected function getNodeContext($eventId, GherkinNodeTested $event)
157
    {
158
        $action = 'IN';
159
        $line = $event->getNode()->getLine();
160
        if ($event instanceof AfterTested) {
161
            $action = 'OUT';
162
            $node = $event->getNode();
163
            if ($node instanceof StepContainerInterface) {
164
                $stepList = $node->getSteps();
165
                $lastStep = array_pop($stepList);
166
                // Check if StepContainer is not empty
167
                if ($lastStep instanceof StepNode) {
168
                    $line = $lastStep->getLine();
169
                }
170
            }
171
        }
172
        $header = sprintf('[%s][%s]', $eventId, $action);
173
174
        return [$header, $line];
175
    }
176
}
177