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() |
|
|
|
|
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() |
|
|
|
|
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() |
|
|
|
|
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'); |
|
|
|
|
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
|
|
|
|
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.