Completed
Push — master ( a8cd91...de13b2 )
by Alfred
02:05
created

GherkinToDusk::setWriteUnitTest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
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\WritePHPUnitFile;
14
use Illuminate\Filesystem\Filesystem;
15
use Symfony\Component\Yaml\Yaml;
16
17
class GherkinToDusk extends BaseGherkinToDusk
18
{
19
    use BuildOutContent;
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
    /**
44
     * @var WritePHPUnitFile
45
     */
46
    protected $write_unit_test;
47
48 21
    public function initializeFeature()
49
    {
50 21
        $this->loadFileContent();
51
52 21
        $this->buildDuskTestName();
53
54 21
        $this->passThroughParser();
55
56 21
        $this->breakIntoMethods();
57
58 21
        switch ($this->context) {
59 21
            case 'domain':
60 21
                $this->featureToUnit();
61 21
                break;
62
            case 'browser':
63
                $this->featureToBrowser();
64
                break;
65
            default:
66
                //more coming soon
67
                break;
68 14
        }
69 21
    }
70
71
    protected function featureToBrowser()
72
    {
73
    }
74
75 21
    protected function featureToUnit()
76
    {
77
78 21
        $this->getWriteUnitTest()->writeTest(
79 21
            $this->getDestinationFolderRoot(),
80 21
            $this->getDuskTestName(),
81 21
            $this->getDuskClassAndMethods()
82 14
        );
83 21
    }
84
85
    /**
86
     * @return Parser
87
     */
88 24
    public function getParser()
89
    {
90 24
        return $this->parser;
91
    }
92
93
    /**
94
     * @param Parser $parser
95
     */
96
    public function setParser($parser)
97
    {
98
        $this->parser = $parser;
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 21
    private function loadFileContent()
134
    {
135 21
        $this->feature_content = $this->getFilesystem()->get($this->getFullPathToFileAndFileName());
136 21
    }
137
138 21
    private function passThroughParser()
139
    {
140 21
        $this->parsed_feature = $this->getParser()->parse($this->feature_content);
141 21
    }
142
143
    /**
144
     * @return \Behat\Gherkin\Node\FeatureNode
145
     */
146 3
    public function getParsedFeature()
147
    {
148 3
        return $this->parsed_feature;
149
    }
150
151
    /**
152
     * @param \Behat\Gherkin\Node\FeatureNode $parsed_feature
153
     */
154
    public function setParsedFeature($parsed_feature)
155
    {
156
        $this->parsed_feature = $parsed_feature;
157
    }
158
159 21
    private function breakIntoMethods()
160
    {
161 21
        $this->iterateOverScenariosAndBuildUpClassMethods();
162 21
    }
163
164 21
    private function iterateOverScenariosAndBuildUpClassMethods()
165
    {
166
        /** @var  $feature \Behat\Gherkin\Node\ScenarioNode */
167 21
        foreach ($this->parsed_feature->getScenarios() as $scenario_index => $scenario) {
168 21
            $parent_method_name = ucfirst(camel_case($scenario->getTitle()));
169
170 21
            $parent_method_name_camelized_and_prefix_test = sprintf('test%s', $parent_method_name);
171
172 21
            $this->dusk_class_and_methods[$scenario_index] = [
173 21
                'parent' => $parent_method_name_camelized_and_prefix_test,
174 21
                'parent_content' => $this->getParentLevelContent($parent_method_name_camelized_and_prefix_test)
175 14
            ];
176
177 21
            $this->buildOutSteps($scenario, $scenario_index);
178 14
        }
179 21
    }
180
181
    /**
182
     * @param $scenario \Behat\Gherkin\Node\ScenarioNode
183
     */
184 21
    protected function buildOutSteps($scenario, $scenario_index)
185
    {
186 21
        foreach ($scenario->getSteps() as $step_index => $step) {
187 21
            $method_name = camel_case(sprintf("%s %s", $step->getKeyword(), $step->getText()));
188 21
            $step_method_name_camalized = camel_case(sprintf("%s %s", $step->getKeyword(), $step->getText()));
189 21
            $this->dusk_class_and_methods[$scenario_index]['steps'][$step_index]['name'] =
190
                $method_name;
191 21
            $this->dusk_class_and_methods[$scenario_index]['steps'][$step_index] =
192 21
                $this->getStepLevelContent($step_method_name_camalized);
193 14
        }
194 21
    }
195
196 21
    private function buildDuskTestName()
197
    {
198 21
        if (!$this->dusk_test_name) {
199 21
            $name = $this->getFilesystem()->name($this->getFullPathToFileAndFileName());
200 21
            $this->dusk_test_name = ucfirst(camel_case($name) . 'Test');
201 14
        }
202 21
    }
203
204 21
    private function getFullPathToFileAndFileName()
205
    {
206 21
        return getcwd() . DIRECTORY_SEPARATOR . $this->getPathToFeature();
207
    }
208
209
    /**
210
     * @return mixed
211
     */
212 21
    public function getDuskClassAndMethods()
213
    {
214 21
        return $this->dusk_class_and_methods;
215
    }
216
217
    /**
218
     * @param mixed $dusk_class_and_methods
219
     */
220
    public function setDuskClassAndMethods($dusk_class_and_methods)
221
    {
222
        $this->dusk_class_and_methods = $dusk_class_and_methods;
223
    }
224
225
    /**
226
     * @return mixed
227
     */
228 21
    public function getDuskTestName()
229
    {
230 21
        return $this->dusk_test_name;
231
    }
232
233
    /**
234
     * @param mixed $dusk_test_name
235
     */
236
    public function setDuskTestName($dusk_test_name)
237
    {
238
        $this->dusk_test_name = $dusk_test_name;
239
    }
240
241 21
    public function getWriteUnitTest()
242
    {
243
244 21
        if (!$this->write_unit_test) {
245 21
            $this->setWriteUnitTest();
246 14
        }
247
248 21
        return $this->write_unit_test;
249
    }
250
251
    /**
252
     * @param WritePHPUnitFile $write_unit_test
253
     * @return GherkinToDusk
254
     */
255 21
    public function setWriteUnitTest($write_unit_test = null)
256
    {
257 21
        if (!$write_unit_test) {
258 21
            $write_unit_test = new WritePHPUnitFile();
259 14
        }
260
261 21
        $this->write_unit_test = $write_unit_test;
262 21
        return $this;
263
    }
264
}
265