Completed
Push — master ( 2c6ed4...a8d10a )
by Alfred
01:53
created

GherkinToDusk::getDuskClassAndMethods()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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 Illuminate\Filesystem\Filesystem;
13
use Symfony\Component\Yaml\Yaml;
14
15
class GherkinToDusk extends BaseGherkinToDusk
16
{
17
18
    protected $component = false;
19
20
    protected $string_contents = null;
21
22
    /**
23
     * Yml Content of a test yml
24
     * @var array
25
     */
26
    protected $feature_content;
27
28
    /**
29
     * @var \Behat\Gherkin\Node\FeatureNode
30
     */
31
    protected $parsed_feature;
32
33
    /**
34
     *
35
     */
36
    protected $dusk_class_and_methods;
37
38
    protected $dusk_test_name;
39
40
    public function initializeFeature()
41
    {
42
        $this->loadFileContent();
43
44
        $this->buildDuskTestName();
45
46
        $this->passThroughParser();
47
48
        $this->breakIntoMethods();
49
50
        if ($this->context == 'domain') {
51
            $this->featureToUnit();
52
        }
53
    }
54
55
    protected function featureToUnit()
56
    {
57
    }
58
59
    /**
60
     * @return Parser
61
     */
62
    public function getParser()
63
    {
64
        return $this->parser;
65
    }
66
67
    /**
68
     * @param Parser $parser
69
     */
70
    public function setParser($parser)
71
    {
72
        $this->parser = $parser;
73
    }
74
75
    /**
76
     * @return boolean
77
     */
78
    public function isComponent()
79
    {
80
        return $this->component;
81
    }
82
83
    /**
84
     * @param boolean $component
85
     */
86
    public function setComponent($component)
87
    {
88
        $this->component = $component;
89
    }
90
91
    /**
92
     * @return mixed
93
     */
94
    public function getFeatureContent()
95
    {
96
        return $this->feature_content;
97
    }
98
99
    /**
100
     * @param mixed $feature_content
101
     */
102
    public function setFeatureContent($feature_content)
103
    {
104
        $this->feature_content = $feature_content;
0 ignored issues
show
Documentation Bug introduced by
It seems like $feature_content of type * is incompatible with the declared type array of property $feature_content.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
105
    }
106
107
    private function loadFileContent()
108
    {
109
        $this->feature_content = $this->filesystem->get($this->getFullPathToFileAndFileName());
0 ignored issues
show
Bug introduced by
The method get cannot be called on $this->filesystem (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
110
    }
111
112
    private function passThroughParser()
113
    {
114
        $this->parsed_feature = $this->getParser()->parse($this->feature_content);
0 ignored issues
show
Documentation introduced by
$this->feature_content is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
115
    }
116
117
    /**
118
     * @return \Behat\Gherkin\Node\FeatureNode
119
     */
120
    public function getParsedFeature()
121
    {
122
        return $this->parsed_feature;
123
    }
124
125
    /**
126
     * @param \Behat\Gherkin\Node\FeatureNode $parsed_feature
127
     */
128
    public function setParsedFeature($parsed_feature)
129
    {
130
        $this->parsed_feature = $parsed_feature;
131
    }
132
133
    private function breakIntoMethods()
134
    {
135
        //take the parsed content and build out the methods needed for the file
136
        //1) the public method to test eg the Scenario
137
        //   set the name right
138
139
        $this->iterateOverScenariosAndBuildUpClassMethods();
140
141
        //2) and sub items it has eg the steps
142
        //   makesure their names are correct
143
    }
144
145
    private function iterateOverScenariosAndBuildUpClassMethods()
146
    {
147
        /** @var  $feature \Behat\Gherkin\Node\ScenarioNode */
148
        foreach ($this->parsed_feature->getScenarios() as $scenario_index => $scenario) {
149
            $parent_method_name = ucfirst(camel_case($scenario->getTitle()));
150
151
            $this->dusk_class_and_methods[$scenario_index] = [
152
                'parent' => sprintf('test%s', $parent_method_name)
153
            ];
154
155
            foreach ($scenario->getSteps() as $step_index => $step) {
156
                $method_name = camel_case(sprintf("%s %s", $step->getKeyword(), $step->getText()));
157
                $this->dusk_class_and_methods[$scenario_index]['steps'][$step_index] = $method_name;
158
            }
159
        }
160
    }
161
162
    private function buildDuskTestName()
163
    {
164
        if (!$this->dusk_test_name) {
165
            $name = $this->filesystem->name($this->getFullPathToFileAndFileName());
0 ignored issues
show
Bug introduced by
The method name cannot be called on $this->filesystem (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
166
            $this->dusk_test_name = ucfirst(camel_case($name) . 'Test');
167
        }
168
    }
169
170
    private function getFullPathToFileAndFileName()
171
    {
172
        return getcwd() . DIRECTORY_SEPARATOR . $this->getPathToFeature();
173
    }
174
175
    /**
176
     * @return mixed
177
     */
178
    public function getDuskClassAndMethods()
179
    {
180
        return $this->dusk_class_and_methods;
181
    }
182
183
    /**
184
     * @param mixed $dusk_class_and_methods
185
     */
186
    public function setDuskClassAndMethods($dusk_class_and_methods)
187
    {
188
        $this->dusk_class_and_methods = $dusk_class_and_methods;
189
    }
190
191
    /**
192
     * @return mixed
193
     */
194
    public function getDuskTestName()
195
    {
196
        return $this->dusk_test_name;
197
    }
198
199
    /**
200
     * @param mixed $dusk_test_name
201
     */
202
    public function setDuskTestName($dusk_test_name)
203
    {
204
        $this->dusk_test_name = $dusk_test_name;
205
    }
206
}
207