Completed
Push — master ( 835840...ce2316 )
by Pablo
02:59
created

PhpCsFixerToolHandlerTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 12
dl 0
loc 111
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
B itShouldThrowsException() 0 34 2
B itShouldWorksFine() 0 54 5
1
<?php
2
3
namespace PhpGitHooks\Module\PhpCsFixer\Tests\Behaviour;
4
5
use PhpGitHooks\Module\Configuration\Tests\Stub\ConfigurationDataResponseStub;
6
use PhpGitHooks\Module\Configuration\Tests\Stub\PhpCsFixerOptionsStub;
7
use PhpGitHooks\Module\Git\Contract\Response\BadJobLogoResponse;
8
use PhpGitHooks\Module\Git\Service\PreCommitOutputWriter;
9
use PhpGitHooks\Module\Git\Tests\Stub\FilesCommittedStub;
10
use PhpGitHooks\Module\PhpCsFixer\Contract\Command\PhpCsFixerTool;
11
use PhpGitHooks\Module\PhpCsFixer\Contract\Command\PhpCsFixerToolHandler;
12
use PhpGitHooks\Module\PhpCsFixer\Contract\Exception\PhpCsFixerViolationsException;
13
use PhpGitHooks\Module\PhpCsFixer\Tests\Infrastructure\PhpCsFixerUnitTestCase;
14
15
class PhpCsFixerToolHandlerTest extends PhpCsFixerUnitTestCase
16
{
17
    /**
18
     * @var PhpCsFixerToolHandler
19
     */
20
    private $phpCsFixerToolCommandHandler;
21
22
    protected function setUp()
23
    {
24
        $this->phpCsFixerToolCommandHandler = new PhpCsFixerToolHandler(
25
            $this->getOutputInterface(),
26
            $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...lHandler::__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...
27
        );
28
    }
29
30
    /**
31
     * @test
32
     */
33
    public function itShouldThrowsException()
34
    {
35
        $this->expectException(PhpCsFixerViolationsException::class);
36
37
        $phpCsFixerOptions = PhpCsFixerOptionsStub::random();
38
        $configurationData = ConfigurationDataResponseStub::createAllEnabled();
39
        $phpFiles = FilesCommittedStub::createOnlyPhpFiles();
40
41
        $outputMessage = new PreCommitOutputWriter('Checking PSR0 code style with PHP-CS-FIXER');
42
        $this->shouldWriteOutput($outputMessage->getMessage());
43
44
        $errors = null;
45
        foreach ($phpFiles as $file) {
46
            $errorText = 'ERROR';
47
            $this->shouldProcessPhpCsFixerTool($file, 'PSR0', $phpCsFixerOptions->value(), $errorText);
48
            $errors .= $errorText;
49
        }
50
51
        $this->shouldWriteLnOutput($outputMessage->getFailMessage());
52
        $this->shouldWriteLnOutput($outputMessage->setError($errors));
53
        $this->shouldWriteLnOutput(BadJobLogoResponse::paint($configurationData->getPreCommit()->getErrorMessage()));
54
55
        $this->phpCsFixerToolCommandHandler->handle(
56
            new PhpCsFixerTool(
57
                $phpFiles,
58
                $configurationData->getPreCommit()->getPhpCsFixer()->isPhpCsFixerPsr0(),
59
                $configurationData->getPreCommit()->getPhpCsFixer()->isPhpCsFixerPsr1(),
60
                $configurationData->getPreCommit()->getPhpCsFixer()->isPhpCsFixerPsr2(),
61
                $configurationData->getPreCommit()->getPhpCsFixer()->isPhpCsFixerSymfony(),
62
                $phpCsFixerOptions->value(),
63
                $configurationData->getPreCommit()->getErrorMessage()
64
            )
65
        );
66
    }
67
68
    /**
69
     * @test
70
     */
71
    public function itShouldWorksFine()
72
    {
73
        $phpCsFixerOptions = PhpCsFixerOptionsStub::random();
74
        $configurationData = ConfigurationDataResponseStub::createAllEnabled();
75
        $phpFiles = FilesCommittedStub::createOnlyPhpFiles();
76
77
        $outputMessagePsr0 = new PreCommitOutputWriter('Checking PSR0 code style with PHP-CS-FIXER');
78
        $this->shouldWriteOutput($outputMessagePsr0->getMessage());
79
80
        foreach ($phpFiles as $file) {
81
            $this->shouldProcessPhpCsFixerTool($file, 'PSR0', $phpCsFixerOptions->value(), null);
82
        }
83
84
        $this->shouldWriteLnOutput($outputMessagePsr0->getSuccessfulMessage());
85
86
        $outputMessagePsr1 = new PreCommitOutputWriter('Checking PSR1 code style with PHP-CS-FIXER');
87
        $this->shouldWriteOutput($outputMessagePsr1->getMessage());
88
89
        foreach ($phpFiles as $file) {
90
            $this->shouldProcessPhpCsFixerTool($file, 'PSR1', $phpCsFixerOptions->value(), null);
91
        }
92
93
        $this->shouldWriteLnOutput($outputMessagePsr1->getSuccessfulMessage());
94
95
        $outputMessagePsr2 = new PreCommitOutputWriter('Checking PSR2 code style with PHP-CS-FIXER');
96
        $this->shouldWriteOutput($outputMessagePsr2->getMessage());
97
98
        foreach ($phpFiles as $file) {
99
            $this->shouldProcessPhpCsFixerTool($file, 'PSR2', $phpCsFixerOptions->value(), null);
100
        }
101
102
        $this->shouldWriteLnOutput($outputMessagePsr2->getSuccessfulMessage());
103
104
        $outputMessageSymfony = new PreCommitOutputWriter('Checking SYMFONY code style with PHP-CS-FIXER');
105
        $this->shouldWriteOutput($outputMessageSymfony->getMessage());
106
107
        foreach ($phpFiles as $file) {
108
            $this->shouldProcessPhpCsFixerTool($file, 'SYMFONY', $phpCsFixerOptions->value(), null);
109
        }
110
111
        $this->shouldWriteLnOutput($outputMessageSymfony->getSuccessfulMessage());
112
113
        $this->phpCsFixerToolCommandHandler->handle(
114
            new PhpCsFixerTool(
115
                $phpFiles,
116
                $configurationData->getPreCommit()->getPhpCsFixer()->isPhpCsFixerPsr0(),
117
                $configurationData->getPreCommit()->getPhpCsFixer()->isPhpCsFixerPsr1(),
118
                $configurationData->getPreCommit()->getPhpCsFixer()->isPhpCsFixerPsr2(),
119
                $configurationData->getPreCommit()->getPhpCsFixer()->isPhpCsFixerSymfony(),
120
                $phpCsFixerOptions->value(),
121
                $configurationData->getPreCommit()->getErrorMessage()
122
            )
123
        );
124
    }
125
}
126