Skip to content

Commit 9771150

Browse files
jrfnlgrogy
authored andcommitted
PHPUnit: use annotations for fixtures / cross-version compat up to PHPUnit 9.x
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 `setUp()` method to another, descriptive name and using the `@before` annotation, 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. As PHPUnit has a caching feature build in as of PHPUnit 8, we need to add the `.phpunit.result.cache` file to `.gitignore`. There is also one assertion which has changed, so using an if/else to work around that.
1 parent 1ac508e commit 9771150

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ build
22
vendor
33
composer.lock
44
phpunit.xml
5+
.phpunit.result.cache
56
.phpcs.xml
67
phpcs.xml

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"php": ">=5.4.0"
1919
},
2020
"require-dev": {
21-
"phpunit/phpunit": "^4.8.36 || ^5.0 || ^6.0 || ^7.0",
21+
"phpunit/phpunit": "^4.8.36 || ^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0",
2222
"php-parallel-lint/php-parallel-lint": "^1.0",
2323
"php-parallel-lint/php-var-dump-check": "0.*",
2424
"squizlabs/php_codesniffer": "3.*",

tests/ConsoleColorTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ class ConsoleColorTest extends TestCase
3636
/** @var ConsoleColorWithForceSupport */
3737
private $uut;
3838

39-
protected function setUp()
39+
/**
40+
* @before
41+
*/
42+
protected function setUpConsoleColor()
4043
{
4144
$this->uut = new ConsoleColorWithForceSupport();
4245
}
@@ -180,7 +183,13 @@ public function testForceStyle()
180183

181184
public function testGetPossibleStyles()
182185
{
183-
$this->assertInternalType('array', $this->uut->getPossibleStyles());
186+
if (\method_exists($this, 'assertIsArray')) {
187+
// PHPUnit 7.5+.
188+
$this->assertIsArray($this->uut->getPossibleStyles());
189+
} else {
190+
// PHPUnit < 7.5.
191+
$this->assertInternalType('array', $this->uut->getPossibleStyles());
192+
}
184193
$this->assertNotEmpty($this->uut->getPossibleStyles());
185194
}
186195

0 commit comments

Comments
 (0)