Skip to content

Commit 16ad55b

Browse files
committed
Tests: implement use of the new ConfigDouble class
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.
1 parent 6e9d03b commit 16ad55b

10 files changed

+43
-95
lines changed

tests/Core/AbstractMethodUnitTest.php

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
namespace PHP_CodeSniffer\Tests\Core;
1111

12-
use PHP_CodeSniffer\Config;
1312
use PHP_CodeSniffer\Ruleset;
1413
use PHP_CodeSniffer\Files\DummyFile;
14+
use PHP_CodeSniffer\Tests\ConfigDouble;
1515
use PHPUnit\Framework\TestCase;
1616
use ReflectionProperty;
1717

@@ -57,22 +57,7 @@ abstract class AbstractMethodUnitTest extends TestCase
5757
*/
5858
public static function initializeFile()
5959
{
60-
/*
61-
* Set the static properties in the Config class to specific values for performance
62-
* and to clear out values from other tests.
63-
*/
64-
65-
self::setStaticConfigProperty('executablePaths', []);
66-
67-
// Set to a usable value to circumvent Config trying to find a phpcs.xml config file.
68-
self::setStaticConfigProperty('overriddenDefaults', ['standards' => ['PSR1']]);
69-
70-
// Set to values which prevent the test-runner user's `CodeSniffer.conf` file
71-
// from being read and influencing the tests. Also prevent an `exec()` call to stty.
72-
self::setStaticConfigProperty('configData', ['report_width' => 80]);
73-
self::setStaticConfigProperty('configDataFile', '');
74-
75-
$config = new Config();
60+
$config = new ConfigDouble();
7661
// Also set a tab-width to enable testing tab-replaced vs `orig_content`.
7762
$config->tabWidth = static::$tabWidth;
7863

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

9580

96-
/**
97-
* Clean up after finished test.
98-
*
99-
* @afterClass
100-
*
101-
* @return void
102-
*/
103-
public static function resetFile()
104-
{
105-
self::$phpcsFile = null;
106-
107-
// Reset the static properties in the Config class to their defaults to prevent tests influencing each other.
108-
self::setStaticConfigProperty('overriddenDefaults', []);
109-
self::setStaticConfigProperty('executablePaths', []);
110-
self::setStaticConfigProperty('configData', null);
111-
self::setStaticConfigProperty('configDataFile', null);
112-
113-
}//end resetFile()
114-
115-
116-
/**
117-
* Helper function to set the value of a private static property on the Config class.
118-
*
119-
* @param string $name The name of the property to set.
120-
* @param mixed $value The value to set the property to.
121-
*
122-
* @return void
123-
*/
124-
public static function setStaticConfigProperty($name, $value)
125-
{
126-
$property = new ReflectionProperty('PHP_CodeSniffer\Config', $name);
127-
$property->setAccessible(true);
128-
$property->setValue(null, $value);
129-
$property->setAccessible(false);
130-
131-
}//end setStaticConfigProperty()
132-
133-
13481
/**
13582
* Get the token pointer for a target token based on a specific comment found on the line before.
13683
*

tests/Core/ErrorSuppressionTest.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
namespace PHP_CodeSniffer\Tests\Core;
1111

12-
use PHP_CodeSniffer\Config;
1312
use PHP_CodeSniffer\Ruleset;
1413
use PHP_CodeSniffer\Files\DummyFile;
14+
use PHP_CodeSniffer\Tests\ConfigDouble;
1515
use PHPUnit\Framework\TestCase;
1616

1717
/**
@@ -41,7 +41,7 @@ public function testSuppressError($before, $after, $expectedErrors=0)
4141
static $config, $ruleset;
4242

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

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

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

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

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

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

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

@@ -445,7 +445,7 @@ public static function dataSuppressLine()
445445
*/
446446
public function testSuppressLineMidLine()
447447
{
448-
$config = new Config();
448+
$config = new ConfigDouble();
449449
$config->standards = ['Generic'];
450450
$config->sniffs = ['Generic.PHP.LowerCaseConstant'];
451451

@@ -468,7 +468,7 @@ public function testSuppressLineMidLine()
468468
*/
469469
public function testSuppressLineWithinDocblock()
470470
{
471-
$config = new Config();
471+
$config = new ConfigDouble();
472472
$config->standards = ['Generic'];
473473
$config->sniffs = ['Generic.Files.LineLength'];
474474

@@ -508,7 +508,7 @@ public function testNestedSuppressLine($before, $after)
508508
static $config, $ruleset;
509509

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

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

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

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

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

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

811811
if (isset($config, $ruleset) === false) {
812-
$config = new Config();
812+
$config = new ConfigDouble();
813813
$config->standards = ['Generic'];
814814
$config->sniffs = [
815815
'Generic.PHP.LowerCaseConstant',
@@ -926,7 +926,7 @@ public function testEnableSelected($code, $expectedErrors, $expectedWarnings)
926926
static $config, $ruleset;
927927

928928
if (isset($config, $ruleset) === false) {
929-
$config = new Config();
929+
$config = new ConfigDouble();
930930
$config->standards = ['Generic'];
931931
$config->sniffs = [
932932
'Generic.PHP.LowerCaseConstant',
@@ -1100,7 +1100,7 @@ public function testIgnoreSelected($before, $expectedErrors, $expectedWarnings)
11001100
static $config, $ruleset;
11011101

11021102
if (isset($config, $ruleset) === false) {
1103-
$config = new Config();
1103+
$config = new ConfigDouble();
11041104
$config->standards = ['Generic'];
11051105
$config->sniffs = [
11061106
'Generic.PHP.LowerCaseConstant',
@@ -1191,7 +1191,7 @@ public function testCommenting($code, $expectedErrors, $expectedWarnings)
11911191
static $config, $ruleset;
11921192

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

tests/Core/Filters/AbstractFilterTestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
namespace PHP_CodeSniffer\Tests\Core\Filters;
1111

12-
use PHP_CodeSniffer\Config;
1312
use PHP_CodeSniffer\Filters\Filter;
1413
use PHP_CodeSniffer\Ruleset;
14+
use PHP_CodeSniffer\Tests\ConfigDouble;
1515
use PHPUnit\Framework\TestCase;
1616
use RecursiveIteratorIterator;
1717

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

5151
}//end initializeConfigAndRuleset()

tests/Core/Filters/Filter/AcceptTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PHP_CodeSniffer\Config;
1414
use PHP_CodeSniffer\Filters\Filter;
1515
use PHP_CodeSniffer\Ruleset;
16+
use PHP_CodeSniffer\Tests\ConfigDouble;
1617
use PHP_CodeSniffer\Tests\Core\Filters\AbstractFilterTestCase;
1718
use RecursiveArrayIterator;
1819

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

4142
}//end initializeConfigAndRuleset()

tests/Core/Ruleset/ExplainTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
namespace PHP_CodeSniffer\Tests\Core\Ruleset;
1111

12-
use PHP_CodeSniffer\Config;
1312
use PHP_CodeSniffer\Ruleset;
1413
use PHP_CodeSniffer\Runner;
14+
use PHP_CodeSniffer\Tests\ConfigDouble;
1515
use PHPUnit\Framework\TestCase;
1616

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

3737
$expected = PHP_EOL;
@@ -66,7 +66,7 @@ public function testExplain()
6666
public function testExplainAlwaysDisplaysCompleteSniffName()
6767
{
6868
// Set up the ruleset.
69-
$config = new Config(['--standard=PSR1', '-e', '--report-width=30']);
69+
$config = new ConfigDouble(['--standard=PSR1', '-e', '--report-width=30']);
7070
$ruleset = new Ruleset($config);
7171

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

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

141141
$expected = PHP_EOL;

tests/Core/Ruleset/RuleInclusionAbsoluteLinuxTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
namespace PHP_CodeSniffer\Tests\Core\Ruleset;
1111

12-
use PHP_CodeSniffer\Config;
1312
use PHP_CodeSniffer\Ruleset;
13+
use PHP_CodeSniffer\Tests\ConfigDouble;
1414
use PHPUnit\Framework\TestCase;
1515

1616
/**
@@ -71,7 +71,7 @@ public function initializeConfigAndRuleset()
7171
}
7272

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

7777
}//end initializeConfigAndRuleset()

tests/Core/Ruleset/RuleInclusionAbsoluteWindowsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
namespace PHP_CodeSniffer\Tests\Core\Ruleset;
1111

12-
use PHP_CodeSniffer\Config;
1312
use PHP_CodeSniffer\Ruleset;
13+
use PHP_CodeSniffer\Tests\ConfigDouble;
1414
use PHPUnit\Framework\TestCase;
1515

1616
/**
@@ -70,7 +70,7 @@ public function initializeConfigAndRuleset()
7070
}
7171

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

7676
}//end initializeConfigAndRuleset()

tests/Core/Ruleset/RuleInclusionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
namespace PHP_CodeSniffer\Tests\Core\Ruleset;
1111

12-
use PHP_CodeSniffer\Config;
1312
use PHP_CodeSniffer\Ruleset;
13+
use PHP_CodeSniffer\Tests\ConfigDouble;
1414
use PHPUnit\Framework\TestCase;
1515
use ReflectionObject;
1616

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

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

8080
}//end initializeConfigAndRuleset()

0 commit comments

Comments
 (0)