/
RegisterExternalStandardsTest.php
343 lines (296 loc) · 12.8 KB
/
RegisterExternalStandardsTest.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<?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;
/**
* Test registering external standards.
*/
final class RegisterExternalStandardsTest extends TestCase
{
private $configOneStandard = array(
'name' => 'phpcs-composer-installer/register-external-stnds-one-stnd',
'require-dev' => array(
'squizlabs/php_codesniffer' => null,
'phpcs-composer-installer/dummy-subdir' => '*',
'dealerdirect/phpcodesniffer-composer-installer' => '*',
),
);
private $configOneStandardMultiRuleset = array(
'name' => 'phpcs-composer-installer/register-external-stnds-multistnd',
'require-dev' => array(
'squizlabs/php_codesniffer' => null,
'phpcs-composer-installer/multistandard' => '*',
),
);
private $configOneStandardInSrcSubdir = array(
'name' => 'phpcs-composer-installer/register-external-stnds-in-src-subdir',
'require-dev' => array(
'squizlabs/php_codesniffer' => null,
'phpcs-composer-installer/dummy-src' => '*',
),
);
/**
* Set up test environment before each test.
*/
protected function set_up()
{
$this->createTestEnvironment();
}
/**
* Clean up after each test.
*/
protected function tear_down()
{
$this->removeTestEnvironment();
}
/**
* Test registering one external standard for a Composer GLOBAL install.
*
* @dataProvider dataRegisterOneStandard
*
* @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 testRegisterOneStandardGlobal($phpcsVersion)
{
$config = $this->configOneStandard;
$config['require-dev']['squizlabs/php_codesniffer'] = $phpcsVersion;
$this->writeComposerJsonFile($config, static::$tempGlobalPath);
$this->assertComposerValidates(static::$tempGlobalPath);
// Install the dependencies.
$this->assertExecute(
'composer global install --no-plugins',
0, // Expected exit code.
null, // No stdout expectation.
null, // No stderr expectation.
'Failed to install dependencies.'
);
// Verify that the standard registers correctly.
$installResult = $this->executeCliCommand('composer global install-codestandards --no-ansi');
$this->assertSame(0, $installResult['exitcode'], 'Exitcode for install-codestandards did not match 0');
$this->assertMatchesRegularExpression(
'`^PHP CodeSniffer Config installed_paths set to [^\s]+/dummy-subdir$`',
trim($installResult['stdout']),
'Installing the standards failed.'
);
// Make sure the CodeSniffer.conf file has been created.
$this->assertFileExists(
static::$tempGlobalPath . '/vendor/squizlabs/php_codesniffer/CodeSniffer.conf'
);
// Verify that PHPCS sees the external standard.
$this->assertExecute(
'"vendor/bin/phpcs" -i',
0, // Expected exit code.
'and DummySubDir', // Expected stdout.
'', // Empty stderr expectation.
'Running phpcs -i failed.',
static::$tempGlobalPath
);
// Verify that PHPCS can with the external standard set as the standard.
$phpcsCommand = '"vendor/bin/phpcs" --standard=DummySubDir -e';
$phpcsResult = $this->executeCliCommand($phpcsCommand, static::$tempGlobalPath);
$this->assertSame(0, $phpcsResult['exitcode'], 'Exitcode for PHPCS explain did not match 0');
$this->assertMatchesRegularExpression(
'`DummySubDir \(1 sniffs?\)\s+[-]+\s+DummySubDir\.Demo\.Demo(?:[\r\n]+|$)`',
$phpcsResult['stdout'],
'Output of the PHPCS explain command did not match the expectation.'
);
}
/**
* Test registering one external standard for a Composer LOCAL install.
*
* @dataProvider dataRegisterOneStandard
*
* @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 testRegisterOneStandardLocal($phpcsVersion)
{
$config = $this->configOneStandard;
$config['require-dev']['squizlabs/php_codesniffer'] = $phpcsVersion;
$this->writeComposerJsonFile($config, static::$tempLocalPath);
$this->assertComposerValidates(static::$tempLocalPath);
// Install the dependencies.
$this->assertExecute(
sprintf('composer install --no-plugins --working-dir=%s', escapeshellarg(static::$tempLocalPath)),
0, // Expected exit code.
null, // No stdout expectation.
null, // No stderr expectation.
'Failed to install dependencies.'
);
// Verify that the standard registers correctly.
$installCommand = sprintf(
'composer install-codestandards --no-ansi --working-dir=%s',
escapeshellarg(static::$tempLocalPath)
);
$installResult = $this->executeCliCommand($installCommand);
$this->assertSame(0, $installResult['exitcode'], 'Exitcode for install-codestandards did not match 0');
$this->assertMatchesRegularExpression(
'`^PHP CodeSniffer Config installed_paths set to [^\s]+/dummy-subdir$`',
trim($installResult['stdout']),
'Installing the standards failed.'
);
// Make sure the CodeSniffer.conf file has been created.
$this->assertFileExists(
static::$tempLocalPath . '/vendor/squizlabs/php_codesniffer/CodeSniffer.conf'
);
// Verify that PHPCS sees the external standard.
$this->assertExecute(
'"vendor/bin/phpcs" -i',
0, // Expected exit code.
'and DummySubDir', // Expected stdout.
'', // Empty stderr expectation.
'Running phpcs -i failed.',
static::$tempLocalPath
);
// Verify that PHPCS can with the external standard set as the standard.
$phpcsCommand = '"vendor/bin/phpcs" --standard=DummySubDir -e';
$phpcsResult = $this->executeCliCommand($phpcsCommand, static::$tempLocalPath);
$this->assertSame(0, $phpcsResult['exitcode'], 'Exitcode for PHPCS explain did not match 0');
$this->assertMatchesRegularExpression(
'`DummySubDir \(1 sniffs?\)\s+[-]+\s+DummySubDir\.Demo\.Demo(?:[\r\n]+|$)`',
$phpcsResult['stdout'],
'Output of the PHPCS explain command did not match the expectation.'
);
}
/**
* Data provider.
*
* @return array
*/
public function dataRegisterOneStandard()
{
// Get two PHPCS versions suitable for this PHP version + `master` + PHPCS 4.x dev.
$versions = PHPCSVersions::get(2, true, true);
return PHPCSVersions::toDataprovider($versions);
}
/**
* Test registering one external standard with multiple rulesets.
*
* @dataProvider dataRegisterOneStandardMultipleRulesets
*
* @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 testRegisterOneStandardWithMultipleRulesets($phpcsVersion)
{
$config = $this->configOneStandardMultiRuleset;
$config['require-dev']['squizlabs/php_codesniffer'] = $phpcsVersion;
$this->writeComposerJsonFile($config, static::$tempLocalPath);
// Install the dependencies and verify that the plugin has run.
$this->assertExecute(
sprintf('composer install -v --no-ansi --working-dir=%s', escapeshellarg(static::$tempLocalPath)),
0, // Expected exit code.
'PHP CodeSniffer Config installed_paths set to ', // Expectation for stdout.
null, // No stderr expectation.
'Failed to install dependencies.'
);
// Verify that only the one path is registered.
$result = $this->executeCliCommand('"vendor/bin/phpcs" --config-show', static::$tempLocalPath);
$this->assertSame(0, $result['exitcode'], 'Exitcode for "phpcs --config-show" did not match 0');
$expected = array(
'/phpcs-composer-installer/multistandard',
);
$this->assertSame(
$expected,
$this->configShowToPathsArray($result['stdout']),
'Paths as updated by the plugin does not contain the expected path'
);
// Verify that PHPCS sees all three external standards.
$result = $this->executeCliCommand('"vendor/bin/phpcs" -i', static::$tempLocalPath);
$this->assertSame(0, $result['exitcode'], 'Exitcode for "phpcs -i" did not match 0');
$expected = PHPCSVersions::getStandards($phpcsVersion);
$expected[] = 'MyFirstStandard';
$expected[] = 'MySecondStandard';
$expected[] = 'My-Third-Standard';
sort($expected, \SORT_NATURAL);
$this->assertSame(
$expected,
$this->standardsPhraseToArray($result['stdout']),
'Installed standards do not match the expected standards.'
);
}
/**
* Data provider.
*
* @return array
*/
public function dataRegisterOneStandardMultipleRulesets()
{
// Test against the highest and lowest supported PHPCS version of each major + PHPCS 4.x dev.
$versions = PHPCSVersions::getHighLowEachMajor(false, true);
return PHPCSVersions::toDataprovider($versions);
}
/**
* Test registering an external standard which has the ruleset in a subdirectory nested in `src`.
*
* @dataProvider dataRandomPHPCSVersion
*
* @param string $phpcsVersion PHPCS version to use in this test.
*
* @return void
*/
public function testRegisterOneStandardInSrcSubdir($phpcsVersion)
{
$config = $this->configOneStandardInSrcSubdir;
$config['require-dev']['squizlabs/php_codesniffer'] = $phpcsVersion;
$this->writeComposerJsonFile($config, static::$tempGlobalPath);
// Install the dependencies and verify that the plugin has run.
$this->assertExecute(
'composer global install -v --no-ansi',
0, // Expected exit code.
'PHP CodeSniffer Config installed_paths set to ', // Expectation for stdout.
null, // No stderr expectation.
'Failed to install dependencies.'
);
// Verify that the path for the directory above the ruleset is registered.
$result = $this->executeCliCommand('"vendor/bin/phpcs" --config-show', static::$tempGlobalPath);
$this->assertSame(0, $result['exitcode'], 'Exitcode for "phpcs --config-show" did not match 0');
$expected = array(
'/phpcs-composer-installer/dummy-src/src',
);
$this->assertSame(
$expected,
$this->configShowToPathsArray($result['stdout']),
'Paths as updated by the plugin does not contain the expected path'
);
// Verify that PHPCS sees the external standard.
$result = $this->executeCliCommand('"vendor/bin/phpcs" -i', static::$tempGlobalPath);
$this->assertSame(0, $result['exitcode'], 'Exitcode for "phpcs -i" did not match 0');
$expected = PHPCSVersions::getStandards($phpcsVersion);
$expected[] = 'DummySrcSubDir';
sort($expected, \SORT_NATURAL);
$this->assertSame(
$expected,
$this->standardsPhraseToArray($result['stdout']),
'Installed standards do not match the expected standards.'
);
}
/**
* Data provider.
*
* @return array
*/
public function dataRandomPHPCSVersion()
{
// Test against one random PHPCS version.
$versions = array(PHPCSVersions::getRandom(true, true));
return PHPCSVersions::toDataprovider($versions);
}
}