Skip to content

Commit e8f5e7d

Browse files
committed
Unit tests can be run from a PEAR install again
1 parent 4b8aa0d commit e8f5e7d

File tree

6 files changed

+56
-25
lines changed

6 files changed

+56
-25
lines changed

autoload.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,14 @@ public static function load($class)
5151

5252
if (substr($class, 0, 16) === 'PHP_CodeSniffer\\') {
5353
if (substr($class, 0, 22) === 'PHP_CodeSniffer\Tests\\') {
54-
$path = __DIR__.$ds.'tests'.$ds.substr(str_replace('\\', $ds, $class), 22).'.php';
54+
$isInstalled = !is_dir(__DIR__.$ds.'tests');
55+
if ($isInstalled === false) {
56+
$path = __DIR__.$ds.'tests';
57+
} else {
58+
$path = '@test_dir@'.$ds.'PHP_CodeSniffer'.$ds.'CodeSniffer';
59+
}
60+
61+
$path .= $ds.substr(str_replace('\\', $ds, $class), 22).'.php';
5562
} else {
5663
$path = __DIR__.$ds.'src'.$ds.substr(str_replace('\\', $ds, $class), 16).'.php';
5764
}

package.xml

+3-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
100100
</notes>
101101
<contents>
102102
<dir name="/">
103-
<file baseinstalldir="PHP/CodeSniffer" name="autoload.php" role="php" />
103+
<file baseinstalldir="PHP/CodeSniffer" name="autoload.php" role="php">
104+
<tasks:replace from="@test_dir@" to="test_dir" type="pear-config" />
105+
</file>
104106
<file baseinstalldir="PHP/CodeSniffer" name="CodeSniffer.conf.dist" role="data" />
105107
<file baseinstalldir="PHP/CodeSniffer" name="README.md" role="doc" />
106108
<file baseinstalldir="PHP/CodeSniffer" name="CONTRIBUTING.md" role="doc" />

tests/AllTests.php

+12-4
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,19 @@
2323
define('PHP_CODESNIFFER_VERBOSITY', 0);
2424
}
2525

26-
require_once __DIR__.'/../autoload.php';
26+
if (is_file(__DIR__.'/../autoload.php') === true) {
27+
include_once __DIR__.'/../autoload.php';
28+
include_once 'Core/AllTests.php';
29+
include_once 'Standards/AllSniffs.php';
30+
} else {
31+
include_once 'PHP/CodeSniffer/autoload.php';
32+
include_once 'CodeSniffer/Core/AllTests.php';
33+
include_once 'CodeSniffer/Standards/AllSniffs.php';
34+
}
2735

28-
$tokens = new Tokens();
36+
require_once 'TestSuite.php';
2937

30-
require_once 'Core/AllTests.php';
31-
require_once 'Standards/AllSniffs.php';
38+
$tokens = new Tokens();
3239

3340
class PHP_CodeSniffer_AllTests
3441
{
@@ -54,6 +61,7 @@ public static function main()
5461
public static function suite()
5562
{
5663
$GLOBALS['PHP_CODESNIFFER_STANDARD_DIRS'] = array();
64+
$GLOBALS['PHP_CODESNIFFER_TEST_DIRS'] = array();
5765

5866
// Use a special PHP_CodeSniffer test suite so that we can
5967
// unset our autoload function after the run.

tests/Core/AllTests.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@
2323
define('PHP_CODESNIFFER_VERBOSITY', 0);
2424
}
2525

26-
require_once __DIR__.'/../../autoload.php';
26+
if (is_file(__DIR__.'/../../autoload.php') === true) {
27+
include_once __DIR__.'/../../autoload.php';
28+
} else {
29+
include_once 'PHP/CodeSniffer/autoload.php';
30+
}
2731

2832
$tokens = new Tokens();
2933

tests/Standards/AbstractSniffUnitTest.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ abstract class AbstractSniffUnitTest extends \PHPUnit_Framework_TestCase
3939
*/
4040
public $standardsDir = null;
4141

42+
/**
43+
* The path to the standard's test directory.
44+
*
45+
* @var string
46+
*/
47+
public $testsDir = null;
48+
4249

4350
/**
4451
* Sets up this unit test.
@@ -49,6 +56,7 @@ protected function setUp()
4956
{
5057
$class = get_class($this);
5158
$this->standardsDir = $GLOBALS['PHP_CODESNIFFER_STANDARD_DIRS'][$class];
59+
$this->testsDir = $GLOBALS['PHP_CODESNIFFER_TEST_DIRS'][$class];
5260

5361
}//end setUp()
5462

@@ -81,7 +89,7 @@ final public function testSniff()
8189
$sniffCode = Common::getSniffCode(get_class($this));
8290
list($standardName, $categoryName, $sniffName) = explode('.', $sniffCode);
8391

84-
$testFileBase = $this->standardsDir.DIRECTORY_SEPARATOR.'Tests'.DIRECTORY_SEPARATOR.$categoryName.DIRECTORY_SEPARATOR.$sniffName.'UnitTest.';
92+
$testFileBase = $this->testsDir.$categoryName.DIRECTORY_SEPARATOR.$sniffName.'UnitTest.';
8593

8694
// Get a list of all test files to check. These will have the same base
8795
// name but different extensions. We ignore the .php file as it is the class.

tests/Standards/AllSniffs.php

+19-17
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PHP_CodeSniffer\Util\Tokens;
1313
use PHP_CodeSniffer\Util\Standards;
1414
use PHP_CodeSniffer\Autoload;
15+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
1516

1617
if (defined('PHP_CODESNIFFER_IN_TESTS') === false) {
1718
define('PHP_CODESNIFFER_IN_TESTS', true);
@@ -25,7 +26,11 @@
2526
define('PHP_CODESNIFFER_VERBOSITY', 0);
2627
}
2728

28-
require_once __DIR__.'/../../autoload.php';
29+
if (is_file(__DIR__.'/../../autoload.php') === true) {
30+
include_once __DIR__.'/../../autoload.php';
31+
} else {
32+
include_once 'PHP/CodeSniffer/autoload.php';
33+
}
2934

3035
$tokens = new Tokens();
3136

@@ -60,38 +65,34 @@ public static function suite()
6065

6166
$suite = new \PHPUnit_Framework_TestSuite('PHP CodeSniffer Standards');
6267

63-
/*
64-
$isInstalled = !is_file(dirname(__FILE__).'/../../CodeSniffer.php');
65-
*/
68+
$isInstalled = !is_file(__DIR__.'/../../autoload.php');
6669

6770
$installedPaths = Standards::getInstalledStandardPaths();
71+
6872
foreach ($installedPaths as $path) {
6973
$standards = Standards::getInstalledStandards(true, $path);
7074

71-
/*
72-
// If the test is running PEAR installed, the built-in standards
73-
// are split into different directories; one for the sniffs and
74-
// a different file system location for tests.
75-
if ($isInstalled === true
76-
&& is_dir($path.DIRECTORY_SEPARATOR.'Generic') === true
77-
) {
78-
$path = dirname(__FILE__);
79-
}
80-
*/
75+
// If the test is running PEAR installed, the built-in standards
76+
// are split into different directories; one for the sniffs and
77+
// a different file system location for tests.
78+
if ($isInstalled === true && is_dir($path.DIRECTORY_SEPARATOR.'Generic') === true) {
79+
$testPath = realpath(__DIR__.'/../../src/Standards');
80+
} else {
81+
$testPath = $path;
82+
}
8183

8284
foreach ($standards as $standard) {
8385
$standardDir = $path.DIRECTORY_SEPARATOR.$standard;
84-
$testsDir = $standardDir.DIRECTORY_SEPARATOR.'Tests'.DIRECTORY_SEPARATOR;
86+
$testsDir = $testPath.DIRECTORY_SEPARATOR.$standard.DIRECTORY_SEPARATOR.'Tests'.DIRECTORY_SEPARATOR;
8587

8688
if (is_dir($testsDir) === false) {
8789
// Check if the installed path is actually a standard itself.
8890
$standardDir = $path;
89-
$testsDir = $standardDir.DIRECTORY_SEPARATOR.'Tests'.DIRECTORY_SEPARATOR;
91+
$testsDir = $testPath.DIRECTORY_SEPARATOR.'Tests'.DIRECTORY_SEPARATOR;
9092
if (is_dir($testsDir) === false) {
9193
// No tests for this standard.
9294
continue;
9395
}
94-
} else {
9596
}
9697

9798
$di = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($testsDir));
@@ -111,6 +112,7 @@ public static function suite()
111112

112113
$className = Autoload::loadFile($file->getPathname());
113114
$GLOBALS['PHP_CODESNIFFER_STANDARD_DIRS'][$className] = $standardDir;
115+
$GLOBALS['PHP_CODESNIFFER_TEST_DIRS'][$className] = $testsDir;
114116
$suite->addTestSuite($className);
115117
}
116118
}//end foreach

0 commit comments

Comments
 (0)