testShouldReturnErrorIfThereIsFilesWithWrongStyle()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 74

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 74
rs 8.5672
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 PHPUnit\Framework\TestCase;
28
use Symfony\Component\Console\Application;
29
use Symfony\Component\Console\Tester\CommandTester;
30
31
/**
32
 * Tests for {@see \KawaiiGherkin\Command\CheckGherkinCodeStyle}
33
 *
34
 * @author Jefersson Nathan <[email protected]>
35
 * @covers \KawaiiGherkin\Command\CheckGherkinCodeStyle
36
 * @group Coverage
37
 * @license MIT
38
 */
39
final class CheckGherkinCodeStyleTest extends TestCase
40
{
41
    public function testShouldReturnOkIfThereIsNoFilesFound()
42
    {
43
        /* @var \Behat\Gherkin\Parser|\PHPUnit_Framework_MockObject_MockObject $parser */
44
        $parser = $this->getMockBuilder(Parser::class)
45
            ->disableOriginalConstructor()
46
            ->getMock();
47
48
        $parser->expects(self::exactly(0))
49
            ->method('parse');
50
51
        $application = new Application('test');
52
        $application->add(new CheckGherkinCodeStyle(null, $parser));
53
54
        $command       = $application->find('gherkin:check');
55
        $commandTester = new CommandTester($command);
56
        $commandTester->execute(
57
            [
58
                'directory' => __DIR__,
59
            ]
60
        );
61
62
        self::assertRegExp('/Everything is OK!/', $commandTester->getDisplay());
63
    }
64
65
    public function testShouldReturnErrorIfThereIsFilesWithWrongStyle()
66
    {
67
        /* @var \Behat\Gherkin\Parser|\PHPUnit_Framework_MockObject_MockObject $parser */
68
        $parser = $this->getMockBuilder(Parser::class)
69
            ->disableOriginalConstructor()
70
            ->getMock();
71
72
        /* @var \Behat\Gherkin\Node\FeatureNode|\PHPUnit_Framework_MockObject_MockObject $feature */
73
        $feature = $this->getMockBuilder(FeatureNode::class)
74
            ->disableOriginalConstructor()
75
            ->getMock();
76
77
        $feature->expects(self::once())
78
            ->method('hasTags')
79
            ->willReturn(true);
80
81
        $feature->expects(self::once())
82
            ->method('getKeyword')
83
            ->willReturn('Feature');
84
85
        $feature->expects(self::once())
86
            ->method('getTags')
87
            ->willReturn(
88
                ['users', 'another-feature', 'another-tag']
89
            );
90
91
        $feature->expects(self::once())
92
            ->method('getTitle')
93
            ->willReturn(
94
                'User registration'
95
            );
96
97
        $feature->expects(self::once())
98
            ->method('hasDescription')
99
            ->willReturn(true);
100
101
        $feature->expects(self::once())
102
            ->method('getDescription')
103
            ->willReturn(
104
                "In order to order products\n" .
105
                "As a visitor\n" .
106
                "I need to be able to create an account in the store"
107
            );
108
109
        $feature->expects(self::once())
110
            ->method('hasBackground')
111
            ->willReturn(true);
112
113
        $feature->expects(self::once())
114
            ->method('getBackground')
115
            ->willReturn($this->getBackground());
116
117
        $feature->expects(self::once())
118
            ->method('hasScenarios')
119
            ->willReturn(false);
120
121
        $parser->expects(self::once())
122
            ->method('parse')
123
            ->willReturn($feature);
124
125
        $application = new Application('test');
126
        $application->add(new CheckGherkinCodeStyle(null, $parser));
127
128
        $command       = $application->find('gherkin:check');
129
        $commandTester = new CommandTester($command);
130
        $commandTester->execute(
131
            [
132
                'directory' => __DIR__ . '/../assets/left-aligned.feature',
133
            ]
134
        );
135
136
        self::assertRegExp('/Wrong style/', $commandTester->getDisplay());
137
        self::assertNotRegExp('/I need to be able to create an account in the store/', $commandTester->getDisplay());
138
    }
139
140
    /**
141
     * @return BackgroundNode|\PHPUnit_Framework_MockObject_MockObject
142
     */
143
    private function getBackground()
144
    {
145
        /* @var \Behat\Gherkin\Node\BackgroundNode|\PHPUnit_Framework_MockObject_MockObject $background */
146
        $background = $this->getMockBuilder(BackgroundNode::class)
147
            ->disableOriginalConstructor()
148
            ->getMock();
149
150
        $background->expects(self::once())
151
            ->method('getKeyword')
152
            ->willReturn('Background');
153
154
        $background->expects(self::once())
155
            ->method('getTitle')
156
            ->willReturn('Nice Background');
157
158
        $background->expects(self::once())
159
            ->method('getSteps')
160
            ->willReturn([
161
                new StepNode('Given', 'store has default configuration', [], 1),
162
                new StepNode('And', 'there are following users:', [new TableNode([['email', 'password'], ['[email protected]', 'foo1sasdasdasdadsasd']])], 2)
163
            ]);
164
165
        return $background;
166
    }
167
}
168