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
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private $outlineTitle; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Initializes outline. |
51
|
|
|
* |
52
|
|
|
* @param string $title |
53
|
|
|
* @param string[] $tags |
54
|
|
|
* @param StepNode[] $outlineSteps |
55
|
|
|
* @param string[] $tokens |
56
|
|
|
* @param integer $line |
57
|
|
|
* @param string|null $outlineTitle |
58
|
|
|
*/ |
59
|
3 |
|
public function __construct($title, array $tags, $outlineSteps, array $tokens, $line, $outlineTitle = null) |
60
|
|
|
{ |
61
|
3 |
|
$this->title = $title; |
62
|
3 |
|
$this->tags = $tags; |
63
|
3 |
|
$this->outlineSteps = $outlineSteps; |
64
|
3 |
|
$this->tokens = $tokens; |
65
|
3 |
|
$this->line = $line; |
66
|
3 |
|
$this->outlineTitle = $outlineTitle; |
67
|
3 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns node type string |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public function getNodeType() |
75
|
|
|
{ |
76
|
|
|
return 'Example'; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns node keyword. |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public function getKeyword() |
85
|
|
|
{ |
86
|
|
|
return $this->getNodeType(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Returns example title. |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
public function getTitle() |
95
|
|
|
{ |
96
|
|
|
return $this->title; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Checks if outline is tagged with tag. |
101
|
|
|
* |
102
|
|
|
* @param string $tag |
103
|
|
|
* |
104
|
|
|
* @return Boolean |
105
|
|
|
*/ |
106
|
|
|
public function hasTag($tag) |
107
|
|
|
{ |
108
|
|
|
return in_array($tag, $this->getTags()); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Checks if outline has tags (both inherited from feature and own). |
113
|
|
|
* |
114
|
|
|
* @return Boolean |
115
|
|
|
*/ |
116
|
|
|
public function hasTags() |
117
|
|
|
{ |
118
|
|
|
return 0 < count($this->getTags()); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Returns outline tags (including inherited from feature). |
123
|
|
|
* |
124
|
|
|
* @return string[] |
125
|
|
|
*/ |
126
|
|
|
public function getTags() |
127
|
|
|
{ |
128
|
|
|
return $this->tags; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Checks if outline has steps. |
133
|
|
|
* |
134
|
|
|
* @return Boolean |
135
|
|
|
*/ |
136
|
|
|
public function hasSteps() |
137
|
|
|
{ |
138
|
|
|
return 0 < count($this->outlineSteps); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Returns outline steps. |
143
|
|
|
* |
144
|
|
|
* @return StepNode[] |
145
|
|
|
*/ |
146
|
2 |
|
public function getSteps() |
147
|
|
|
{ |
148
|
2 |
|
return $this->steps = $this->steps ? : $this->createExampleSteps(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Returns example tokens. |
153
|
|
|
* |
154
|
|
|
* @return string[] |
155
|
|
|
*/ |
156
|
1 |
|
public function getTokens() |
157
|
|
|
{ |
158
|
1 |
|
return $this->tokens; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Returns outline declaration line number. |
163
|
|
|
* |
164
|
|
|
* @return integer |
165
|
|
|
*/ |
166
|
1 |
|
public function getLine() |
167
|
|
|
{ |
168
|
1 |
|
return $this->line; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Returns outline title. |
173
|
|
|
* |
174
|
|
|
* @return string |
175
|
|
|
*/ |
176
|
|
|
public function getOutlineTitle() |
177
|
|
|
{ |
178
|
|
|
return $this->outlineTitle; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Creates steps for this example from abstract outline steps. |
183
|
|
|
* |
184
|
|
|
* @return StepNode[] |
185
|
|
|
*/ |
186
|
2 |
|
protected function createExampleSteps() |
187
|
|
|
{ |
188
|
2 |
|
$steps = array(); |
189
|
2 |
|
foreach ($this->outlineSteps as $outlineStep) { |
190
|
2 |
|
$keyword = $outlineStep->getKeyword(); |
191
|
2 |
|
$keywordType = $outlineStep->getKeywordType(); |
192
|
2 |
|
$text = $this->replaceTextTokens($outlineStep->getText()); |
193
|
2 |
|
$args = $this->replaceArgumentsTokens($outlineStep->getArguments()); |
194
|
2 |
|
$line = $outlineStep->getLine(); |
195
|
|
|
|
196
|
2 |
|
$steps[] = new StepNode($keyword, $text, $args, $line, $keywordType); |
197
|
2 |
|
} |
198
|
|
|
|
199
|
2 |
|
return $steps; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Replaces tokens in arguments with row values. |
204
|
|
|
* |
205
|
|
|
* @param ArgumentInterface[] $arguments |
206
|
|
|
* |
207
|
|
|
* @return ArgumentInterface[] |
208
|
|
|
*/ |
209
|
2 |
|
protected function replaceArgumentsTokens(array $arguments) |
210
|
|
|
{ |
211
|
2 |
|
foreach ($arguments as $num => $argument) { |
212
|
1 |
|
if ($argument instanceof TableNode) { |
213
|
1 |
|
$arguments[$num] = $this->replaceTableArgumentTokens($argument); |
214
|
1 |
|
} |
215
|
1 |
|
if ($argument instanceof PyStringNode) { |
216
|
1 |
|
$arguments[$num] = $this->replacePyStringArgumentTokens($argument); |
217
|
1 |
|
} |
218
|
2 |
|
} |
219
|
|
|
|
220
|
2 |
|
return $arguments; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Replaces tokens in table with row values. |
225
|
|
|
* |
226
|
|
|
* @param TableNode $argument |
227
|
|
|
* |
228
|
|
|
* @return TableNode |
229
|
|
|
*/ |
230
|
1 |
|
protected function replaceTableArgumentTokens(TableNode $argument) |
231
|
|
|
{ |
232
|
1 |
|
$table = $argument->getTable(); |
233
|
1 |
|
foreach ($table as $line => $row) { |
234
|
1 |
|
foreach (array_keys($row) as $col) { |
235
|
1 |
|
$table[$line][$col] = $this->replaceTextTokens($table[$line][$col]); |
236
|
1 |
|
} |
237
|
1 |
|
} |
238
|
|
|
|
239
|
1 |
|
return new TableNode($table); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Replaces tokens in PyString with row values. |
244
|
|
|
* |
245
|
|
|
* @param PyStringNode $argument |
246
|
|
|
* |
247
|
|
|
* @return PyStringNode |
248
|
|
|
*/ |
249
|
1 |
|
protected function replacePyStringArgumentTokens(PyStringNode $argument) |
250
|
|
|
{ |
251
|
1 |
|
$strings = $argument->getStrings(); |
252
|
1 |
|
foreach ($strings as $line => $string) { |
253
|
1 |
|
$strings[$line] = $this->replaceTextTokens($strings[$line]); |
254
|
1 |
|
} |
255
|
|
|
|
256
|
1 |
|
return new PyStringNode($strings, $argument->getLine()); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Replaces tokens in text with row values. |
261
|
|
|
* |
262
|
|
|
* @param string $text |
263
|
|
|
* |
264
|
|
|
* @return string |
265
|
|
|
*/ |
266
|
2 |
|
protected function replaceTextTokens($text) |
267
|
|
|
{ |
268
|
2 |
|
foreach ($this->tokens as $key => $val) { |
269
|
2 |
|
$text = str_replace('<' . $key . '>', $val, $text); |
270
|
2 |
|
} |
271
|
|
|
|
272
|
2 |
|
return $text; |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|