1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GD; |
4
|
|
|
|
5
|
|
|
use Behat\Gherkin\Gherkin; |
6
|
|
|
use Behat\Gherkin\Keywords\CucumberKeywords; |
7
|
|
|
use Behat\Gherkin\Lexer; |
8
|
|
|
use Behat\Gherkin\Loader\GherkinFileLoader; |
9
|
|
|
use Behat\Gherkin\Loader\YamlFileLoader; |
10
|
|
|
use Behat\Gherkin\Parser; |
11
|
|
|
use GD\Exceptions\MustSetFileNameAndPath; |
12
|
|
|
use GD\Helpers\BuildOutContent; |
13
|
|
|
use GD\Helpers\WriteBrowserFile; |
14
|
|
|
use GD\Helpers\WritePHPUnitFile; |
15
|
|
|
use Illuminate\Filesystem\Filesystem; |
16
|
|
|
use Symfony\Component\Yaml\Yaml; |
17
|
|
|
|
18
|
|
|
class GherkinToDusk extends BaseGherkinToDusk |
19
|
|
|
{ |
20
|
|
|
use BuildOutContent; |
21
|
|
|
|
22
|
|
|
protected $component = false; |
23
|
|
|
|
24
|
|
|
protected $string_contents = null; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Yml Content of a test yml |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $feature_content; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var \Behat\Gherkin\Node\FeatureNode |
34
|
|
|
*/ |
35
|
|
|
protected $parsed_feature; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* |
39
|
|
|
*/ |
40
|
|
|
protected $dusk_class_and_methods; |
41
|
|
|
|
42
|
|
|
protected $dusk_test_name; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var WritePHPUnitFile |
46
|
|
|
*/ |
47
|
|
|
protected $write_unit_test; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var WriteBrowserFile |
51
|
|
|
*/ |
52
|
|
|
protected $write_browser_test; |
53
|
|
|
|
54
|
27 |
|
public function initializeFeature() |
55
|
|
|
{ |
56
|
27 |
|
$this->loadFileContent(); |
57
|
|
|
|
58
|
27 |
|
$this->buildDuskTestName(); |
59
|
|
|
|
60
|
27 |
|
$this->passThroughParser(); |
61
|
|
|
|
62
|
27 |
|
$this->breakIntoMethods(); |
63
|
|
|
|
64
|
27 |
|
switch ($this->context) { |
65
|
27 |
|
case 'unit': |
66
|
27 |
|
case 'domain': |
67
|
21 |
|
$this->featureToUnit(); |
68
|
18 |
|
break; |
69
|
6 |
|
case 'ui': |
70
|
6 |
|
case 'browser': |
71
|
6 |
|
$this->featureToBrowser(); |
72
|
6 |
|
break; |
73
|
|
|
default: |
74
|
|
|
//more coming soon |
75
|
|
|
break; |
76
|
16 |
|
} |
77
|
24 |
|
} |
78
|
|
|
|
79
|
6 |
|
protected function featureToBrowser() |
80
|
|
|
{ |
81
|
6 |
|
$this->checkIfFileExists(); |
82
|
|
|
|
83
|
6 |
|
$this->getWriteBrowserTest()->writeTest( |
84
|
6 |
|
$this->getDestinationFolderRoot(), |
85
|
6 |
|
$this->getDuskTestName(), |
86
|
6 |
|
$this->getDuskClassAndMethods() |
87
|
4 |
|
); |
88
|
6 |
|
} |
89
|
|
|
|
90
|
21 |
|
protected function featureToUnit() |
91
|
|
|
{ |
92
|
21 |
|
$this->checkIfFileExists(); |
93
|
|
|
|
94
|
18 |
|
$this->getWriteUnitTest()->writeTest( |
95
|
18 |
|
$this->getDestinationFolderRoot(), |
96
|
18 |
|
$this->getDuskTestName(), |
97
|
18 |
|
$this->getDuskClassAndMethods() |
98
|
12 |
|
); |
99
|
18 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return Parser |
103
|
|
|
*/ |
104
|
30 |
|
public function getParser() |
105
|
|
|
{ |
106
|
30 |
|
return $this->parser; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param Parser $parser |
111
|
|
|
*/ |
112
|
|
|
public function setParser($parser) |
113
|
|
|
{ |
114
|
|
|
$this->parser = $parser; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return boolean |
119
|
|
|
*/ |
120
|
|
|
public function isComponent() |
121
|
|
|
{ |
122
|
|
|
return $this->component; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param boolean $component |
127
|
|
|
*/ |
128
|
|
|
public function setComponent($component) |
129
|
|
|
{ |
130
|
|
|
$this->component = $component; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return mixed |
135
|
|
|
*/ |
136
|
3 |
|
public function getFeatureContent() |
137
|
|
|
{ |
138
|
3 |
|
return $this->feature_content; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param mixed $feature_content |
143
|
|
|
*/ |
144
|
|
|
public function setFeatureContent($feature_content) |
145
|
|
|
{ |
146
|
|
|
$this->feature_content = $feature_content; |
147
|
|
|
} |
148
|
|
|
|
149
|
27 |
|
private function loadFileContent() |
150
|
|
|
{ |
151
|
27 |
|
$this->feature_content = |
152
|
27 |
|
$this->getFilesystem()->get($this->getFullPathToFileAndFileName()); |
153
|
27 |
|
} |
154
|
|
|
|
155
|
27 |
|
private function passThroughParser() |
156
|
|
|
{ |
157
|
27 |
|
$this->parsed_feature = $this->getParser()->parse($this->feature_content); |
158
|
27 |
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return \Behat\Gherkin\Node\FeatureNode |
162
|
|
|
*/ |
163
|
3 |
|
public function getParsedFeature() |
164
|
|
|
{ |
165
|
3 |
|
return $this->parsed_feature; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param \Behat\Gherkin\Node\FeatureNode $parsed_feature |
170
|
|
|
*/ |
171
|
|
|
public function setParsedFeature($parsed_feature) |
172
|
|
|
{ |
173
|
|
|
$this->parsed_feature = $parsed_feature; |
174
|
|
|
} |
175
|
|
|
|
176
|
27 |
|
private function breakIntoMethods() |
177
|
|
|
{ |
178
|
27 |
|
$this->iterateOverScenariosAndBuildUpClassMethods(); |
179
|
27 |
|
} |
180
|
|
|
|
181
|
27 |
|
private function iterateOverScenariosAndBuildUpClassMethods() |
182
|
|
|
{ |
183
|
|
|
/** @var $feature \Behat\Gherkin\Node\ScenarioNode */ |
184
|
27 |
|
foreach ($this->parsed_feature->getScenarios() as $scenario_index => $scenario) { |
185
|
27 |
|
$parent_method_name = ucfirst(camel_case($scenario->getTitle())); |
186
|
|
|
|
187
|
27 |
|
$parent_method_name_camelized_and_prefix_test = sprintf('test%s', $parent_method_name); |
188
|
|
|
|
189
|
27 |
|
$this->dusk_class_and_methods[$scenario_index] = [ |
190
|
27 |
|
'parent' => $parent_method_name_camelized_and_prefix_test, |
191
|
27 |
|
'parent_content' => $this->getParentLevelContent($parent_method_name_camelized_and_prefix_test) |
192
|
18 |
|
]; |
193
|
|
|
|
194
|
27 |
|
$this->buildOutSteps($scenario, $scenario_index); |
195
|
18 |
|
} |
196
|
27 |
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param $scenario \Behat\Gherkin\Node\ScenarioNode |
200
|
|
|
*/ |
201
|
27 |
|
protected function buildOutSteps($scenario, $scenario_index) |
202
|
|
|
{ |
203
|
27 |
|
foreach ($scenario->getSteps() as $step_index => $step) { |
204
|
27 |
|
$method_name = camel_case(sprintf("%s %s", $step->getKeyword(), $step->getText())); |
205
|
27 |
|
$step_method_name_camalized = camel_case(sprintf("%s %s", $step->getKeyword(), $step->getText())); |
206
|
27 |
|
$this->dusk_class_and_methods[$scenario_index]['steps'][$step_index]['name'] = |
207
|
|
|
$method_name; |
208
|
27 |
|
$this->dusk_class_and_methods[$scenario_index]['steps'][$step_index] = |
209
|
27 |
|
$this->getStepLevelContent($step_method_name_camalized); |
210
|
18 |
|
} |
211
|
27 |
|
} |
212
|
|
|
|
213
|
27 |
|
private function buildDuskTestName() |
214
|
|
|
{ |
215
|
27 |
|
if (!$this->dusk_test_name) { |
216
|
27 |
|
$name = $this->getFilesystem()->name($this->getFullPathToFileAndFileName()); |
217
|
27 |
|
$this->dusk_test_name = ucfirst(camel_case($name) . 'Test'); |
218
|
18 |
|
} |
219
|
27 |
|
} |
220
|
|
|
|
221
|
27 |
|
private function getFullPathToFileAndFileName() |
222
|
|
|
{ |
223
|
27 |
|
return getcwd() . DIRECTORY_SEPARATOR . $this->getPathToFeature(); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @return mixed |
228
|
|
|
*/ |
229
|
24 |
|
public function getDuskClassAndMethods() |
230
|
|
|
{ |
231
|
24 |
|
return $this->dusk_class_and_methods; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @param mixed $dusk_class_and_methods |
236
|
|
|
*/ |
237
|
|
|
public function setDuskClassAndMethods($dusk_class_and_methods) |
238
|
|
|
{ |
239
|
|
|
$this->dusk_class_and_methods = $dusk_class_and_methods; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @return mixed |
244
|
|
|
*/ |
245
|
27 |
|
public function getDuskTestName() |
246
|
|
|
{ |
247
|
27 |
|
return $this->dusk_test_name; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @param mixed $dusk_test_name |
252
|
|
|
*/ |
253
|
|
|
public function setDuskTestName($dusk_test_name) |
254
|
|
|
{ |
255
|
|
|
$this->dusk_test_name = $dusk_test_name; |
256
|
|
|
} |
257
|
|
|
|
258
|
6 |
|
public function getWriteBrowserTest() |
259
|
|
|
{ |
260
|
|
|
|
261
|
6 |
|
if (!$this->write_browser_test) { |
262
|
6 |
|
$this->setWriteBrowserTest(); |
263
|
4 |
|
} |
264
|
|
|
|
265
|
6 |
|
return $this->write_browser_test; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @param null $write_browser_test |
270
|
|
|
* @return GherkinToDusk |
271
|
|
|
* @internal param WritePHPUnitFile $write_unit_test |
272
|
|
|
*/ |
273
|
6 |
|
public function setWriteBrowserTest($write_browser_test = null) |
274
|
|
|
{ |
275
|
6 |
|
if (!$write_browser_test) { |
276
|
6 |
|
$write_browser_test = new WriteBrowserFile(); |
277
|
4 |
|
} |
278
|
|
|
|
279
|
6 |
|
$this->write_browser_test = $write_browser_test; |
280
|
6 |
|
return $this; |
281
|
|
|
} |
282
|
|
|
|
283
|
18 |
|
public function getWriteUnitTest() |
284
|
|
|
{ |
285
|
|
|
|
286
|
18 |
|
if (!$this->write_unit_test) { |
287
|
18 |
|
$this->setWriteUnitTest(); |
288
|
12 |
|
} |
289
|
|
|
|
290
|
18 |
|
return $this->write_unit_test; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* @param WritePHPUnitFile $write_unit_test |
295
|
|
|
* @return GherkinToDusk |
296
|
|
|
*/ |
297
|
18 |
|
public function setWriteUnitTest($write_unit_test = null) |
298
|
|
|
{ |
299
|
18 |
|
if (!$write_unit_test) { |
300
|
18 |
|
$write_unit_test = new WritePHPUnitFile(); |
301
|
12 |
|
} |
302
|
|
|
|
303
|
18 |
|
$this->write_unit_test = $write_unit_test; |
304
|
18 |
|
return $this; |
305
|
|
|
} |
306
|
|
|
|
307
|
27 |
|
private function checkIfFileExists() |
308
|
|
|
{ |
309
|
27 |
|
if ($this->filesystem->exists($this->fullPathToDestinationFile())) { |
310
|
3 |
|
$path = $this->fullPathToDestinationFile(); |
311
|
3 |
|
$message = sprintf("The test file exists already %s please use `append` command", $path); |
312
|
3 |
|
throw new \GD\Exceptions\TestFileExists($message); |
313
|
|
|
} |
314
|
24 |
|
} |
315
|
|
|
|
316
|
27 |
|
public function fullPathToDestinationFile() |
317
|
|
|
{ |
318
|
27 |
|
return $this->getDestinationFolderRoot() . '/' . $this->getDuskTestName() . '.php'; |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
|