Completed
Push — master ( f87faf...b3e46e )
by Pablo
03:15
created

PreCommitConfigTest::getMessagesIsNotNull()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 1
eloc 11
nc 1
nop 0
1
<?php
2
3
namespace PhpGitHooks\Tests\Infrastructure\Config;
4
5
use PhpGitHooks\Application\Config\PreCommitConfig;
6
use PhpGitHooks\Infrastructure\Disk\Config\InMemoryConfigFileReader;
7
use Symfony\Component\Config\Definition\Exception\Exception;
8
9
/**
10
 * Class PreCommitConfigTest.
11
 */
12
class PreCommitConfigTest extends \PHPUnit_Framework_TestCase
13
{
14
    const RIGHT_MESSAGE_KEY = 'right-message';
15
    const ERROR_MESSAGE_KEY = 'error-message';
16
    const RIGHT_MESSAGE = 'ok';
17
    const ERROR_MESSAGE = 'error';
18
    /** @var  PreCommitConfig */
19
    private $preCommitConfig;
20
    /** @var  InMemoryConfigFileReader */
21
    private $configFileReader;
22
23
    protected function setUp()
24
    {
25
        $this->configFileReader = new InMemoryConfigFileReader();
26
        $this->preCommitConfig = new PreCommitConfig($this->configFileReader);
27
    }
28
29
    /**
30
     * @test
31
     */
32 View Code Duplication
    public function invalidConfigData()
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...
33
    {
34
        $data = [
35
            'pre-commit' => [
36
                'execute' => ['phpunit' => 'dfaa'],
37
            ],
38
        ];
39
40
        $this->configFileReader->fileContents = $data;
41
42
        $this->assertFalse($this->preCommitConfig->isEnabled('phpunit'));
43
    }
44
45
    /**
46
     * @test
47
     */
48 View Code Duplication
    public function serviceNameNotExists()
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...
49
    {
50
        $data = [
51
            'pre-commit' => [
52
                'execute' => ['phpunit' => true],
53
            ],
54
        ];
55
56
        $this->configFileReader->fileContents = $data;
57
58
        $this->assertFalse($this->preCommitConfig->isEnabled('serviceName'));
59
    }
60
61
    /**
62
     * @test
63
     */
64 View Code Duplication
    public function serviceIsEnabled()
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...
65
    {
66
        $data = [
67
            'pre-commit' => [
68
                'execute' => ['phpunit' => true],
69
            ],
70
        ];
71
72
        $this->configFileReader->fileContents = $data;
73
74
        $this->assertTrue($this->preCommitConfig->isEnabled('phpunit'));
75
    }
76
77
    /**
78
     * @test
79
     */
80
    public function noServiceDataThrowsException()
81
    {
82
        $this->setExpectedException(Exception::class);
83
84
        $this->configFileReader->fileContents = [];
85
86
        $this->preCommitConfig->isEnabled('phpunit');
87
    }
88
89
    /**
90
     * @test
91
     */
92
    public function getExtraOptions()
93
    {
94
        $this->configFileReader->fileContents = [
95
            'pre-commit' => [
96
                'execute' => [
97
                    'php-cs-fixer' => [
98
                        'enabled' => true,
99
                        'level' => 'psr0',
100
                    ],
101
                ],
102
            ],
103
        ];
104
105
        $this->preCommitConfig->extraOptions('php-cs-fixer');
0 ignored issues
show
Documentation introduced by
'php-cs-fixer' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
106
    }
107
108
    /**
109
     * @test
110
     */
111
    public function getMessagesIsNotNull()
112
    {
113
        $this->configFileReader->fileContents = [
114
            'pre-commit' => [
115
                'message' => [
116
                        self::RIGHT_MESSAGE_KEY => self::RIGHT_MESSAGE,
117
                        self::ERROR_MESSAGE_KEY => self::ERROR_MESSAGE,
118
                    ],
119
            ],
120
        ];
121
122
        $messages = $this->preCommitConfig->getMessages();
123
124
        $this->assertArrayHasKey(self::RIGHT_MESSAGE_KEY, $messages);
125
        $this->assertArrayHasKey(self::ERROR_MESSAGE_KEY, $messages);
126
        $this->assertSame(self::RIGHT_MESSAGE, $messages[self::RIGHT_MESSAGE_KEY]);
127
        $this->assertSame(self::ERROR_MESSAGE, $messages[self::ERROR_MESSAGE_KEY]);
128
    }
129
}
130