Skip to content

Commit 0421e80

Browse files
committed
Tests: rename fixture methods and use annotations
As of PHPUnit 8.x, the method signature for the `setUpBeforeClass()`, `setUp()`, `tearDown()` and `tearDownAfterClass()` fixture methods has changed to require the `void` return type. As the `void` return type isn't available until PHP 7.1, this cannot be implemented. Annotations to the rescue. By renaming the `setUpBeforeClass()`, `setUp()`, `tearDown()` and `tearDownAfterClass()` methods to another, descriptive name and using the `@beforeClass`, `@before`, `@after` and `@afterClass` annotations, the tests can be made cross-version compatible up to PHPUnit 9.x. With this change, the unit tests can now be run on PHPUnit 4 - 9. This constitutes a minor BC-break for external standards which a) extend the PHPCS native testsuite and b) overload the `AbstractSniffUnitTest::setUp()` method. While quite a few external standards extends the PHPCS native testsuite, I very much doubt any of these overload the `setUp()` method, so IMO and taking into account that this is a test-only change, this is an acceptable change to include in the next PHPCS minor. Ref: https://docs.phpunit.de/en/7.5/annotations.html#before
1 parent 09af586 commit 0421e80

File tree

11 files changed

+74
-40
lines changed

11 files changed

+74
-40
lines changed

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/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/Filters/Filter/AcceptTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ class AcceptTest extends TestCase
3636
/**
3737
* Initialize the test.
3838
*
39+
* @before
40+
*
3941
* @return void
4042
*/
41-
public function setUp()
43+
public function skipOnPEAR()
4244
{
4345
if ($GLOBALS['PHP_CODESNIFFER_PEAR'] === true) {
4446
// PEAR installs test and sniff files into different locations
@@ -47,15 +49,17 @@ public function setUp()
4749
$this->markTestSkipped('Test cannot run from a PEAR install');
4850
}
4951

50-
}//end setUp()
52+
}//end skipOnPEAR()
5153

5254

5355
/**
5456
* Initialize the config and ruleset objects based on the `AcceptTest.xml` ruleset file.
5557
*
58+
* @beforeClass
59+
*
5660
* @return void
5761
*/
58-
public static function setUpBeforeClass()
62+
public static function initializeConfigAndRuleset()
5963
{
6064
if ($GLOBALS['PHP_CODESNIFFER_PEAR'] === true) {
6165
// This test will be skipped.
@@ -66,7 +70,7 @@ public static function setUpBeforeClass()
6670
self::$config = new Config(["--standard=$standard", "--ignore=*/somethingelse/*"]);
6771
self::$ruleset = new Ruleset(self::$config);
6872

69-
}//end setUpBeforeClass()
73+
}//end initializeConfigAndRuleset()
7074

7175

7276
/**

tests/Core/Ruleset/RuleInclusionAbsoluteLinuxTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ class RuleInclusionAbsoluteLinuxTest extends TestCase
4141
/**
4242
* Initialize the config and ruleset objects.
4343
*
44+
* @before
45+
*
4446
* @return void
4547
*/
46-
public function setUp()
48+
public function initializeConfigAndRuleset()
4749
{
4850
if ($GLOBALS['PHP_CODESNIFFER_PEAR'] === true) {
4951
// PEAR installs test and sniff files into different locations
@@ -74,19 +76,21 @@ public function setUp()
7476
$config = new Config(["--standard={$this->standard}"]);
7577
$this->ruleset = new Ruleset($config);
7678

77-
}//end setUp()
79+
}//end initializeConfigAndRuleset()
7880

7981

8082
/**
8183
* Reset ruleset file.
8284
*
85+
* @after
86+
*
8387
* @return void
8488
*/
85-
public function tearDown()
89+
public function resetRuleset()
8690
{
8791
file_put_contents($this->standard, $this->contents);
8892

89-
}//end tearDown()
93+
}//end resetRuleset()
9094

9195

9296
/**

tests/Core/Ruleset/RuleInclusionAbsoluteWindowsTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ class RuleInclusionAbsoluteWindowsTest extends TestCase
4141
/**
4242
* Initialize the config and ruleset objects.
4343
*
44+
* @before
45+
*
4446
* @return void
4547
*/
46-
public function setUp()
48+
public function initializeConfigAndRuleset()
4749
{
4850
if (DIRECTORY_SEPARATOR === '/') {
4951
$this->markTestSkipped('Windows specific test');
@@ -73,21 +75,23 @@ public function setUp()
7375
$config = new Config(["--standard={$this->standard}"]);
7476
$this->ruleset = new Ruleset($config);
7577

76-
}//end setUp()
78+
}//end initializeConfigAndRuleset()
7779

7880

7981
/**
8082
* Reset ruleset file.
8183
*
84+
* @after
85+
*
8286
* @return void
8387
*/
84-
public function tearDown()
88+
public function resetRuleset()
8589
{
8690
if (DIRECTORY_SEPARATOR !== '/') {
8791
file_put_contents($this->standard, $this->contents);
8892
}
8993

90-
}//end tearDown()
94+
}//end resetRuleset()
9195

9296

9397
/**

tests/Core/Ruleset/RuleInclusionTest.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ class RuleInclusionTest extends TestCase
4242
/**
4343
* Initialize the test.
4444
*
45+
* @before
46+
*
4547
* @return void
4648
*/
47-
public function setUp()
49+
public function skipOnPEAR()
4850
{
4951
if ($GLOBALS['PHP_CODESNIFFER_PEAR'] === true) {
5052
// PEAR installs test and sniff files into different locations
@@ -53,15 +55,17 @@ public function setUp()
5355
$this->markTestSkipped('Test cannot run from a PEAR install');
5456
}
5557

56-
}//end setUp()
58+
}//end skipOnPEAR()
5759

5860

5961
/**
6062
* Initialize the config and ruleset objects based on the `RuleInclusionTest.xml` ruleset file.
6163
*
64+
* @beforeClass
65+
*
6266
* @return void
6367
*/
64-
public static function setUpBeforeClass()
68+
public static function initializeConfigAndRuleset()
6569
{
6670
if ($GLOBALS['PHP_CODESNIFFER_PEAR'] === true) {
6771
// This test will be skipped.
@@ -92,19 +96,21 @@ public static function setUpBeforeClass()
9296
$config = new Config(["--standard=$standard"]);
9397
self::$ruleset = new Ruleset($config);
9498

95-
}//end setUpBeforeClass()
99+
}//end initializeConfigAndRuleset()
96100

97101

98102
/**
99103
* Reset ruleset file.
100104
*
105+
* @after
106+
*
101107
* @return void
102108
*/
103-
public function tearDown()
109+
public function resetRuleset()
104110
{
105111
file_put_contents(self::$standard, self::$contents);
106112

107-
}//end tearDown()
113+
}//end resetRuleset()
108114

109115

110116
/**

tests/Core/Ruleset/SetSniffPropertyTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ class SetSniffPropertyTest extends TestCase
2626
/**
2727
* Initialize the test.
2828
*
29+
* @before
30+
*
2931
* @return void
3032
*/
31-
public function setUp()
33+
public function skipOnPEAR()
3234
{
3335
if ($GLOBALS['PHP_CODESNIFFER_PEAR'] === true) {
3436
// PEAR installs test and sniff files into different locations
@@ -37,7 +39,7 @@ public function setUp()
3739
$this->markTestSkipped('Test cannot run from a PEAR install');
3840
}
3941

40-
}//end setUp()
42+
}//end skipOnPEAR()
4143

4244

4345
/**

tests/Core/Sniffs/AbstractArraySniffTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,16 @@ class AbstractArraySniffTest extends AbstractMethodUnitTest
3131
* The test case file for a unit test class has to be in the same directory
3232
* directory and use the same file name as the test class, using the .inc extension.
3333
*
34+
* @beforeClass
35+
*
3436
* @return void
3537
*/
36-
public static function setUpBeforeClass()
38+
public static function initializeFile()
3739
{
3840
self::$sniff = new AbstractArraySniffTestable();
39-
parent::setUpBeforeClass();
41+
parent::initializeFile();
4042

41-
}//end setUpBeforeClass()
43+
}//end initializeFile()
4244

4345

4446
/**

tests/Core/Tokenizer/HeredocNowdocCloserTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ class HeredocNowdocCloserTest extends AbstractMethodUnitTest
2929
* {@internal This is a near duplicate of the original method. Only difference is that
3030
* tab replacement is enabled for this test.}
3131
*
32+
* @beforeClass
33+
*
3234
* @return void
3335
*/
34-
public static function setUpBeforeClass()
36+
public static function initializeFile()
3537
{
3638
$config = new Config();
3739
$config->standards = ['PSR1'];
@@ -51,7 +53,7 @@ public static function setUpBeforeClass()
5153
self::$phpcsFile = new DummyFile($contents, $ruleset, $config);
5254
self::$phpcsFile->process();
5355

54-
}//end setUpBeforeClass()
56+
}//end initializeFile()
5557

5658

5759
/**

tests/Standards/AbstractSniffUnitTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,17 @@ abstract class AbstractSniffUnitTest extends TestCase
5050
/**
5151
* Sets up this unit test.
5252
*
53+
* @before
54+
*
5355
* @return void
5456
*/
55-
protected function setUp()
57+
protected function setUpPrerequisites()
5658
{
5759
$class = get_class($this);
5860
$this->standardsDir = $GLOBALS['PHP_CODESNIFFER_STANDARD_DIRS'][$class];
5961
$this->testsDir = $GLOBALS['PHP_CODESNIFFER_TEST_DIRS'][$class];
6062

61-
}//end setUp()
63+
}//end setUpPrerequisites()
6264

6365

6466
/**

0 commit comments

Comments
 (0)