Skip to content

Commit be84dd8

Browse files
authored
Merge pull request #994 from PHPCSStandards/phpcs-4.0/feature/tests-drop-support-phpunit-lt-8
Tests: drop support for PHPUnit < 8
2 parents 5380a4b + b1effa3 commit be84dd8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+144
-380
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"ext-xmlwriter": "*"
3737
},
3838
"require-dev": {
39-
"phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.3.4"
39+
"phpunit/phpunit": "^8.0 || ^9.3.4"
4040
},
4141
"bin": [
4242
"bin/phpcbf",

tests/AllTests.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,7 @@
1111

1212
require_once 'Core/AllTests.php';
1313
require_once 'Standards/AllSniffs.php';
14-
15-
// PHPUnit 7 made the TestSuite run() method incompatible with
16-
// older PHPUnit versions due to return type hints, so maintain
17-
// two different suite objects.
18-
$phpunit7 = false;
19-
if (class_exists('\PHPUnit\Runner\Version') === true) {
20-
$version = \PHPUnit\Runner\Version::id();
21-
if (version_compare($version, '7.0', '>=') === true) {
22-
$phpunit7 = true;
23-
}
24-
}
25-
26-
if ($phpunit7 === true) {
27-
include_once 'TestSuite7.php';
28-
} else {
29-
include_once 'TestSuite.php';
30-
}
14+
require_once 'TestSuite.php';
3115

3216
class PHP_CodeSniffer_AllTests
3317
{

tests/Core/AbstractMethodUnitTest.php

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,9 @@ abstract class AbstractMethodUnitTest extends TestCase
4242
* The test case file for a unit test class has to be in the same directory
4343
* directory and use the same file name as the test class, using the .inc extension.
4444
*
45-
* @beforeClass
46-
*
4745
* @return void
4846
*/
49-
public static function initializeFile()
47+
public static function setUpBeforeClass(): void
5048
{
5149
$_SERVER['argv'] = [];
5250
$config = new ConfigDouble();
@@ -67,20 +65,15 @@ public static function initializeFile()
6765
self::$phpcsFile = new DummyFile($contents, $ruleset, $config);
6866
self::$phpcsFile->parse();
6967

70-
}//end initializeFile()
68+
}//end setUpBeforeClass()
7169

7270

7371
/**
7472
* Clean up after finished test by resetting all static properties on the class to their default values.
7573
*
76-
* Note: This is a PHPUnit cross-version compatible {@see \PHPUnit\Framework\TestCase::tearDownAfterClass()}
77-
* method.
78-
*
79-
* @afterClass
80-
*
8174
* @return void
8275
*/
83-
public static function reset()
76+
public static function tearDownAfterClass(): void
8477
{
8578
// Explicitly trigger __destruct() on the ConfigDouble to reset the Config statics.
8679
// The explicit method call prevents potential stray test-local references to the $config object
@@ -93,7 +86,7 @@ public static function reset()
9386
self::$tabWidth = 4;
9487
self::$phpcsFile = null;
9588

96-
}//end reset()
89+
}//end tearDownAfterClass()
9790

9891

9992
/**
@@ -246,16 +239,8 @@ public static function getTargetTokenFromFile(File $phpcsFile, $commentString, $
246239
*/
247240
public function expectRunTimeException($message)
248241
{
249-
$exception = 'PHP_CodeSniffer\Exceptions\RuntimeException';
250-
251-
if (method_exists($this, 'expectException') === true) {
252-
// PHPUnit 5+.
253-
$this->expectException($exception);
254-
$this->expectExceptionMessage($message);
255-
} else {
256-
// PHPUnit 4.
257-
$this->setExpectedException($exception, $message);
258-
}
242+
$this->expectException('PHP_CodeSniffer\Exceptions\RuntimeException');
243+
$this->expectExceptionMessage($message);
259244

260245
}//end expectRunTimeException()
261246

tests/Core/Autoloader/DetermineLoadedClassTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,13 @@ final class DetermineLoadedClassTest extends TestCase
2424
/**
2525
* Load the test files.
2626
*
27-
* @beforeClass
28-
*
2927
* @return void
3028
*/
31-
public static function includeFixture()
29+
public static function setUpBeforeClass(): void
3230
{
3331
include __DIR__.'/TestFiles/Sub/C.inc';
3432

35-
}//end includeFixture()
33+
}//end setUpBeforeClass()
3634

3735

3836
/**

tests/Core/Config/AbstractRealConfigTestCase.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@ abstract class AbstractRealConfigTestCase extends TestCase
2020
/**
2121
* Set static properties in the Config class to prevent tests influencing each other.
2222
*
23-
* @before
24-
*
2523
* @return void
2624
*/
27-
protected function setConfigStatics()
25+
protected function setUp(): void
2826
{
2927
// Set to the property's default value to clear out potentially set values from other tests.
3028
self::setStaticConfigProperty('overriddenDefaults', []);
@@ -35,40 +33,36 @@ protected function setConfigStatics()
3533
self::setStaticConfigProperty('configData', []);
3634
self::setStaticConfigProperty('configDataFile', '');
3735

38-
}//end setConfigStatics()
36+
}//end setUp()
3937

4038

4139
/**
4240
* Clean up after each finished test.
4341
*
44-
* @after
45-
*
4642
* @return void
4743
*/
48-
protected function clearArgv()
44+
protected function tearDown(): void
4945
{
5046
$_SERVER['argv'] = [];
5147

52-
}//end clearArgv()
48+
}//end tearDown()
5349

5450

5551
/**
5652
* Reset the static properties in the Config class to their true defaults to prevent this class
5753
* from influencing other tests.
5854
*
59-
* @afterClass
60-
*
6155
* @return void
6256
*/
63-
public static function resetConfigToDefaults()
57+
public static function tearDownAfterClass(): void
6458
{
6559
self::setStaticConfigProperty('overriddenDefaults', []);
6660
self::setStaticConfigProperty('executablePaths', []);
6761
self::setStaticConfigProperty('configData', null);
6862
self::setStaticConfigProperty('configDataFile', null);
6963
$_SERVER['argv'] = [];
7064

71-
}//end resetConfigToDefaults()
65+
}//end tearDownAfterClass()
7266

7367

7468
/**

tests/Core/Config/GeneratorArgTest.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,8 @@ public function testInvalidGenerator($generatorName)
112112
$exception = 'PHP_CodeSniffer\Exceptions\DeepExitException';
113113
$message = 'ERROR: "'.$generatorName.'" is not a valid generator. The following generators are supported: Text, HTML and Markdown.';
114114

115-
if (method_exists($this, 'expectException') === true) {
116-
// PHPUnit 5+.
117-
$this->expectException($exception);
118-
$this->expectExceptionMessage($message);
119-
} else {
120-
// PHPUnit 4.
121-
$this->setExpectedException($exception, $message);
122-
}
115+
$this->expectException($exception);
116+
$this->expectExceptionMessage($message);
123117

124118
new ConfigDouble(["--generator={$generatorName}"]);
125119

tests/Core/Config/ReportWidthTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function testReportWidthDefault()
3434
$config = new Config(['--standard=PSR1']);
3535

3636
// Can't test the exact value as "auto" will resolve differently depending on the machine running the tests.
37-
$this->assertTrue(is_int($config->reportWidth), 'Report width is not an integer');
37+
$this->assertIsInt($config->reportWidth, 'Report width is not an integer');
3838
$this->assertGreaterThan(0, $config->reportWidth, 'Report width is not greater than 0');
3939

4040
}//end testReportWidthDefault()
@@ -60,7 +60,7 @@ public function testReportWidthWillBeSetFromAutoWhenNotFoundInConfFile()
6060
$config = new Config(['--standard=PSR1']);
6161

6262
// Can't test the exact value as "auto" will resolve differently depending on the machine running the tests.
63-
$this->assertTrue(is_int($config->reportWidth), 'Report width is not an integer');
63+
$this->assertIsInt($config->reportWidth, 'Report width is not an integer');
6464
$this->assertGreaterThan(0, $config->reportWidth, 'Report width is not greater than 0');
6565

6666
}//end testReportWidthWillBeSetFromAutoWhenNotFoundInConfFile()
@@ -181,7 +181,7 @@ public function testReportWidthInputHandlingForAuto()
181181
$config->reportWidth = 'auto';
182182

183183
// Can't test the exact value as "auto" will resolve differently depending on the machine running the tests.
184-
$this->assertTrue(is_int($config->reportWidth), 'Report width is not an integer');
184+
$this->assertIsInt($config->reportWidth, 'Report width is not an integer');
185185
$this->assertGreaterThan(0, $config->reportWidth, 'Report width is not greater than 0');
186186

187187
}//end testReportWidthInputHandlingForAuto()

tests/Core/Config/SniffsExcludeArgsTest.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,8 @@ public function testInvalid($argument, $value, $errors, $suggestion)
5050
$message .= 'Run "phpcs --help" for usage information'.PHP_EOL;
5151
$message .= PHP_EOL;
5252

53-
if (method_exists($this, 'expectException') === true) {
54-
// PHPUnit 5+.
55-
$this->expectException($exception);
56-
$this->expectExceptionMessage($message);
57-
} else {
58-
// PHPUnit 4.
59-
$this->setExpectedException($exception, $message);
60-
}
53+
$this->expectException($exception);
54+
$this->expectExceptionMessage($message);
6155

6256
new ConfigDouble(["--$argument=$value"]);
6357

tests/Core/File/GetConditionTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,9 @@ final class GetConditionTest extends AbstractMethodUnitTest
123123
* Retrieves the test tokens and marker token stack pointer positions
124124
* only once and caches them as they won't change between the tests anyway.
125125
*
126-
* @before
127-
*
128126
* @return void
129127
*/
130-
protected function setUpCaches()
128+
protected function setUp(): void
131129
{
132130
if (empty(self::$testTokens) === true) {
133131
foreach (self::$testTargets as $targetToken => $marker) {
@@ -141,7 +139,7 @@ protected function setUpCaches()
141139
}
142140
}
143141

144-
}//end setUpCaches()
142+
}//end setUp()
145143

146144

147145
/**

tests/Core/Filters/AbstractFilterTestCase.php

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,22 @@ abstract class AbstractFilterTestCase extends TestCase
3939
/**
4040
* Initialize the config and ruleset objects.
4141
*
42-
* @beforeClass
43-
*
4442
* @return void
4543
*/
46-
public static function initializeConfigAndRuleset()
44+
public static function setUpBeforeClass(): void
4745
{
4846
self::$config = new ConfigDouble();
4947
self::$ruleset = new Ruleset(self::$config);
5048

51-
}//end initializeConfigAndRuleset()
49+
}//end setUpBeforeClass()
5250

5351

5452
/**
5553
* Clean up after finished test by resetting all static properties on the Config class to their default values.
5654
*
57-
* Note: This is a PHPUnit cross-version compatible {@see \PHPUnit\Framework\TestCase::tearDownAfterClass()}
58-
* method.
59-
*
60-
* @afterClass
61-
*
6255
* @return void
6356
*/
64-
public static function reset()
57+
public static function tearDownAfterClass(): void
6558
{
6659
// Explicitly trigger __destruct() on the ConfigDouble to reset the Config statics.
6760
// The explicit method call prevents potential stray test-local references to the $config object
@@ -71,7 +64,7 @@ public static function reset()
7164
self::$config->__destruct();
7265
}
7366

74-
}//end reset()
67+
}//end tearDownAfterClass()
7568

7669

7770
/**
@@ -96,24 +89,15 @@ protected function getMockedClass($className, array $constructorArgs=[], $method
9689
{
9790
$mockedObj = $this->getMockBuilder($className);
9891

99-
if (method_exists($mockedObj, 'onlyMethods') === true) {
100-
// PHPUnit 8+.
101-
if (is_array($methodsToMock) === true) {
102-
return $mockedObj
103-
->setConstructorArgs($constructorArgs)
104-
->onlyMethods($methodsToMock)
105-
->getMock();
106-
}
107-
108-
return $mockedObj->getMock()
109-
->setConstructorArgs($constructorArgs);
92+
if (is_array($methodsToMock) === true) {
93+
return $mockedObj
94+
->setConstructorArgs($constructorArgs)
95+
->onlyMethods($methodsToMock)
96+
->getMock();
11097
}
11198

112-
// PHPUnit < 8.
113-
return $mockedObj
114-
->setConstructorArgs($constructorArgs)
115-
->setMethods($methodsToMock)
116-
->getMock();
99+
return $mockedObj->getMock()
100+
->setConstructorArgs($constructorArgs);
117101

118102
}//end getMockedClass()
119103

0 commit comments

Comments
 (0)