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\WritePHPUnitFile; |
14
|
|
|
use Illuminate\Filesystem\Filesystem; |
15
|
|
|
use Symfony\Component\Yaml\Yaml; |
16
|
|
|
|
17
|
|
|
class GherkinToDusk extends BaseGherkinToDusk |
18
|
|
|
{ |
19
|
|
|
use BuildOutContent, WritePHPUnitFile; |
20
|
|
|
|
21
|
|
|
protected $component = false; |
22
|
|
|
|
23
|
|
|
protected $string_contents = null; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Yml Content of a test yml |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $feature_content; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var \Behat\Gherkin\Node\FeatureNode |
33
|
|
|
*/ |
34
|
|
|
protected $parsed_feature; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* |
38
|
|
|
*/ |
39
|
|
|
protected $dusk_class_and_methods; |
40
|
|
|
|
41
|
|
|
protected $dusk_test_name; |
42
|
|
|
|
43
|
21 |
|
public function initializeFeature() |
44
|
|
|
{ |
45
|
21 |
|
$this->loadFileContent(); |
46
|
|
|
|
47
|
21 |
|
$this->buildDuskTestName(); |
48
|
|
|
|
49
|
21 |
|
$this->passThroughParser(); |
50
|
|
|
|
51
|
21 |
|
$this->breakIntoMethods(); |
52
|
|
|
|
53
|
21 |
|
if ($this->context == 'domain') { |
54
|
21 |
|
$this->featureToUnit(); |
55
|
14 |
|
} |
56
|
21 |
|
} |
57
|
|
|
|
58
|
21 |
|
protected function featureToUnit() |
59
|
|
|
{ |
60
|
|
|
|
61
|
21 |
|
$this->writeUnitTest( |
62
|
21 |
|
$this->getDestinationFolderRoot(), |
63
|
21 |
|
$this->getDuskTestName(), |
64
|
21 |
|
$this->getDuskClassAndMethods() |
65
|
14 |
|
); |
66
|
21 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return Parser |
70
|
|
|
*/ |
71
|
24 |
|
public function getParser() |
72
|
|
|
{ |
73
|
24 |
|
return $this->parser; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param Parser $parser |
78
|
|
|
*/ |
79
|
|
|
public function setParser($parser) |
80
|
|
|
{ |
81
|
|
|
$this->parser = $parser; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return boolean |
86
|
|
|
*/ |
87
|
|
|
public function isComponent() |
88
|
|
|
{ |
89
|
|
|
return $this->component; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param boolean $component |
94
|
|
|
*/ |
95
|
|
|
public function setComponent($component) |
96
|
|
|
{ |
97
|
|
|
$this->component = $component; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return mixed |
102
|
|
|
*/ |
103
|
3 |
|
public function getFeatureContent() |
104
|
|
|
{ |
105
|
3 |
|
return $this->feature_content; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param mixed $feature_content |
110
|
|
|
*/ |
111
|
|
|
public function setFeatureContent($feature_content) |
112
|
|
|
{ |
113
|
|
|
$this->feature_content = $feature_content; |
114
|
|
|
} |
115
|
|
|
|
116
|
21 |
|
private function loadFileContent() |
117
|
|
|
{ |
118
|
21 |
|
$this->feature_content = $this->getFilesystem()->get($this->getFullPathToFileAndFileName()); |
119
|
21 |
|
} |
120
|
|
|
|
121
|
21 |
|
private function passThroughParser() |
122
|
|
|
{ |
123
|
21 |
|
$this->parsed_feature = $this->getParser()->parse($this->feature_content); |
124
|
21 |
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return \Behat\Gherkin\Node\FeatureNode |
128
|
|
|
*/ |
129
|
3 |
|
public function getParsedFeature() |
130
|
|
|
{ |
131
|
3 |
|
return $this->parsed_feature; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param \Behat\Gherkin\Node\FeatureNode $parsed_feature |
136
|
|
|
*/ |
137
|
|
|
public function setParsedFeature($parsed_feature) |
138
|
|
|
{ |
139
|
|
|
$this->parsed_feature = $parsed_feature; |
140
|
|
|
} |
141
|
|
|
|
142
|
21 |
|
private function breakIntoMethods() |
143
|
|
|
{ |
144
|
21 |
|
$this->iterateOverScenariosAndBuildUpClassMethods(); |
145
|
21 |
|
} |
146
|
|
|
|
147
|
21 |
|
private function iterateOverScenariosAndBuildUpClassMethods() |
148
|
|
|
{ |
149
|
|
|
/** @var $feature \Behat\Gherkin\Node\ScenarioNode */ |
150
|
21 |
|
foreach ($this->parsed_feature->getScenarios() as $scenario_index => $scenario) { |
151
|
21 |
|
$parent_method_name = ucfirst(camel_case($scenario->getTitle())); |
152
|
|
|
|
153
|
21 |
|
$parent_method_name_camelized_and_prefix_test = sprintf('test%s', $parent_method_name); |
154
|
|
|
|
155
|
21 |
|
$this->dusk_class_and_methods[$scenario_index] = [ |
156
|
21 |
|
'parent' => $parent_method_name_camelized_and_prefix_test, |
157
|
21 |
|
'parent_content' => $this->getParentLevelContent($parent_method_name_camelized_and_prefix_test) |
158
|
14 |
|
]; |
159
|
|
|
|
160
|
21 |
|
$this->buildOutSteps($scenario, $scenario_index); |
161
|
14 |
|
} |
162
|
21 |
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param $scenario \Behat\Gherkin\Node\ScenarioNode |
166
|
|
|
*/ |
167
|
21 |
|
protected function buildOutSteps($scenario, $scenario_index) |
168
|
|
|
{ |
169
|
21 |
|
foreach ($scenario->getSteps() as $step_index => $step) { |
170
|
21 |
|
$method_name = camel_case(sprintf("%s %s", $step->getKeyword(), $step->getText())); |
171
|
21 |
|
$step_method_name_camalized = camel_case(sprintf("%s %s", $step->getKeyword(), $step->getText())); |
172
|
21 |
|
$this->dusk_class_and_methods[$scenario_index]['steps'][$step_index]['name'] = |
173
|
|
|
$method_name; |
174
|
21 |
|
$this->dusk_class_and_methods[$scenario_index]['steps'][$step_index] = |
175
|
21 |
|
$this->getStepLevelContent($step_method_name_camalized); |
176
|
14 |
|
} |
177
|
21 |
|
} |
178
|
|
|
|
179
|
21 |
|
private function buildDuskTestName() |
180
|
|
|
{ |
181
|
21 |
|
if (!$this->dusk_test_name) { |
182
|
21 |
|
$name = $this->getFilesystem()->name($this->getFullPathToFileAndFileName()); |
183
|
21 |
|
$this->dusk_test_name = ucfirst(camel_case($name) . 'Test'); |
184
|
14 |
|
} |
185
|
21 |
|
} |
186
|
|
|
|
187
|
21 |
|
private function getFullPathToFileAndFileName() |
188
|
|
|
{ |
189
|
21 |
|
return getcwd() . DIRECTORY_SEPARATOR . $this->getPathToFeature(); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @return mixed |
194
|
|
|
*/ |
195
|
21 |
|
public function getDuskClassAndMethods() |
196
|
|
|
{ |
197
|
21 |
|
return $this->dusk_class_and_methods; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param mixed $dusk_class_and_methods |
202
|
|
|
*/ |
203
|
|
|
public function setDuskClassAndMethods($dusk_class_and_methods) |
204
|
|
|
{ |
205
|
|
|
$this->dusk_class_and_methods = $dusk_class_and_methods; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @return mixed |
210
|
|
|
*/ |
211
|
21 |
|
public function getDuskTestName() |
212
|
|
|
{ |
213
|
21 |
|
return $this->dusk_test_name; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param mixed $dusk_test_name |
218
|
|
|
*/ |
219
|
|
|
public function setDuskTestName($dusk_test_name) |
220
|
|
|
{ |
221
|
|
|
$this->dusk_test_name = $dusk_test_name; |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|