Skip to content

Commit

Permalink
Make test suite compatible with PHPCS 3.8.0 (#304)
Browse files Browse the repository at this point in the history
As per the deprecation notice being thrown:
> `setSniffProperty: the format of the $settings parameter has changed from (mixed) $value to array('scope' => 'sniff|standard', 'value' => $value). Please update your integration code. See PR #3629 for more information.`

... the format of the `$settings` parameter for the `Ruleset::setSniffProperty()` method has changed to allow PHPCS to prevent notices about dynamic properties.

This commit updates the test suite to allow for both the PHPCS < 3.8.0 as well as the PHPCS 3.8.0+ way of setting properties directly on the Ruleset by adding a helper method to the `BaseTestCase` to set the properties in the correct way depending on the PHPCS version used.

Ref: squizlabs/php_codesniffer 3629

Co-authored-by: jrfnl <jrfnl@users.noreply.github.com>
  • Loading branch information
jrfnl and jrfnl authored Jul 19, 2023
1 parent dc5582d commit 71d9f67
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 230 deletions.
20 changes: 20 additions & 0 deletions Tests/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,24 @@ public function getFixture($fixtureFilename)
{
return realpath(__DIR__ . '/VariableAnalysisSniff/fixtures/' . $fixtureFilename);
}

public function setSniffProperty($phpcsFile, $property, $value)
{
if (version_compare(Config::VERSION, '3.8.0', '>=')) {
$phpcsFile->ruleset->setSniffProperty(
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
$property,
[
'scope' => 'sniff',
'value' => $value,
]
);
} else {
$phpcsFile->ruleset->setSniffProperty(
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
$property,
$value
);
}
}
}
12 changes: 2 additions & 10 deletions Tests/VariableAnalysisSniff/ArrowFunctionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ public function testArrowFunctions()
{
$fixtureFile = $this->getFixture('ArrowFunctionFixture.php');
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
$phpcsFile->ruleset->setSniffProperty(
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
'allowUnusedParametersBeforeUsed',
'true'
);
$this->setSniffProperty($phpcsFile, 'allowUnusedParametersBeforeUsed', 'true');
$phpcsFile->process();
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
$expectedWarnings = [
Expand All @@ -41,11 +37,7 @@ public function testArrowFunctionsWithoutUnusedBeforeUsed()
{
$fixtureFile = $this->getFixture('ArrowFunctionFixture.php');
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
$phpcsFile->ruleset->setSniffProperty(
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
'allowUnusedParametersBeforeUsed',
'false'
);
$this->setSniffProperty($phpcsFile, 'allowUnusedParametersBeforeUsed', 'false');
$phpcsFile->process();
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
$expectedWarnings = [
Expand Down
18 changes: 3 additions & 15 deletions Tests/VariableAnalysisSniff/GlobalScopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ public function testGlobalScopeWarnings()
{
$fixtureFile = $this->getFixture('GlobalScopeFixture.php');
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
$phpcsFile->ruleset->setSniffProperty(
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
'allowUndefinedVariablesInFileScope',
'false'
);
$this->setSniffProperty($phpcsFile, 'allowUndefinedVariablesInFileScope', 'false');
$phpcsFile->process();
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
$expectedErrors = [
Expand All @@ -31,11 +27,7 @@ public function testGlobalScopeWarningsWithAllowUndefinedVariablesInFileScope()
{
$fixtureFile = $this->getFixture('GlobalScopeFixture.php');
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
$phpcsFile->ruleset->setSniffProperty(
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
'allowUndefinedVariablesInFileScope',
'true'
);
$this->setSniffProperty($phpcsFile, 'allowUndefinedVariablesInFileScope', 'true');
$phpcsFile->process();
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
$expectedErrors = [
Expand All @@ -51,11 +43,7 @@ public function testGlobalScopeWarningsWithAllowUnusedVariablesInFileScope()
{
$fixtureFile = $this->getFixture('GlobalScopeFixture.php');
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
$phpcsFile->ruleset->setSniffProperty(
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
'allowUnusedVariablesInFileScope',
'true'
);
$this->setSniffProperty($phpcsFile, 'allowUnusedVariablesInFileScope', 'true');
$phpcsFile->process();
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
$expectedErrors = [
Expand Down
36 changes: 6 additions & 30 deletions Tests/VariableAnalysisSniff/IfConditionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ public function testIfConditionWarnings()
{
$fixtureFile = $this->getFixture('FunctionWithIfConditionFixture.php');
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
$phpcsFile->ruleset->setSniffProperty(
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
'allowUnusedParametersBeforeUsed',
'true'
);
$this->setSniffProperty($phpcsFile, 'allowUnusedParametersBeforeUsed', 'true');
$phpcsFile->process();
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
$expectedWarnings = [
Expand Down Expand Up @@ -43,16 +39,8 @@ public function testIfConditionWarningsWithValidUndefinedVariableNames()
{
$fixtureFile = $this->getFixture('FunctionWithIfConditionFixture.php');
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
$phpcsFile->ruleset->setSniffProperty(
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
'validUndefinedVariableNames',
'second'
);
$phpcsFile->ruleset->setSniffProperty(
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
'allowUnusedParametersBeforeUsed',
'true'
);
$this->setSniffProperty($phpcsFile, 'validUndefinedVariableNames', 'second');
$this->setSniffProperty($phpcsFile, 'allowUnusedParametersBeforeUsed', 'true');
$phpcsFile->process();
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
$expectedWarnings = [
Expand All @@ -75,11 +63,7 @@ public function testInlineIfConditionWarnings()
{
$fixtureFile = $this->getFixture('FunctionWithInlineIfConditionFixture.php');
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
$phpcsFile->ruleset->setSniffProperty(
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
'allowUnusedParametersBeforeUsed',
'true'
);
$this->setSniffProperty($phpcsFile, 'allowUnusedParametersBeforeUsed', 'true');
$phpcsFile->process();
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
$expectedWarnings = [
Expand Down Expand Up @@ -110,16 +94,8 @@ public function testInlineIfConditionWarningsWithValidUndefinedVariableNames()
{
$fixtureFile = $this->getFixture('FunctionWithInlineIfConditionFixture.php');
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
$phpcsFile->ruleset->setSniffProperty(
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
'validUndefinedVariableNames',
'second'
);
$phpcsFile->ruleset->setSniffProperty(
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
'allowUnusedParametersBeforeUsed',
'true'
);
$this->setSniffProperty($phpcsFile, 'validUndefinedVariableNames', 'second');
$this->setSniffProperty($phpcsFile, 'allowUnusedParametersBeforeUsed', 'true');
$phpcsFile->process();
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
$expectedWarnings = [
Expand Down
12 changes: 2 additions & 10 deletions Tests/VariableAnalysisSniff/UnusedFollowedByRequireTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ public function testUnusedFollowedByRequireDoesNotWarnWhenSet()
{
$fixtureFile = $this->getFixture('UnusedFollowedByRequireFixture.php');
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
$phpcsFile->ruleset->setSniffProperty(
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
'allowUnusedVariablesBeforeRequire',
'true'
);
$this->setSniffProperty($phpcsFile, 'allowUnusedVariablesBeforeRequire', 'true');
$phpcsFile->process();
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
$expectedWarnings = [
Expand All @@ -53,11 +49,7 @@ public function testUnusedFollowedByRequireDoesNotBreakOtherThingsWhenSet()
{
$fixtureFile = $this->getFixture('FunctionWithoutParamFixture.php');
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
$phpcsFile->ruleset->setSniffProperty(
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
'allowUnusedVariablesBeforeRequire',
'true'
);
$this->setSniffProperty($phpcsFile, 'allowUnusedVariablesBeforeRequire', 'true');
$phpcsFile->process();
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
$expectedWarnings = [
Expand Down
Loading

0 comments on commit 71d9f67

Please sign in to comment.