/
RegisterExternalStandardsTest.php
191 lines (166 loc) · 7.11 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
<?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' => '*',
),
);
/**
* 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);
}
}