Skip to content

Commit 2418376

Browse files
authored
Merge pull request #59 from PHPCSStandards/U-3803/feature/allow-for-phpunit-8-9
Tests: allow the test suite to run on PHPUnit 8.x and 9.x
2 parents bc5d011 + 10b01fe commit 2418376

24 files changed

+172
-92
lines changed

.github/CONTRIBUTING.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ Note: There may be an issue or PR open already. If so, please join the discussio
137137

138138
1. Fork/clone the repository.
139139
2. Run `composer install`.
140-
When installing on PHP >= 8.0, use `composer install --ignore-platform-req=php+`.
141140
3. Create a new branch off the `master` branch to hold your patch.
142141
If there is an open issue associated with your patch, including the issue number in the branch name is good practice.
143142

@@ -152,10 +151,8 @@ To help you with this, a number of convenience scripts are available:
152151
* `composer check-all` will run the `cs` + `test` checks in one go.
153152
* `composer cs` will check for code style violations.
154153
* `composer cbf` will run the autofixers for code style violations.
155-
* `composer test` will run the unit tests (only works when on PHP < 8.1).
156-
* `composer test-php8` will run the unit tests when you are working on PHP 8.1+.
157-
Please note that using a `phpunit.xml` overload config file will not work with this script!
158-
* `composer coverage` will run the unit tests with code coverage (only works when on PHP < 8.1).
154+
* `composer test` will run the unit tests.
155+
* `composer coverage` will run the unit tests with code coverage.
159156
Note: you may want to use a custom `phpunit.xml` overload config file to tell PHPUnit where to place an HTML report.
160157
Alternative run it like so: `composer coverage -- --coverage-html /path/to/report-dir/` to specify the location for the HTML report on the command line.
161158
* `composer build` will build the phpcs.phar and phpcbf.phar files.

.github/workflows/phpstan.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
- name: Install PHP
3131
uses: shivammathur/setup-php@v2
3232
with:
33-
php-version: '7.4'
33+
php-version: 'latest'
3434
coverage: none
3535
tools: phpstan
3636

.github/workflows/test.yml

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -131,37 +131,20 @@ jobs:
131131
132132
# Install dependencies and handle caching in one go.
133133
# @link https://github.com/marketplace/actions/install-php-dependencies-with-composer
134-
- name: Install Composer dependencies - normal
135-
if: ${{ matrix.php < '8.0' }}
134+
- name: Install Composer dependencies
136135
uses: "ramsey/composer-install@v2"
137136
with:
138137
# Bust the cache at least once a month - output format: YYYY-MM.
139138
custom-cache-suffix: $(date -u "+%Y-%m")
140139

141-
# For PHP 8.0+, we need to install with ignore platform reqs as PHPUnit 7 is still used.
142-
- name: Install Composer dependencies - with ignore platform
143-
if: ${{ matrix.php >= '8.0' }}
144-
uses: "ramsey/composer-install@v2"
145-
with:
146-
composer-options: --ignore-platform-req=php+
147-
custom-cache-suffix: $(date -u "+%Y-%m")
148-
149140
# Note: The code style check is run multiple times against every PHP version
150141
# as it also acts as an integration test.
151142
- name: 'PHPCS: set the path to PHP'
152143
run: php bin/phpcs --config-set php_path php
153144

154145
- name: 'PHPUnit: run the tests'
155-
if: ${{ matrix.php != '8.1' && matrix.php != '8.2' }}
156146
run: vendor/bin/phpunit tests/AllTests.php
157147

158-
# We need to ignore the config file so that PHPUnit doesn't try to read it.
159-
# The config file causes an error on PHP 8.1+ with PHPunit 7, but it's not needed here anyway
160-
# as we can pass all required settings in the phpunit command.
161-
- name: 'PHPUnit: run the tests on PHP > 8.0'
162-
if: ${{ matrix.php == '8.1' || matrix.php == '8.2' }}
163-
run: vendor/bin/phpunit tests/AllTests.php --no-configuration --bootstrap=tests/bootstrap.php --dont-report-useless-tests
164-
165148
- name: 'PHPCS: check code style without cache, no parallel'
166149
if: ${{ matrix.custom_ini == false && matrix.php != '7.4' }}
167150
run: php bin/phpcs --no-cache --parallel=1

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/CodeSniffer.conf
22
/phpcs.xml
33
/phpunit.xml
4+
.phpunit.result.cache
45
.idea/*
56
/vendor/
67
composer.lock

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ The file documents changes to the PHP_CodeSniffer project.
3636
- PSR2.Methods.FunctionCallSignature
3737
- PSR2.Methods.FunctionClosingBrace
3838
- Thanks to Atsushi Okui (@blue32a) for the patch
39+
- Support for PHPUnit 8 and 9 to the test suite.
40+
- Test suites for external standards which run via the PHPCS native test suite can now run on PHPUnit 4-9 (was 4-7).
41+
- If any of these tests use the PHPUnit `setUp()`/`tearDown()` methods or overload the `setUp()` in the `AbstractSniffUnitTest` test case, they will need to be adjusted. See the [PR details for further information](https://github.com/PHPCSStandards/PHP_CodeSniffer/pull/59/commits/26384ebfcc0b1c1651b0e1e40c9b6c8c22881832).
3942

4043
### Changed
4144
- Changes have been made to the way PHPCS handles invalid sniff properties being set in a custom ruleset

composer.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"ext-simplexml": "*"
4242
},
4343
"require-dev": {
44-
"phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0"
44+
"phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0"
4545
},
4646
"replace": {
4747
"squizlabs/php_codesniffer": "> 2.0"
@@ -61,10 +61,6 @@
6161
"Composer\\Config::disableProcessTimeout",
6262
"@php ./vendor/phpunit/phpunit/phpunit tests/AllTests.php --no-coverage"
6363
],
64-
"test-php8": [
65-
"Composer\\Config::disableProcessTimeout",
66-
"@php ./vendor/phpunit/phpunit/phpunit tests/AllTests.php --no-configuration --bootstrap=tests/bootstrap.php --dont-report-useless-tests --no-coverage"
67-
],
6864
"coverage": [
6965
"Composer\\Config::disableProcessTimeout",
7066
"@php ./vendor/phpunit/phpunit/phpunit tests/AllTests.php -d max_execution_time=0"
@@ -82,7 +78,6 @@
8278
"cs": "Check for code style violations.",
8379
"cbf": "Fix code style violations.",
8480
"test": "Run the unit tests without code coverage.",
85-
"test-php8": "Run the unit tests without code coverage on PHP 8.1 or higher.",
8681
"coverage": "Run the unit tests with code coverage.",
8782
"build": "Create PHAR files for PHPCS and PHPCBF.",
8883
"check-all": "Run all checks (phpcs, tests)."

phpunit.xml.dist

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit beStrictAboutTestsThatDoNotTestAnything="false" bootstrap="tests/bootstrap.php">
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.2/phpunit.xsd"
5+
backupGlobals="true"
6+
beStrictAboutTestsThatDoNotTestAnything="false"
7+
bootstrap="tests/bootstrap.php"
8+
convertErrorsToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
convertNoticesToExceptions="true"
11+
convertDeprecationsToExceptions="true"
12+
>
313
<testsuites>
414
<testsuite name="PHP_CodeSniffer Test Suite">
515
<file>tests/AllTests.php</file>

src/Standards/Generic/Tests/Debug/ESLintUnitTest.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,31 +36,33 @@ class ESLintUnitTest extends AbstractSniffUnitTest
3636
/**
3737
* Sets up this unit test.
3838
*
39+
* @before
40+
*
3941
* @return void
4042
*/
41-
protected function setUp()
43+
protected function setUpPrerequisites()
4244
{
43-
parent::setUp();
45+
parent::setUpPrerequisites();
4446

4547
$cwd = getcwd();
4648
file_put_contents($cwd.'/.eslintrc.json', self::ESLINT_CONFIG);
4749

48-
}//end setUp()
50+
}//end setUpPrerequisites()
4951

5052

5153
/**
5254
* Remove artifact.
5355
*
56+
* @after
57+
*
5458
* @return void
5559
*/
56-
protected function tearDown()
60+
protected function resetProperties()
5761
{
58-
parent::tearDown();
59-
6062
$cwd = getcwd();
6163
unlink($cwd.'/.eslintrc.json');
6264

63-
}//end tearDown()
65+
}//end resetProperties()
6466

6567

6668
/**

tests/AllTests.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
$phpunit7 = false;
1919
if (class_exists('\PHPUnit\Runner\Version') === true) {
2020
$version = \PHPUnit\Runner\Version::id();
21-
if ($version[0] === '7') {
21+
if (version_compare($version, '7.0', '>=') === true) {
2222
$phpunit7 = true;
2323
}
2424
}

tests/Core/AbstractMethodUnitTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ abstract class AbstractMethodUnitTest extends TestCase
4141
* The test case file for a unit test class has to be in the same directory
4242
* directory and use the same file name as the test class, using the .inc extension.
4343
*
44+
* @beforeClass
45+
*
4446
* @return void
4547
*/
46-
public static function setUpBeforeClass()
48+
public static function initializeFile()
4749
{
4850
$config = new Config();
4951
$config->standards = ['PSR1'];
@@ -62,19 +64,21 @@ public static function setUpBeforeClass()
6264
self::$phpcsFile = new DummyFile($contents, $ruleset, $config);
6365
self::$phpcsFile->process();
6466

65-
}//end setUpBeforeClass()
67+
}//end initializeFile()
6668

6769

6870
/**
6971
* Clean up after finished test.
7072
*
73+
* @afterClass
74+
*
7175
* @return void
7276
*/
73-
public static function tearDownAfterClass()
77+
public static function resetFile()
7478
{
7579
self::$phpcsFile = null;
7680

77-
}//end tearDownAfterClass()
81+
}//end resetFile()
7882

7983

8084
/**

tests/Core/Autoloader/DetermineLoadedClassTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ class DetermineLoadedClassTest extends TestCase
1919
/**
2020
* Load the test files.
2121
*
22+
* @beforeClass
23+
*
2224
* @return void
2325
*/
24-
public static function setUpBeforeClass()
26+
public static function includeFixture()
2527
{
2628
include __DIR__.'/TestFiles/Sub/C.inc';
2729

28-
}//end setUpBeforeClass()
30+
}//end includeFixture()
2931

3032

3133
/**

tests/Core/File/GetClassPropertiesTest.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,22 @@ class GetClassPropertiesTest extends AbstractMethodUnitTest
2323
*
2424
* @dataProvider dataNotAClassException
2525
*
26-
* @expectedException PHP_CodeSniffer\Exceptions\RuntimeException
27-
* @expectedExceptionMessage $stackPtr must be of type T_CLASS
28-
*
2926
* @return void
3027
*/
3128
public function testNotAClassException($testMarker, $tokenType)
3229
{
30+
$msg = '$stackPtr must be of type T_CLASS';
31+
$exception = 'PHP_CodeSniffer\Exceptions\RuntimeException';
32+
33+
if (\method_exists($this, 'expectException') === true) {
34+
// PHPUnit 5+.
35+
$this->expectException($exception);
36+
$this->expectExceptionMessage($msg);
37+
} else {
38+
// PHPUnit 4.
39+
$this->setExpectedException($exception, $msg);
40+
}
41+
3342
$target = $this->getTargetToken($testMarker, $tokenType);
3443
self::$phpcsFile->getClassProperties($target);
3544

tests/Core/File/GetMemberPropertiesTest.php

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ public function testGetMemberProperties($identifier, $expected)
3030
$variable = $this->getTargetToken($identifier, T_VARIABLE);
3131
$result = self::$phpcsFile->getMemberProperties($variable);
3232

33-
$this->assertArraySubset($expected, $result, true);
33+
// Unset those indexes which are not being tested.
34+
unset($result['type_token'], $result['type_end_token']);
35+
36+
$this->assertSame($expected, $result);
3437

3538
}//end testGetMemberProperties()
3639

@@ -764,6 +767,7 @@ public function dataGetMemberProperties()
764767
'scope' => 'public',
765768
'scope_specified' => true,
766769
'is_static' => false,
770+
'is_readonly' => false,
767771
'type' => 'Foo&Bar',
768772
'nullable_type' => false,
769773
],
@@ -774,6 +778,7 @@ public function dataGetMemberProperties()
774778
'scope' => 'public',
775779
'scope_specified' => true,
776780
'is_static' => false,
781+
'is_readonly' => false,
777782
'type' => 'Foo&Bar&Baz',
778783
'nullable_type' => false,
779784
],
@@ -784,6 +789,7 @@ public function dataGetMemberProperties()
784789
'scope' => 'public',
785790
'scope_specified' => true,
786791
'is_static' => false,
792+
'is_readonly' => false,
787793
'type' => 'int&string',
788794
'nullable_type' => false,
789795
],
@@ -794,6 +800,7 @@ public function dataGetMemberProperties()
794800
'scope' => 'public',
795801
'scope_specified' => true,
796802
'is_static' => false,
803+
'is_readonly' => false,
797804
'type' => '?Foo&Bar',
798805
'nullable_type' => true,
799806
],
@@ -808,15 +815,24 @@ public function dataGetMemberProperties()
808815
*
809816
* @param string $identifier Comment which precedes the test case.
810817
*
811-
* @expectedException PHP_CodeSniffer\Exceptions\RuntimeException
812-
* @expectedExceptionMessage $stackPtr is not a class member var
813-
*
814818
* @dataProvider dataNotClassProperty
815819
*
816820
* @return void
817821
*/
818822
public function testNotClassPropertyException($identifier)
819823
{
824+
$msg = '$stackPtr is not a class member var';
825+
$exception = 'PHP_CodeSniffer\Exceptions\RuntimeException';
826+
827+
if (\method_exists($this, 'expectException') === true) {
828+
// PHPUnit 5+.
829+
$this->expectException($exception);
830+
$this->expectExceptionMessage($msg);
831+
} else {
832+
// PHPUnit 4.
833+
$this->setExpectedException($exception, $msg);
834+
}
835+
820836
$variable = $this->getTargetToken($identifier, T_VARIABLE);
821837
$result = self::$phpcsFile->getMemberProperties($variable);
822838

@@ -848,13 +864,22 @@ public function dataNotClassProperty()
848864
/**
849865
* Test receiving an expected exception when a non variable is passed.
850866
*
851-
* @expectedException PHP_CodeSniffer\Exceptions\RuntimeException
852-
* @expectedExceptionMessage $stackPtr must be of type T_VARIABLE
853-
*
854867
* @return void
855868
*/
856869
public function testNotAVariableException()
857870
{
871+
$msg = '$stackPtr must be of type T_VARIABLE';
872+
$exception = 'PHP_CodeSniffer\Exceptions\RuntimeException';
873+
874+
if (\method_exists($this, 'expectException') === true) {
875+
// PHPUnit 5+.
876+
$this->expectException($exception);
877+
$this->expectExceptionMessage($msg);
878+
} else {
879+
// PHPUnit 4.
880+
$this->setExpectedException($exception, $msg);
881+
}
882+
858883
$next = $this->getTargetToken('/* testNotAVariable */', T_RETURN);
859884
$result = self::$phpcsFile->getMemberProperties($next);
860885

0 commit comments

Comments
 (0)