/
PreexistingPHPCSInstalledPathsConfigTest.php
291 lines (255 loc) · 10.9 KB
/
PreexistingPHPCSInstalledPathsConfigTest.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
/**
* This file is part of the Dealerdirect PHP_CodeSniffer Standards
* Composer Installer Plugin package.
*
* @copyright 2022 PHPCodeSniffer Composer Installer Contributors
* @license MIT
*/
namespace Dealerdirect\Composer\Plugin\Installers\PHPCodeSniffer\Tests\IntegrationTest;
use Dealerdirect\Composer\Plugin\Installers\PHPCodeSniffer\Tests\PHPCSVersions;
use Dealerdirect\Composer\Plugin\Installers\PHPCodeSniffer\Tests\TestCase;
use RuntimeException;
/**
* Test correctly handling a pre-existing PHPCS configuration file, which includes a valid `installed_paths` setting.
*
* These tests verify:
* - That the plugin does not remove or alter any valid paths which already existed in `installed_paths`.
* - That the plugin removes invalid paths which already existed in `installed_paths`.
*
* Note: it is important to run these tests against multiple PHPCS versions, as the output returned
* by the `--config-show` command has changed across versions and we need to make sure that
* the plugin handles this correctly in all supported PHPCS versions.
*/
final class PreexistingPHPCSInstalledPathsConfigTest extends TestCase
{
private $tempExtraStndsSubdir = '/extrastnds/dummy-subdir';
private $tempExtraStndsPath;
private $composerConfig = array(
'name' => 'phpcs-composer-installer/preexisting-config-test',
'require-dev' => array(
'squizlabs/php_codesniffer' => null,
'dealerdirect/phpcodesniffer-composer-installer' => '*',
),
);
/**
* Set up test environment before each test.
*/
protected function set_up()
{
$this->createTestEnvironment();
/*
* Create an extra directory in the test environment, but outside of the directory used
* in the test (where Composer is run) and copy an external standard fixture to it.
*/
$this->tempExtraStndsPath = static::$tempDir . $this->tempExtraStndsSubdir;
// Create all needed subdirectories in one go.
$path = $this->tempExtraStndsPath . '/DummySubDir';
if (mkdir($path, 0766, true) === false || is_dir($path) === false) {
throw new RuntimeException("Failed to create the $path directory for the test");
}
// We only need the ruleset.xml file for the standard to be valid.
copy(
dirname(__DIR__) . '/fixtures/dummy-subdir/DummySubDir/ruleset.xml',
$this->tempExtraStndsPath . '/DummySubDir/ruleset.xml'
);
}
/**
* Clean up after each test.
*/
protected function tear_down()
{
$this->removeTestEnvironment();
}
/**
* Test correctly handling a pre-existing PHPCS configuration file which includes a pre-set,
* valid `installed_paths`.
*
* @dataProvider dataPHPCSVersions
*
* @param string $phpcsVersion PHPCS version to use in this test.
* This version is randomly selected from the PHPCS versions compatible
* with the PHP version used in the test.
*
* @return void
*/
public function testPreexistingValidInstalledPathsConfigIsKeptIntact($phpcsVersion)
{
$config = $this->composerConfig;
$config['require-dev']['squizlabs/php_codesniffer'] = $phpcsVersion;
$this->writeComposerJsonFile($config, static::$tempGlobalPath);
/*
* 1. Install PHPCS and the plugin.
*/
$this->assertExecute(
'composer global install',
0, // Expected exit code.
null, // No stdout expectation.
null, // No stderr expectation.
'Failed to install PHPCS.'
);
/*
* 2. Set the installed_paths and verify it is registered correctly.
*/
$command = sprintf(
'"vendor/bin/phpcs" --config-set installed_paths %s',
escapeshellarg($this->tempExtraStndsPath)
);
$result = $this->executeCliCommand($command, static::$tempGlobalPath);
$this->assertSame(
0,
$result['exitcode'],
'Exitcode for "phpcs --config-set installed_paths" did not match 0'
);
// Verify that the config contains the newly set value.
$result = $this->executeCliCommand('"vendor/bin/phpcs" --config-show', static::$tempGlobalPath);
$this->assertSame(0, $result['exitcode'], 'Exitcode for "phpcs --config-show" did not match 0 (first run)');
$expected = array(
$this->tempExtraStndsPath,
);
$this->assertSame(
$expected,
$this->configShowToPathsArray($result['stdout']),
'PHPCS configuration does not show the manually set installed_paths correctly'
);
/*
* 3. Install an external standard.
*/
$command = 'composer global require --dev phpcs-composer-installer/dummy-subdir --no-ansi -v';
$this->assertExecute(
$command,
0, // Expected exit code.
'PHP CodeSniffer Config installed_paths set to ', // Expectation for stdout.
null, // No stderr expectation.
'Failed to install Dummy subdir standard.'
);
// Verify that the originally set path is retained and the new standard is registered correctly as well.
$result = $this->executeCliCommand('"vendor/bin/phpcs" --config-show', static::$tempGlobalPath);
$this->assertSame(0, $result['exitcode'], 'Exitcode for "phpcs --config-show" did not match 0 (second run)');
$expected = array(
$this->tempExtraStndsPath,
'/phpcs-composer-installer/dummy-subdir',
);
sort($expected, \SORT_NATURAL);
$this->assertSame(
$expected,
$this->configShowToPathsArray($result['stdout']),
'Paths as updated by the plugin does not contain the expected paths'
);
}
/**
* Test correctly handling a pre-existing PHPCS configuration file which includes both
* a pre-set, valid path, as well as an invalid path in `installed_paths`.
*
* @dataProvider dataPHPCSVersions
*
* @param string $phpcsVersion PHPCS version to use in this test.
* This version is randomly selected from the PHPCS versions compatible
* with the PHP version used in the test.
*
* @return void
*/
public function testPreexistingInvalidInstalledPathsConfigIsRemoved($phpcsVersion)
{
$config = $this->composerConfig;
$config['require-dev']['squizlabs/php_codesniffer'] = $phpcsVersion;
$this->writeComposerJsonFile($config, static::$tempLocalPath);
/*
* 1. Install PHPCS and the plugin.
*/
$this->assertExecute(
sprintf('composer install -v --working-dir=%s', escapeshellarg(static::$tempLocalPath)),
0, // Expected exit code.
null, // No stdout expectation.
null, // No stderr expectation.
'Failed to install PHPCS.'
);
/*
* 2. Set the installed_paths and verify it is registered correctly.
*/
$command = sprintf(
'"vendor/bin/phpcs" --config-set installed_paths %s',
escapeshellarg($this->tempExtraStndsPath)
);
$result = $this->executeCliCommand($command, static::$tempLocalPath);
$this->assertSame(
0,
$result['exitcode'],
'Exitcode for "phpcs --config-set installed_paths" did not match 0'
);
/*
* Manipulate the value of installed_paths as registered in PHPCS.
*
* Note: for the test we do this "manually". In real life, this may be a standard which
* used to be installed, but was removed without the installed_paths having been updated
* in PHPCS (prior to the plugin being used).
*
* Also note: depending on the OS and the PHP version, passing an invalid path to `--config-set`
* will error on an exception from the DirectoryIterator as used by PHPCS itself.
* The manual setting prevents this exception, but still allows us to test this use-case.
*/
$confFile = static::$tempLocalPath . '/vendor/squizlabs/php_codesniffer/CodeSniffer.conf';
$confContents = file_get_contents($confFile);
$this->assertNotFalse($confContents);
$confContents = str_replace(
$this->tempExtraStndsSubdir,
$this->tempExtraStndsSubdir . ',path/to/somecloned-stnd',
$confContents
);
$this->assertNotFalse(file_put_contents($confFile, $confContents));
// Verify that the config contains the newly set value.
$result = $this->executeCliCommand('"vendor/bin/phpcs" --config-show', static::$tempLocalPath);
$this->assertSame(0, $result['exitcode'], 'Exitcode for "phpcs --config-show" did not match 0 (first run)');
$expected = array(
$this->tempExtraStndsPath,
'path/to/somecloned-stnd',
);
sort($expected, \SORT_NATURAL);
$this->assertSame(
$expected,
$this->configShowToPathsArray($result['stdout']),
'PHPCS configuration does not show the manually set installed_paths correctly'
);
/*
* 3. Install an external standard.
*/
$command = sprintf(
'composer require --dev phpcs-composer-installer/dummy-subdir --no-ansi -v --working-dir=%s',
escapeshellarg(static::$tempLocalPath)
);
$this->assertExecute(
$command,
0, // Expected exit code.
'PHP CodeSniffer Config installed_paths set to ', // Expectation for stdout.
null, // No stderr expectation.
'Failed to install Dummy subdir standard.'
);
/*
* Verify that the valid preset path is retained, that the invalid path is removed
* and the new standard is registered correctly.
*/
$result = $this->executeCliCommand('"vendor/bin/phpcs" --config-show', static::$tempLocalPath);
$this->assertSame(0, $result['exitcode'], 'Exitcode for "phpcs --config-show" did not match 0 (second run)');
$expected = array(
$this->tempExtraStndsPath,
'/phpcs-composer-installer/dummy-subdir',
);
sort($expected, \SORT_NATURAL);
$this->assertSame(
$expected,
$this->configShowToPathsArray($result['stdout']),
'Paths as updated by the plugin does not contain the expected paths'
);
}
/**
* Data provider.
*
* @return array
*/
public function dataPHPCSVersions()
{
// Test against the highest and lowest supported PHPCS version for each major + `master` + PHPCS 4.x dev.
$versions = PHPCSVersions::getHighLowEachMajor(true, true);
return PHPCSVersions::toDataprovider($versions);
}
}