Skip to content

Commit

Permalink
Tests: implement use of the new ConfigDouble class
Browse files Browse the repository at this point in the history
Note: for the `AbstractSniffUnitTest` class, this change has little to no effect, other than protecting the sniff tests from changes made to the static properties in the `Config` class by the `Core` tests.
This is due to the `Config` being cached to a global variable. Fixing that is outside the scope of this PR.

Related issues: squizlabs/PHP_CodeSniffer 2899 and 25.
  • Loading branch information
jrfnl committed Jan 15, 2024
1 parent 986207e commit 19b34bf
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 95 deletions.
57 changes: 2 additions & 55 deletions tests/Core/AbstractMethodUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

namespace PHP_CodeSniffer\Tests\Core;

use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Files\DummyFile;
use PHP_CodeSniffer\Tests\ConfigDouble;
use PHPUnit\Framework\TestCase;
use ReflectionProperty;

Expand Down Expand Up @@ -57,22 +57,7 @@ abstract class AbstractMethodUnitTest extends TestCase
*/
public static function initializeFile()
{
/*
* Set the static properties in the Config class to specific values for performance
* and to clear out values from other tests.
*/

self::setStaticConfigProperty('executablePaths', []);

// Set to a usable value to circumvent Config trying to find a phpcs.xml config file.
self::setStaticConfigProperty('overriddenDefaults', ['standards' => ['PSR1']]);

// Set to values which prevent the test-runner user's `CodeSniffer.conf` file
// from being read and influencing the tests. Also prevent an `exec()` call to stty.
self::setStaticConfigProperty('configData', ['report_width' => 80]);
self::setStaticConfigProperty('configDataFile', '');

$config = new Config();
$config = new ConfigDouble();
// Also set a tab-width to enable testing tab-replaced vs `orig_content`.
$config->tabWidth = static::$tabWidth;

Expand All @@ -93,44 +78,6 @@ public static function initializeFile()
}//end initializeFile()


/**
* Clean up after finished test.
*
* @afterClass
*
* @return void
*/
public static function resetFile()
{
self::$phpcsFile = null;

// Reset the static properties in the Config class to their defaults to prevent tests influencing each other.
self::setStaticConfigProperty('overriddenDefaults', []);
self::setStaticConfigProperty('executablePaths', []);
self::setStaticConfigProperty('configData', null);
self::setStaticConfigProperty('configDataFile', null);

}//end resetFile()


/**
* Helper function to set the value of a private 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()


/**
* Get the token pointer for a target token based on a specific comment found on the line before.
*
Expand Down
28 changes: 14 additions & 14 deletions tests/Core/ErrorSuppressionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

namespace PHP_CodeSniffer\Tests\Core;

use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Files\DummyFile;
use PHP_CodeSniffer\Tests\ConfigDouble;
use PHPUnit\Framework\TestCase;

/**
Expand Down Expand Up @@ -41,7 +41,7 @@ public function testSuppressError($before, $after, $expectedErrors=0)
static $config, $ruleset;

if (isset($config, $ruleset) === false) {
$config = new Config();
$config = new ConfigDouble();
$config->standards = ['Generic'];
$config->sniffs = ['Generic.PHP.LowerCaseConstant'];

Expand Down Expand Up @@ -173,7 +173,7 @@ public function testSuppressSomeErrors($before, $between, $expectedErrors=1)
static $config, $ruleset;

if (isset($config, $ruleset) === false) {
$config = new Config();
$config = new ConfigDouble();
$config->standards = ['Generic'];
$config->sniffs = ['Generic.PHP.LowerCaseConstant'];

Expand Down Expand Up @@ -265,7 +265,7 @@ public function testSuppressWarning($before, $after, $expectedWarnings=0)
static $config, $ruleset;

if (isset($config, $ruleset) === false) {
$config = new Config();
$config = new ConfigDouble();
$config->standards = ['Generic'];
$config->sniffs = ['Generic.Commenting.Todo'];

Expand Down Expand Up @@ -349,7 +349,7 @@ public function testSuppressLine($before, $after='', $expectedErrors=1)
static $config, $ruleset;

if (isset($config, $ruleset) === false) {
$config = new Config();
$config = new ConfigDouble();
$config->standards = ['Generic'];
$config->sniffs = ['Generic.PHP.LowerCaseConstant'];

Expand Down Expand Up @@ -445,7 +445,7 @@ public static function dataSuppressLine()
*/
public function testSuppressLineMidLine()
{
$config = new Config();
$config = new ConfigDouble();
$config->standards = ['Generic'];
$config->sniffs = ['Generic.PHP.LowerCaseConstant'];

Expand All @@ -468,7 +468,7 @@ public function testSuppressLineMidLine()
*/
public function testSuppressLineWithinDocblock()
{
$config = new Config();
$config = new ConfigDouble();
$config->standards = ['Generic'];
$config->sniffs = ['Generic.Files.LineLength'];

Expand Down Expand Up @@ -508,7 +508,7 @@ public function testNestedSuppressLine($before, $after)
static $config, $ruleset;

if (isset($config, $ruleset) === false) {
$config = new Config();
$config = new ConfigDouble();
$config->standards = ['Generic'];
$config->sniffs = ['Generic.PHP.LowerCaseConstant'];

Expand Down Expand Up @@ -598,7 +598,7 @@ public function testSuppressScope($before, $after, $expectedErrors=0)
static $config, $ruleset;

if (isset($config, $ruleset) === false) {
$config = new Config();
$config = new ConfigDouble();
$config->standards = ['PEAR'];
$config->sniffs = ['PEAR.Functions.FunctionDeclaration'];

Expand Down Expand Up @@ -695,7 +695,7 @@ public function testSuppressFile($before, $after='', $expectedWarnings=0)
static $config, $ruleset;

if (isset($config, $ruleset) === false) {
$config = new Config();
$config = new ConfigDouble();
$config->standards = ['Generic'];
$config->sniffs = ['Generic.Commenting.Todo'];

Expand Down Expand Up @@ -809,7 +809,7 @@ public function testDisableSelected($before, $expectedErrors=0, $expectedWarning
static $config, $ruleset;

if (isset($config, $ruleset) === false) {
$config = new Config();
$config = new ConfigDouble();
$config->standards = ['Generic'];
$config->sniffs = [
'Generic.PHP.LowerCaseConstant',
Expand Down Expand Up @@ -926,7 +926,7 @@ public function testEnableSelected($code, $expectedErrors, $expectedWarnings)
static $config, $ruleset;

if (isset($config, $ruleset) === false) {
$config = new Config();
$config = new ConfigDouble();
$config->standards = ['Generic'];
$config->sniffs = [
'Generic.PHP.LowerCaseConstant',
Expand Down Expand Up @@ -1100,7 +1100,7 @@ public function testIgnoreSelected($before, $expectedErrors, $expectedWarnings)
static $config, $ruleset;

if (isset($config, $ruleset) === false) {
$config = new Config();
$config = new ConfigDouble();
$config->standards = ['Generic'];
$config->sniffs = [
'Generic.PHP.LowerCaseConstant',
Expand Down Expand Up @@ -1191,7 +1191,7 @@ public function testCommenting($code, $expectedErrors, $expectedWarnings)
static $config, $ruleset;

if (isset($config, $ruleset) === false) {
$config = new Config();
$config = new ConfigDouble();
$config->standards = ['Generic'];
$config->sniffs = [
'Generic.PHP.LowerCaseConstant',
Expand Down
4 changes: 2 additions & 2 deletions tests/Core/Filters/AbstractFilterTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

namespace PHP_CodeSniffer\Tests\Core\Filters;

use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Filters\Filter;
use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Tests\ConfigDouble;
use PHPUnit\Framework\TestCase;
use RecursiveIteratorIterator;

Expand Down Expand Up @@ -45,7 +45,7 @@ abstract class AbstractFilterTestCase extends TestCase
*/
public static function initializeConfigAndRuleset()
{
self::$config = new Config(['--standard=PSR1', '--extensions=php,inc/php,js,css', '--report-width=80']);
self::$config = new ConfigDouble(['--extensions=php,inc/php,js,css']);
self::$ruleset = new Ruleset(self::$config);

}//end initializeConfigAndRuleset()
Expand Down
3 changes: 2 additions & 1 deletion tests/Core/Filters/Filter/AcceptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Filters\Filter;
use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Tests\ConfigDouble;
use PHP_CodeSniffer\Tests\Core\Filters\AbstractFilterTestCase;
use RecursiveArrayIterator;

Expand All @@ -35,7 +36,7 @@ final class AcceptTest extends AbstractFilterTestCase
public static function initializeConfigAndRuleset()
{
$standard = __DIR__.'/'.basename(__FILE__, '.php').'.xml';
self::$config = new Config(["--standard=$standard", '--ignore=*/somethingelse/*', '--report-width=80']);
self::$config = new ConfigDouble(["--standard=$standard", '--ignore=*/somethingelse/*']);
self::$ruleset = new Ruleset(self::$config);

}//end initializeConfigAndRuleset()
Expand Down
10 changes: 5 additions & 5 deletions tests/Core/Ruleset/ExplainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

namespace PHP_CodeSniffer\Tests\Core\Ruleset;

use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Runner;
use PHP_CodeSniffer\Tests\ConfigDouble;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -31,7 +31,7 @@ final class ExplainTest extends TestCase
public function testExplain()
{
// Set up the ruleset.
$config = new Config(['--standard=PSR1', '-e', '--report-width=80']);
$config = new ConfigDouble(['--standard=PSR1', '-e']);
$ruleset = new Ruleset($config);

$expected = PHP_EOL;
Expand Down Expand Up @@ -66,7 +66,7 @@ public function testExplain()
public function testExplainAlwaysDisplaysCompleteSniffName()
{
// Set up the ruleset.
$config = new Config(['--standard=PSR1', '-e', '--report-width=30']);
$config = new ConfigDouble(['--standard=PSR1', '-e', '--report-width=30']);
$ruleset = new Ruleset($config);

$expected = PHP_EOL;
Expand Down Expand Up @@ -104,7 +104,7 @@ public function testExplainSingleSniff()
{
// Set up the ruleset.
$standard = __DIR__.'/ExplainSingleSniffTest.xml';
$config = new Config(["--standard=$standard", '-e', '--report-width=80']);
$config = new ConfigDouble(["--standard=$standard", '-e']);
$ruleset = new Ruleset($config);

$expected = PHP_EOL;
Expand Down Expand Up @@ -135,7 +135,7 @@ public function testExplainCustomRuleset()
{
// Set up the ruleset.
$standard = __DIR__.'/ExplainCustomRulesetTest.xml';
$config = new Config(["--standard=$standard", '-e', '--report-width=80']);
$config = new ConfigDouble(["--standard=$standard", '-e']);
$ruleset = new Ruleset($config);

$expected = PHP_EOL;
Expand Down
4 changes: 2 additions & 2 deletions tests/Core/Ruleset/RuleInclusionAbsoluteLinuxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

namespace PHP_CodeSniffer\Tests\Core\Ruleset;

use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Tests\ConfigDouble;
use PHPUnit\Framework\TestCase;

/**
Expand Down Expand Up @@ -71,7 +71,7 @@ public function initializeConfigAndRuleset()
}

// Initialize the config and ruleset objects for the test.
$config = new Config(["--standard={$this->standard}"]);
$config = new ConfigDouble(["--standard={$this->standard}"]);
$this->ruleset = new Ruleset($config);

}//end initializeConfigAndRuleset()
Expand Down
4 changes: 2 additions & 2 deletions tests/Core/Ruleset/RuleInclusionAbsoluteWindowsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

namespace PHP_CodeSniffer\Tests\Core\Ruleset;

use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Tests\ConfigDouble;
use PHPUnit\Framework\TestCase;

/**
Expand Down Expand Up @@ -70,7 +70,7 @@ public function initializeConfigAndRuleset()
}

// Initialize the config and ruleset objects for the test.
$config = new Config(["--standard={$this->standard}"]);
$config = new ConfigDouble(["--standard={$this->standard}"]);
$this->ruleset = new Ruleset($config);

}//end initializeConfigAndRuleset()
Expand Down
4 changes: 2 additions & 2 deletions tests/Core/Ruleset/RuleInclusionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

namespace PHP_CodeSniffer\Tests\Core\Ruleset;

use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Tests\ConfigDouble;
use PHPUnit\Framework\TestCase;
use ReflectionObject;

Expand Down Expand Up @@ -74,7 +74,7 @@ public static function initializeConfigAndRuleset()
self::markTestSkipped('On the fly ruleset adjustment failed');
}

$config = new Config(["--standard=$standard"]);
$config = new ConfigDouble(["--standard=$standard"]);
self::$ruleset = new Ruleset($config);

}//end initializeConfigAndRuleset()
Expand Down
Loading

0 comments on commit 19b34bf

Please sign in to comment.