Completed
Pull Request — master (#162)
by
unknown
02:07
created

ArrayLoader::loadScenarioHash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 8
cts 8
cp 1
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Behat Gherkin.
5
 * (c) Konstantin Kudryashov <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Behat\Gherkin\Loader;
12
13
use Behat\Gherkin\Node\BackgroundNode;
14
use Behat\Gherkin\Node\ExampleTableNode;
15
use Behat\Gherkin\Node\FeatureNode;
16
use Behat\Gherkin\Node\OutlineNode;
17
use Behat\Gherkin\Node\PyStringNode;
18
use Behat\Gherkin\Node\ScenarioNode;
19
use Behat\Gherkin\Node\StepNode;
20
use Behat\Gherkin\Node\TableNode;
21
22
/**
23
 * From-array loader.
24
 *
25
 * @author Konstantin Kudryashov <[email protected]>
26
 */
27
class ArrayLoader implements LoaderInterface
28
{
29
    /**
30
     * Checks if current loader supports provided resource.
31
     *
32
     * @param mixed $resource Resource to load
33
     *
34
     * @return Boolean
35
     */
36 1
    public function supports($resource)
37
    {
38 1
        return is_array($resource) && (isset($resource['features']) || isset($resource['feature']));
39
    }
40
41
    /**
42
     * Loads features from provided resource.
43
     *
44
     * @param mixed $resource Resource to load
45
     *
46
     * @return FeatureNode[]
47
     */
48 11
    public function load($resource)
49
    {
50 11
        $features = array();
51
52 11
        if (isset($resource['features'])) {
53 8
            foreach ($resource['features'] as $iterator => $hash) {
54 7
                $feature = $this->loadFeatureHash($hash, $iterator);
55 7
                $features[] = $feature;
56
            }
57 3
        } elseif (isset($resource['feature'])) {
58 3
            $feature = $this->loadFeatureHash($resource['feature']);
59 3
            $features[] = $feature;
60
        }
61
62 11
        return $features;
63
    }
64
65
    /**
66
     * Loads feature from provided feature hash.
67
     *
68
     * @param array   $hash Feature hash
69
     * @param integer $line
70
     *
71
     * @return FeatureNode
72
     */
73 10
    protected function loadFeatureHash(array $hash, $line = 0)
74
    {
75 10
        $hash = array_merge(
76
            array(
77 10
                'title' => null,
78
                'description' => null,
79
                'tags' => array(),
80 10
                'keyword' => 'Feature',
81 10
                'language' => 'en',
82 10
                'line' => $line,
83
                'scenarios' => array(),
84
            ),
85 10
            $hash
86
        );
87 10
        $background = isset($hash['background']) ? $this->loadBackgroundHash($hash['background']) : null;
88
89 10
        $scenarios = array();
90 10
        foreach ((array) $hash['scenarios'] as $scenarioIterator => $scenarioHash) {
91 6
            if (isset($scenarioHash['type']) && 'outline' === $scenarioHash['type']) {
92 4
                $scenarios[] = $this->loadOutlineHash($scenarioHash, $scenarioIterator);
93
            } else {
94 3
                $scenarios[] = $this->loadScenarioHash($scenarioHash, $scenarioIterator);
95
            }
96
        }
97
98 10
        var_dump($scenarios);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($scenarios); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
99 10
        return new FeatureNode($hash['title'], $hash['description'], $hash['tags'], $background, $scenarios, $hash['keyword'], $hash['language'], null, $hash['line']);
100
    }
101
102
    /**
103
     * Loads background from provided hash.
104
     *
105
     * @param array $hash Background hash
106
     *
107
     * @return BackgroundNode
108
     */
109 4
    protected function loadBackgroundHash(array $hash)
110
    {
111 4
        $hash = array_merge(
112
            array(
113 4
                'title' => null,
114
                'keyword' => 'Background',
115
                'line' => 0,
116
                'steps' => array(),
117
            ),
118 4
            $hash
119
        );
120
121 4
        $steps = $this->loadStepsHash($hash['steps']);
122
123 4 View Code Duplication
        if (isset($hash['examples']['keyword'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
            $examplesKeyword = $hash['examples']['keyword'];
125
            unset($hash['examples']['keyword']);
126
        } else {
127 4
            $examplesKeyword = 'Examples';
128
        }
129 4
        if (isset($hash['examples'])) {
130 1
            $examplesTable = $hash['examples'];
131
        } else {
132 3
            $examplesTable = array();
133
        }
134
135 4
        $examples = new ExampleTableNode($examplesTable, $examplesKeyword);
136 4
        $bg = new BackgroundNode($hash['title'], $steps, $hash['keyword'], $hash['line'], $examples);
137 4
        return $bg;
138
    }
139
140
    /**
141
     * Loads scenario from provided scenario hash.
142
     *
143
     * @param array   $hash Scenario hash
144
     * @param integer $line Scenario definition line
145
     *
146
     * @return ScenarioNode
147
     */
148 3
    protected function loadScenarioHash(array $hash, $line = 0)
149
    {
150 3
        $hash = array_merge(
151
            array(
152 3
                'title' => null,
153
                'tags' => array(),
154 3
                'keyword' => 'Scenario',
155 3
                'line' => $line,
156
                'steps' => array(),
157
            ),
158 3
            $hash
159
        );
160
161 3
        $steps = $this->loadStepsHash($hash['steps']);
162
163 3
        return new ScenarioNode($hash['title'], $hash['tags'], $steps, $hash['keyword'], $hash['line']);
164
    }
165
166
    /**
167
     * Loads outline from provided outline hash.
168
     *
169
     * @param array   $hash Outline hash
170
     * @param integer $line Outline definition line
171
     *
172
     * @return OutlineNode
173
     */
174 4
    protected function loadOutlineHash(array $hash, $line = 0)
175
    {
176 4
        $hash = array_merge(
177
            array(
178 4
                'title' => null,
179
                'tags' => array(),
180 4
                'keyword' => 'Scenario Outline',
181 4
                'line' => $line,
182
                'steps' => array(),
183
                'examples' => array(),
184
            ),
185 4
            $hash
186
        );
187
188 4
        $steps = $this->loadStepsHash($hash['steps']);
189
190 4 View Code Duplication
        if (isset($hash['examples']['keyword'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
191
            $examplesKeyword = $hash['examples']['keyword'];
192
            unset($hash['examples']['keyword']);
193
        } else {
194 4
            $examplesKeyword = 'Examples';
195
        }
196
197 4
        $examples = new ExampleTableNode($hash['examples'], $examplesKeyword);
198
199 4
        return new OutlineNode($hash['title'], $hash['tags'], $steps, $examples, $hash['keyword'], $hash['line']);
200
    }
201
202
    /**
203
     * Loads steps from provided hash.
204
     *
205
     * @param array $hash
206
     *
207
     * @return StepNode[]
208
     */
209 8
    private function loadStepsHash(array $hash)
210
    {
211 8
        $steps = array();
212 8
        foreach ($hash as $stepIterator => $stepHash) {
213 4
            $steps[] = $this->loadStepHash($stepHash, $stepIterator);
214
        }
215
216 8
        return $steps;
217
    }
218
219
    /**
220
     * Loads step from provided hash.
221
     *
222
     * @param array   $hash Step hash
223
     * @param integer $line Step definition line
224
     *
225
     * @return StepNode
226
     */
227 4
    protected function loadStepHash(array $hash, $line = 0)
228
    {
229 4
        $hash = array_merge(
230
            array(
231 4
                'keyword_type' => 'Given',
232 4
                'type' => 'Given',
233
                'text' => null,
234 4
                'keyword' => 'Scenario',
235 4
                'line' => $line,
236
                'arguments' => array(),
237
            ),
238 4
            $hash
239
        );
240
241 4
        $arguments = array();
242 4
        foreach ($hash['arguments'] as $argumentHash) {
243 1
            if ('table' === $argumentHash['type']) {
244 1
                $arguments[] = $this->loadTableHash($argumentHash['rows']);
245 1
            } elseif ('pystring' === $argumentHash['type']) {
246 1
                $arguments[] = $this->loadPyStringHash($argumentHash, $hash['line'] + 1);
247
            }
248
        }
249
250 4
        return new StepNode($hash['type'], $hash['text'], $arguments, $hash['line'], $hash['keyword_type']);
251
    }
252
253
    /**
254
     * Loads table from provided hash.
255
     *
256
     * @param array $hash Table hash
257
     *
258
     * @return TableNode
259
     */
260 1
    protected function loadTableHash(array $hash)
261
    {
262 1
        return new TableNode($hash);
263
    }
264
265
    /**
266
     * Loads PyString from provided hash.
267
     *
268
     * @param array   $hash PyString hash
269
     * @param integer $line
270
     *
271
     * @return PyStringNode
272
     */
273 1
    protected function loadPyStringHash(array $hash, $line = 0)
274
    {
275 1
        $line = isset($hash['line']) ? $hash['line'] : $line;
276
277 1
        $strings = array();
278 1
        foreach (explode("\n", $hash['text']) as $string) {
279 1
            $strings[] = $string;
280
        }
281
282 1
        return new PyStringNode($strings, $line);
283
    }
284
}
285