Completed
Push — master ( c67234...8b2a99 )
by Pablo
10s
created

GuardCoverageToolCommandHandlerTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 77
Duplicated Lines 22.08 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 4
c 3
b 0
f 1
lcom 1
cbo 5
dl 17
loc 77
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 11 1
A itShouldShowWarningMessage() 0 23 1
A itShouldWorksFine() 17 17 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\GuardCoverageCommand;
8
use PhpGitHooks\Module\PhpUnit\Contract\CommandHandler\GuardCoverageToolCommandHandler;
9
use PhpGitHooks\Module\PhpUnit\Service\GuardCoverageTool;
10
use PhpGitHooks\Module\PhpUnit\Tests\Infrastructure\PhpUnitUnitTestCase;
11
12
class GuardCoverageToolCommandHandlerTest extends PhpUnitUnitTestCase
13
{
14
    /**
15
     * @var GuardCoverageToolCommandHandler
16
     */
17
    private $guardCoverageToolCommandHandler;
18
19
    protected function setUp()
20
    {
21
        $this->guardCoverageToolCommandHandler = new GuardCoverageToolCommandHandler(
22
            new GuardCoverageTool(
23
                $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\PhpUn...rageTool::__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...
24
                $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...rageTool::__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...
25
                $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...rageTool::__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...
26
                $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...rageTool::__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...
27
            )
28
        );
29
    }
30
31
    /**
32
     * @test
33
     */
34
    public function itShouldShowWarningMessage()
35
    {
36
        $outputMessage = new PreCommitOutputWriter(GuardCoverageTool::CHECKING_MESSAGE);
37
        $currentCoverage = 60.00;
38
        $previousCoverage = 70.00;
39
40
        $this->shouldProcessStrictCoverage($currentCoverage);
41
        $this->shouldWriteOutput($outputMessage->getMessage());
42
        $this->shouldReadGuardCoverage($previousCoverage);
43
        $this->shouldWriteLnOutput(
44
            sprintf(
45
                "\n<bg=yellow;options=bold>%s Previous coverage %s, current coverage %s.</>",
46
                HookQuestions::PHPUNIT_GUARD_COVERAGE_MESSAGE_DEFAULT,
47
                $previousCoverage,
48
                $currentCoverage
49
            )
50
        );
51
        $this->shouldWriteGuardCoverage($currentCoverage);
52
53
        $this->guardCoverageToolCommandHandler->handle(
54
            new GuardCoverageCommand(HookQuestions::PHPUNIT_GUARD_COVERAGE_MESSAGE_DEFAULT)
55
        );
56
    }
57
58
    /**
59
     * @test
60
     */
61 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...
62
    {
63
        $outputMessage = new PreCommitOutputWriter(GuardCoverageTool::CHECKING_MESSAGE);
64
        $currentCoverage = 70.00;
65
        $previousCoverage = 60.00;
66
67
        $this->shouldProcessStrictCoverage($currentCoverage);
68
        $this->shouldWriteOutput($outputMessage->getMessage());
69
        $this->shouldReadGuardCoverage($previousCoverage);
70
        $this->shouldWriteLnOutput($this->buildStrictCoverageSuccessfulMessage($currentCoverage, $previousCoverage,
71
            $outputMessage->getSuccessfulMessage()));
72
        $this->shouldWriteGuardCoverage($currentCoverage);
73
74
        $this->guardCoverageToolCommandHandler->handle(
75
            new GuardCoverageCommand(HookQuestions::PHPUNIT_GUARD_COVERAGE_MESSAGE_DEFAULT)
76
        );
77
    }
78
79
    private function buildStrictCoverageSuccessfulMessage($currentCoverage, $previousCoverage, $getSuccessfulMessage)
80
    {
81
        return $getSuccessfulMessage .
82
        ' <comment>[' .
83
        round($currentCoverage, 0) .
84
        '% >= ' .
85
        round($previousCoverage, 0) .
86
        '%]</comment>';
87
    }
88
}
89