From e8f5e7d6fc010630ed48ebd04fc9998b92831b4f Mon Sep 17 00:00:00 2001 From: Greg Sherwood Date: Wed, 20 Jul 2016 10:55:26 +1000 Subject: [PATCH] Unit tests can be run from a PEAR install again --- autoload.php | 9 +++++- package.xml | 4 ++- tests/AllTests.php | 16 +++++++--- tests/Core/AllTests.php | 6 +++- tests/Standards/AbstractSniffUnitTest.php | 10 ++++++- tests/Standards/AllSniffs.php | 36 ++++++++++++----------- 6 files changed, 56 insertions(+), 25 deletions(-) diff --git a/autoload.php b/autoload.php index 22f0346b6c..fc493cf3ae 100644 --- a/autoload.php +++ b/autoload.php @@ -51,7 +51,14 @@ public static function load($class) if (substr($class, 0, 16) === 'PHP_CodeSniffer\\') { if (substr($class, 0, 22) === 'PHP_CodeSniffer\Tests\\') { - $path = __DIR__.$ds.'tests'.$ds.substr(str_replace('\\', $ds, $class), 22).'.php'; + $isInstalled = !is_dir(__DIR__.$ds.'tests'); + if ($isInstalled === false) { + $path = __DIR__.$ds.'tests'; + } else { + $path = '@test_dir@'.$ds.'PHP_CodeSniffer'.$ds.'CodeSniffer'; + } + + $path .= $ds.substr(str_replace('\\', $ds, $class), 22).'.php'; } else { $path = __DIR__.$ds.'src'.$ds.substr(str_replace('\\', $ds, $class), 16).'.php'; } diff --git a/package.xml b/package.xml index 2c5f31e787..22753afe60 100644 --- a/package.xml +++ b/package.xml @@ -100,7 +100,9 @@ http://pear.php.net/dtd/package-2.0.xsd"> - + + + diff --git a/tests/AllTests.php b/tests/AllTests.php index b74403f3c8..bacde72adb 100644 --- a/tests/AllTests.php +++ b/tests/AllTests.php @@ -23,12 +23,19 @@ define('PHP_CODESNIFFER_VERBOSITY', 0); } -require_once __DIR__.'/../autoload.php'; +if (is_file(__DIR__.'/../autoload.php') === true) { + include_once __DIR__.'/../autoload.php'; + include_once 'Core/AllTests.php'; + include_once 'Standards/AllSniffs.php'; +} else { + include_once 'PHP/CodeSniffer/autoload.php'; + include_once 'CodeSniffer/Core/AllTests.php'; + include_once 'CodeSniffer/Standards/AllSniffs.php'; +} -$tokens = new Tokens(); +require_once 'TestSuite.php'; -require_once 'Core/AllTests.php'; -require_once 'Standards/AllSniffs.php'; +$tokens = new Tokens(); class PHP_CodeSniffer_AllTests { @@ -54,6 +61,7 @@ public static function main() public static function suite() { $GLOBALS['PHP_CODESNIFFER_STANDARD_DIRS'] = array(); + $GLOBALS['PHP_CODESNIFFER_TEST_DIRS'] = array(); // Use a special PHP_CodeSniffer test suite so that we can // unset our autoload function after the run. diff --git a/tests/Core/AllTests.php b/tests/Core/AllTests.php index 6fa46fc7e5..bffd7103ac 100644 --- a/tests/Core/AllTests.php +++ b/tests/Core/AllTests.php @@ -23,7 +23,11 @@ define('PHP_CODESNIFFER_VERBOSITY', 0); } -require_once __DIR__.'/../../autoload.php'; +if (is_file(__DIR__.'/../../autoload.php') === true) { + include_once __DIR__.'/../../autoload.php'; +} else { + include_once 'PHP/CodeSniffer/autoload.php'; +} $tokens = new Tokens(); diff --git a/tests/Standards/AbstractSniffUnitTest.php b/tests/Standards/AbstractSniffUnitTest.php index c3bbc7ed43..2a029ad358 100644 --- a/tests/Standards/AbstractSniffUnitTest.php +++ b/tests/Standards/AbstractSniffUnitTest.php @@ -39,6 +39,13 @@ abstract class AbstractSniffUnitTest extends \PHPUnit_Framework_TestCase */ public $standardsDir = null; + /** + * The path to the standard's test directory. + * + * @var string + */ + public $testsDir = null; + /** * Sets up this unit test. @@ -49,6 +56,7 @@ protected function setUp() { $class = get_class($this); $this->standardsDir = $GLOBALS['PHP_CODESNIFFER_STANDARD_DIRS'][$class]; + $this->testsDir = $GLOBALS['PHP_CODESNIFFER_TEST_DIRS'][$class]; }//end setUp() @@ -81,7 +89,7 @@ final public function testSniff() $sniffCode = Common::getSniffCode(get_class($this)); list($standardName, $categoryName, $sniffName) = explode('.', $sniffCode); - $testFileBase = $this->standardsDir.DIRECTORY_SEPARATOR.'Tests'.DIRECTORY_SEPARATOR.$categoryName.DIRECTORY_SEPARATOR.$sniffName.'UnitTest.'; + $testFileBase = $this->testsDir.$categoryName.DIRECTORY_SEPARATOR.$sniffName.'UnitTest.'; // Get a list of all test files to check. These will have the same base // name but different extensions. We ignore the .php file as it is the class. diff --git a/tests/Standards/AllSniffs.php b/tests/Standards/AllSniffs.php index fc3af25269..af553bcdc0 100644 --- a/tests/Standards/AllSniffs.php +++ b/tests/Standards/AllSniffs.php @@ -12,6 +12,7 @@ use PHP_CodeSniffer\Util\Tokens; use PHP_CodeSniffer\Util\Standards; use PHP_CodeSniffer\Autoload; +use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; if (defined('PHP_CODESNIFFER_IN_TESTS') === false) { define('PHP_CODESNIFFER_IN_TESTS', true); @@ -25,7 +26,11 @@ define('PHP_CODESNIFFER_VERBOSITY', 0); } -require_once __DIR__.'/../../autoload.php'; +if (is_file(__DIR__.'/../../autoload.php') === true) { + include_once __DIR__.'/../../autoload.php'; +} else { + include_once 'PHP/CodeSniffer/autoload.php'; +} $tokens = new Tokens(); @@ -60,38 +65,34 @@ public static function suite() $suite = new \PHPUnit_Framework_TestSuite('PHP CodeSniffer Standards'); - /* - $isInstalled = !is_file(dirname(__FILE__).'/../../CodeSniffer.php'); - */ + $isInstalled = !is_file(__DIR__.'/../../autoload.php'); $installedPaths = Standards::getInstalledStandardPaths(); + foreach ($installedPaths as $path) { $standards = Standards::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__); - } - */ + // 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) { + $testPath = realpath(__DIR__.'/../../src/Standards'); + } else { + $testPath = $path; + } foreach ($standards as $standard) { $standardDir = $path.DIRECTORY_SEPARATOR.$standard; - $testsDir = $standardDir.DIRECTORY_SEPARATOR.'Tests'.DIRECTORY_SEPARATOR; + $testsDir = $testPath.DIRECTORY_SEPARATOR.$standard.DIRECTORY_SEPARATOR.'Tests'.DIRECTORY_SEPARATOR; if (is_dir($testsDir) === false) { // Check if the installed path is actually a standard itself. $standardDir = $path; - $testsDir = $standardDir.DIRECTORY_SEPARATOR.'Tests'.DIRECTORY_SEPARATOR; + $testsDir = $testPath.DIRECTORY_SEPARATOR.'Tests'.DIRECTORY_SEPARATOR; if (is_dir($testsDir) === false) { // No tests for this standard. continue; } - } else { } $di = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($testsDir)); @@ -111,6 +112,7 @@ public static function suite() $className = Autoload::loadFile($file->getPathname()); $GLOBALS['PHP_CODESNIFFER_STANDARD_DIRS'][$className] = $standardDir; + $GLOBALS['PHP_CODESNIFFER_TEST_DIRS'][$className] = $testsDir; $suite->addTestSuite($className); } }//end foreach