Completed
Push — master ( 444125...462f30 )
by Alfred
03:14
created

GherkinToDusk   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 257
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 85.71%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 32
c 3
b 0
f 0
lcom 1
cbo 9
dl 0
loc 257
ccs 96
cts 112
cp 0.8571
rs 9.6

21 Methods

Rating   Name   Duplication   Size   Complexity  
A isComponent() 0 4 1
A setComponent() 0 4 1
A getFeatureContent() 0 4 1
A setFeatureContent() 0 4 1
B initializeFeature() 0 24 5
A featureToBrowser() 0 10 1
A featureToUnit() 0 10 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 getDuskClassAndMethods() 0 4 1
A setDuskClassAndMethods() 0 4 1
A getWriteBrowserTest() 0 9 2
A setWriteBrowserTest() 0 9 2
A getWriteUnitTest() 0 9 2
A setWriteUnitTest() 0 9 2
A checkIfFileExists() 0 8 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
43
    /**
44
     * @var WritePHPUnitFile
45
     */
46
    protected $write_unit_test;
47
48
    /**
49
     * @var WriteBrowserFile
50
     */
51
    protected $write_browser_test;
52
53 27
    public function initializeFeature()
54
    {
55 27
        $this->loadFileContent();
56
57 27
        $this->buildDuskTestName();
58
59 27
        $this->passThroughParser();
60
61 27
        $this->breakIntoMethods();
62
63 27
        switch ($this->context) {
64 27
            case 'unit':
65 27
            case 'domain':
66 21
                $this->featureToUnit();
67 18
                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 16
        }
76 24
    }
77
78 6
    protected function featureToBrowser()
79
    {
80 6
        $this->checkIfFileExists();
81
82 6
        $this->getWriteBrowserTest()->writeTest(
83 6
            $this->getDestinationFolderRoot(),
84 6
            $this->getDuskTestName(),
85 6
            $this->getDuskClassAndMethods()
86 4
        );
87 6
    }
88
89 21
    protected function featureToUnit()
90
    {
91 21
        $this->checkIfFileExists();
92
93 18
        $this->getWriteUnitTest()->writeTest(
94 18
            $this->getDestinationFolderRoot(),
95 18
            $this->getDuskTestName(),
96 18
            $this->getDuskClassAndMethods()
97 12
        );
98 18
    }
99
100
101
    /**
102
     * @return boolean
103
     */
104
    public function isComponent()
105
    {
106
        return $this->component;
107
    }
108
109
    /**
110
     * @param boolean $component
111
     */
112
    public function setComponent($component)
113
    {
114
        $this->component = $component;
115
    }
116
117
    /**
118
     * @return mixed
119
     */
120 3
    public function getFeatureContent()
121
    {
122 3
        return $this->feature_content;
123
    }
124
125
    /**
126
     * @param mixed $feature_content
127
     */
128
    public function setFeatureContent($feature_content)
129
    {
130
        $this->feature_content = $feature_content;
131
    }
132
133 27
    private function loadFileContent()
134
    {
135 27
        $this->feature_content =
136 27
            $this->getFilesystem()->get($this->getFullPathToFileAndFileName());
137 27
    }
138
139 27
    private function passThroughParser()
140
    {
141 27
        $this->parsed_feature = $this->getParser()->parse($this->feature_content);
142 27
    }
143
144
    /**
145
     * @return \Behat\Gherkin\Node\FeatureNode
146
     */
147 3
    public function getParsedFeature()
148
    {
149 3
        return $this->parsed_feature;
150
    }
151
152
    /**
153
     * @param \Behat\Gherkin\Node\FeatureNode $parsed_feature
154
     */
155
    public function setParsedFeature($parsed_feature)
156
    {
157
        $this->parsed_feature = $parsed_feature;
158
    }
159
160 27
    private function breakIntoMethods()
161
    {
162 27
        $this->iterateOverScenariosAndBuildUpClassMethods();
163 27
    }
164
165 27
    private function iterateOverScenariosAndBuildUpClassMethods()
166
    {
167
        /** @var  $feature \Behat\Gherkin\Node\ScenarioNode */
168 27
        foreach ($this->parsed_feature->getScenarios() as $scenario_index => $scenario) {
169 27
            $parent_method_name = ucfirst(camel_case($scenario->getTitle()));
170
171 27
            $parent_method_name_camelized_and_prefix_test = sprintf('test%s', $parent_method_name);
172
173 27
            $this->dusk_class_and_methods[$scenario_index] = [
174 27
                'parent' => $parent_method_name_camelized_and_prefix_test,
175 27
                'parent_content' => $this->getParentLevelContent($parent_method_name_camelized_and_prefix_test)
176 18
            ];
177
178 27
            $this->buildOutSteps($scenario, $scenario_index);
179 18
        }
180 27
    }
181
182
    /**
183
     * @param $scenario \Behat\Gherkin\Node\ScenarioNode
184
     */
185 27
    protected function buildOutSteps($scenario, $scenario_index)
186
    {
187 27
        foreach ($scenario->getSteps() as $step_index => $step) {
188 27
            $method_name = camel_case(sprintf("%s %s", $step->getKeyword(), $step->getText()));
189 27
            $step_method_name_camalized = camel_case(sprintf("%s %s", $step->getKeyword(), $step->getText()));
190 27
            $this->dusk_class_and_methods[$scenario_index]['steps'][$step_index]['name'] =
191
                $method_name;
192 27
            $this->dusk_class_and_methods[$scenario_index]['steps'][$step_index] =
193 27
                $this->getStepLevelContent($step_method_name_camalized);
194 18
        }
195 27
    }
196
197
198
199
    /**
200
     * @return mixed
201
     */
202 24
    public function getDuskClassAndMethods()
203
    {
204 24
        return $this->dusk_class_and_methods;
205
    }
206
207
    /**
208
     * @param mixed $dusk_class_and_methods
209
     */
210
    public function setDuskClassAndMethods($dusk_class_and_methods)
211
    {
212
        $this->dusk_class_and_methods = $dusk_class_and_methods;
213
    }
214
215
216
217 6
    public function getWriteBrowserTest()
218
    {
219
220 6
        if (!$this->write_browser_test) {
221 6
            $this->setWriteBrowserTest();
222 4
        }
223
224 6
        return $this->write_browser_test;
225
    }
226
227
    /**
228
     * @param null $write_browser_test
229
     * @return GherkinToDusk
230
     * @internal param WritePHPUnitFile $write_unit_test
231
     */
232 6
    public function setWriteBrowserTest($write_browser_test = null)
233
    {
234 6
        if (!$write_browser_test) {
235 6
            $write_browser_test = new WriteBrowserFile();
236 4
        }
237
238 6
        $this->write_browser_test = $write_browser_test;
239 6
        return $this;
240
    }
241
242 18
    public function getWriteUnitTest()
243
    {
244
245 18
        if (!$this->write_unit_test) {
246 18
            $this->setWriteUnitTest();
247 12
        }
248
249 18
        return $this->write_unit_test;
250
    }
251
252
    /**
253
     * @param WritePHPUnitFile $write_unit_test
254
     * @return GherkinToDusk
255
     */
256 18
    public function setWriteUnitTest($write_unit_test = null)
257
    {
258 18
        if (!$write_unit_test) {
259 18
            $write_unit_test = new WritePHPUnitFile();
260 12
        }
261
262 18
        $this->write_unit_test = $write_unit_test;
263 18
        return $this;
264
    }
265
266 27
    private function checkIfFileExists()
267
    {
268 27
        if ($this->filesystem->exists($this->fullPathToDestinationFile())) {
269 3
            $path = $this->fullPathToDestinationFile();
270 3
            $message = sprintf("The test file exists already %s please use `append` command", $path);
271 3
            throw new \GD\Exceptions\TestFileExists($message);
272
        }
273 24
    }
274
}
275