mirror of
https://github.com/WordPress/WordPress-Coding-Standards.git
synced 2026-07-26 10:26:58 +08:00
As of PHPCS 4.0, the base sniff test class has been renamed from `AbstractSniffUnitTest` to `AbstractSniffTestCase`. Additionally, the PHPCS test setup no longer uses the outdated custom test suite creation. This means that to allow for the tests to run on both PHPCS 3.x and 4.x, some changes are needed. This commit handles this by: * Changing all test files to `extend` the new test case class and adding a class alias to the test bootstrap for compatibility with PHPCS 3.x. * Adding separate scripts to the `composer.json` file for invoking the tests on PHPCS 3.x vs 4.x. * Updating the minimum PHPUnit 9 version to 9.3.4 as required by the PHPCS 4.x native test framework. * Add GH Actions jobs to test against PHPCS 4.x to all `test` matrices. * Updating the `quicktest` and `unit-tests` GH Actions jobs to use the correct Composer script based on the installed PHPCS version. * Run the `basic-qa` code-sniff and ruleset-test jobs against both PHPCS 3.x-dev and 4.x-dev. * Update `basic-qa` to accept the phpcbf NON_FIXABLE exit code (2) on PHPCS 4.x in the fixer-conflict check. PHPCS 4.0 reworked the phpcbf exit codes into a bitmask. _Note: even though PHPCS 4.x supports PHPUnit 10 and 11, we cannot widen the version restrictions for PHPUnit (yet) while PHPCS 3.x is also supported as it would lead to PHPUnit 10/11 being installed for PHPCS 3.x on PHP >= 8.1, which would break the test runs._ --------- Co-authored-by: Juliette <663378+jrfnl@users.noreply.github.com>
95 lines
3 KiB
PHP
95 lines
3 KiB
PHP
<?php
|
|
/**
|
|
* WordPress Coding Standard.
|
|
*
|
|
* Bootstrap file for running the tests.
|
|
*
|
|
* - Load the PHPCS PHPUnit bootstrap file providing cross-version PHPUnit support.
|
|
* {@link https://github.com/squizlabs/PHP_CodeSniffer/pull/1384}
|
|
* - Load the Composer autoload file.
|
|
* - Automatically limit the testing to the WordPressCS tests.
|
|
*
|
|
* @package WPCS\WordPressCodingStandards
|
|
* @link https://github.com/WordPress/WordPress-Coding-Standards
|
|
* @license https://opensource.org/licenses/MIT MIT
|
|
*/
|
|
|
|
if ( ! defined( 'PHP_CODESNIFFER_IN_TESTS' ) ) {
|
|
define( 'PHP_CODESNIFFER_IN_TESTS', true );
|
|
}
|
|
|
|
$ds = DIRECTORY_SEPARATOR;
|
|
|
|
/*
|
|
* Load the necessary PHPCS files.
|
|
*/
|
|
// Get the PHPCS dir from an environment variable.
|
|
$phpcsDir = getenv( 'PHPCS_DIR' );
|
|
$composerPHPCSPath = dirname( __DIR__ ) . $ds . 'vendor' . $ds . 'squizlabs' . $ds . 'php_codesniffer';
|
|
|
|
if ( false === $phpcsDir && is_dir( $composerPHPCSPath ) ) {
|
|
// PHPCS installed via Composer.
|
|
$phpcsDir = $composerPHPCSPath;
|
|
} elseif ( false !== $phpcsDir ) {
|
|
/*
|
|
* PHPCS in a custom directory.
|
|
* For this to work, the `PHPCS_DIR` needs to be set in a custom `phpunit.xml` file.
|
|
*/
|
|
$phpcsDir = realpath( $phpcsDir );
|
|
}
|
|
|
|
// Try and load the PHPCS autoloader.
|
|
if ( false !== $phpcsDir
|
|
&& file_exists( $phpcsDir . $ds . 'autoload.php' )
|
|
&& file_exists( $phpcsDir . $ds . 'tests' . $ds . 'bootstrap.php' )
|
|
) {
|
|
require_once $phpcsDir . $ds . 'autoload.php';
|
|
require_once $phpcsDir . $ds . 'tests' . $ds . 'bootstrap.php'; // PHPUnit 6.x+ support.
|
|
} else {
|
|
echo 'Uh oh... can\'t find PHPCS.
|
|
|
|
If you use Composer, please run `composer install`.
|
|
Otherwise, make sure you set a `PHPCS_DIR` environment variable in your phpunit.xml file
|
|
pointing to the PHPCS directory and that PHPCSUtils is included in the `installed_paths`
|
|
for that PHPCS install.
|
|
';
|
|
|
|
die( 1 );
|
|
}
|
|
|
|
// Alias the PHPCS 3.x test case to the PHPCS 4.x name.
|
|
if ( class_exists( 'PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest' ) === true
|
|
&& class_exists( 'PHP_CodeSniffer\Tests\Standards\AbstractSniffTestCase' ) === false
|
|
) {
|
|
class_alias(
|
|
'PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest',
|
|
'PHP_CodeSniffer\Tests\Standards\AbstractSniffTestCase'
|
|
);
|
|
}
|
|
|
|
/*
|
|
* Set the PHPCS_IGNORE_TEST environment variable to ignore tests from other standards.
|
|
*/
|
|
$wpcsStandards = array(
|
|
'WordPress' => true,
|
|
);
|
|
|
|
$allStandards = PHP_CodeSniffer\Util\Standards::getInstalledStandards();
|
|
$allStandards[] = 'Generic';
|
|
|
|
$standardsToIgnore = array();
|
|
foreach ( $allStandards as $standard ) {
|
|
if ( isset( $wpcsStandards[ $standard ] ) === true ) {
|
|
continue;
|
|
}
|
|
|
|
$standardsToIgnore[] = $standard;
|
|
}
|
|
|
|
$standardsToIgnoreString = implode( ',', $standardsToIgnore );
|
|
|
|
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.runtime_configuration_putenv -- This is not production, but test code.
|
|
putenv( "PHPCS_IGNORE_TESTS={$standardsToIgnoreString}" );
|
|
|
|
// Clean up.
|
|
unset( $ds, $phpcsDir, $composerPHPCSPath, $allStandards, $standardsToIgnore, $standard, $standardsToIgnoreString );
|