mirror of
https://gh.wpcy.net/https://github.com/WordPress/WordPress-Coding-Standards.git
synced 2026-04-27 01:12:39 +08:00
The principle of how the unit tests will be run is now significantly different for PHPCS 2.x vs 3.x. * To get the unit tests running, we need to make sure that our class aliases are in place, however to alias the PHPCS native classes, PHP needs to be able to find them, so we need to load the PHPCS native autoloader first. * Additionally, the PHPCS native autoloader has a bug which means that non-sniff classes (abstracts, helper class) are not loaded correctly. This bug is fixed in PHPCS `master`, but is not contained (yet) in a released version. As a work-around, a WPCS autoloader is registered to make sure our classes can be loaded anyway. This work-around can be removed once the WPCS minimum required PHPCS 3.x version goes up to 3.1.0. See: squizlabs/PHP_CodeSniffer/issues/1564 * The PHPCS 2.x unit test suite does not handle namespaced unit test classes. Or rather: it is not capable of translating the namespaced unit test classes to the namespaced sniff they belong to. * To work around this, two small snippets of code need to be added to the PHPCS 2.x unit test classes for which we overload select methods. * However, one of the methods we need to overload is declared as `final`, so for the `AbstractSniffUnitTest` class, we need a complete copy of the file and cannot just overload the relevant method. * The changes made to the classes are fenced with inline comments annotating the change. * The code style of these copied in classes has been left as is and these files will be excluded from the WPCS code style check. * The WPCS version of the `AbstractSniffUnitTest` class is aliased to the PHPCS 3.x name via the PHPUnit bootstrap, so all the unit test classes can already use the PHPCS 3.x class name in their `use` statements. This also means that the command to run the unit tests will be different in PHPCS 2.x vs 3.x: ``` For running the unit tests with PHPCS 3.x: phpunit --bootstrap="./Test/phpcs3-bootstrap.php" --filter WordPress /path/to/PHP_CodeSniffer/tests/AllTests.php For running the unit tests with PHPCS 2.x: phpunit --bootstrap="./Test/phpcs2-bootstrap.php" --filter WordPress ./Test/AllTests.php ``` The contributing instructions will be updated to reflect this. In travis, a `PHPCS_BRANCH` environment variable will be available, so there is also a generic `bootstrap.php` file which toggles between the version specific bootstraps based on the environment variable.
157 lines
6 KiB
PHP
157 lines
6 KiB
PHP
<?php
|
|
/**
|
|
* A test class for testing all sniffs for installed standards.
|
|
*
|
|
* PHP version 5
|
|
*
|
|
* {@internal WPCS: File copied from PHPCS 2.x to overload one method.}}
|
|
*
|
|
* @category PHP
|
|
* @package PHP_CodeSniffer
|
|
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
* @author Marc McIntyre <mmcintyre@squiz.net>
|
|
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
|
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
*/
|
|
|
|
/* Start of WPCS adjustment */
|
|
namespace WordPressCS\Test;
|
|
|
|
use PHP_CodeSniffer_Standards_AllSniffs;
|
|
use PHP_CodeSniffer;
|
|
use PHPUnit_Framework_TestSuite;
|
|
use RecursiveIteratorIterator;
|
|
use RecursiveDirectoryIterator;
|
|
/* End of WPCS adjustment */
|
|
|
|
/**
|
|
* A test class for testing all sniffs for installed standards.
|
|
*
|
|
* Usage: phpunit AllSniffs.php
|
|
*
|
|
* This test class loads all unit tests for all installed standards into a
|
|
* single test suite and runs them. Errors are reported on the command line.
|
|
*
|
|
* @category PHP
|
|
* @package PHP_CodeSniffer
|
|
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
* @author Marc McIntyre <mmcintyre@squiz.net>
|
|
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
|
* @version Release: @package_version@
|
|
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
*/
|
|
class AllSniffs extends PHP_CodeSniffer_Standards_AllSniffs
|
|
{
|
|
|
|
/**
|
|
* Add all sniff unit tests into a test suite.
|
|
*
|
|
* Sniff unit tests are found by recursing through the 'Tests' directory
|
|
* of each installed coding standard.
|
|
*
|
|
* @return PHPUnit_Framework_TestSuite
|
|
*/
|
|
public static function suite()
|
|
{
|
|
$suite = new PHPUnit_Framework_TestSuite('PHP CodeSniffer Standards');
|
|
|
|
/* Start of WPCS adjustment */
|
|
// Set the correct path to PHPCS.
|
|
$isInstalled = !is_file(PHPCS_DIR.'/CodeSniffer.php');
|
|
/* End of WPCS adjustment */
|
|
|
|
// Optionally allow for ignoring the tests for one or more standards.
|
|
$ignoreTestsForStandards = getenv('PHPCS_IGNORE_TESTS');
|
|
if ($ignoreTestsForStandards === false) {
|
|
$ignoreTestsForStandards = array();
|
|
} else {
|
|
$ignoreTestsForStandards = explode(',', $ignoreTestsForStandards);
|
|
}
|
|
|
|
$installedPaths = PHP_CodeSniffer::getInstalledStandardPaths();
|
|
foreach ($installedPaths as $path) {
|
|
$path = realpath($path);
|
|
$origPath = $path;
|
|
$standards = PHP_CodeSniffer::getInstalledStandards(true, $path);
|
|
|
|
// If the test is running PEAR installed, the built-in standards
|
|
// are split into different directories; one for the sniffs and
|
|
// a different file system location for tests.
|
|
if ($isInstalled === true
|
|
&& is_dir($path.DIRECTORY_SEPARATOR.'Generic') === true
|
|
) {
|
|
$path = dirname(__FILE__);
|
|
}
|
|
|
|
foreach ($standards as $standard) {
|
|
if (in_array($standard, $ignoreTestsForStandards, true)) {
|
|
continue;
|
|
}
|
|
|
|
$testsDir = $path.DIRECTORY_SEPARATOR.$standard.DIRECTORY_SEPARATOR.'Tests'.DIRECTORY_SEPARATOR;
|
|
|
|
if (is_dir($testsDir) === false) {
|
|
// No tests for this standard.
|
|
continue;
|
|
}
|
|
|
|
$di = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($testsDir));
|
|
|
|
foreach ($di as $file) {
|
|
// Skip hidden files.
|
|
if (substr($file->getFilename(), 0, 1) === '.') {
|
|
continue;
|
|
}
|
|
|
|
// Tests must have the extension 'php'.
|
|
$parts = explode('.', $file);
|
|
$ext = array_pop($parts);
|
|
if ($ext !== 'php') {
|
|
continue;
|
|
}
|
|
|
|
$filePath = $file->getPathname();
|
|
$className = str_replace($path.DIRECTORY_SEPARATOR, '', $filePath);
|
|
$className = substr($className, 0, -4);
|
|
$className = str_replace(DIRECTORY_SEPARATOR, '_', $className);
|
|
|
|
// Include the sniff here so tests can use it in their setup() methods.
|
|
$parts = explode('_', $className);
|
|
if (isset($parts[0],$parts[2],$parts[3]) === true) {
|
|
$sniffPath = $origPath.DIRECTORY_SEPARATOR.$parts[0].DIRECTORY_SEPARATOR.'Sniffs'.DIRECTORY_SEPARATOR.$parts[2].DIRECTORY_SEPARATOR.$parts[3];
|
|
$sniffPath = substr($sniffPath, 0, -8).'Sniff.php';
|
|
|
|
if (file_exists($sniffPath) === true) {
|
|
include_once $sniffPath;
|
|
include_once $filePath;
|
|
|
|
/* Start of WPCS adjustment */
|
|
// Support the use of PHP namespaces. If the class name we included
|
|
// contains namespace separators instead of underscores, use this as the
|
|
// class name from now on.
|
|
$classNameNS = str_replace('_', '\\', $className);
|
|
if (class_exists($classNameNS, false) === true) {
|
|
$className = $classNameNS;
|
|
}
|
|
/* End of WPCS adjustment */
|
|
|
|
$GLOBALS['PHP_CODESNIFFER_STANDARD_DIRS'][$className] = $path;
|
|
$suite->addTestSuite($className);
|
|
} else {
|
|
self::$orphanedTests[] = $filePath;
|
|
}
|
|
} else {
|
|
self::$orphanedTests[] = $filePath;
|
|
}
|
|
}//end foreach
|
|
}//end foreach
|
|
}//end foreach
|
|
|
|
return $suite;
|
|
|
|
}//end suite()
|
|
|
|
|
|
}//end class
|