Skip to content

Commit 8072d01

Browse files
authored
Merge pull request #46 from PHPCSStandards/feature/make-tests-stable
PHPCSDebug/Tests: change set up
2 parents 3a93067 + 34d0bed commit 8072d01

File tree

6 files changed

+77
-138
lines changed

6 files changed

+77
-138
lines changed

.travis.yml

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ env:
2424
# PHPCS `master`
2525
- PHPCS_VERSION="dev-master" LINT=1
2626
# Lowest supported PHPCS version.
27-
- PHPCS_VERSION="3.0.2"
27+
- PHPCS_VERSION="3.1.0"
2828

2929
# Define the stages used.
3030
# For non-PRs, only the sniff and quicktest stages are run.
@@ -72,14 +72,14 @@ jobs:
7272
env: PHPCS_VERSION="dev-master" LINT=1
7373
- stage: quicktest
7474
php: 7.2
75-
env: PHPCS_VERSION="3.0.2"
75+
env: PHPCS_VERSION="3.1.0"
7676

7777
- stage: quicktest
7878
php: 5.4
7979
env: PHPCS_VERSION="dev-master" LINT=1
8080
- stage: quicktest
8181
php: 5.4
82-
env: PHPCS_VERSION="3.0.2"
82+
env: PHPCS_VERSION="3.1.0"
8383

8484
#### TEST STAGE ####
8585
# Additional builds to prevent issues with PHPCS versions incompatible with certain PHP versions.
@@ -135,12 +135,6 @@ before_install:
135135
if [[ "${TRAVIS_BUILD_STAGE_NAME^}" == "Sniff" ]]; then
136136
# The sniff stage doesn't run the unit tests, so no need for PHPUnit.
137137
composer remove --dev phpunit/phpunit --no-update --no-scripts
138-
elif [[ "$PHPCS_VERSION" < "3.1.0" ]]; then
139-
# PHPCS < 3.1.0 is not compatible with PHPUnit 6.x.
140-
composer require --dev phpunit/phpunit:"^4.0||^5.0" --no-update --no-scripts
141-
elif [[ "$PHPCS_VERSION" < "3.2.3" ]]; then
142-
# PHPCS < 3.2.3 is not compatible with PHPUnit 7.x.
143-
composer require --dev phpunit/phpunit:"^4.0||^5.0||^6.0" --no-update --no-scripts
144138
fi
145139
146140
# --prefer-dist will allow for optimal use of the travis caching ability.
@@ -149,9 +143,6 @@ before_install:
149143
if [[ $TRAVIS_PHP_VERSION == "nightly" ]]; then
150144
# Not all dependencies allow for installing on nightly yet, so ignore platform requirements.
151145
composer install --prefer-dist --no-suggest --ignore-platform-reqs
152-
elif [[ "$PHPCS_VERSION" == "4.0.x-dev" ]]; then
153-
# PHPCS 4.x does not ship by default with the base test case, so source is needed.
154-
composer install --prefer-source --no-suggest
155146
else
156147
composer install --prefer-dist --no-suggest
157148
fi

PHPCSDebug/Tests/Debug/TokenListUnitTest.php

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace PHPCSDebug\Tests\Debug;
1212

13-
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
13+
use PHPCSUtils\TestUtils\UtilityMethodTestCase;
1414

1515
/**
1616
* Unit test class for the TokenList sniff.
@@ -19,57 +19,39 @@
1919
*
2020
* @since 1.0.0
2121
*/
22-
class TokenListUnitTest extends AbstractSniffUnitTest
22+
class TokenListUnitTest extends UtilityMethodTestCase
2323
{
2424

2525
/**
26-
* Cache any output generated during the test.
26+
* Set the name of a sniff to pass to PHPCS to limit the run (and force it to record errors).
2727
*
28-
* @var string
28+
* @var array
2929
*/
30-
public static $output = '';
30+
protected static $selectedSniff = ['PHPCSDebug.Debug.TokenList'];
3131

3232
/**
33-
* Sets up this unit test.
33+
* Test the actual output of the TokenList sniff.
3434
*
3535
* @return void
3636
*/
37-
protected function setUp()
37+
public function testOutput()
3838
{
39-
parent::setUp();
40-
4139
\ob_start();
42-
}
40+
self::$phpcsFile->process();
41+
$output = \ob_get_flush();
4342

44-
/**
45-
* Clean up.
46-
*
47-
* @return void
48-
*/
49-
protected function tearDown()
50-
{
51-
self::$output = \ob_get_flush();
43+
$output = \str_replace(["\r\n", "\r"], "\n", $output);
5244

53-
parent::tearDown();
54-
}
45+
$this->assertNotEmpty($output);
5546

56-
/**
57-
* Returns the lines where errors should occur.
58-
*
59-
* @return array <int line number> => <int number of errors>
60-
*/
61-
public function getErrorList()
62-
{
63-
return [];
64-
}
47+
$expected = "\n";
48+
$expected .= 'Ptr :: Ln :: Col :: Cond :: Token Type :: [len]: Content' . "\n";
49+
$expected .= '-------------------------------------------------------------------------' . "\n";
50+
$expected .= ' 0 :: L1 :: C 1 :: CC 0 :: T_OPEN_TAG :: [5]: <?php' . "\n\n";
51+
$expected .= ' 1 :: L2 :: C 1 :: CC 0 :: T_WHITESPACE :: [0]: ' . "\n\n";
52+
$expected .= ' 2 :: L3 :: C 1 :: CC 0 :: T_FUNCTION :: [8]: function' . "\n";
53+
$expected .= ' 3 :: L3 :: C 9 :: CC 0 :: T_WHITESPACE :: [0]: ' . "\n\n";
6554

66-
/**
67-
* Returns the lines where warnings should occur.
68-
*
69-
* @return array <int line number> => <int number of warnings>
70-
*/
71-
public function getWarningList()
72-
{
73-
return [];
55+
$this->assertSame($expected, $output);
7456
}
7557
}

PHPCSDebug/Tests/Debug/TokenListZUnitTest.php

Lines changed: 0 additions & 46 deletions
This file was deleted.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ Once this project is installed, you will see a new `PHPCSDebug` ruleset in the l
122122
For now, this standard only contains one sniff: `PHPCSDebug.Debug.TokenList`.
123123
This sniff will display compact, but detailed information about the tokens found in a (test case) file.
124124

125-
This sniff is compatible with PHPCS 3.0.2+.
125+
This sniff is compatible with PHPCS 3.1.0+.
126126

127127
Typical usage:
128128
* Set up a test case file for a new sniff you intend to write.

composer.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,22 @@
2121
},
2222
"require" : {
2323
"php" : ">=5.4",
24-
"squizlabs/php_codesniffer" : "^3.0.2",
24+
"squizlabs/php_codesniffer" : "^3.1.0",
2525
"dealerdirect/phpcodesniffer-composer-installer" : "^0.4.1 || ^0.5 || ^0.6.2 || ^0.7"
2626
},
2727
"require-dev" : {
2828
"roave/security-advisories" : "dev-master",
29-
"phpunit/phpunit" : "^4.5 || ^5.0 || ^6.0 || ^7.0",
29+
"phpunit/phpunit" : "^4.5 || ^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0",
3030
"php-parallel-lint/php-parallel-lint": "^1.0",
3131
"php-parallel-lint/php-console-highlighter": "^0.5",
32-
"phpcsstandards/phpcsdevcs": "^1.1.1"
32+
"phpcsstandards/phpcsdevcs": "^1.1.1",
33+
"phpcsstandards/phpcsutils" : "^1.0"
3334
},
3435
"bin": [
3536
"bin/phpcs-check-feature-completeness"
3637
],
38+
"minimum-stability": "dev",
39+
"prefer-stable": true,
3740
"scripts" : {
3841
"lint": [
3942
"@php ./vendor/php-parallel-lint/php-parallel-lint/parallel-lint . -e php --exclude vendor --exclude .git"
@@ -45,7 +48,7 @@
4548
"@php ./vendor/squizlabs/php_codesniffer/bin/phpcbf"
4649
],
4750
"run-tests": [
48-
"@php ./vendor/phpunit/phpunit/phpunit --filter PHPCSDebug ./vendor/squizlabs/php_codesniffer/tests/AllTests.php"
51+
"@php ./vendor/phpunit/phpunit/phpunit"
4952
],
5053
"check-complete": [
5154
"@php ./bin/phpcs-check-feature-completeness ./PHPCSDebug"

phpunit-bootstrap.php

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,51 @@
1717
\define('PHP_CODESNIFFER_IN_TESTS', true);
1818
}
1919

20+
if (\defined('PHP_CODESNIFFER_CBF') === false) {
21+
\define('PHP_CODESNIFFER_CBF', false);
22+
}
23+
24+
if (\defined('PHP_CODESNIFFER_VERBOSITY') === false) {
25+
\define('PHP_CODESNIFFER_VERBOSITY', 0);
26+
}
27+
2028
$ds = \DIRECTORY_SEPARATOR;
2129

2230
/*
2331
* Load the necessary PHPCS files.
2432
*/
2533
// Get the PHPCS dir from an environment variable.
26-
$phpcsDir = \getenv('PHPCS_DIR');
27-
$composerPHPCSPath = __DIR__ . $ds . 'vendor' . $ds . 'squizlabs' . $ds . 'php_codesniffer';
28-
29-
if ($phpcsDir === false && \is_dir($composerPHPCSPath)) {
30-
// PHPCS installed via Composer.
31-
$phpcsDir = $composerPHPCSPath;
32-
} elseif ($phpcsDir !== false) {
33-
/*
34-
* PHPCS in a custom directory.
35-
* For this to work, the `PHPCS_DIR` needs to be set in a custom `phpunit.xml` file.
36-
*/
34+
$phpcsDir = \getenv('PHPCS_DIR');
35+
36+
// Get the PHPCSUtils dir from an environment variable.
37+
$phpcsUtilsDir = \getenv('PHPCSUTILS_DIR');
38+
39+
// This may be a Composer install.
40+
if (\is_dir(__DIR__ . $ds . 'vendor')) {
41+
$vendorDir = __DIR__ . $ds . 'vendor';
42+
if ($phpcsDir === false && \is_dir($vendorDir . $ds . 'squizlabs' . $ds . 'php_codesniffer')) {
43+
$phpcsDir = $vendorDir . $ds . 'squizlabs' . $ds . 'php_codesniffer';
44+
}
45+
if ($phpcsUtilsDir === false && \is_dir($vendorDir . $ds . 'phpcsstandards' . $ds . 'phpcsutils')) {
46+
$phpcsUtilsDir = $vendorDir . $ds . 'phpcsstandards' . $ds . 'phpcsutils';
47+
}
48+
}
49+
50+
if ($phpcsDir !== false) {
3751
$phpcsDir = \realpath($phpcsDir);
3852
}
3953

40-
// Try and load the PHPCS bootstrap which loads the autoloader and PHPUnit aliases.
41-
if ($phpcsDir !== false && \is_dir($phpcsDir)) {
42-
if (\file_exists($phpcsDir . $ds . 'tests' . $ds . 'bootstrap.php')) {
43-
require_once $phpcsDir . $ds . 'tests' . $ds . 'bootstrap.php'; // PHPUnit 6.x+ support as of PHPCS 3.1.0.
44-
} elseif (\file_exists($phpcsDir . $ds . 'autoload.php')) {
45-
require_once $phpcsDir . $ds . 'autoload.php'; // PHPCS < 3.1.0.
46-
}
54+
if ($phpcsUtilsDir !== false) {
55+
$phpcsUtilsDir = \realpath($phpcsUtilsDir);
56+
}
57+
58+
// Try and load the PHPCS autoloader.
59+
if ($phpcsDir !== false && \file_exists($phpcsDir . $ds . 'autoload.php')) {
60+
// PHPCS 3.x.
61+
require_once $phpcsDir . $ds . 'autoload.php';
62+
63+
// Pre-load the token back-fills to prevent undefined constant notices.
64+
require_once $phpcsDir . '/src/Util/Tokens.php';
4765
} else {
4866
echo 'Uh oh... can\'t find PHPCS.
4967
@@ -55,28 +73,19 @@
5573
die(1);
5674
}
5775

58-
/*
59-
* Set the PHPCS_IGNORE_TEST environment variable to ignore tests from other standards.
60-
* Ref: https://github.com/squizlabs/PHP_CodeSniffer/pull/1146
61-
*/
62-
$phpcsDevtoolsStandards = [
63-
'PHPCSDebug' => true,
64-
];
65-
66-
$allStandards = PHP_CodeSniffer\Util\Standards::getInstalledStandards();
67-
$allStandards[] = 'Generic';
76+
// Try and load the PHPCSUtils autoloader.
77+
if ($phpcsUtilsDir !== false && \file_exists($phpcsUtilsDir . $ds . 'phpcsutils-autoload.php')) {
78+
require_once $phpcsUtilsDir . $ds . 'phpcsutils-autoload.php';
79+
} else {
80+
echo 'Uh oh... can\'t find PHPCSUtils.
6881
69-
$standardsToIgnore = [];
70-
foreach ($allStandards as $standard) {
71-
if (isset($phpcsDevtoolsStandards[$standard]) === true) {
72-
continue;
73-
}
82+
If you use Composer, please run `composer install`.
83+
Otherwise, make sure you set a `PHPCSUTILS_DIR` environment variable in your phpunit.xml file
84+
pointing to the PHPCSUtils directory.
85+
';
7486

75-
$standardsToIgnore[] = $standard;
87+
die(1);
7688
}
7789

78-
$standardsToIgnoreString = \implode(',', $standardsToIgnore);
79-
\putenv("PHPCS_IGNORE_TESTS={$standardsToIgnoreString}");
80-
8190
// Clean up.
82-
unset($ds, $phpcsDir, $composerPHPCSPath, $allStandards, $standardsToIgnore, $standard, $standardsToIgnoreString);
91+
unset($ds, $phpcsDir, $phpcsUtilsDir, $vendorDir);

0 commit comments

Comments
 (0)