1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Incenteev\ParameterHandler\Tests; |
4
|
|
|
|
5
|
|
|
use Incenteev\ParameterHandler\Processor; |
6
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
7
|
|
|
use Symfony\Component\Yaml\Yaml; |
8
|
|
|
|
9
|
|
|
class ProcessorTest extends \PHPUnit_Framework_TestCase |
10
|
|
|
{ |
11
|
|
|
private $io; |
12
|
|
|
private $environmentBackup = array(); |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var Processor |
16
|
|
|
*/ |
17
|
|
|
private $processor; |
18
|
|
|
|
19
|
|
|
protected function setUp() |
20
|
|
|
{ |
21
|
|
|
parent::setUp(); |
22
|
|
|
|
23
|
|
|
$this->io = $this->prophesize('Composer\IO\IOInterface'); |
24
|
|
|
$this->processor = new Processor($this->io->reveal()); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
protected function tearDown() |
28
|
|
|
{ |
29
|
|
|
parent::tearDown(); |
30
|
|
|
|
31
|
|
|
foreach ($this->environmentBackup as $var => $value) { |
32
|
|
|
if (false === $value) { |
33
|
|
|
putenv($var); |
34
|
|
|
} else { |
35
|
|
|
putenv($var.'='.$value); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @dataProvider provideInvalidConfiguration |
42
|
|
|
*/ |
43
|
|
|
public function testInvalidConfiguration(array $config, $exceptionMessage) |
44
|
|
|
{ |
45
|
|
|
chdir(__DIR__); |
46
|
|
|
|
47
|
|
|
$this->setExpectedException('InvalidArgumentException', $exceptionMessage); |
|
|
|
|
48
|
|
|
|
49
|
|
|
$this->processor->processFile($config); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function provideInvalidConfiguration() |
53
|
|
|
{ |
54
|
|
|
return array( |
55
|
|
|
'yml: no file' => array( |
56
|
|
|
array(), |
57
|
|
|
'The extra.incenteev-parameters.file setting is required to use this script handler.', |
58
|
|
|
), |
59
|
|
|
'yml: no file type' => array( |
60
|
|
|
array( |
61
|
|
|
'file' => 'fixtures/existent/yml/dist.yml', |
62
|
|
|
), |
63
|
|
|
'The extra.incenteev-parameters.file-type setting is required to use this script handler.', |
64
|
|
|
), |
65
|
|
|
'yml: missing default dist file' => array( |
66
|
|
|
array( |
67
|
|
|
'file' => 'fixtures/invalid/yml/missing.yml', |
68
|
|
|
'file-type' => 'yml', |
69
|
|
|
), |
70
|
|
|
'The dist file "fixtures/invalid/yml/missing.yml.dist" does not exist. Check your dist-file config or create it.', |
71
|
|
|
), |
72
|
|
|
'yml: missing custom dist file' => array( |
73
|
|
|
array( |
74
|
|
|
'file' => 'fixtures/invalid/yml/missing.yml', |
75
|
|
|
'dist-file' => 'fixtures/invalid/yml/non-existent.dist.yml', |
76
|
|
|
'file-type' => 'yml', |
77
|
|
|
), |
78
|
|
|
'The dist file "fixtures/invalid/yml/non-existent.dist.yml" does not exist. Check your dist-file config or create it.', |
79
|
|
|
), |
80
|
|
|
'yml: missing top level key in dist file' => array( |
81
|
|
|
array( |
82
|
|
|
'file' => 'fixtures/invalid/yml/missing_top_level.yml', |
83
|
|
|
'file-type' => 'yml', |
84
|
|
|
), |
85
|
|
|
'The top-level key parameters is missing.', |
86
|
|
|
), |
87
|
|
|
'yml: invalid values in the existing file' => array( |
88
|
|
|
array( |
89
|
|
|
'file' => 'fixtures/invalid/yml/invalid_existing_values.yml', |
90
|
|
|
'file-type' => 'yml', |
91
|
|
|
), |
92
|
|
|
'The existing "fixtures/invalid/yml/invalid_existing_values.yml" file does not contain an array', |
93
|
|
|
), |
94
|
|
|
'php: no file' => array( |
95
|
|
|
array(), |
96
|
|
|
'The extra.incenteev-parameters.file setting is required to use this script handler.', |
97
|
|
|
), |
98
|
|
|
'php: no file type' => array( |
99
|
|
|
array( |
100
|
|
|
'file' => 'fixtures/existent/php/dist.php', |
101
|
|
|
), |
102
|
|
|
'The extra.incenteev-parameters.file-type setting is required to use this script handler.', |
103
|
|
|
), |
104
|
|
|
'php: missing default dist file' => array( |
105
|
|
|
array( |
106
|
|
|
'file' => 'fixtures/invalid/php/missing.php', |
107
|
|
|
'file-type' => 'php', |
108
|
|
|
), |
109
|
|
|
'The dist file "fixtures/invalid/php/missing.php.dist" does not exist. Check your dist-file config or create it.', |
110
|
|
|
), |
111
|
|
|
'php: missing custom dist file' => array( |
112
|
|
|
array( |
113
|
|
|
'file' => 'fixtures/invalid/php/missing.php', |
114
|
|
|
'dist-file' => 'fixtures/invalid/php/non-existent.dist.php', |
115
|
|
|
'file-type' => 'php', |
116
|
|
|
), |
117
|
|
|
'The dist file "fixtures/invalid/php/non-existent.dist.php" does not exist. Check your dist-file config or create it.', |
118
|
|
|
), |
119
|
|
|
'php: missing top level key in dist file' => array( |
120
|
|
|
array( |
121
|
|
|
'file' => 'fixtures/invalid/php/missing_top_level.php', |
122
|
|
|
'file-type' => 'php', |
123
|
|
|
), |
124
|
|
|
'The top-level key parameters is missing.', |
125
|
|
|
), |
126
|
|
|
'php: invalid values in the existing file' => array( |
127
|
|
|
array( |
128
|
|
|
'file' => 'fixtures/invalid/php/invalid_existing_values.php', |
129
|
|
|
'file-type' => 'php', |
130
|
|
|
), |
131
|
|
|
'The existing "fixtures/invalid/php/invalid_existing_values.php" file does not contain an array', |
132
|
|
|
), |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @dataProvider provideParameterHandlingTestCases |
138
|
|
|
*/ |
139
|
|
|
public function testParameterHandling($testCaseName, $fileType) |
140
|
|
|
{ |
141
|
|
|
$dataDir = __DIR__.'/fixtures/testcases/'. $fileType . '/' .$testCaseName; |
142
|
|
|
|
143
|
|
|
$testCase = array_replace_recursive( |
144
|
|
|
array( |
145
|
|
|
'title' => 'unknown test', |
146
|
|
|
'config' => array( |
147
|
|
|
'file' => 'parameters.' . $fileType, |
148
|
|
|
'file-type' => $fileType, |
149
|
|
|
), |
150
|
|
|
'dist-file' => 'parameters.'. $fileType .'.dist', |
151
|
|
|
'environment' => array(), |
152
|
|
|
'interactive' => false, |
153
|
|
|
), |
154
|
|
|
(array) Yaml::parse(file_get_contents($dataDir.'/setup.yml')) |
155
|
|
|
); |
156
|
|
|
|
157
|
|
|
$workingDir = sys_get_temp_dir() . '/incenteev_parameter_handler'; |
158
|
|
|
$exists = $this->initializeTestCase($testCase, $dataDir, $workingDir, $fileType); |
159
|
|
|
|
160
|
|
|
$message = sprintf('<info>%s the "%s" file</info>', $exists ? 'Updating' : 'Creating', $testCase['config']['file']); |
161
|
|
|
$this->io->write($message)->shouldBeCalled(); |
162
|
|
|
|
163
|
|
|
$this->setInteractionExpectations($testCase); |
164
|
|
|
|
165
|
|
|
$this->processor->processFile($testCase['config']); |
166
|
|
|
|
167
|
|
|
$this->assertFileEquals($dataDir.'/expected.' . $fileType, $workingDir.'/'.$testCase['config']['file'], $testCase['title']); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
private function initializeTestCase(array $testCase, $dataDir, $workingDir, $fileType) |
171
|
|
|
{ |
172
|
|
|
$fs = new Filesystem(); |
173
|
|
|
|
174
|
|
|
if (is_dir($workingDir)) { |
175
|
|
|
$fs->remove($workingDir); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$fs->copy($dataDir.'/dist.' . $fileType, $workingDir.'/'. $testCase['dist-file']); |
179
|
|
|
|
180
|
|
|
if ($exists = file_exists($dataDir.'/existing.' . $fileType)) { |
181
|
|
|
$fs->copy($dataDir.'/existing.' . $fileType, $workingDir.'/'.$testCase['config']['file']); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
foreach ($testCase['environment'] as $var => $value) { |
185
|
|
|
$this->environmentBackup[$var] = getenv($var); |
186
|
|
|
putenv($var.'='.$value); |
187
|
|
|
}; |
188
|
|
|
|
189
|
|
|
chdir($workingDir); |
190
|
|
|
|
191
|
|
|
return $exists; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
private function setInteractionExpectations(array $testCase) |
195
|
|
|
{ |
196
|
|
|
$this->io->isInteractive()->willReturn($testCase['interactive']); |
197
|
|
|
|
198
|
|
|
if (!$testCase['interactive']) { |
199
|
|
|
return; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
if (!empty($testCase['requested_params'])) { |
203
|
|
|
$this->io->write('<comment>Some parameters are missing. Please provide them.</comment>')->shouldBeCalledTimes(1); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
foreach ($testCase['requested_params'] as $param => $settings) { |
207
|
|
|
$this->io->ask(sprintf('<question>%s</question> (<comment>%s</comment>): ', $param, $settings['default']), $settings['default']) |
208
|
|
|
->willReturn($settings['input']) |
209
|
|
|
->shouldBeCalled(); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function provideParameterHandlingTestCases() |
214
|
|
|
{ |
215
|
|
|
$tests = array(); |
216
|
|
|
|
217
|
|
View Code Duplication |
foreach (glob(__DIR__.'/fixtures/testcases/yml/*/') as $folder) { |
|
|
|
|
218
|
|
|
$tests[] = array(basename($folder), 'yml'); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
View Code Duplication |
foreach (glob(__DIR__.'/fixtures/testcases/php/*/') as $folder) { |
|
|
|
|
222
|
|
|
$tests[] = array(basename($folder), 'php'); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
return $tests; |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.