Skip to content

Commit

Permalink
Unit tests can be run from a PEAR install again
Browse files Browse the repository at this point in the history
  • Loading branch information
gsherwood committed Jul 20, 2016
1 parent 4b8aa0d commit e8f5e7d
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 25 deletions.
9 changes: 8 additions & 1 deletion autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand Down
4 changes: 3 additions & 1 deletion package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
</notes>
<contents>
<dir name="/">
<file baseinstalldir="PHP/CodeSniffer" name="autoload.php" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="autoload.php" role="php">
<tasks:replace from="@test_dir@" to="test_dir" type="pear-config" />
</file>
<file baseinstalldir="PHP/CodeSniffer" name="CodeSniffer.conf.dist" role="data" />
<file baseinstalldir="PHP/CodeSniffer" name="README.md" role="doc" />
<file baseinstalldir="PHP/CodeSniffer" name="CONTRIBUTING.md" role="doc" />
Expand Down
16 changes: 12 additions & 4 deletions tests/AllTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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.
Expand Down
6 changes: 5 additions & 1 deletion tests/Core/AllTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
10 changes: 9 additions & 1 deletion tests/Standards/AbstractSniffUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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()

Expand Down Expand Up @@ -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.
Expand Down
36 changes: 19 additions & 17 deletions tests/Standards/AllSniffs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();

Expand Down Expand Up @@ -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));
Expand All @@ -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
Expand Down

0 comments on commit e8f5e7d

Please sign in to comment.