Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
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.
- Loading branch information
Showing
with
88 additions
and 7 deletions.
- +1 −0 .gitattributes
- +85 −0 Tests/bootstrap.php
- +1 −1 composer.json
- +1 −6 phpunit.xml.dist
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -9,6 +9,7 @@ | ||
/phpunit.xml.dist export-ignore | ||
/.github export-ignore | ||
/bin export-ignore | ||
/Tests export-ignore | ||
/WordPress/Tests export-ignore | ||
|
||
# | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,85 @@ | ||
<?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 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters