WordPress-Coding-Standards/Tests/bootstrap.php
jrfnl a9d6ed3651 Tests: add a bootstrap file
While not strictly necessary, it makes life easier to have this bootstrap in place.

The bootstrap file will:
* Load the PHPCS autoloader (was previously done from the command-line);
* Load the Composer autoload file.
* Will automatically set the `PHPCS_IGNORE_TESTS` environment variable to the correct value to prevent running tests from other standards.
2020-06-30 19:33:42 +02:00

85 lines
2.6 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 );
}
/*
* 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 );