Skip to content

Commit

Permalink
PHPCSDebug: fix tests
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jrfnl committed Mar 4, 2024
1 parent aa6f5d6 commit 8c77b05
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
12 changes: 12 additions & 0 deletions PHPCSDebug/Tests/Debug/TokenListCssTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace PHPCSDebug\Tests\Debug;

use PHP_CodeSniffer\Util\Common;
use PHPCSUtils\TestUtils\UtilityMethodTestCase;

/**
Expand Down Expand Up @@ -115,6 +116,17 @@ public function testOutput()
$this->expectOutputString($expected);
$this->setOutputCallback([$this, 'normalizeLineEndings']);

if (empty($this->ruleset->tokenListeners)) {
// PHPCSUtils 1.0.9+.
$sniffFile = \dirname(\dirname(__DIR__)) . \DIRECTORY_SEPARATOR . 'Sniffs';
$sniffFile .= \DIRECTORY_SEPARATOR . 'Debug' . \DIRECTORY_SEPARATOR . 'TokenListSniff.php';
$sniffClassName = Common::cleanSniffClass('PHPCSDebug\\Sniffs\\Debug\\TokenListSniff');

$restrictions = [\strtolower($sniffClassName) => true];
self::$phpcsFile->ruleset->registerSniffs([$sniffFile], $restrictions, []);
self::$phpcsFile->ruleset->populateTokenListeners();
}

self::$phpcsFile->process();
}

Expand Down
12 changes: 12 additions & 0 deletions PHPCSDebug/Tests/Debug/TokenListJsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace PHPCSDebug\Tests\Debug;

use PHP_CodeSniffer\Util\Common;
use PHPCSUtils\TestUtils\UtilityMethodTestCase;

/**
Expand Down Expand Up @@ -135,6 +136,17 @@ public function testOutput()
$this->expectOutputString($expected);
$this->setOutputCallback([$this, 'normalizeLineEndings']);

if (empty($this->ruleset->tokenListeners)) {
// PHPCSUtils 1.0.9+.
$sniffFile = \dirname(\dirname(__DIR__)) . \DIRECTORY_SEPARATOR . 'Sniffs';
$sniffFile .= \DIRECTORY_SEPARATOR . 'Debug' . \DIRECTORY_SEPARATOR . 'TokenListSniff.php';
$sniffClassName = Common::cleanSniffClass('PHPCSDebug\\Sniffs\\Debug\\TokenListSniff');

$restrictions = [\strtolower($sniffClassName) => true];
self::$phpcsFile->ruleset->registerSniffs([$sniffFile], $restrictions, []);
self::$phpcsFile->ruleset->populateTokenListeners();
}

self::$phpcsFile->process();
}

Expand Down
12 changes: 12 additions & 0 deletions PHPCSDebug/Tests/Debug/TokenListUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace PHPCSDebug\Tests\Debug;

use PHP_CodeSniffer\Util\Common;
use PHPCSUtils\TestUtils\UtilityMethodTestCase;

/**
Expand Down Expand Up @@ -108,6 +109,17 @@ public function testOutput()
$this->expectOutputString($expected);
$this->setOutputCallback([$this, 'normalizeLineEndings']);

if (empty($this->ruleset->tokenListeners)) {
// PHPCSUtils 1.0.9+.
$sniffFile = \dirname(\dirname(__DIR__)) . \DIRECTORY_SEPARATOR . 'Sniffs';
$sniffFile .= \DIRECTORY_SEPARATOR . 'Debug' . \DIRECTORY_SEPARATOR . 'TokenListSniff.php';
$sniffClassName = Common::cleanSniffClass('PHPCSDebug\\Sniffs\\Debug\\TokenListSniff');

$restrictions = [\strtolower($sniffClassName) => true];
self::$phpcsFile->ruleset->registerSniffs([$sniffFile], $restrictions, []);
self::$phpcsFile->ruleset->populateTokenListeners();
}

self::$phpcsFile->process();
}

Expand Down
5 changes: 5 additions & 0 deletions phpunit-bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@
die(1);
}

$installedStandards = \PHP_CodeSniffer\Util\Standards::getInstalledStandardDetails();
foreach ($installedStandards as $details) {
\PHP_CodeSniffer\Autoload::addSearchPath($details['path'], $details['namespace']);
}

// Try and load the PHPCSUtils autoloader.
if ($phpcsUtilsDir !== false && \file_exists($phpcsUtilsDir . $ds . 'phpcsutils-autoload.php')) {
require_once $phpcsUtilsDir . $ds . 'phpcsutils-autoload.php';
Expand Down

0 comments on commit 8c77b05

Please sign in to comment.