ArrayLoaderTest   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 375
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 7
dl 0
loc 375
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testSupports() 0 10 1
A testLoadEmpty() 0 4 1
A testLoadFeatures() 0 32 1
A testLoadScenarios() 0 44 1
A testLoadOutline() 0 37 1
A testOutlineExamples() 0 37 1
A testLoadBackground() 0 25 1
B testLoadSteps() 0 79 1
B testLoadStepArguments() 0 76 1
A testSingleFeatureArray() 0 11 1
1
<?php
2
3
namespace Tests\Behat\Gherkin\Loader;
4
5
use Behat\Gherkin\Loader\ArrayLoader;
6
use Behat\Gherkin\Node\OutlineNode;
7
use PHPUnit\Framework\TestCase;
8
9
class ArrayLoaderTest extends TestCase
10
{
11
    /** @var ArrayLoader  */
12
    private $loader;
13
14
    protected function setUp()
15
    {
16
        $this->loader = new ArrayLoader();
17
    }
18
19
    public function testSupports()
20
    {
21
        $this->assertFalse($this->loader->supports(__DIR__));
22
        $this->assertFalse($this->loader->supports(__FILE__));
23
        $this->assertFalse($this->loader->supports('string'));
24
        $this->assertFalse($this->loader->supports(array('wrong_root')));
25
        $this->assertFalse($this->loader->supports(array('features')));
26
        $this->assertTrue($this->loader->supports(array('features' => array())));
27
        $this->assertTrue($this->loader->supports(array('feature' => array())));
28
    }
29
30
    public function testLoadEmpty()
31
    {
32
        $this->assertEquals(array(), $this->loader->load(array('features' => array())));
33
    }
34
35
    public function testLoadFeatures()
36
    {
37
        $features = $this->loader->load(array(
38
            'features' => array(
39
                array(
40
                    'title'         => 'First feature',
41
                    'line'          => 3,
42
                ),
43
                array(
44
                    'description'   => 'Second feature description',
45
                    'language'      => 'ru',
46
                    'tags'          => array('some', 'tags')
47
                )
48
            ),
49
        ));
50
51
        $this->assertEquals(2, count($features));
52
53
        $this->assertEquals(3, $features[0]->getLine());
54
        $this->assertEquals('First feature', $features[0]->getTitle());
55
        $this->assertNull($features[0]->getDescription());
56
        $this->assertNull($features[0]->getFile());
57
        $this->assertEquals('en', $features[0]->getLanguage());
58
        $this->assertFalse($features[0]->hasTags());
59
60
        $this->assertEquals(1, $features[1]->getLine());
61
        $this->assertNull($features[1]->getTitle());
62
        $this->assertEquals('Second feature description', $features[1]->getDescription());
63
        $this->assertNull($features[1]->getFile());
64
        $this->assertEquals('ru', $features[1]->getLanguage());
65
        $this->assertEquals(array('some', 'tags'), $features[1]->getTags());
66
    }
67
68
    public function testLoadScenarios()
69
    {
70
        $features = $this->loader->load(array(
71
            'features' => array(
72
                array(
73
                    'title'     => 'Feature',
74
                    'scenarios' => array(
75
                        array(
76
                            'title' => 'First scenario',
77
                            'line'  => 2
78
                        ),
79
                        array(
80
                            'tags'  => array('second', 'scenario', 'tags')
81
                        ),
82
                        array(
83
                            'tags'  => array('third', 'scenario'),
84
                            'line'  => 3
85
                        )
86
                    )
87
                )
88
            ),
89
        ));
90
91
        $this->assertEquals(1, count($features));
92
93
        $scenarios = $features[0]->getScenarios();
94
95
        $this->assertEquals(3, count($scenarios));
96
97
        $this->assertInstanceOf('Behat\Gherkin\Node\ScenarioNode', $scenarios[0]);
98
        $this->assertEquals('First scenario', $scenarios[0]->getTitle());
99
        $this->assertFalse($scenarios[0]->hasTags());
100
        $this->assertEquals(2, $scenarios[0]->getLine());
101
102
        $this->assertInstanceOf('Behat\Gherkin\Node\ScenarioNode', $scenarios[1]);
103
        $this->assertNull($scenarios[1]->getTitle());
104
        $this->assertEquals(array('second', 'scenario', 'tags'), $scenarios[1]->getTags());
105
        $this->assertEquals(1, $scenarios[1]->getLine());
106
107
        $this->assertInstanceOf('Behat\Gherkin\Node\ScenarioNode', $scenarios[2]);
108
        $this->assertNull($scenarios[2]->getTitle());
109
        $this->assertEquals(array('third', 'scenario'), $scenarios[2]->getTags());
110
        $this->assertEquals(3, $scenarios[2]->getLine());
111
    }
112
113
    public function testLoadOutline()
114
    {
115
        $features = $this->loader->load(array(
116
            'features' => array(
117
                array(
118
                    'title'     => 'Feature',
119
                    'scenarios' => array(
120
                        array(
121
                            'type'  => 'outline',
122
                            'title' => 'First outline',
123
                            'line'  => 2
124
                        ),
125
                        array(
126
                            'type'  => 'outline',
127
                            'tags'  => array('second', 'outline', 'tags')
128
                        )
129
                    )
130
                )
131
            ),
132
        ));
133
134
        $this->assertEquals(1, count($features));
135
136
        $outlines = $features[0]->getScenarios();
137
138
        $this->assertEquals(2, count($outlines));
139
140
        $this->assertInstanceOf('Behat\Gherkin\Node\OutlineNode', $outlines[0]);
141
        $this->assertEquals('First outline', $outlines[0]->getTitle());
142
        $this->assertFalse($outlines[0]->hasTags());
143
        $this->assertEquals(2, $outlines[0]->getLine());
144
145
        $this->assertInstanceOf('Behat\Gherkin\Node\OutlineNode', $outlines[1]);
146
        $this->assertNull($outlines[1]->getTitle());
147
        $this->assertEquals(array('second', 'outline', 'tags'), $outlines[1]->getTags());
148
        $this->assertEquals(1, $outlines[1]->getLine());
149
    }
150
151
    public function testOutlineExamples()
152
    {
153
        $features = $this->loader->load(array(
154
            'features' => array(
155
                array(
156
                    'title'     => 'Feature',
157
                    'scenarios' => array(
158
                        array(
159
                            'type'      => 'outline',
160
                            'title'     => 'First outline',
161
                            'line'      => 2,
162
                            'examples'  => array(
163
                                11 => array('user', 'pass'),
164
                                12 => array('ever', 'sdsd'),
165
                                13 => array('anto', 'fdfd')
166
                            )
167
                        ),
168
                        array(
169
                            'type'  => 'outline',
170
                            'tags'  => array('second', 'outline', 'tags')
171
                        )
172
                    )
173
                )
174
            ),
175
        ));
176
177
        $this->assertEquals(1, count($features));
178
179
        /** @var OutlineNode[] $scenarios */
180
        $scenarios = $features[0]->getScenarios();
181
        $scenario  = $scenarios[0];
182
183
        $this->assertEquals(
184
            array(array('user' => 'ever', 'pass' => 'sdsd'), array('user' => 'anto', 'pass' => 'fdfd')),
185
            $scenario->getExampleTable()->getHash()
0 ignored issues
show
Deprecated Code introduced by
The method Behat\Gherkin\Node\OutlineNode::getExampleTable() has been deprecated with message: use getExampleTables instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
186
        );
187
    }
188
189
    public function testLoadBackground()
190
    {
191
        $features = $this->loader->load(array(
192
            'features' => array(
193
                array(
194
                ),
195
                array(
196
                    'background' => array()
197
                ),
198
                array(
199
                    'background' => array(
200
                        'line' => 2
201
                    )
202
                ),
203
            )
204
        ));
205
206
        $this->assertEquals(3, count($features));
207
208
        $this->assertFalse($features[0]->hasBackground());
209
        $this->assertTrue($features[1]->hasBackground());
210
        $this->assertEquals(0, $features[1]->getBackground()->getLine());
211
        $this->assertTrue($features[2]->hasBackground());
212
        $this->assertEquals(2, $features[2]->getBackground()->getLine());
213
    }
214
215
    public function testLoadSteps()
216
    {
217
        $features = $this->loader->load(array(
218
            'features' => array(
219
                array(
220
                    'background' => array(
221
                        'steps' => array(
222
                            array('type' => 'Gangway!', 'keyword_type' => 'Given', 'text' => 'bg step 1', 'line' => 3),
223
                            array('type' => 'Blimey!', 'keyword_type' => 'When', 'text' => 'bg step 2')
224
                        )
225
                    ),
226
                    'scenarios' => array(
227
                        array(
228
                            'title' => 'Scenario',
229
                            'steps' => array(
230
                                array('type' => 'Gangway!', 'keyword_type' => 'Given', 'text' => 'sc step 1'),
231
                                array('type' => 'Blimey!', 'keyword_type' => 'When', 'text' => 'sc step 2')
232
                            )
233
                        ),
234
                        array(
235
                            'title' => 'Outline',
236
                            'type'  => 'outline',
237
                            'steps' => array(
238
                                array('type' => 'Gangway!', 'keyword_type' => 'Given', 'text' => 'out step 1'),
239
                                array('type' => 'Blimey!', 'keyword_type' => 'When', 'text' => 'out step 2')
240
                            )
241
                        )
242
                    )
243
                )
244
            )
245
        ));
246
247
        $background = $features[0]->getBackground();
248
        $this->assertTrue($background->hasSteps());
249
        $this->assertEquals(2, count($background->getSteps()));
250
        $steps = $background->getSteps();
251
        $this->assertEquals('Gangway!', $steps[0]->getType());
252
        $this->assertEquals('Gangway!', $steps[0]->getKeyword());
253
        $this->assertEquals('Given', $steps[0]->getKeywordType());
254
        $this->assertEquals('bg step 1', $steps[0]->getText());
255
        $this->assertEquals(3, $steps[0]->getLine());
256
        $this->assertEquals('Blimey!', $steps[1]->getType());
257
        $this->assertEquals('Blimey!', $steps[1]->getKeyword());
258
        $this->assertEquals('When', $steps[1]->getKeywordType());
259
        $this->assertEquals('bg step 2', $steps[1]->getText());
260
        $this->assertEquals(1, $steps[1]->getLine());
261
262
        $scenarios  = $features[0]->getScenarios();
263
264
        $scenario = $scenarios[0];
265
        $this->assertTrue($scenario->hasSteps());
266
        $this->assertEquals(2, count($scenario->getSteps()));
267
        $steps = $scenario->getSteps();
268
        $this->assertEquals('Gangway!', $steps[0]->getType());
0 ignored issues
show
Deprecated Code introduced by
The method Behat\Gherkin\Node\StepNode::getType() has been deprecated with message: use getKeyword() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
269
        $this->assertEquals('Gangway!', $steps[0]->getKeyword());
270
        $this->assertEquals('Given', $steps[0]->getKeywordType());
271
        $this->assertEquals('sc step 1', $steps[0]->getText());
272
        $this->assertEquals(0, $steps[0]->getLine());
273
        $this->assertEquals('Blimey!', $steps[1]->getType());
0 ignored issues
show
Deprecated Code introduced by
The method Behat\Gherkin\Node\StepNode::getType() has been deprecated with message: use getKeyword() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
274
        $this->assertEquals('Blimey!', $steps[1]->getKeyword());
275
        $this->assertEquals('When', $steps[1]->getKeywordType());
276
        $this->assertEquals('sc step 2', $steps[1]->getText());
277
        $this->assertEquals(1, $steps[1]->getLine());
278
279
        $outline = $scenarios[1];
280
        $this->assertTrue($outline->hasSteps());
281
        $this->assertEquals(2, count($outline->getSteps()));
282
        $steps = $outline->getSteps();
283
        $this->assertEquals('Gangway!', $steps[0]->getType());
0 ignored issues
show
Deprecated Code introduced by
The method Behat\Gherkin\Node\StepNode::getType() has been deprecated with message: use getKeyword() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
284
        $this->assertEquals('Gangway!', $steps[0]->getKeyword());
285
        $this->assertEquals('Given', $steps[0]->getKeywordType());
286
        $this->assertEquals('out step 1', $steps[0]->getText());
287
        $this->assertEquals(0, $steps[0]->getLine());
288
        $this->assertEquals('Blimey!', $steps[1]->getType());
0 ignored issues
show
Deprecated Code introduced by
The method Behat\Gherkin\Node\StepNode::getType() has been deprecated with message: use getKeyword() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
289
        $this->assertEquals('Blimey!', $steps[1]->getKeyword());
290
        $this->assertEquals('When', $steps[1]->getKeywordType());
291
        $this->assertEquals('out step 2', $steps[1]->getText());
292
        $this->assertEquals(1, $steps[1]->getLine());
293
    }
294
295
    public function testLoadStepArguments()
296
    {
297
        $features = $this->loader->load(array(
298
            'features' => array(
299
                array(
300
                    'background' => array(
301
                        'steps' => array(
302
                            array(
303
                                'type' => 'Gangway!', 'keyword_type' => 'Given', 'text' => 'step with table argument',
304
                                'arguments' => array(
305
                                    array(
306
                                        'type'  => 'table',
307
                                        'rows'  => array(
308
                                            array('key', 'val'),
309
                                            array(1, 2),
310
                                            array(3, 4)
311
                                        )
312
                                    )
313
                                )
314
                            ),
315
                            array(
316
                                'type' => 'Blimey!', 'keyword_type' => 'When', 'text' => 'step with pystring argument',
317
                                'arguments' => array(
318
                                    array(
319
                                        'type'      => 'pystring',
320
                                        'text'      => '    some text',
321
                                    )
322
                                )
323
                            ),
324
                            array(
325
                                'type' => 'Let go and haul', 'keyword_type' => 'Then', 'text' => '2nd step with pystring argument',
326
                                'arguments' => array(
327
                                    array(
328
                                        'type'      => 'pystring',
329
                                        'text'      => 'some text',
330
                                    )
331
                                )
332
                            )
333
                        )
334
                    )
335
                )
336
            )
337
        ));
338
339
        $background = $features[0]->getBackground();
340
341
        $this->assertTrue($background->hasSteps());
342
343
        $steps = $background->getSteps();
344
345
        $this->assertEquals(3, count($steps));
346
347
        $arguments = $steps[0]->getArguments();
348
        $this->assertEquals('Gangway!', $steps[0]->getType());
349
        $this->assertEquals('Gangway!', $steps[0]->getKeyword());
350
        $this->assertEquals('Given', $steps[0]->getKeywordType());
351
        $this->assertEquals('step with table argument', $steps[0]->getText());
352
        $this->assertInstanceOf('Behat\Gherkin\Node\TableNode', $arguments[0]);
353
        $this->assertEquals(array(array('key'=>1, 'val'=>2), array('key'=>3,'val'=>4)), $arguments[0]->getHash());
354
355
        $arguments = $steps[1]->getArguments();
356
        $this->assertEquals('Blimey!', $steps[1]->getType());
357
        $this->assertEquals('Blimey!', $steps[1]->getKeyword());
358
        $this->assertEquals('When', $steps[1]->getKeywordType());
359
        $this->assertEquals('step with pystring argument', $steps[1]->getText());
360
        $this->assertInstanceOf('Behat\Gherkin\Node\PyStringNode', $arguments[0]);
361
        $this->assertEquals('    some text', (string) $arguments[0]);
362
363
        $arguments = $steps[2]->getArguments();
364
        $this->assertEquals('Let go and haul', $steps[2]->getType());
365
        $this->assertEquals('Let go and haul', $steps[2]->getKeyword());
366
        $this->assertEquals('Then', $steps[2]->getKeywordType());
367
        $this->assertEquals('2nd step with pystring argument', $steps[2]->getText());
368
        $this->assertInstanceOf('Behat\Gherkin\Node\PyStringNode', $arguments[0]);
369
        $this->assertEquals('some text', (string) $arguments[0]);
370
    }
371
372
    public function testSingleFeatureArray()
373
    {
374
        $features = $this->loader->load(array(
375
            'feature' => array(
376
                'title' => 'Some feature'
377
            )
378
        ));
379
380
        $this->assertEquals(1, count($features));
381
        $this->assertEquals('Some feature', $features[0]->getTitle());
382
    }
383
}
384