Completed
Push — master ( c65851...604016 )
by Alfred
02:09
created

GherkinToDusk::getWriteBrowserTest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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