Completed
Pull Request — master (#100)
by
unknown
03:31
created

testExamplesIncludesTheOutlineNode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 23
c 1
b 0
f 1
rs 9.0857
cc 2
eloc 16
nc 2
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
11
class ExampleNodeTest extends \PHPUnit_Framework_TestCase
12
{
13
    public function testCreateExampleSteps()
14
    {
15
        $steps = array(
16
            $step1 = new StepNode('Gangway!', 'I am <name>', array(), null, 'Given'),
17
            $step2 = new StepNode('Aye!', 'my email is <email>', array(), null, 'And'),
18
            $step3 = new StepNode('Blimey!', 'I open homepage', array(), null, 'When'),
19
            $step4 = new StepNode('Let go and haul', 'website should recognise me', array(), null, 'Then'),
20
        );
21
22
        $table = new ExampleTableNode(array(
23
            array('name', 'email'),
24
            array('everzet', '[email protected]'),
25
            array('example', '[email protected]')
26
        ), 'Examples');
27
28
        $outline = new OutlineNode(null, array(), $steps, $table, null, null);
29
        $examples = $outline->getExamples();
30
31
        $this->assertCount(4, $steps = $examples[0]->getSteps());
32
33
        $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...
34
        $this->assertEquals('Gangway!', $steps[0]->getKeyword());
35
        $this->assertEquals('Given', $steps[0]->getKeywordType());
36
        $this->assertEquals('I am everzet', $steps[0]->getText());
37
        $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...
38
        $this->assertEquals('Aye!', $steps[1]->getKeyword());
39
        $this->assertEquals('And', $steps[1]->getKeywordType());
40
        $this->assertEquals('my email is [email protected]', $steps[1]->getText());
41
        $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...
42
        $this->assertEquals('Blimey!', $steps[2]->getKeyword());
43
        $this->assertEquals('When', $steps[2]->getKeywordType());
44
        $this->assertEquals('I open homepage', $steps[2]->getText());
45
46
        $this->assertCount(4, $steps = $examples[1]->getSteps());
47
48
        $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...
49
        $this->assertEquals('Gangway!', $steps[0]->getKeyword());
50
        $this->assertEquals('Given', $steps[0]->getKeywordType());
51
        $this->assertEquals('I am example', $steps[0]->getText());
52
        $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...
53
        $this->assertEquals('Aye!', $steps[1]->getKeyword());
54
        $this->assertEquals('And', $steps[1]->getKeywordType());
55
        $this->assertEquals('my email is [email protected]', $steps[1]->getText());
56
        $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...
57
        $this->assertEquals('Blimey!', $steps[2]->getKeyword());
58
        $this->assertEquals('When', $steps[2]->getKeywordType());
59
        $this->assertEquals('I open homepage', $steps[2]->getText());
60
    }
61
62
    public function testCreateExampleStepsWithArguments()
63
    {
64
        $steps = array(
65
            $step1 = new StepNode('Gangway!', 'I am <name>', array(), null, 'Given'),
66
            $step2 = new StepNode('Aye!', 'my email is <email>', array(), null, 'And'),
67
            $step3 = new StepNode('Blimey!', 'I open:', array(
68
                new PyStringNode(array('page: <url>'), null)
69
            ), null, 'When'),
70
            $step4 = new StepNode('Let go and haul',  'website should recognise me', array(
0 ignored issues
show
Coding Style introduced by
Expected 1 space instead of 2 after comma in function call.
Loading history...
71
                new TableNode(array(array('page', '<url>')))
72
            ), null, 'Then'),
73
        );
74
75
        $table = new ExampleTableNode(array(
76
            array('name', 'email', 'url'),
77
            array('everzet', '[email protected]', 'homepage'),
78
            array('example', '[email protected]', 'other page')
79
        ), 'Examples');
80
81
        $outline = new OutlineNode(null, array(), $steps, $table, null, null);
82
        $examples = $outline->getExamples();
83
84
        $steps = $examples[0]->getSteps();
85
86
        $args = $steps[2]->getArguments();
87
        $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...
88
89
        $args = $steps[3]->getArguments();
90
        $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...
91
    }
92
93
    public function testExamplesIncludesTheOutlineNode()
94
    {
95
        $steps = array(
96
            $step1 = new StepNode('Gangway!', 'I am <name>', array(), null, 'Given'),
97
            $step2 = new StepNode('Aye!', 'my email is <email>', array(), null, 'And'),
98
            $step3 = new StepNode('Blimey!', 'I open homepage', array(), null, 'When'),
99
            $step4 = new StepNode('Let go and haul', 'website should recognise me', array(), null, 'Then'),
100
        );
101
102
        $table = new ExampleTableNode(array(
103
            array('name', 'email'),
104
            array('user1', '[email protected]'),
105
            array('user2', '[email protected]'),
106
        ), 'Examples');
107
108
        $outline = new OutlineNode('outline test title', array(), $steps, $table, null, null);
109
        $examples = $outline->getExamples();
110
111
        for($i = 0; $i < count($examples); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
Coding Style introduced by
Expected 1 space after FOR keyword; 0 found
Loading history...
112
            $this->assertTrue($examples[$i]->getOutline() instanceof OutlineNode);
113
            $this->assertEquals('outline test title', $examples[$i]->getOutline()->getTitle());
114
        }
115
    }
116
}
117