Skip to content

Commit

Permalink
Tests: add Before/After[Class] attributes
Browse files Browse the repository at this point in the history
PHPUnit 10 introduced attributes as replacements for docblock annotations.
PHPUnit 11 deprecates the use of docblock annotations in favour of attributes.

If both an attribute as well as an annotation are found, no PHPUnit deprecation warning will be thrown.

However, between PHPUnit 10.0 and PHPUnit 11.3.2, if the `failOnDeprecation` is set to `true` (which it is for this library as the builds should fail on deprecations in PHP), builds will _also_ fail on deprecation notices from PHPUnit itself.

This behaviour was (finally) changed in PHPUnit 10.5.32 and 11.3.3 (released this week), but that's insufficient for our needs.
For this library running the tests on high/low PHPUnit on each PHP version is imperative. It is also not the job of this library to decide the target PHPUnit versions for the consumer projects, so we cannot raise the minimum PHPUnit 11 version to 11.3.3.

This means that without the attributes, the test runs for the package would fail on PHPUnit 11.0.0 - 11.3.2.

As these attributes are already available in PHPUnit 10, it makes sense then to add them for both the 2.x branch, as well as the (upcoming) 3.x branch.

This commit adds the `Before/After*` attributes in all the appropriate places.

The `@before/after*` annotations remain as the tests also still need to run on PHP 5.6 - 8.0 using PHPUnit 5.x - 9.x.
These can be removed once the codebase has a PHP 8.1/PHPUnit 10 minimum requirement.

Note: due to the syntax for attributes, these can be safely added as they are ignored as comments on PHP < 8.0.
Along the same line, if there is no "listener" for the attributes (PHP 8.0/PHPUnit 9.x), they are ignored by PHP as well.
  • Loading branch information
jrfnl committed Sep 6, 2024
1 parent 61ce3e9 commit 5cecae1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/TestCases/XTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace Yoast\PHPUnitPolyfills\TestCases;

use PHPUnit\Framework\Attributes\After;
use PHPUnit\Framework\Attributes\AfterClass;
use PHPUnit\Framework\Attributes\Before;
use PHPUnit\Framework\Attributes\BeforeClass;
use PHPUnit\Framework\TestCase as PHPUnit_TestCase;
use Yoast\PHPUnitPolyfills\Helpers\AssertAttributeHelper;
use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource;
Expand Down Expand Up @@ -55,6 +59,7 @@ abstract class XTestCase extends PHPUnit_TestCase {
*
* @return void
*/
#[BeforeClass]
public static function setUpFixturesBeforeClass() {
parent::setUpBeforeClass();
}
Expand All @@ -68,6 +73,7 @@ public static function setUpFixturesBeforeClass() {
*
* @return void
*/
#[Before]
protected function setUpFixtures() {
parent::setUp();
}
Expand All @@ -81,6 +87,7 @@ protected function setUpFixtures() {
*
* @return void
*/
#[After]
protected function tearDownFixtures() {
parent::tearDown();
}
Expand All @@ -94,6 +101,7 @@ protected function tearDownFixtures() {
*
* @return void
*/
#[AfterClass]
public static function tearDownFixturesAfterClass() {
parent::tearDownAfterClass();
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Polyfills/AssertClosedResourceShmopTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Yoast\PHPUnitPolyfills\Tests\Polyfills;

use PHPUnit\Framework\Attributes\Before;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use Yoast\PHPUnitPolyfills\Helpers\ResourceHelper;
Expand Down Expand Up @@ -44,6 +45,7 @@ final class AssertClosedResourceShmopTest extends AssertClosedResourceTestCase {
*
* @return void
*/
#[Before]
protected function skipOnIncompatiblePHP() {
if ( \PHP_VERSION_ID < 70000 || \PHP_VERSION_ID >= 80000 ) {
$this->markTestSkipped( 'This test requires PHP 7.x.' );
Expand Down
2 changes: 2 additions & 0 deletions tests/Polyfills/AssertObjectEqualsPHPUnitLt940Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Yoast\PHPUnitPolyfills\Tests\Polyfills;

use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\Attributes\Before;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\RequiresPhp;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -57,6 +58,7 @@ final class AssertObjectEqualsPHPUnitLt940Test extends TestCase {
*
* @return void
*/
#[Before]
public function maybeSkipTest() {
if ( \version_compare( PHPUnit_Version::id(), '9.4.0', '>=' ) ) {
$this->markTestSkipped( 'This test can not be run with the PHPUnit native implementation of assertObjectEquals()' );
Expand Down
8 changes: 8 additions & 0 deletions tests/TestCases/XTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace Yoast\PHPUnitPolyfills\Tests\TestCases;

use PHPUnit\Framework\Attributes\After;
use PHPUnit\Framework\Attributes\AfterClass;
use PHPUnit\Framework\Attributes\Before;
use PHPUnit\Framework\Attributes\BeforeClass;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use Yoast\PHPUnitPolyfills\TestCases\XTestCase;
Expand Down Expand Up @@ -47,6 +51,7 @@ final class XTestCaseTest extends XTestCase {
*
* @return void
*/
#[BeforeClass]
public static function setUpFixturesBeforeClass() {
parent::setUpFixturesBeforeClass();

Expand All @@ -60,6 +65,7 @@ public static function setUpFixturesBeforeClass() {
*
* @return void
*/
#[Before]
protected function setUpFixtures() {
parent::setUpFixtures();

Expand All @@ -73,6 +79,7 @@ protected function setUpFixtures() {
*
* @return void
*/
#[After]
protected function tearDownFixtures() {
++self::$after;

Expand All @@ -86,6 +93,7 @@ protected function tearDownFixtures() {
*
* @return void
*/
#[AfterClass]
public static function tearDownFixturesAfterClass() {
// Reset.
self::$beforeClass = 0;
Expand Down

0 comments on commit 5cecae1

Please sign in to comment.