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
|
<?php
/**
* Tests for the \PHP_CodeSniffer\Config reportWidth value.
*
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
* @copyright 2006-2023 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/
namespace PHP_CodeSniffer\Tests\Core\Config;
use PHP_CodeSniffer\Config;
use PHPUnit\Framework\TestCase;
use ReflectionProperty;
/**
* Tests for the \PHP_CodeSniffer\Config reportWidth value.
*
* @covers \PHP_CodeSniffer\Config::__get
*/
final class ReportWidthTest extends TestCase
{
/**
* Set static properties in the Config class to prevent tests influencing each other.
*
* @before
*
* @return void
*/
public static function cleanConfig()
{
// Set to the property's default value to clear out potentially set values from other tests.
self::setStaticProperty('executablePaths', []);
// Set to a usable value to circumvent Config trying to find a phpcs.xml config file.
self::setStaticProperty('overriddenDefaults', ['standards' => ['PSR1']]);
// Set to values which prevent the test-runner user's `CodeSniffer.conf` file
// from being read and influencing the tests.
self::setStaticProperty('configData', []);
self::setStaticProperty('configDataFile', '');
}//end cleanConfig()
/**
* Clean up after each finished test.
*
* @after
*
* @return void
*/
public function resetConfig()
{
$_SERVER['argv'] = [];
}//end resetConfig()
/**
* Reset the static properties in the Config class to their true defaults to prevent this class
* from influencing other tests.
*
* @afterClass
*
* @return void
*/
public static function resetConfigToDefaults()
{
self::setStaticProperty('overriddenDefaults', []);
self::setStaticProperty('executablePaths', []);
self::setStaticProperty('configData', null);
self::setStaticProperty('configDataFile', null);
$_SERVER['argv'] = [];
}//end resetConfigToDefaults()
/**
* Test that report width without overrules will always be set to a non-0 positive integer.
*
* @covers \PHP_CodeSniffer\Config::__set
* @covers \PHP_CodeSniffer\Config::restoreDefaults
*
* @return void
*/
public function testReportWidthDefault()
{
$config = new Config();
// Can't test the exact value as "auto" will resolve differently depending on the machine running the tests.
$this->assertTrue(is_int($config->reportWidth), 'Report width is not an integer');
$this->assertGreaterThan(0, $config->reportWidth, 'Report width is not greater than 0');
}//end testReportWidthDefault()
/**
* Test that the report width will be set to a non-0 positive integer when not found in the CodeSniffer.conf file.
*
* @covers \PHP_CodeSniffer\Config::__set
* @covers \PHP_CodeSniffer\Config::restoreDefaults
*
* @return void
*/
public function testReportWidthWillBeSetFromAutoWhenNotFoundInConfFile()
{
$phpCodeSnifferConfig = [
'default_standard' => 'PSR2',
'show_warnings' => '0',
];
$this->setStaticProperty('configData', $phpCodeSnifferConfig);
$config = new Config();
// Can't test the exact value as "auto" will resolve differently depending on the machine running the tests.
$this->assertTrue(is_int($config->reportWidth), 'Report width is not an integer');
$this->assertGreaterThan(0, $config->reportWidth, 'Report width is not greater than 0');
}//end testReportWidthWillBeSetFromAutoWhenNotFoundInConfFile()
/**
* Test that the report width will be set correctly when found in the CodeSniffer.conf file.
*
* @covers \PHP_CodeSniffer\Config::__set
* @covers \PHP_CodeSniffer\Config::getConfigData
* @covers \PHP_CodeSniffer\Config::restoreDefaults
*
* @return void
*/
public function testReportWidthCanBeSetFromConfFile()
{
$phpCodeSnifferConfig = [
'default_standard' => 'PSR2',
'report_width' => '120',
];
$this->setStaticProperty('configData', $phpCodeSnifferConfig);
$config = new Config();
$this->assertSame(120, $config->reportWidth);
}//end testReportWidthCanBeSetFromConfFile()
/**
* Test that the report width will be set correctly when passed as a CLI argument.
*
* @covers \PHP_CodeSniffer\Config::__set
* @covers \PHP_CodeSniffer\Config::processLongArgument
*
* @return void
*/
public function testReportWidthCanBeSetFromCLI()
{
$_SERVER['argv'] = [
'phpcs',
'--report-width=100',
];
$config = new Config();
$this->assertSame(100, $config->reportWidth);
}//end testReportWidthCanBeSetFromCLI()
/**
* Test that the report width will be set correctly when multiple report widths are passed on the CLI.
*
* @covers \PHP_CodeSniffer\Config::__set
* @covers \PHP_CodeSniffer\Config::processLongArgument
*
* @return void
*/
public function testReportWidthWhenSetFromCLIFirstValuePrevails()
{
$_SERVER['argv'] = [
'phpcs',
'--report-width=100',
'--report-width=200',
];
$config = new Config();
$this->assertSame(100, $config->reportWidth);
}//end testReportWidthWhenSetFromCLIFirstValuePrevails()
/**
* Test that a report width passed as a CLI argument will overrule a report width set in a CodeSniffer.conf file.
*
* @covers \PHP_CodeSniffer\Config::__set
* @covers \PHP_CodeSniffer\Config::processLongArgument
* @covers \PHP_CodeSniffer\Config::getConfigData
*
* @return void
*/
public function testReportWidthSetFromCLIOverrulesConfFile()
{
$phpCodeSnifferConfig = [
'default_standard' => 'PSR2',
'report_format' => 'summary',
'show_warnings' => '0',
'show_progress' => '1',
'report_width' => '120',
];
$this->setStaticProperty('configData', $phpCodeSnifferConfig);
$cliArgs = [
'phpcs',
'--report-width=180',
];
$config = new Config($cliArgs);
$this->assertSame(180, $config->reportWidth);
}//end testReportWidthSetFromCLIOverrulesConfFile()
/**
* Test that the report width will be set to a non-0 positive integer when set to "auto".
*
* @covers \PHP_CodeSniffer\Config::__set
*
* @return void
*/
public function testReportWidthInputHandlingForAuto()
{
$config = new Config();
$config->reportWidth = 'auto';
// Can't test the exact value as "auto" will resolve differently depending on the machine running the tests.
$this->assertTrue(is_int($config->reportWidth), 'Report width is not an integer');
$this->assertGreaterThan(0, $config->reportWidth, 'Report width is not greater than 0');
}//end testReportWidthInputHandlingForAuto()
/**
* Test that the report width will be set correctly for various types of input.
*
* @param mixed $value Input value received.
* @param int $expected Expected report width.
*
* @dataProvider dataReportWidthInputHandling
* @covers \PHP_CodeSniffer\Config::__set
*
* @return void
*/
public function testReportWidthInputHandling($value, $expected)
{
$config = new Config();
$config->reportWidth = $value;
$this->assertSame($expected, $config->reportWidth);
}//end testReportWidthInputHandling()
/**
* Data provider.
*
* @return array<string, array<string, mixed>>
*/
public static function dataReportWidthInputHandling()
{
return [
'No value (empty string)' => [
'value' => '',
'expected' => Config::DEFAULT_REPORT_WIDTH,
],
'Value: invalid input type null' => [
'value' => null,
'expected' => Config::DEFAULT_REPORT_WIDTH,
],
'Value: invalid input type false' => [
'value' => false,
'expected' => Config::DEFAULT_REPORT_WIDTH,
],
'Value: invalid input type float' => [
'value' => 100.50,
'expected' => Config::DEFAULT_REPORT_WIDTH,
],
'Value: invalid string value "invalid"' => [
'value' => 'invalid',
'expected' => Config::DEFAULT_REPORT_WIDTH,
],
'Value: invalid string value, non-integer string "50.25"' => [
'value' => '50.25',
'expected' => Config::DEFAULT_REPORT_WIDTH,
],
'Value: valid numeric string value' => [
'value' => '250',
'expected' => 250,
],
'Value: valid int value' => [
'value' => 220,
'expected' => 220,
],
'Value: negative int value becomes positive int' => [
'value' => -180,
'expected' => 180,
],
];
}//end dataReportWidthInputHandling()
/**
* Helper function to set a static property on the Config class.
*
* @param string $name The name of the property to set.
* @param mixed $value The value to set the property to.
*
* @return void
*/
public static function setStaticProperty($name, $value)
{
$property = new ReflectionProperty('PHP_CodeSniffer\Config', $name);
$property->setAccessible(true);
$property->setValue(null, $value);
$property->setAccessible(false);
}//end setStaticProperty()
}//end class
|