1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpGitHooks\Application\PhpUnit; |
4
|
|
|
|
5
|
|
|
use PhpGitHooks\Application\Config\AbstractToolConfigData; |
6
|
|
|
|
7
|
|
|
final class PhpUnitConfigData extends AbstractToolConfigData |
8
|
|
|
{ |
9
|
|
|
const TOOL = 'phpunit'; |
10
|
|
|
const ENABLED_KEY = 'enabled'; |
11
|
|
|
const RANDOM_MODE_KEY = 'random-mode'; |
12
|
|
|
const SUITE_KEY = 'suite'; |
13
|
|
|
const DEFAULT_ANSWER = 'Y'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param array $data |
17
|
|
|
* |
18
|
|
|
* @return array |
19
|
|
|
* |
20
|
|
|
* @throws PhpUnitConfigDataException |
21
|
|
|
*/ |
22
|
7 |
|
public function createConfigData(array $data) |
23
|
|
|
{ |
24
|
7 |
|
$this->configData = $data; |
25
|
7 |
|
$this->setEnabled(); |
26
|
5 |
|
$this->setRandomizerOption(); |
27
|
5 |
|
$this->setTestSuiteOption(); |
28
|
|
|
|
29
|
5 |
|
return $this->configData[self::TOOL]; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @throws PhpUnitConfigDataException |
34
|
|
|
*/ |
35
|
5 |
|
private function checkConfigData() |
36
|
|
|
{ |
37
|
5 |
|
$configData = $this->configData[self::TOOL]; |
38
|
5 |
|
if (!is_array($configData)) { |
39
|
1 |
|
throw new PhpUnitConfigDataException(); |
40
|
|
|
} |
41
|
|
|
|
42
|
4 |
|
if (!isset($configData[self::ENABLED_KEY])) { |
43
|
1 |
|
throw new PhpUnitConfigDataException(); |
44
|
|
|
} |
45
|
3 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
5 |
|
protected function getToolName() |
51
|
|
|
{ |
52
|
5 |
|
return self::TOOL; |
53
|
|
|
} |
54
|
|
|
|
55
|
5 |
|
private function setTestSuiteOption() |
56
|
|
|
{ |
57
|
5 |
|
if (!isset($this->configData[self::TOOL][self::SUITE_KEY])) { |
58
|
4 |
|
$answer = $this |
59
|
4 |
|
->setQuestion( |
60
|
4 |
|
sprintf('Do you want run %s tool for a specific test suite?', strtoupper(self::TOOL)), |
61
|
4 |
|
self::DEFAULT_ANSWER, |
62
|
|
|
'Y/n' |
63
|
4 |
|
); |
64
|
4 |
|
$answer = self::DEFAULT_ANSWER === strtoupper($answer) ? true : false; |
65
|
4 |
|
if ($answer) { |
66
|
3 |
|
$answer = $this |
67
|
3 |
|
->setQuestion( |
68
|
3 |
|
sprintf('Name the specific test suite to be run in %s', strtoupper(self::TOOL)), |
69
|
3 |
|
'PhpGitHooks Unit Tests', |
70
|
|
|
'PhpGitHooks Unit Tests' |
71
|
3 |
|
); |
72
|
3 |
|
} |
73
|
4 |
|
} else { |
74
|
1 |
|
$answer = $this->configData[self::TOOL][self::SUITE_KEY]; |
75
|
|
|
} |
76
|
|
|
|
77
|
5 |
|
$this->configData[self::TOOL][self::SUITE_KEY] = $answer; |
78
|
5 |
|
} |
79
|
|
|
|
80
|
5 |
|
private function setRandomizerOption() |
81
|
|
|
{ |
82
|
5 |
|
if (!isset($this->configData[self::TOOL][self::RANDOM_MODE_KEY])) { |
83
|
2 |
|
$answer = $this |
84
|
2 |
|
->setQuestion( |
85
|
2 |
|
sprintf('Do you want run %s tool in randomize mode?', strtoupper(self::TOOL)), |
86
|
2 |
|
self::DEFAULT_ANSWER, |
87
|
|
|
'Y/n' |
88
|
2 |
|
); |
89
|
2 |
|
$answer = self::DEFAULT_ANSWER === strtoupper($answer) ? true : false; |
90
|
2 |
|
} else { |
91
|
3 |
|
$answer = $this->configData[self::TOOL][self::RANDOM_MODE_KEY]; |
92
|
|
|
} |
93
|
|
|
|
94
|
5 |
|
$this->configData[self::TOOL][self::RANDOM_MODE_KEY] = $answer; |
95
|
5 |
|
} |
96
|
|
|
|
97
|
7 |
View Code Duplication |
private function setEnabled() |
|
|
|
|
98
|
|
|
{ |
99
|
7 |
|
if (!isset($this->configData[self::TOOL])) { |
100
|
2 |
|
$answer = $this->setQuestion( |
101
|
2 |
|
sprintf('Do you want enable %s tool: ', strtoupper(self::TOOL)), |
102
|
2 |
|
self::DEFAULT_ANSWER, |
103
|
|
|
'Y/n' |
104
|
2 |
|
); |
105
|
2 |
|
$answer = self::DEFAULT_ANSWER === strtoupper($answer) ? true : false; |
106
|
2 |
|
} else { |
107
|
5 |
|
$this->checkConfigData(); |
108
|
3 |
|
$answer = $this->configData[self::TOOL][self::ENABLED_KEY]; |
109
|
|
|
} |
110
|
|
|
|
111
|
5 |
|
$this->enableTool($answer); |
112
|
5 |
|
} |
113
|
|
|
} |
114
|
|
|
|
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.