Skip to content

Commit 92cf10f

Browse files
jrfnlgsherwood
andcommitted
Tests: remove work-arounds for setup/teardown methods
As of PHPUnit 8, the `setUpBeforeClass()`, `setUp()`, `tearDown()` and `tearDownAfterClass()` method were required to have a `void` return type declaration. This could previously not be applied as the test suite had to stay compatible with PHP < 7.2, so as a work-around, the `@beforeClass`, `@before`, `@after` and `@afterClass` annotations were used for these setup/teardown methods. As support for PHPUnit < 8 has now been dropped, this work-around is no longer needed and the methods can use the more customary PHPUnit fixture method signature again. Co-authored-by: Greg Sherwood <[email protected]>
1 parent 8c5505f commit 92cf10f

25 files changed

+81
-167
lines changed

tests/Core/AbstractMethodUnitTest.php

Lines changed: 4 additions & 11 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
/**

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/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: 4 additions & 11 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
/**

tests/Core/Filters/Filter/AcceptTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,15 @@ final class AcceptTest extends AbstractFilterTestCase
2828
/**
2929
* Initialize the config and ruleset objects based on the `AcceptTest.xml` ruleset file.
3030
*
31-
* @beforeClass
32-
*
3331
* @return void
3432
*/
35-
public static function initializeConfigAndRuleset()
33+
public static function setUpBeforeClass(): void
3634
{
3735
$standard = __DIR__.'/'.basename(__FILE__, '.php').'.xml';
3836
self::$config = new ConfigDouble(["--standard=$standard", '--ignore=*/somethingelse/*']);
3937
self::$ruleset = new Ruleset(self::$config);
4038

41-
}//end initializeConfigAndRuleset()
39+
}//end setUpBeforeClass()
4240

4341

4442
/**

tests/Core/Fixer/GenerateDiffTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,9 @@ final class GenerateDiffTest extends TestCase
4646
* prevent doing tab replacement when parsing the file. This is to allow for testing a
4747
* diff with tabs vs spaces (which wouldn't yield a diff if tabs had already been replaced).
4848
*
49-
* @beforeClass
50-
*
5149
* @return void
5250
*/
53-
public static function initializeFile()
51+
public static function setUpBeforeClass(): void
5452
{
5553
$config = new ConfigDouble();
5654
$ruleset = new Ruleset($config);
@@ -59,7 +57,7 @@ public static function initializeFile()
5957
self::$phpcsFile->parse();
6058
self::$phpcsFile->fixer->startFile(self::$phpcsFile);
6159

62-
}//end initializeFile()
60+
}//end setUpBeforeClass()
6361

6462

6563
/**

tests/Core/Ruleset/ExpandRulesetReferenceHomePathTest.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,21 @@ final class ExpandRulesetReferenceHomePathTest extends AbstractRulesetTestCase
3232
/**
3333
* Store the user's home path.
3434
*
35-
* @beforeClass
36-
*
3735
* @return void
3836
*/
39-
public static function storeHomePath()
37+
public static function setUpBeforeClass(): void
4038
{
4139
self::$homepath = getenv('HOME');
4240

43-
}//end storeHomePath()
41+
}//end setUpBeforeClass()
4442

4543

4644
/**
4745
* Restore the user's home path environment variable in case the test changed it or created it.
4846
*
49-
* @afterClass
50-
*
5147
* @return void
5248
*/
53-
public static function restoreHomePath()
49+
public static function tearDownAfterClass(): void
5450
{
5551
if (is_string(self::$homepath) === true) {
5652
putenv('HOME='.self::$homepath);
@@ -59,22 +55,20 @@ public static function restoreHomePath()
5955
putenv('HOME');
6056
}
6157

62-
}//end restoreHomePath()
58+
}//end tearDownAfterClass()
6359

6460

6561
/**
6662
* Set the home path to an alternative location.
6763
*
68-
* @before
69-
*
7064
* @return void
7165
*/
72-
protected function setHomePath()
66+
protected function setUp(): void
7367
{
7468
$fakeHomePath = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'FakeHomePath';
7569
putenv("HOME=$fakeHomePath");
7670

77-
}//end setHomePath()
71+
}//end setUp()
7872

7973

8074
/**

tests/Core/Ruleset/GetIgnorePatternsTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,16 @@ final class GetIgnorePatternsTest extends TestCase
3232
/**
3333
* Initialize the config and ruleset objects for this test.
3434
*
35-
* @beforeClass
36-
*
3735
* @return void
3836
*/
39-
public static function initializeConfigAndRuleset()
37+
public static function setUpBeforeClass(): void
4038
{
4139
// Set up the ruleset.
4240
$standard = __DIR__."/GetIgnorePatternsTest.xml";
4341
$config = new ConfigDouble(["--standard=$standard"]);
4442
self::$ruleset = new Ruleset($config);
4543

46-
}//end initializeConfigAndRuleset()
44+
}//end setUpBeforeClass()
4745

4846

4947
/**

tests/Core/Ruleset/GetIncludePatternsTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,16 @@ final class GetIncludePatternsTest extends TestCase
3232
/**
3333
* Initialize the config and ruleset objects for this test.
3434
*
35-
* @beforeClass
36-
*
3735
* @return void
3836
*/
39-
public static function initializeConfigAndRuleset()
37+
public static function setUpBeforeClass(): void
4038
{
4139
// Set up the ruleset.
4240
$standard = __DIR__."/GetIncludePatternsTest.xml";
4341
$config = new ConfigDouble(["--standard=$standard"]);
4442
self::$ruleset = new Ruleset($config);
4543

46-
}//end initializeConfigAndRuleset()
44+
}//end setUpBeforeClass()
4745

4846

4947
/**

0 commit comments

Comments
 (0)