Skip to content

Commit bc302dd

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 86e363c commit bc302dd

File tree

10 files changed

+62
-34
lines changed

10 files changed

+62
-34
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: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,17 @@ class AcceptTest extends TestCase
3636
/**
3737
* Initialize the config and ruleset objects based on the `AcceptTest.xml` ruleset file.
3838
*
39+
* @beforeClass
40+
*
3941
* @return void
4042
*/
41-
public static function setUpBeforeClass()
43+
public static function initializeConfigAndRuleset()
4244
{
4345
$standard = __DIR__.'/'.basename(__FILE__, '.php').'.xml';
4446
self::$config = new Config(["--standard=$standard", "--ignore=*/somethingelse/*"]);
4547
self::$ruleset = new Ruleset(self::$config);
4648

47-
}//end setUpBeforeClass()
49+
}//end initializeConfigAndRuleset()
4850

4951

5052
/**

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
$this->standard = __DIR__.'/'.basename(__FILE__, '.php').'.xml';
4951
$repoRootDir = dirname(dirname(dirname(__DIR__)));
@@ -67,19 +69,21 @@ public function setUp()
6769
$config = new Config(["--standard={$this->standard}"]);
6870
$this->ruleset = new Ruleset($config);
6971

70-
}//end setUp()
72+
}//end initializeConfigAndRuleset()
7173

7274

7375
/**
7476
* Reset ruleset file.
7577
*
78+
* @after
79+
*
7680
* @return void
7781
*/
78-
public function tearDown()
82+
public function resetRuleset()
7983
{
8084
file_put_contents($this->standard, $this->contents);
8185

82-
}//end tearDown()
86+
}//end resetRuleset()
8387

8488

8589
/**

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');
@@ -66,21 +68,23 @@ public function setUp()
6668
$config = new Config(["--standard={$this->standard}"]);
6769
$this->ruleset = new Ruleset($config);
6870

69-
}//end setUp()
71+
}//end initializeConfigAndRuleset()
7072

7173

7274
/**
7375
* Reset ruleset file.
7476
*
77+
* @after
78+
*
7579
* @return void
7680
*/
77-
public function tearDown()
81+
public function resetRuleset()
7882
{
7983
if (DIRECTORY_SEPARATOR !== '/') {
8084
file_put_contents($this->standard, $this->contents);
8185
}
8286

83-
}//end tearDown()
87+
}//end resetRuleset()
8488

8589

8690
/**

tests/Core/Ruleset/RuleInclusionTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ class RuleInclusionTest extends TestCase
4242
/**
4343
* Initialize the config and ruleset objects based on the `RuleInclusionTest.xml` ruleset file.
4444
*
45+
* @beforeClass
46+
*
4547
* @return void
4648
*/
47-
public static function setUpBeforeClass()
49+
public static function initializeConfigAndRuleset()
4850
{
4951
$standard = __DIR__.'/'.basename(__FILE__, '.php').'.xml';
5052
self::$standard = $standard;
@@ -70,19 +72,21 @@ public static function setUpBeforeClass()
7072
$config = new Config(["--standard=$standard"]);
7173
self::$ruleset = new Ruleset($config);
7274

73-
}//end setUpBeforeClass()
75+
}//end initializeConfigAndRuleset()
7476

7577

7678
/**
7779
* Reset ruleset file.
7880
*
81+
* @after
82+
*
7983
* @return void
8084
*/
81-
public function tearDown()
85+
public function resetRuleset()
8286
{
8387
file_put_contents(self::$standard, self::$contents);
8488

85-
}//end tearDown()
89+
}//end resetRuleset()
8690

8791

8892
/**

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)