Completed
Push — master ( de13b2...fb5d5d )
by Alfred
04:26
created

GherkinToDusk   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 285
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 81.82%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 36
c 2
b 0
f 0
lcom 1
cbo 8
dl 0
loc 285
ccs 99
cts 121
cp 0.8182
rs 8.8

26 Methods

Rating   Name   Duplication   Size   Complexity  
B initializeFeature() 0 23 4
A featureToBrowser() 0 8 1
A featureToUnit() 0 9 1
A getParser() 0 4 1
A setParser() 0 4 1
A isComponent() 0 4 1
A setComponent() 0 4 1
A getFeatureContent() 0 4 1
A setFeatureContent() 0 4 1
A loadFileContent() 0 5 1
A passThroughParser() 0 4 1
A getParsedFeature() 0 4 1
A setParsedFeature() 0 4 1
A breakIntoMethods() 0 4 1
A iterateOverScenariosAndBuildUpClassMethods() 0 16 2
A buildOutSteps() 0 11 2
A buildDuskTestName() 0 7 2
A getFullPathToFileAndFileName() 0 4 1
A getDuskClassAndMethods() 0 4 1
A setDuskClassAndMethods() 0 4 1
A getDuskTestName() 0 4 1
A setDuskTestName() 0 4 1
A getWriteBrowserTest() 0 9 2
A setWriteBrowserTest() 0 9 2
A getWriteUnitTest() 0 9 2
A setWriteUnitTest() 0 9 2
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 'domain':
66 21
                $this->featureToUnit();
67 21
                break;
68 6
            case 'ui':
69 6
            case 'browser':
70 6
                $this->featureToBrowser();
71 6
                break;
72
            default:
73
                //more coming soon
74
                break;
75 18
        }
76 27
    }
77
78 6
    protected function featureToBrowser()
79
    {
80 6
        $this->getWriteBrowserTest()->writeTest(
81 6
            $this->getDestinationFolderRoot(),
82 6
            $this->getDuskTestName(),
83 6
            $this->getDuskClassAndMethods()
84 4
        );
85 6
    }
86
87 21
    protected function featureToUnit()
88
    {
89
90 21
        $this->getWriteUnitTest()->writeTest(
91 21
            $this->getDestinationFolderRoot(),
92 21
            $this->getDuskTestName(),
93 21
            $this->getDuskClassAndMethods()
94 14
        );
95 21
    }
96
97
    /**
98
     * @return Parser
99
     */
100 30
    public function getParser()
101
    {
102 30
        return $this->parser;
103
    }
104
105
    /**
106
     * @param Parser $parser
107
     */
108
    public function setParser($parser)
109
    {
110
        $this->parser = $parser;
111
    }
112
113
    /**
114
     * @return boolean
115
     */
116
    public function isComponent()
117
    {
118
        return $this->component;
119
    }
120
121
    /**
122
     * @param boolean $component
123
     */
124
    public function setComponent($component)
125
    {
126
        $this->component = $component;
127
    }
128
129
    /**
130
     * @return mixed
131
     */
132 3
    public function getFeatureContent()
133
    {
134 3
        return $this->feature_content;
135
    }
136
137
    /**
138
     * @param mixed $feature_content
139
     */
140
    public function setFeatureContent($feature_content)
141
    {
142
        $this->feature_content = $feature_content;
143
    }
144
145 27
    private function loadFileContent()
146
    {
147 27
        $this->feature_content =
148 27
            $this->getFilesystem()->get($this->getFullPathToFileAndFileName());
149 27
    }
150
151 27
    private function passThroughParser()
152
    {
153 27
        $this->parsed_feature = $this->getParser()->parse($this->feature_content);
154 27
    }
155
156
    /**
157
     * @return \Behat\Gherkin\Node\FeatureNode
158
     */
159 3
    public function getParsedFeature()
160
    {
161 3
        return $this->parsed_feature;
162
    }
163
164
    /**
165
     * @param \Behat\Gherkin\Node\FeatureNode $parsed_feature
166
     */
167
    public function setParsedFeature($parsed_feature)
168
    {
169
        $this->parsed_feature = $parsed_feature;
170
    }
171
172 27
    private function breakIntoMethods()
173
    {
174 27
        $this->iterateOverScenariosAndBuildUpClassMethods();
175 27
    }
176
177 27
    private function iterateOverScenariosAndBuildUpClassMethods()
178
    {
179
        /** @var  $feature \Behat\Gherkin\Node\ScenarioNode */
180 27
        foreach ($this->parsed_feature->getScenarios() as $scenario_index => $scenario) {
181 27
            $parent_method_name = ucfirst(camel_case($scenario->getTitle()));
182
183 27
            $parent_method_name_camelized_and_prefix_test = sprintf('test%s', $parent_method_name);
184
185 27
            $this->dusk_class_and_methods[$scenario_index] = [
186 27
                'parent' => $parent_method_name_camelized_and_prefix_test,
187 27
                'parent_content' => $this->getParentLevelContent($parent_method_name_camelized_and_prefix_test)
188 18
            ];
189
190 27
            $this->buildOutSteps($scenario, $scenario_index);
191 18
        }
192 27
    }
193
194
    /**
195
     * @param $scenario \Behat\Gherkin\Node\ScenarioNode
196
     */
197 27
    protected function buildOutSteps($scenario, $scenario_index)
198
    {
199 27
        foreach ($scenario->getSteps() as $step_index => $step) {
200 27
            $method_name = camel_case(sprintf("%s %s", $step->getKeyword(), $step->getText()));
201 27
            $step_method_name_camalized = camel_case(sprintf("%s %s", $step->getKeyword(), $step->getText()));
202 27
            $this->dusk_class_and_methods[$scenario_index]['steps'][$step_index]['name'] =
203
                $method_name;
204 27
            $this->dusk_class_and_methods[$scenario_index]['steps'][$step_index] =
205 27
                $this->getStepLevelContent($step_method_name_camalized);
206 18
        }
207 27
    }
208
209 27
    private function buildDuskTestName()
210
    {
211 27
        if (!$this->dusk_test_name) {
212 27
            $name = $this->getFilesystem()->name($this->getFullPathToFileAndFileName());
213 27
            $this->dusk_test_name = ucfirst(camel_case($name) . 'Test');
214 18
        }
215 27
    }
216
217 27
    private function getFullPathToFileAndFileName()
218
    {
219 27
        return getcwd() . DIRECTORY_SEPARATOR . $this->getPathToFeature();
220
    }
221
222
    /**
223
     * @return mixed
224
     */
225 27
    public function getDuskClassAndMethods()
226
    {
227 27
        return $this->dusk_class_and_methods;
228
    }
229
230
    /**
231
     * @param mixed $dusk_class_and_methods
232
     */
233
    public function setDuskClassAndMethods($dusk_class_and_methods)
234
    {
235
        $this->dusk_class_and_methods = $dusk_class_and_methods;
236
    }
237
238
    /**
239
     * @return mixed
240
     */
241 27
    public function getDuskTestName()
242
    {
243 27
        return $this->dusk_test_name;
244
    }
245
246
    /**
247
     * @param mixed $dusk_test_name
248
     */
249
    public function setDuskTestName($dusk_test_name)
250
    {
251
        $this->dusk_test_name = $dusk_test_name;
252
    }
253
254 6
    public function getWriteBrowserTest()
255
    {
256
257 6
        if (!$this->write_browser_test) {
258 6
            $this->setWriteBrowserTest();
259 4
        }
260
261 6
        return $this->write_browser_test;
262
    }
263
264
    /**
265
     * @param null $write_browser_test
266
     * @return GherkinToDusk
267
     * @internal param WritePHPUnitFile $write_unit_test
268
     */
269 6
    public function setWriteBrowserTest($write_browser_test = null)
270
    {
271 6
        if (!$write_browser_test) {
272 6
            $write_browser_test = new WriteBrowserFile();
273 4
        }
274
275 6
        $this->write_browser_test = $write_browser_test;
276 6
        return $this;
277
    }
278
279 21
    public function getWriteUnitTest()
280
    {
281
282 21
        if (!$this->write_unit_test) {
283 21
            $this->setWriteUnitTest();
284 14
        }
285
286 21
        return $this->write_unit_test;
287
    }
288
289
    /**
290
     * @param WritePHPUnitFile $write_unit_test
291
     * @return GherkinToDusk
292
     */
293 21
    public function setWriteUnitTest($write_unit_test = null)
294
    {
295 21
        if (!$write_unit_test) {
296 21
            $write_unit_test = new WritePHPUnitFile();
297 14
        }
298
299 21
        $this->write_unit_test = $write_unit_test;
300 21
        return $this;
301
    }
302
}
303