Completed
Pull Request — master (#1)
by Jefersson
03:34 queued 39s
created

CheckGherkinCodeStyleTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7
Metric Value
wmc 3
lcom 1
cbo 7
dl 0
loc 124
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B testShouldReturnOkIfThereIsNoFilesFound() 0 24 1
A testShouldReturnErrorIfThereIsFilesWithWrongStyle() 0 68 1
B getBackground() 0 24 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license.
17
 */
18
19
namespace Command;
20
21
use Behat\Gherkin\Node\BackgroundNode;
22
use Behat\Gherkin\Node\FeatureNode;
23
use Behat\Gherkin\Node\StepNode;
24
use Behat\Gherkin\Node\TableNode;
25
use Behat\Gherkin\Parser;
26
use KawaiiGherkin\Command\CheckGherkinCodeStyle;
27
use Symfony\Component\Console\Application;
28
use Symfony\Component\Console\Tester\CommandTester;
29
30
/**
31
 * Tests for {@see \KawaiiGherkin\Command\CheckGherkinCodeStyle}
32
 *
33
 * @author Jefersson Nathan <[email protected]>
34
 * @covers \KawaiiGherkin\Command\CheckGherkinCodeStyle
35
 * @group Coverage
36
 * @license MIT
37
 */
38
final class CheckGherkinCodeStyleTest extends \PHPUnit_Framework_TestCase
39
{
40
    public function testShouldReturnOkIfThereIsNoFilesFound()
41
    {
42
        $kernel = $this->getMock(\StdClass::class);
43
        /* @var \Behat\Gherkin\Parser|\PHPUnit_Framework_MockObject_MockObject $parser */
44
        $parser = $this->getMockBuilder(Parser::class)
45
            ->disableOriginalConstructor()
46
            ->getMock();
47
48
        $parser->expects($this->exactly(0))
1 ignored issue
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Behat\Gherkin\Parser.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
49
            ->method('parse');
50
51
        $application = new Application($kernel);
52
        $application->add(new CheckGherkinCodeStyle(null, $parser));
53
54
        $command       = $application->find('kawaii:gherkin:check');
55
        $commandTester = new CommandTester($command);
56
        $commandTester->execute(
57
            [
58
                'directory' => __DIR__,
59
            ]
60
        );
61
62
        $this->assertRegExp('/Everything is OK!/', $commandTester->getDisplay());
63
    }
64
65
    public function testShouldReturnErrorIfThereIsFilesWithWrongStyle()
66
    {
67
        $kernel = $this->getMock(\StdClass::class);
68
69
        /* @var \Behat\Gherkin\Parser|\PHPUnit_Framework_MockObject_MockObject $parser */
70
        $parser = $this->getMockBuilder(Parser::class)
71
            ->disableOriginalConstructor()
72
            ->getMock();
73
74
        /* @var \Behat\Gherkin\Node\FeatureNode|\PHPUnit_Framework_MockObject_MockObject $feature */
75
        $feature = $this->getMockBuilder(FeatureNode::class)
76
            ->disableOriginalConstructor()
77
            ->getMock();
78
79
        $feature->expects($this->once())
1 ignored issue
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Behat\Gherkin\Node\FeatureNode.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
80
            ->method('hasTags')
81
            ->willReturn(true);
82
83
        $feature->expects($this->once())
84
            ->method('getTags')
85
            ->willReturn(
86
                ['users', 'another-feature', 'another-tag']
87
            );
88
89
        $feature->expects($this->once())
90
            ->method('getTitle')
91
            ->willReturn(
92
                'User registration'
93
            );
94
95
        $feature->expects($this->once())
96
            ->method('getDescription')
97
            ->willReturn(
98
                "In order to order products\n" .
99
                "As a visitor\n" .
100
                "I need to be able to create an account in the store"
101
            );
102
103
        $feature->expects($this->once())
104
            ->method('hasBackground')
105
            ->willReturn(true);
106
107
        $feature->expects($this->once())
108
            ->method('getBackground')
109
            ->willReturn($this->getBackground());
110
111
        $feature->expects($this->once())
112
            ->method('hasScenarios')
113
            ->willReturn(false);
114
115
        $parser->expects($this->once())
1 ignored issue
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Behat\Gherkin\Parser.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
116
            ->method('parse')
117
            ->willReturn($feature);
118
119
        $application = new Application($kernel);
120
        $application->add(new CheckGherkinCodeStyle(null, $parser));
121
122
        $command       = $application->find('kawaii:gherkin:check');
123
        $commandTester = new CommandTester($command);
124
        $commandTester->execute(
125
            [
126
                'directory' => __DIR__ . '/../assets/',
127
            ]
128
        );
129
130
        $this->assertRegExp('/Wrong style/', $commandTester->getDisplay());
131
        $this->assertNotRegExp('/I need to be able to create an account in the store/', $commandTester->getDisplay());
132
    }
133
134
    /**
135
     * @return BackgroundNode|\PHPUnit_Framework_MockObject_MockObject
136
     */
137
    private function getBackground()
138
    {
139
        /* @var \Behat\Gherkin\Node\BackgroundNode|\PHPUnit_Framework_MockObject_MockObject $background */
140
        $background = $this->getMockBuilder(BackgroundNode::class)
141
            ->disableOriginalConstructor()
142
            ->getMock();
143
144
        $background->expects($this->once())
1 ignored issue
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Behat\Gherkin\Node\BackgroundNode.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
145
            ->method('getKeyword')
146
            ->willReturn('Background');
147
148
        $background->expects($this->once())
149
            ->method('getTitle')
150
            ->willReturn('Nice Background');
151
152
        $background->expects($this->once())
153
            ->method('getSteps')
154
            ->willReturn([
155
                new StepNode('Given', 'store has default configuration', [], 1),
156
                new StepNode('And', 'there are following users:', [new TableNode([['email', 'password'], ['[email protected]', 'foo1sasdasdasdadsasd']])], 2)
157
            ]);
158
159
        return $background;
160
    }
161
}
162