Skip to content

Commit

Permalink
PHPCS 3.x compat: Get the unit tests working on both PHPCS 2.x as wel…
Browse files Browse the repository at this point in the history
…l as 3.x

The principle of how the unit tests will be run is now significantly different for PHPCS 2.x vs 3.x.

### PHPCS 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. See: squizlabs/PHP_CodeSniffer/issues/1564

### PHPCS 2.x:
* 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.

### Bringing it all together.
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.
  • Loading branch information
jrfnl committed Jul 23, 2017
1 parent c37e712 commit de30363
Show file tree
Hide file tree
Showing 7 changed files with 857 additions and 0 deletions.
72 changes: 72 additions & 0 deletions Test/AllTests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* A test class for running all PHP_CodeSniffer unit tests.
*
* PHP version 5
*
* {@internal WPCS: File copied from PHPCS 2.x to overload one method.}}
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <[email protected]>
* @author Marc McIntyre <[email protected]>
* @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 WordPressCS\Test\AllSniffs;
use PHP_CodeSniffer_AllTests;
use PHP_CodeSniffer_TestSuite;
/* End of WPCS adjustment */

/**
* A test class for running all PHP_CodeSniffer unit tests.
*
* Usage: phpunit AllTests.php
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <[email protected]>
* @author Marc McIntyre <[email protected]>
* @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 AllTests extends PHP_CodeSniffer_AllTests {

/**
* Add all PHP_CodeSniffer test suites into a single test suite.
*
* @return PHPUnit_Framework_TestSuite
*/
public static function suite()
{
$GLOBALS['PHP_CODESNIFFER_STANDARD_DIRS'] = array();

// Use a special PHP_CodeSniffer test suite so that we can
// unset our autoload function after the run.
$suite = new PHP_CodeSniffer_TestSuite('PHP CodeSniffer');

/* Start of WPCS adjustment */
// We need to point to the WPCS version of the referenced class
// and we may as well bypass the loading of the PHPCS core unit tests
// while we're at it too.
$suite->addTest(AllSniffs::suite());
/* End of WPCS adjustment */

// Unregister this here because the PEAR tester loads
// all package suites before running then, so our autoloader
// will cause problems for the packages included after us.
spl_autoload_unregister(array('PHP_CodeSniffer', 'autoload'));

return $suite;

}//end suite()


}//end class
Loading

0 comments on commit de30363

Please sign in to comment.