Completed
Pull Request — master (#112)
by Christophe
02:50
created

ArrayLoaderTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 373
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 373
wmc 11
lcom 1
cbo 5
rs 10

11 Methods

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

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
182
        );
183
    }
184
185
    public function testLoadBackground()
186
    {
187
        $features = $this->loader->load(array(
188
            'features' => array(
189
                array(
190
                ),
191
                array(
192
                    'background' => array()
193
                ),
194
                array(
195
                    'background' => array(
196
                        'line' => 2
197
                    )
198
                ),
199
            )
200
        ));
201
202
        $this->assertEquals(3, count($features));
203
204
        $this->assertFalse($features[0]->hasBackground());
205
        $this->assertTrue($features[1]->hasBackground());
206
        $this->assertEquals(0, $features[1]->getBackground()->getLine());
207
        $this->assertTrue($features[2]->hasBackground());
208
        $this->assertEquals(2, $features[2]->getBackground()->getLine());
209
    }
210
211
    public function testLoadSteps()
212
    {
213
        $features = $this->loader->load(array(
214
            'features' => array(
215
                array(
216
                    'background' => array(
217
                        'steps' => array(
218
                            array('type' => 'Gangway!', 'keyword_type' => 'Given', 'text' => 'bg step 1', 'line' => 3),
219
                            array('type' => 'Blimey!', 'keyword_type' => 'When', 'text' => 'bg step 2')
220
                        )
221
                    ),
222
                    'scenarios' => array(
223
                        array(
224
                            'title' => 'Scenario',
225
                            'steps' => array(
226
                                array('type' => 'Gangway!', 'keyword_type' => 'Given', 'text' => 'sc step 1'),
227
                                array('type' => 'Blimey!', 'keyword_type' => 'When', 'text' => 'sc step 2')
228
                            )
229
                        ),
230
                        array(
231
                            'title' => 'Outline',
232
                            'type'  => 'outline',
233
                            'steps' => array(
234
                                array('type' => 'Gangway!', 'keyword_type' => 'Given', 'text' => 'out step 1'),
235
                                array('type' => 'Blimey!', 'keyword_type' => 'When', 'text' => 'out step 2')
236
                            )
237
                        )
238
                    )
239
                )
240
            )
241
        ));
242
243
        $background = $features[0]->getBackground();
244
        $this->assertTrue($background->hasSteps());
245
        $this->assertEquals(2, count($background->getSteps()));
246
        $steps = $background->getSteps();
247
        $this->assertEquals('Gangway!', $steps[0]->getType());
248
        $this->assertEquals('Gangway!', $steps[0]->getKeyword());
249
        $this->assertEquals('Given', $steps[0]->getKeywordType());
250
        $this->assertEquals('bg step 1', $steps[0]->getText());
251
        $this->assertEquals(3, $steps[0]->getLine());
252
        $this->assertEquals('Blimey!', $steps[1]->getType());
253
        $this->assertEquals('Blimey!', $steps[1]->getKeyword());
254
        $this->assertEquals('When', $steps[1]->getKeywordType());
255
        $this->assertEquals('bg step 2', $steps[1]->getText());
256
        $this->assertEquals(1, $steps[1]->getLine());
257
258
        $scenarios  = $features[0]->getScenarios();
259
260
        $scenario = $scenarios[0];
261
        $this->assertTrue($scenario->hasSteps());
262
        $this->assertEquals(2, count($scenario->getSteps()));
263
        $steps = $scenario->getSteps();
264
        $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...
265
        $this->assertEquals('Gangway!', $steps[0]->getKeyword());
266
        $this->assertEquals('Given', $steps[0]->getKeywordType());
267
        $this->assertEquals('sc step 1', $steps[0]->getText());
268
        $this->assertEquals(0, $steps[0]->getLine());
269
        $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...
270
        $this->assertEquals('Blimey!', $steps[1]->getKeyword());
271
        $this->assertEquals('When', $steps[1]->getKeywordType());
272
        $this->assertEquals('sc step 2', $steps[1]->getText());
273
        $this->assertEquals(1, $steps[1]->getLine());
274
275
        $outline = $scenarios[1];
276
        $this->assertTrue($outline->hasSteps());
277
        $this->assertEquals(2, count($outline->getSteps()));
278
        $steps = $outline->getSteps();
279
        $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...
280
        $this->assertEquals('Gangway!', $steps[0]->getKeyword());
281
        $this->assertEquals('Given', $steps[0]->getKeywordType());
282
        $this->assertEquals('out step 1', $steps[0]->getText());
283
        $this->assertEquals(0, $steps[0]->getLine());
284
        $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...
285
        $this->assertEquals('Blimey!', $steps[1]->getKeyword());
286
        $this->assertEquals('When', $steps[1]->getKeywordType());
287
        $this->assertEquals('out step 2', $steps[1]->getText());
288
        $this->assertEquals(1, $steps[1]->getLine());
289
    }
290
291
    public function testLoadStepArguments()
292
    {
293
        $features = $this->loader->load(array(
294
            'features' => array(
295
                array(
296
                    'background' => array(
297
                        'steps' => array(
298
                            array(
299
                                'type' => 'Gangway!', 'keyword_type' => 'Given', 'text' => 'step with table argument',
300
                                'arguments' => array(
301
                                    array(
302
                                        'type'  => 'table',
303
                                        'rows'  => array(
304
                                            array('key', 'val'),
305
                                            array(1, 2),
306
                                            array(3, 4)
307
                                        )
308
                                    )
309
                                )
310
                            ),
311
                            array(
312
                                'type' => 'Blimey!', 'keyword_type' => 'When', 'text' => 'step with pystring argument',
313
                                'arguments' => array(
314
                                    array(
315
                                        'type'      => 'pystring',
316
                                        'text'      => '    some text',
317
                                    )
318
                                )
319
                            ),
320
                            array(
321
                                'type' => 'Let go and haul', 'keyword_type' => 'Then', 'text' => '2nd step with pystring argument',
322
                                'arguments' => array(
323
                                    array(
324
                                        'type'      => 'pystring',
325
                                        'text'      => 'some text',
326
                                    )
327
                                )
328
                            )
329
                        )
330
                    )
331
                )
332
            )
333
        ));
334
335
        $background = $features[0]->getBackground();
336
337
        $this->assertTrue($background->hasSteps());
338
339
        $steps = $background->getSteps();
340
341
        $this->assertEquals(3, count($steps));
342
343
        $arguments = $steps[0]->getArguments();
344
        $this->assertEquals('Gangway!', $steps[0]->getType());
345
        $this->assertEquals('Gangway!', $steps[0]->getKeyword());
346
        $this->assertEquals('Given', $steps[0]->getKeywordType());
347
        $this->assertEquals('step with table argument', $steps[0]->getText());
348
        $this->assertInstanceOf('Behat\Gherkin\Node\TableNode', $arguments[0]);
349
        $this->assertEquals(array(array('key'=>1, 'val'=>2), array('key'=>3,'val'=>4)), $arguments[0]->getHash());
350
351
        $arguments = $steps[1]->getArguments();
352
        $this->assertEquals('Blimey!', $steps[1]->getType());
353
        $this->assertEquals('Blimey!', $steps[1]->getKeyword());
354
        $this->assertEquals('When', $steps[1]->getKeywordType());
355
        $this->assertEquals('step with pystring argument', $steps[1]->getText());
356
        $this->assertInstanceOf('Behat\Gherkin\Node\PyStringNode', $arguments[0]);
357
        $this->assertEquals('    some text', (string) $arguments[0]);
358
359
        $arguments = $steps[2]->getArguments();
360
        $this->assertEquals('Let go and haul', $steps[2]->getType());
361
        $this->assertEquals('Let go and haul', $steps[2]->getKeyword());
362
        $this->assertEquals('Then', $steps[2]->getKeywordType());
363
        $this->assertEquals('2nd step with pystring argument', $steps[2]->getText());
364
        $this->assertInstanceOf('Behat\Gherkin\Node\PyStringNode', $arguments[0]);
365
        $this->assertEquals('some text', (string) $arguments[0]);
366
    }
367
368
    public function testSingleFeatureArray()
369
    {
370
        $features = $this->loader->load(array(
371
            'feature' => array(
372
                'title' => 'Some feature'
373
            )
374
        ));
375
376
        $this->assertEquals(1, count($features));
377
        $this->assertEquals('Some feature', $features[0]->getTitle());
378
    }
379
}
380