Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions tests/Core/Config/AbstractRealConfigTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/**
* Test case with helper methods for tests which need to use the *real* Config class (instead of the ConfigDouble).
*
* This test case should be used sparingly and only when it cannot be avoided.
*
* @copyright 2025 PHPCSStandards and contributors
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

namespace PHP_CodeSniffer\Tests\Core\Config;

use PHPUnit\Framework\TestCase;
use ReflectionProperty;

abstract class AbstractRealConfigTestCase extends TestCase
{


/**
* Set static properties in the Config class to prevent tests influencing each other.
*
* @before
*
* @return void
*/
protected function setConfigStatics()
{
// Set to the property's default value to clear out potentially set values from other tests.
self::setStaticConfigProperty('overriddenDefaults', []);
self::setStaticConfigProperty('executablePaths', []);

// Set to values which prevent the test-runner user's `CodeSniffer.conf` file
// from being read and influencing the tests.
self::setStaticConfigProperty('configData', []);
self::setStaticConfigProperty('configDataFile', '');

}//end setConfigStatics()


/**
* Clean up after each finished test.
*
* @after
*
* @return void
*/
protected function clearArgv()
{
$_SERVER['argv'] = [];

}//end clearArgv()


/**
* 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::setStaticConfigProperty('overriddenDefaults', []);
self::setStaticConfigProperty('executablePaths', []);
self::setStaticConfigProperty('configData', null);
self::setStaticConfigProperty('configDataFile', null);
$_SERVER['argv'] = [];

}//end resetConfigToDefaults()


/**
* 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
*/
protected static function setStaticConfigProperty($name, $value)
{
$property = new ReflectionProperty('PHP_CodeSniffer\Config', $name);
$property->setAccessible(true);
$property->setValue(null, $value);
$property->setAccessible(false);

}//end setStaticConfigProperty()


}//end class
98 changes: 13 additions & 85 deletions tests/Core/Config/ReportWidthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,74 +10,17 @@
namespace PHP_CodeSniffer\Tests\Core\Config;

use PHP_CodeSniffer\Config;
use PHPUnit\Framework\TestCase;
use ReflectionProperty;
use PHP_CodeSniffer\Tests\Core\Config\AbstractRealConfigTestCase;

/**
* Tests for the \PHP_CodeSniffer\Config reportWidth value.
*
* @covers \PHP_CodeSniffer\Config::__get
*/
final class ReportWidthTest extends TestCase
final class ReportWidthTest extends AbstractRealConfigTestCase
{


/**
* Set static properties in the Config class to prevent tests influencing each other.
*
* @before
*
* @return void
*/
protected 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
*/
protected 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.
*
Expand All @@ -88,7 +31,7 @@ public static function resetConfigToDefaults()
*/
public function testReportWidthDefault()
{
$config = new Config();
$config = new Config(['--standard=PSR1']);

// 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');
Expand All @@ -112,9 +55,9 @@ public function testReportWidthWillBeSetFromAutoWhenNotFoundInConfFile()
'show_warnings' => '0',
];

$this->setStaticProperty('configData', $phpCodeSnifferConfig);
$this->setStaticConfigProperty('configData', $phpCodeSnifferConfig);

$config = new Config();
$config = new Config(['--standard=PSR1']);

// 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');
Expand All @@ -139,9 +82,9 @@ public function testReportWidthCanBeSetFromConfFile()
'report_width' => '120',
];

$this->setStaticProperty('configData', $phpCodeSnifferConfig);
$this->setStaticConfigProperty('configData', $phpCodeSnifferConfig);

$config = new Config();
$config = new Config(['--standard=PSR1']);
$this->assertSame(120, $config->reportWidth);

}//end testReportWidthCanBeSetFromConfFile()
Expand All @@ -159,6 +102,7 @@ public function testReportWidthCanBeSetFromCLI()
{
$_SERVER['argv'] = [
'phpcs',
'--standard=PSR1',
'--report-width=100',
];

Expand All @@ -180,6 +124,7 @@ public function testReportWidthWhenSetFromCLIFirstValuePrevails()
{
$_SERVER['argv'] = [
'phpcs',
'--standard=PSR1',
'--report-width=100',
'--report-width=200',
];
Expand Down Expand Up @@ -209,10 +154,11 @@ public function testReportWidthSetFromCLIOverrulesConfFile()
'report_width' => '120',
];

$this->setStaticProperty('configData', $phpCodeSnifferConfig);
$this->setStaticConfigProperty('configData', $phpCodeSnifferConfig);

$cliArgs = [
'phpcs',
'--standard=PSR1',
'--report-width=180',
];

Expand All @@ -231,7 +177,7 @@ public function testReportWidthSetFromCLIOverrulesConfFile()
*/
public function testReportWidthInputHandlingForAuto()
{
$config = new Config();
$config = new Config(['--standard=PSR1']);
$config->reportWidth = 'auto';

// Can't test the exact value as "auto" will resolve differently depending on the machine running the tests.
Expand All @@ -254,7 +200,7 @@ public function testReportWidthInputHandlingForAuto()
*/
public function testReportWidthInputHandling($value, $expected)
{
$config = new Config();
$config = new Config(['--standard=PSR1']);
$config->reportWidth = $value;

$this->assertSame($expected, $config->reportWidth);
Expand Down Expand Up @@ -311,22 +257,4 @@ public static function dataReportWidthInputHandling()
}//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
78 changes: 2 additions & 76 deletions tests/Core/Runner/AbstractRunnerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,83 +11,9 @@

namespace PHP_CodeSniffer\Tests\Core\Runner;

use PHPUnit\Framework\TestCase;
use ReflectionProperty;
use PHP_CodeSniffer\Tests\Core\Config\AbstractRealConfigTestCase;

abstract class AbstractRunnerTestCase extends TestCase
abstract class AbstractRunnerTestCase extends AbstractRealConfigTestCase
{


/**
* Set static properties in the Config class to prevent tests influencing each other.
*
* @before
*
* @return void
*/
public function setConfigStatics()
{
// Set to the property's default value to clear out potentially set values from other tests.
self::setStaticConfigProperty('overriddenDefaults', []);
self::setStaticConfigProperty('executablePaths', []);

// Set to values which prevent the test-runner user's `CodeSniffer.conf` file
// from being read and influencing the tests.
self::setStaticConfigProperty('configData', []);
self::setStaticConfigProperty('configDataFile', '');

}//end setConfigStatics()


/**
* Clean up after each finished test.
*
* @after
*
* @return void
*/
public function clearArgv()
{
$_SERVER['argv'] = [];

}//end clearArgv()


/**
* 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 reset()
{
self::setStaticConfigProperty('overriddenDefaults', []);
self::setStaticConfigProperty('executablePaths', []);
self::setStaticConfigProperty('configData', null);
self::setStaticConfigProperty('configDataFile', null);
$_SERVER['argv'] = [];

}//end reset()


/**
* 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 setStaticConfigProperty($name, $value)
{
$property = new ReflectionProperty('PHP_CodeSniffer\Config', $name);
$property->setAccessible(true);
$property->setValue(null, $value);
$property->setAccessible(false);

}//end setStaticConfigProperty()


}//end class