GuardCoverageToolHandlerTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 80
Duplicated Lines 27.5 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 22
loc 80
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 9 1
A itShouldShowWarningMessage() 0 23 1
A itShouldWorksFine() 22 22 1
A buildStrictCoverageSuccessfulMessage() 0 9 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace PhpGitHooks\Module\PhpUnit\Tests\Behaviour;
4
5
use PhpGitHooks\Module\Configuration\Service\HookQuestions;
6
use PhpGitHooks\Module\Git\Service\PreCommitOutputWriter;
7
use PhpGitHooks\Module\PhpUnit\Contract\Command\GuardCoverage;
8
use PhpGitHooks\Module\PhpUnit\Contract\Command\GuardCoverageToolHandler;
9
use PhpGitHooks\Module\PhpUnit\Tests\Infrastructure\PhpUnitUnitTestCase;
10
11
class GuardCoverageToolHandlerTest extends PhpUnitUnitTestCase
12
{
13
    /**
14
     * @var GuardCoverageToolHandler
15
     */
16
    private $guardCoverageToolCommandHandler;
17
18
    protected function setUp(): void
19
    {
20
        $this->guardCoverageToolCommandHandler = new GuardCoverageToolHandler(
21
            $this->getOutputInterface(),
22
            $this->getStrictCoverageProcessor(),
0 ignored issues
show
Bug introduced by
It seems like $this->getStrictCoverageProcessor() targeting PhpGitHooks\Module\PhpUn...rictCoverageProcessor() can also be of type object<Mockery\MockInterface>; however, PhpGitHooks\Module\PhpUn...lHandler::__construct() does only seem to accept object<PhpGitHooks\Modul...rageProcessorInterface>, 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...
23
            $this->getGuardCoverageFileReader(),
0 ignored issues
show
Bug introduced by
It seems like $this->getGuardCoverageFileReader() targeting PhpGitHooks\Module\PhpUn...ardCoverageFileReader() can also be of type object<Mockery\MockInterface>; however, PhpGitHooks\Module\PhpUn...lHandler::__construct() does only seem to accept object<PhpGitHooks\Modul...ageFileReaderInterface>, 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...
24
            $this->getGuardCoverageFileWriter()
0 ignored issues
show
Bug introduced by
It seems like $this->getGuardCoverageFileWriter() targeting PhpGitHooks\Module\PhpUn...ardCoverageFileWriter() can also be of type object<Mockery\MockInterface>; however, PhpGitHooks\Module\PhpUn...lHandler::__construct() does only seem to accept object<PhpGitHooks\Modul...ageFileWriterInterface>, 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...
25
        );
26
    }
27
28
    /**
29
     * @test
30
     */
31
    public function itShouldShowWarningMessage()
32
    {
33
        $outputMessage = new PreCommitOutputWriter(GuardCoverageToolHandler::CHECKING_MESSAGE);
34
        $currentCoverage = 60.00;
35
        $previousCoverage = 70.00;
36
37
        $this->shouldProcessStrictCoverage($currentCoverage);
38
        $this->shouldWriteOutput($outputMessage->getMessage());
39
        $this->shouldReadGuardCoverage($previousCoverage);
40
        $this->shouldWriteLnOutput(
41
            sprintf(
42
                "\n<bg=yellow;options=bold>%s Previous coverage %s, current coverage %s.</>",
43
                HookQuestions::PHPUNIT_GUARD_COVERAGE_MESSAGE_DEFAULT,
44
                $previousCoverage,
45
                $currentCoverage
46
            )
47
        );
48
        $this->shouldWriteGuardCoverage($currentCoverage);
49
50
        $this->guardCoverageToolCommandHandler->handle(
51
            new GuardCoverage(HookQuestions::PHPUNIT_GUARD_COVERAGE_MESSAGE_DEFAULT)
52
        );
53
    }
54
55
    /**
56
     * @test
57
     */
58 View Code Duplication
    public function itShouldWorksFine()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    {
60
        $outputMessage = new PreCommitOutputWriter(GuardCoverageToolHandler::CHECKING_MESSAGE);
61
        $currentCoverage = 70.00;
62
        $previousCoverage = 60.00;
63
64
        $this->shouldProcessStrictCoverage($currentCoverage);
65
        $this->shouldWriteOutput($outputMessage->getMessage());
66
        $this->shouldReadGuardCoverage($previousCoverage);
67
        $this->shouldWriteLnOutput(
68
            $this->buildStrictCoverageSuccessfulMessage(
69
                $currentCoverage,
70
                $previousCoverage,
71
                $outputMessage->getSuccessfulMessage()
72
            )
73
        );
74
        $this->shouldWriteGuardCoverage($currentCoverage);
75
76
        $this->guardCoverageToolCommandHandler->handle(
77
            new GuardCoverage(HookQuestions::PHPUNIT_GUARD_COVERAGE_MESSAGE_DEFAULT)
78
        );
79
    }
80
81
    private function buildStrictCoverageSuccessfulMessage($currentCoverage, $previousCoverage, $getSuccessfulMessage)
82
    {
83
        return $getSuccessfulMessage .
84
            ' <comment>[' .
85
            round($currentCoverage, 0) .
86
            '% >= ' .
87
            round($previousCoverage, 0) .
88
            '%]</comment>';
89
    }
90
}
91