testCreateExampleStepsWithArguments()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 9.44
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Tests\Behat\Gherkin\Node;
4
5
use Behat\Gherkin\Node\ExampleTableNode;
6
use Behat\Gherkin\Node\OutlineNode;
7
use Behat\Gherkin\Node\PyStringNode;
8
use Behat\Gherkin\Node\StepNode;
9
use Behat\Gherkin\Node\TableNode;
10
use PHPUnit\Framework\TestCase;
11
12
class ExampleNodeTest extends TestCase
13
{
14
    public function testCreateExampleSteps()
15
    {
16
        $steps = array(
17
            $step1 = new StepNode('Gangway!', 'I am <name>', array(), null, 'Given'),
18
            $step2 = new StepNode('Aye!', 'my email is <email>', array(), null, 'And'),
19
            $step3 = new StepNode('Blimey!', 'I open homepage', array(), null, 'When'),
20
            $step4 = new StepNode('Let go and haul', 'website should recognise me', array(), null, 'Then'),
21
        );
22
23
        $table = new ExampleTableNode(array(
24
            array('name', 'email'),
25
            array('everzet', '[email protected]'),
26
            array('example', '[email protected]')
27
        ), 'Examples');
28
29
        $outline = new OutlineNode(null, array(), $steps, $table, null, null);
30
        $examples = $outline->getExamples();
31
32
        $this->assertCount(4, $steps = $examples[0]->getSteps());
0 ignored issues
show
Documentation introduced by
$steps = $examples[0]->getSteps() is of type array<integer,object<Beh...Gherkin\Node\StepNode>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

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...
33
34
        $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...
35
        $this->assertEquals('Gangway!', $steps[0]->getKeyword());
36
        $this->assertEquals('Given', $steps[0]->getKeywordType());
37
        $this->assertEquals('I am everzet', $steps[0]->getText());
38
        $this->assertEquals('Aye!', $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...
39
        $this->assertEquals('Aye!', $steps[1]->getKeyword());
40
        $this->assertEquals('And', $steps[1]->getKeywordType());
41
        $this->assertEquals('my email is [email protected]', $steps[1]->getText());
42
        $this->assertEquals('Blimey!', $steps[2]->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...
43
        $this->assertEquals('Blimey!', $steps[2]->getKeyword());
44
        $this->assertEquals('When', $steps[2]->getKeywordType());
45
        $this->assertEquals('I open homepage', $steps[2]->getText());
46
47
        $this->assertCount(4, $steps = $examples[1]->getSteps());
0 ignored issues
show
Documentation introduced by
$steps = $examples[1]->getSteps() is of type array<integer,object<Beh...Gherkin\Node\StepNode>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

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...
48
49
        $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...
50
        $this->assertEquals('Gangway!', $steps[0]->getKeyword());
51
        $this->assertEquals('Given', $steps[0]->getKeywordType());
52
        $this->assertEquals('I am example', $steps[0]->getText());
53
        $this->assertEquals('Aye!', $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...
54
        $this->assertEquals('Aye!', $steps[1]->getKeyword());
55
        $this->assertEquals('And', $steps[1]->getKeywordType());
56
        $this->assertEquals('my email is [email protected]', $steps[1]->getText());
57
        $this->assertEquals('Blimey!', $steps[2]->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...
58
        $this->assertEquals('Blimey!', $steps[2]->getKeyword());
59
        $this->assertEquals('When', $steps[2]->getKeywordType());
60
        $this->assertEquals('I open homepage', $steps[2]->getText());
61
    }
62
63
    public function testCreateExampleStepsWithArguments()
64
    {
65
        $steps = array(
66
            $step1 = new StepNode('Gangway!', 'I am <name>', array(), null, 'Given'),
67
            $step2 = new StepNode('Aye!', 'my email is <email>', array(), null, 'And'),
68
            $step3 = new StepNode('Blimey!', 'I open:', array(
69
                new PyStringNode(array('page: <url>'), null)
70
            ), null, 'When'),
71
            $step4 = new StepNode('Let go and haul',  'website should recognise me', array(
72
                new TableNode(array(array('page', '<url>')))
73
            ), null, 'Then'),
74
        );
75
76
        $table = new ExampleTableNode(array(
77
            array('name', 'email', 'url'),
78
            array('everzet', '[email protected]', 'homepage'),
79
            array('example', '[email protected]', 'other page')
80
        ), 'Examples');
81
82
        $outline = new OutlineNode(null, array(), $steps, $table, null, null);
83
        $examples = $outline->getExamples();
84
85
        $steps = $examples[0]->getSteps();
86
87
        $args = $steps[2]->getArguments();
88
        $this->assertEquals('page: homepage', $args[0]->getRaw());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Behat\Gherkin\Node\ArgumentInterface as the method getRaw() does only exist in the following implementations of said interface: Behat\Gherkin\Node\PyStringNode.

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...
89
90
        $args = $steps[3]->getArguments();
91
        $this->assertEquals('| page | homepage |', $args[0]->getTableAsString());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Behat\Gherkin\Node\ArgumentInterface as the method getTableAsString() does only exist in the following implementations of said interface: Behat\Gherkin\Node\ExampleTableNode, Behat\Gherkin\Node\TableNode.

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...
92
    }
93
}
94