forked from Incenteev/ParameterHandler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProcessorTest.php
179 lines (146 loc) · 5.64 KB
/
ProcessorTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
namespace Incenteev\ParameterHandler\Tests;
use Incenteev\ParameterHandler\Processor;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Yaml\Yaml;
class ProcessorTest extends TestCase
{
private $io;
private $environmentBackup = array();
/**
* @var Processor
*/
private $processor;
protected function setUp(): void
{
parent::setUp();
$this->io = $this->prophesize('Composer\IO\IOInterface');
$this->processor = new Processor($this->io->reveal());
}
protected function tearDown(): void
{
parent::tearDown();
foreach ($this->environmentBackup as $var => $value) {
if (false === $value) {
putenv($var);
} else {
putenv($var.'='.$value);
}
}
}
/**
* @dataProvider provideInvalidConfiguration
*/
public function testInvalidConfiguration(array $config, $exceptionMessage)
{
chdir(__DIR__);
if (method_exists($this, 'expectException')) {
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage($exceptionMessage);
} else {
$this->setExpectedException('InvalidArgumentException', $exceptionMessage);
}
$this->processor->processFile($config);
}
public function provideInvalidConfiguration()
{
return array(
'no file' => array(
array(),
'The extra.incenteev-parameters.file setting is required to use this script handler.',
),
'missing default dist file' => array(
array(
'file' => 'fixtures/invalid/missing.yml',
),
'The dist file "fixtures/invalid/missing.yml.dist" does not exist. Check your dist-file config or create it.',
),
'missing custom dist file' => array(
array(
'file' => 'fixtures/invalid/missing.yml',
'dist-file' => 'fixtures/invalid/non-existent.dist.yml',
),
'The dist file "fixtures/invalid/non-existent.dist.yml" does not exist. Check your dist-file config or create it.',
),
'missing top level key in dist file' => array(
array(
'file' => 'fixtures/invalid/missing_top_level.yml',
),
'The top-level key parameters is missing.',
),
'invalid values in the existing file' => array(
array(
'file' => 'fixtures/invalid/invalid_existing_values.yml',
),
'The existing "fixtures/invalid/invalid_existing_values.yml" file does not contain an array',
),
);
}
/**
* @dataProvider provideParameterHandlingTestCases
*/
public function testParameterHandling($testCaseName)
{
$dataDir = __DIR__.'/fixtures/testcases/'.$testCaseName;
$testCase = array_replace_recursive(
array(
'title' => 'unknown test',
'config' => array(
'file' => 'parameters.yml',
),
'dist-file' => 'parameters.yml.dist',
'environment' => array(),
'interactive' => false,
),
(array) Yaml::parse(file_get_contents($dataDir.'/setup.yml'))
);
$workingDir = sys_get_temp_dir() . '/incenteev_parameter_handler';
$exists = $this->initializeTestCase($testCase, $dataDir, $workingDir);
$message = sprintf('<info>%s the "%s" file</info>', $exists ? 'Updating' : 'Creating', $testCase['config']['file']);
$this->io->write($message)->shouldBeCalled();
$this->setInteractionExpectations($testCase);
$this->processor->processFile($testCase['config']);
$this->assertFileEquals($dataDir.'/expected.yml', $workingDir.'/'.$testCase['config']['file'], $testCase['title']);
}
private function initializeTestCase(array $testCase, $dataDir, $workingDir)
{
$fs = new Filesystem();
if (is_dir($workingDir)) {
$fs->remove($workingDir);
}
$fs->copy($dataDir.'/dist.yml', $workingDir.'/'. $testCase['dist-file']);
if ($exists = file_exists($dataDir.'/existing.yml')) {
$fs->copy($dataDir.'/existing.yml', $workingDir.'/'.$testCase['config']['file']);
}
foreach ($testCase['environment'] as $var => $value) {
$this->environmentBackup[$var] = getenv($var);
putenv($var.'='.$value);
};
chdir($workingDir);
return $exists;
}
private function setInteractionExpectations(array $testCase)
{
$this->io->isInteractive()->willReturn($testCase['interactive']);
if (!$testCase['interactive']) {
return;
}
if (!empty($testCase['requested_params'])) {
$this->io->write('<comment>Some parameters are missing. Please provide them.</comment>')->shouldBeCalledTimes(1);
}
foreach ($testCase['requested_params'] as $param => $settings) {
$this->io->ask(sprintf('<question>%s</question> (<comment>%s</comment>): ', $param, $settings['default']), $settings['default'])
->willReturn($settings['input'])
->shouldBeCalled();
}
}
public function provideParameterHandlingTestCases()
{
$tests = array();
foreach (glob(__DIR__.'/fixtures/testcases/*/') as $folder) {
$tests[] = array(basename($folder));
}
return $tests;
}
}