Completed
Pull Request — master (#112)
by Christophe
02:45
created

ExampleNode::hasTag()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the Behat Gherkin.
5
 * (c) Konstantin Kudryashov <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Behat\Gherkin\Node;
12
13
/**
14
 * Represents Gherkin Outline Example.
15
 *
16
 * @author Konstantin Kudryashov <[email protected]>
17
 */
18
class ExampleNode implements ScenarioInterface
19
{
20
    /**
21
     * @var string
22
     */
23
    private $title;
24
    /**
25
     * @var string[]
26
     */
27
    private $tags;
28
    /**
29
     * @var StepNode[]
30
     */
31
    private $outlineSteps;
32
    /**
33
     * @var string[]
34
     */
35
    private $tokens;
36
    /**
37
     * @var integer
38
     */
39
    private $line;
40
    /**
41
     * @var null|StepNode[]
42
     */
43
    private $steps;
44
45
    /**
46
     * Initializes outline.
47
     *
48
     * @param string     $title
49
     * @param string[]   $tags
50
     * @param StepNode[] $outlineSteps
51
     * @param string[]   $tokens
52
     * @param integer    $line
53
     */
54 3
    public function __construct($title, array $tags, $outlineSteps, array $tokens, $line)
55
    {
56 3
        $this->title = $title;
57 3
        $this->tags = $tags;
58 3
        $this->outlineSteps = $outlineSteps;
59 3
        $this->tokens = $tokens;
60 3
        $this->line = $line;
61 3
    }
62
63
    /**
64
     * Returns node type string
65
     *
66
     * @return string
67
     */
68
    public function getNodeType()
69
    {
70
        return 'Example';
71
    }
72
73
    /**
74
     * Returns node keyword.
75
     *
76
     * @return string
77
     */
78
    public function getKeyword()
79
    {
80
        return $this->getNodeType();
81
    }
82
83
    /**
84
     * Returns example title.
85
     *
86
     * @return string
87
     */
88
    public function getTitle()
89
    {
90
        return $this->title;
91
    }
92
93
    /**
94
     * Checks if outline is tagged with tag.
95
     *
96
     * @param string $tag
97
     *
98
     * @return Boolean
99
     */
100
    public function hasTag($tag)
101
    {
102
        return in_array($tag, $this->getTags());
103
    }
104
105
    /**
106
     * Checks if outline has tags (both inherited from feature and own).
107
     *
108
     * @return Boolean
109
     */
110
    public function hasTags()
111
    {
112
        return 0 < count($this->getTags());
113
    }
114
115
    /**
116
     * Returns outline tags (including inherited from feature).
117
     *
118
     * @return string[]
119
     */
120
    public function getTags()
121
    {
122
        return $this->tags;
123
    }
124
125
    /**
126
     * Checks if outline has steps.
127
     *
128
     * @return Boolean
129
     */
130
    public function hasSteps()
131
    {
132
        return 0 < count($this->outlineSteps);
133
    }
134
135
    /**
136
     * Returns outline steps.
137
     *
138
     * @return StepNode[]
139
     */
140 2
    public function getSteps()
141
    {
142 2
        return $this->steps = $this->steps ? : $this->createExampleSteps();
143
    }
144
145
    /**
146
     * Returns example tokens.
147
     *
148
     * @return string[]
149
     */
150 1
    public function getTokens()
151
    {
152 1
        return $this->tokens;
153
    }
154
155
    /**
156
     * Returns outline declaration line number.
157
     *
158
     * @return integer
159
     */
160 1
    public function getLine()
161
    {
162 1
        return $this->line;
163
    }
164
165
    /**
166
     * Creates steps for this example from abstract outline steps.
167
     *
168
     * @return StepNode[]
169
     */
170 2
    protected function createExampleSteps()
171
    {
172 2
        $steps = array();
173 2
        foreach ($this->outlineSteps as $outlineStep) {
174 2
            $keyword = $outlineStep->getKeyword();
175 2
            $keywordType = $outlineStep->getKeywordType();
176 2
            $text = $this->replaceTextTokens($outlineStep->getText());
177 2
            $args = $this->replaceArgumentsTokens($outlineStep->getArguments());
178 2
            $line = $outlineStep->getLine();
179
180 2
            $steps[] = new StepNode($keyword, $text, $args, $line, $keywordType);
181 2
        }
182
183 2
        return $steps;
184
    }
185
186
    /**
187
     * Replaces tokens in arguments with row values.
188
     *
189
     * @param ArgumentInterface[] $arguments
190
     *
191
     * @return ArgumentInterface[]
192
     */
193 2
    protected function replaceArgumentsTokens(array $arguments)
194
    {
195 2
        foreach ($arguments as $num => $argument) {
196 1
            if ($argument instanceof TableNode) {
197 1
                $arguments[$num] = $this->replaceTableArgumentTokens($argument);
198 1
            }
199 1
            if ($argument instanceof PyStringNode) {
200 1
                $arguments[$num] = $this->replacePyStringArgumentTokens($argument);
201 1
            }
202 2
        }
203
204 2
        return $arguments;
205
    }
206
207
    /**
208
     * Replaces tokens in table with row values.
209
     *
210
     * @param TableNode $argument
211
     *
212
     * @return TableNode
213
     */
214 1
    protected function replaceTableArgumentTokens(TableNode $argument)
215
    {
216 1
        $table = $argument->getTable();
217 1
        foreach ($table as $line => $row) {
218 1
            foreach (array_keys($row) as $col) {
219 1
                $table[$line][$col] = $this->replaceTextTokens($table[$line][$col]);
220 1
            }
221 1
        }
222
223 1
        return new TableNode($table);
224
    }
225
226
    /**
227
     * Replaces tokens in PyString with row values.
228
     *
229
     * @param PyStringNode $argument
230
     *
231
     * @return PyStringNode
232
     */
233 1
    protected function replacePyStringArgumentTokens(PyStringNode $argument)
234
    {
235 1
        $strings = $argument->getStrings();
236 1
        foreach ($strings as $line => $string) {
237 1
            $strings[$line] = $this->replaceTextTokens($strings[$line]);
238 1
        }
239
240 1
        return new PyStringNode($strings, $argument->getLine());
241
    }
242
243
    /**
244
     * Replaces tokens in text with row values.
245
     *
246
     * @param string $text
247
     *
248
     * @return string
249
     */
250 2
    protected function replaceTextTokens($text)
251
    {
252 2
        foreach ($this->tokens as $key => $val) {
253 2
            $text = str_replace('<' . $key . '>', $val, $text);
254 2
        }
255
256 2
        return $text;
257
    }
258
}
259