Skip to content

Commit 8c77b05

Browse files
committed
PHPCSDebug: fix tests
PHPCSUtils 1.0.9 made a change to the `UtilityMethodTestCase` which causes the selected sniff to no longer register its token listeners as the `installed_paths` from the `CodeSniffer.conf` file are no longer read out. While this is fine for actual utility methods, it breaks the tests for the `PHPCSDebug.Debug.TokenList` test. Fixed now. Refs: * https://github.com/PHPCSStandards/PHPCSUtils/releases/tag/1.0.9 * PHPCSStandards/PHPCSUtils#525
1 parent aa6f5d6 commit 8c77b05

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

PHPCSDebug/Tests/Debug/TokenListCssTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace PHPCSDebug\Tests\Debug;
1212

13+
use PHP_CodeSniffer\Util\Common;
1314
use PHPCSUtils\TestUtils\UtilityMethodTestCase;
1415

1516
/**
@@ -115,6 +116,17 @@ public function testOutput()
115116
$this->expectOutputString($expected);
116117
$this->setOutputCallback([$this, 'normalizeLineEndings']);
117118

119+
if (empty($this->ruleset->tokenListeners)) {
120+
// PHPCSUtils 1.0.9+.
121+
$sniffFile = \dirname(\dirname(__DIR__)) . \DIRECTORY_SEPARATOR . 'Sniffs';
122+
$sniffFile .= \DIRECTORY_SEPARATOR . 'Debug' . \DIRECTORY_SEPARATOR . 'TokenListSniff.php';
123+
$sniffClassName = Common::cleanSniffClass('PHPCSDebug\\Sniffs\\Debug\\TokenListSniff');
124+
125+
$restrictions = [\strtolower($sniffClassName) => true];
126+
self::$phpcsFile->ruleset->registerSniffs([$sniffFile], $restrictions, []);
127+
self::$phpcsFile->ruleset->populateTokenListeners();
128+
}
129+
118130
self::$phpcsFile->process();
119131
}
120132

PHPCSDebug/Tests/Debug/TokenListJsTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace PHPCSDebug\Tests\Debug;
1212

13+
use PHP_CodeSniffer\Util\Common;
1314
use PHPCSUtils\TestUtils\UtilityMethodTestCase;
1415

1516
/**
@@ -135,6 +136,17 @@ public function testOutput()
135136
$this->expectOutputString($expected);
136137
$this->setOutputCallback([$this, 'normalizeLineEndings']);
137138

139+
if (empty($this->ruleset->tokenListeners)) {
140+
// PHPCSUtils 1.0.9+.
141+
$sniffFile = \dirname(\dirname(__DIR__)) . \DIRECTORY_SEPARATOR . 'Sniffs';
142+
$sniffFile .= \DIRECTORY_SEPARATOR . 'Debug' . \DIRECTORY_SEPARATOR . 'TokenListSniff.php';
143+
$sniffClassName = Common::cleanSniffClass('PHPCSDebug\\Sniffs\\Debug\\TokenListSniff');
144+
145+
$restrictions = [\strtolower($sniffClassName) => true];
146+
self::$phpcsFile->ruleset->registerSniffs([$sniffFile], $restrictions, []);
147+
self::$phpcsFile->ruleset->populateTokenListeners();
148+
}
149+
138150
self::$phpcsFile->process();
139151
}
140152

PHPCSDebug/Tests/Debug/TokenListUnitTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace PHPCSDebug\Tests\Debug;
1212

13+
use PHP_CodeSniffer\Util\Common;
1314
use PHPCSUtils\TestUtils\UtilityMethodTestCase;
1415

1516
/**
@@ -108,6 +109,17 @@ public function testOutput()
108109
$this->expectOutputString($expected);
109110
$this->setOutputCallback([$this, 'normalizeLineEndings']);
110111

112+
if (empty($this->ruleset->tokenListeners)) {
113+
// PHPCSUtils 1.0.9+.
114+
$sniffFile = \dirname(\dirname(__DIR__)) . \DIRECTORY_SEPARATOR . 'Sniffs';
115+
$sniffFile .= \DIRECTORY_SEPARATOR . 'Debug' . \DIRECTORY_SEPARATOR . 'TokenListSniff.php';
116+
$sniffClassName = Common::cleanSniffClass('PHPCSDebug\\Sniffs\\Debug\\TokenListSniff');
117+
118+
$restrictions = [\strtolower($sniffClassName) => true];
119+
self::$phpcsFile->ruleset->registerSniffs([$sniffFile], $restrictions, []);
120+
self::$phpcsFile->ruleset->populateTokenListeners();
121+
}
122+
111123
self::$phpcsFile->process();
112124
}
113125

phpunit-bootstrap.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@
7373
die(1);
7474
}
7575

76+
$installedStandards = \PHP_CodeSniffer\Util\Standards::getInstalledStandardDetails();
77+
foreach ($installedStandards as $details) {
78+
\PHP_CodeSniffer\Autoload::addSearchPath($details['path'], $details['namespace']);
79+
}
80+
7681
// Try and load the PHPCSUtils autoloader.
7782
if ($phpcsUtilsDir !== false && \file_exists($phpcsUtilsDir . $ds . 'phpcsutils-autoload.php')) {
7883
require_once $phpcsUtilsDir . $ds . 'phpcsutils-autoload.php';

0 commit comments

Comments
 (0)