Completed
Push — master ( 236db5...b6d10e )
by Pablo
7s
created

itShouldWorksFine()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 52
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 52
rs 8.6868
cc 5
eloc 31
nc 16
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
namespace PhpGitHooks\Module\PhpCsFixer\Tests\Behaviour;
4
5
use PhpGitHooks\Module\Configuration\Tests\Stub\ConfigurationDataResponseStub;
6
use PhpGitHooks\Module\Git\Contract\Response\BadJobLogoResponse;
7
use PhpGitHooks\Module\Git\Service\PreCommitOutputWriter;
8
use PhpGitHooks\Module\Git\Tests\Stub\FilesCommittedStub;
9
use PhpGitHooks\Module\PhpCsFixer\Contract\Command\PhpCsFixerToolCommand;
10
use PhpGitHooks\Module\PhpCsFixer\Contract\CommandHandler\PhpCsFixerToolCommandHandler;
11
use PhpGitHooks\Module\PhpCsFixer\Contract\Exception\PhpCsFixerViolationsException;
12
use PhpGitHooks\Module\PhpCsFixer\Service\PhpCsFixerTool;
13
use PhpGitHooks\Module\PhpCsFixer\Tests\Infrastructure\PhpCsFixerUnitTestCase;
14
15
class PhpCsFixerToolCommandHandlerTest extends PhpCsFixerUnitTestCase
16
{
17
    /**
18
     * @var PhpCsFixerToolCommandHandler
19
     */
20
    private $phpCsFixerToolCommandHandler;
21
22
    protected function setUp()
23
    {
24
        $this->phpCsFixerToolCommandHandler = new PhpCsFixerToolCommandHandler(
25
            new PhpCsFixerTool(
26
                $this->getOutputInterface(),
0 ignored issues
show
Bug introduced by
It seems like $this->getOutputInterface() targeting PhpGitHooks\Module\Git\T...t::getOutputInterface() can also be of type object<Mockery\MockInterface>; however, PhpGitHooks\Module\PhpCs...ixerTool::__construct() does only seem to accept object<Symfony\Component...Output\OutputInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
27
                $this->getPhpCsFixerToolProcessor()
0 ignored issues
show
Bug introduced by
It seems like $this->getPhpCsFixerToolProcessor() targeting PhpGitHooks\Module\PhpCs...pCsFixerToolProcessor() can also be of type object<Mockery\MockInterface>; however, PhpGitHooks\Module\PhpCs...ixerTool::__construct() does only seem to accept object<PhpGitHooks\Modul...ToolProcessorInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
28
            )
29
        );
30
    }
31
32
    /**
33
     * @test
34
     */
35
    public function itShouldThrowsException()
36
    {
37
        $this->expectException(PhpCsFixerViolationsException::class);
38
39
        $configurationData = ConfigurationDataResponseStub::createAllEnabled();
40
        $phpFiles = FilesCommittedStub::createOnlyPhpFiles();
41
42
        $outputMessage = new PreCommitOutputWriter('Checking PSR0 code style with PHP-CS-FIXER');
43
        $this->shouldWriteOutput($outputMessage->getMessage());
44
45
        $errors = null;
46
        foreach ($phpFiles as $file) {
47
            $errorText = 'ERROR';
48
            $this->shouldProcessPhpCsFixerTool($file, 'PSR0', $errorText);
49
            $errors .= $errorText;
50
        }
51
52
        $this->shouldWriteLnOutput($outputMessage->getFailMessage());
53
        $this->shouldWriteLnOutput($outputMessage->setError($errors));
54
        $this->shouldWriteLnOutput(BadJobLogoResponse::paint($configurationData->getPreCommit()->getErrorMessage()));
55
56
        $this->phpCsFixerToolCommandHandler->handle(
57
            new PhpCsFixerToolCommand(
58
                $phpFiles,
59
                $configurationData->getPreCommit()->getPhpCsFixer()->isPhpCsFixerPsr0(),
60
                $configurationData->getPreCommit()->getPhpCsFixer()->isPhpCsFixerPsr1(),
61
                $configurationData->getPreCommit()->getPhpCsFixer()->isPhpCsFixerPsr2(),
62
                $configurationData->getPreCommit()->getPhpCsFixer()->isPhpCsFixerSymfony(),
63
                $configurationData->getPreCommit()->getErrorMessage()
64
            )
65
        );
66
    }
67
68
    /**
69
     * @test
70
     */
71
    public function itShouldWorksFine()
72
    {
73
        $configurationData = ConfigurationDataResponseStub::createAllEnabled();
74
        $phpFiles = FilesCommittedStub::createOnlyPhpFiles();
75
76
        $outputMessagePsr0 = new PreCommitOutputWriter('Checking PSR0 code style with PHP-CS-FIXER');
77
        $this->shouldWriteOutput($outputMessagePsr0->getMessage());
78
79
        foreach ($phpFiles as $file) {
80
            $this->shouldProcessPhpCsFixerTool($file, 'PSR0', null);
81
        }
82
83
        $this->shouldWriteLnOutput($outputMessagePsr0->getSuccessfulMessage());
84
85
        $outputMessagePsr1 = new PreCommitOutputWriter('Checking PSR1 code style with PHP-CS-FIXER');
86
        $this->shouldWriteOutput($outputMessagePsr1->getMessage());
87
88
        foreach ($phpFiles as $file) {
89
            $this->shouldProcessPhpCsFixerTool($file, 'PSR1', null);
90
        }
91
92
        $this->shouldWriteLnOutput($outputMessagePsr1->getSuccessfulMessage());
93
94
        $outputMessagePsr2 = new PreCommitOutputWriter('Checking PSR2 code style with PHP-CS-FIXER');
95
        $this->shouldWriteOutput($outputMessagePsr2->getMessage());
96
97
        foreach ($phpFiles as $file) {
98
            $this->shouldProcessPhpCsFixerTool($file, 'PSR2', null);
99
        }
100
101
        $this->shouldWriteLnOutput($outputMessagePsr2->getSuccessfulMessage());
102
103
        $outputMessageSymfony = new PreCommitOutputWriter('Checking SYMFONY code style with PHP-CS-FIXER');
104
        $this->shouldWriteOutput($outputMessageSymfony->getMessage());
105
106
        foreach ($phpFiles as $file) {
107
            $this->shouldProcessPhpCsFixerTool($file, 'SYMFONY', null);
108
        }
109
110
        $this->shouldWriteLnOutput($outputMessageSymfony->getSuccessfulMessage());
111
112
        $this->phpCsFixerToolCommandHandler->handle(
113
            new PhpCsFixerToolCommand(
114
                $phpFiles,
115
                $configurationData->getPreCommit()->getPhpCsFixer()->isPhpCsFixerPsr0(),
116
                $configurationData->getPreCommit()->getPhpCsFixer()->isPhpCsFixerPsr1(),
117
                $configurationData->getPreCommit()->getPhpCsFixer()->isPhpCsFixerPsr2(),
118
                $configurationData->getPreCommit()->getPhpCsFixer()->isPhpCsFixerSymfony(),
119
                $configurationData->getPreCommit()->getErrorMessage()
120
            )
121
        );
122
    }
123
}
124