Completed
Push — master ( df4d09...dfc47a )
by Ciaran
08:37
created

LineRangeFilterTest::scenarioLineRangeProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Tests\Behat\Gherkin\Filter;
4
5
use Behat\Gherkin\Filter\LineRangeFilter;
6
use Behat\Gherkin\Node\ExampleTableNode;
7
use Behat\Gherkin\Node\FeatureNode;
8
use Behat\Gherkin\Node\OutlineNode;
9
use Behat\Gherkin\Node\ScenarioNode;
10
11
class LineRangeFilterTest extends FilterTest
12
{
13
    public function featureLineRangeProvider()
14
    {
15
        return array(
16
            array('1', '1', true),
17
            array('1', '2', true),
18
            array('1', '*', true),
19
            array('2', '2', false),
20
            array('2', '*', false)
21
        );
22
    }
23
24
    /**
25
     * @dataProvider featureLineRangeProvider
26
     */
27
    public function testIsFeatureMatchFilter($filterMinLine, $filterMaxLine, $expected)
28
    {
29
        $feature = new FeatureNode(null, null, array(), null, array(), null, null, null, 1);
30
31
        $filter = new LineRangeFilter($filterMinLine, $filterMaxLine);
32
        $this->assertSame($expected, $filter->isFeatureMatch($feature));
33
    }
34
35
    public function scenarioLineRangeProvider()
36
    {
37
        return array(
38
            array('1', '2', 1),
39
            array('1', '*', 2),
40
            array('2', '2', 1),
41
            array('2', '*', 2),
42
            array('3', '3', 1),
43
            array('3', '*', 1),
44
            array('1', '1', 0),
45
            array('4', '4', 0),
46
            array('4', '*', 0)
47
        );
48
    }
49
50
    /**
51
     * @dataProvider scenarioLineRangeProvider
52
     */
53
    public function testIsScenarioMatchFilter($filterMinLine, $filterMaxLine, $expectedNumberOfMatches)
54
    {
55
        $scenario = new ScenarioNode(null, array(), array(), null, 2);
56
        $outline = new OutlineNode(null, array(), array(), array(new ExampleTableNode(array(), null)), null, 3);
57
58
        $filter = new LineRangeFilter($filterMinLine, $filterMaxLine);
59
        $this->assertEquals(
60
            $expectedNumberOfMatches,
61
            intval($filter->isScenarioMatch($scenario)) + intval($filter->isScenarioMatch($outline))
62
        );
63
    }
64
65
    public function testFilterFeatureScenario()
66
    {
67
        $filter = new LineRangeFilter(1, 3);
68
        $feature = $filter->filterFeature($this->getParsedFeature());
0 ignored issues
show
Bug introduced by
It seems like $this->getParsedFeature() can be null; however, filterFeature() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
69
        $this->assertCount(1, $scenarios = $feature->getScenarios());
0 ignored issues
show
Documentation introduced by
$scenarios = $feature->getScenarios() is of type array<integer,object<Beh...ode\ScenarioInterface>>, 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...
70
        $this->assertSame('Scenario#1', $scenarios[0]->getTitle());
71
72
        $filter = new LineRangeFilter(5, 9);
73
        $feature = $filter->filterFeature($this->getParsedFeature());
0 ignored issues
show
Bug introduced by
It seems like $this->getParsedFeature() can be null; however, filterFeature() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
74
        $this->assertCount(1, $scenarios = $feature->getScenarios());
0 ignored issues
show
Documentation introduced by
$scenarios = $feature->getScenarios() is of type array<integer,object<Beh...ode\ScenarioInterface>>, 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...
75
        $this->assertSame('Scenario#2', $scenarios[0]->getTitle());
76
77
        $filter = new LineRangeFilter(5, 6);
78
        $feature = $filter->filterFeature($this->getParsedFeature());
0 ignored issues
show
Bug introduced by
It seems like $this->getParsedFeature() can be null; however, filterFeature() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
79
        $this->assertCount(0, $scenarios = $feature->getScenarios());
0 ignored issues
show
Documentation introduced by
$scenarios = $feature->getScenarios() is of type array<integer,object<Beh...ode\ScenarioInterface>>, 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...
80
    }
81
82
    public function testFilterFeatureOutline()
83
    {
84
        $filter = new LineRangeFilter(12, 14);
85
        $feature = $filter->filterFeature($this->getParsedFeature());
0 ignored issues
show
Bug introduced by
It seems like $this->getParsedFeature() can be null; however, filterFeature() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
86
        /** @var OutlineNode[] $scenarios */
87
        $this->assertCount(1, $scenarios = $feature->getScenarios());
0 ignored issues
show
Documentation introduced by
$scenarios = $feature->getScenarios() is of type array<integer,object<Beh...ode\ScenarioInterface>>, 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...
88
        $this->assertSame('Scenario#3', $scenarios[0]->getTitle());
89
        $this->assertFalse($scenarios[0]->hasExamples());
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 hasExamples() 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...
90
91
        $filter = new LineRangeFilter(16, 21);
92
        $feature = $filter->filterFeature($this->getParsedFeature());
0 ignored issues
show
Bug introduced by
It seems like $this->getParsedFeature() can be null; however, filterFeature() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
93
        $this->assertCount(1, $scenarios = $feature->getScenarios());
0 ignored issues
show
Documentation introduced by
$scenarios = $feature->getScenarios() is of type array<integer,object<Beh...ode\ScenarioInterface>>, 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...
94
        $this->assertSame('Scenario#3', $scenarios[0]->getTitle());
95
        $exampleTableNodes = $scenarios[0]->getExampleTables();
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 getExampleTables() 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...
96
        $this->assertEquals(1, count($exampleTableNodes));
97
        $this->assertCount(3, $exampleTableNodes[0]->getRows());
98
        $this->assertSame(array(
99
            array('action', 'outcome'),
100
            array('act#1', 'out#1'),
101
            array('act#2', 'out#2'),
102
        ), $exampleTableNodes[0]->getRows());
103
        $this->assertEquals(array('etag1'), $exampleTableNodes[0]->getTags());
104
105
        $filter = new LineRangeFilter(16, 26);
106
        $feature = $filter->filterFeature($this->getParsedFeature());
0 ignored issues
show
Bug introduced by
It seems like $this->getParsedFeature() can be null; however, filterFeature() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
107
        $this->assertCount(1, $scenarios = $feature->getScenarios());
0 ignored issues
show
Documentation introduced by
$scenarios = $feature->getScenarios() is of type array<integer,object<Beh...ode\ScenarioInterface>>, 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...
108
        $this->assertSame('Scenario#3', $scenarios[0]->getTitle());
109
        $exampleTableNodes = $scenarios[0]->getExampleTables();
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 getExampleTables() 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...
110
        $this->assertEquals(2, count($exampleTableNodes));
111
112
        $this->assertCount(3, $exampleTableNodes[0]->getRows());
113
        $this->assertSame(array(
114
            array('action', 'outcome'),
115
            array('act#1', 'out#1'),
116
            array('act#2', 'out#2'),
117
        ), $exampleTableNodes[0]->getRows());
118
        $this->assertEquals(array('etag1'), $exampleTableNodes[0]->getTags());
119
120
        $this->assertCount(2, $exampleTableNodes[1]->getRows());
121
        $this->assertSame(array(
122
            array('action', 'outcome'),
123
            array('act#3', 'out#3')
124
        ), $exampleTableNodes[1]->getRows());
125
126
        $this->assertEquals(array('etag2'), $exampleTableNodes[1]->getTags());
127
128
        $filter = new LineRangeFilter(25, 26);
129
        $feature = $filter->filterFeature($this->getParsedFeature());
0 ignored issues
show
Bug introduced by
It seems like $this->getParsedFeature() can be null; however, filterFeature() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
130
        $this->assertCount(1, $scenarios = $feature->getScenarios());
0 ignored issues
show
Documentation introduced by
$scenarios = $feature->getScenarios() is of type array<integer,object<Beh...ode\ScenarioInterface>>, 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...
131
        $this->assertSame('Scenario#3', $scenarios[0]->getTitle());
132
        $exampleTableNodes = $scenarios[0]->getExampleTables();
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 getExampleTables() 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...
133
        $this->assertEquals(1, count($exampleTableNodes));
134
        $this->assertCount(2, $exampleTableNodes[0]->getRows());
135
        $this->assertSame(array(
136
            array('action', 'outcome'),
137
            array('act#3', 'out#3'),
138
        ), $exampleTableNodes[0]->getRows());
139
        $this->assertEquals(array('etag2'), $exampleTableNodes[0]->getTags());
140
    }
141
}
142