Skip to content

Commit

Permalink
Tests: add Requires* 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 `Requires*` attributes in all the appropriate places.

The `@requires` 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 78464dc commit 61ce3e9
Show file tree
Hide file tree
Showing 21 changed files with 57 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/Polyfills/AssertClosedResourceBzip2Test.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\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use Yoast\PHPUnitPolyfills\Helpers\ResourceHelper;
use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource;

Expand All @@ -19,6 +20,7 @@
*/
#[CoversClass( AssertClosedResource::class )]
#[CoversClass( ResourceHelper::class )]
#[RequiresPhpExtension( 'bz2' )]
final class AssertClosedResourceBzip2Test extends AssertClosedResourceTestCase {

use AssertClosedResource;
Expand Down
4 changes: 4 additions & 0 deletions tests/Polyfills/AssertClosedResourceCurlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Yoast\PHPUnitPolyfills\Tests\Polyfills;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\RequiresPhp;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use Yoast\PHPUnitPolyfills\Helpers\ResourceHelper;
use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource;

Expand All @@ -23,6 +25,8 @@
*/
#[CoversClass( AssertClosedResource::class )]
#[CoversClass( ResourceHelper::class )]
#[RequiresPhp( '< 8.0' )]
#[RequiresPhpExtension( 'curl' )]
final class AssertClosedResourceCurlTest extends AssertClosedResourceTestCase {

use AssertClosedResource;
Expand Down
4 changes: 4 additions & 0 deletions tests/Polyfills/AssertClosedResourceEnchantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Yoast\PHPUnitPolyfills\Tests\Polyfills;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\RequiresPhp;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use Yoast\PHPUnitPolyfills\Helpers\ResourceHelper;
use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource;

Expand All @@ -26,6 +28,8 @@
*/
#[CoversClass( AssertClosedResource::class )]
#[CoversClass( ResourceHelper::class )]
#[RequiresPhp( '< 8.0' )]
#[RequiresPhpExtension( 'enchant' )]
final class AssertClosedResourceEnchantTest extends AssertClosedResourceTestCase {

use AssertClosedResource;
Expand Down
4 changes: 4 additions & 0 deletions tests/Polyfills/AssertClosedResourceFinfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Yoast\PHPUnitPolyfills\Tests\Polyfills;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\RequiresPhp;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use Yoast\PHPUnitPolyfills\Helpers\ResourceHelper;
use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource;

Expand All @@ -23,6 +25,8 @@
*/
#[CoversClass( AssertClosedResource::class )]
#[CoversClass( ResourceHelper::class )]
#[RequiresPhp( '< 8.1' )]
#[RequiresPhpExtension( 'finfo' )]
final class AssertClosedResourceFinfoTest extends AssertClosedResourceTestCase {

use AssertClosedResource;
Expand Down
4 changes: 4 additions & 0 deletions tests/Polyfills/AssertClosedResourceGdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Yoast\PHPUnitPolyfills\Tests\Polyfills;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\RequiresPhp;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use Yoast\PHPUnitPolyfills\Helpers\ResourceHelper;
use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource;

Expand All @@ -23,6 +25,8 @@
*/
#[CoversClass( AssertClosedResource::class )]
#[CoversClass( ResourceHelper::class )]
#[RequiresPhp( '< 8.0' )]
#[RequiresPhpExtension( 'gd' )]
final class AssertClosedResourceGdTest extends AssertClosedResourceTestCase {

use AssertClosedResource;
Expand Down
2 changes: 2 additions & 0 deletions tests/Polyfills/AssertClosedResourceShmopTest.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\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use Yoast\PHPUnitPolyfills\Helpers\ResourceHelper;
use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource;

Expand All @@ -28,6 +29,7 @@
*/
#[CoversClass( AssertClosedResource::class )]
#[CoversClass( ResourceHelper::class )]
#[RequiresPhpExtension( 'shmop' )]
final class AssertClosedResourceShmopTest extends AssertClosedResourceTestCase {

use AssertClosedResource;
Expand Down
4 changes: 4 additions & 0 deletions tests/Polyfills/AssertClosedResourceWddxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Yoast\PHPUnitPolyfills\Tests\Polyfills;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\RequiresPhp;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use Yoast\PHPUnitPolyfills\Helpers\ResourceHelper;
use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource;

Expand All @@ -26,6 +28,8 @@
*/
#[CoversClass( AssertClosedResource::class )]
#[CoversClass( ResourceHelper::class )]
#[RequiresPhp( '< 7.4' )]
#[RequiresPhpExtension( 'wddx' )]
final class AssertClosedResourceWddxTest extends AssertClosedResourceTestCase {

use AssertClosedResource;
Expand Down
4 changes: 4 additions & 0 deletions tests/Polyfills/AssertClosedResourceXmlParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Yoast\PHPUnitPolyfills\Tests\Polyfills;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\RequiresPhp;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use Yoast\PHPUnitPolyfills\Helpers\ResourceHelper;
use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource;

Expand All @@ -23,6 +25,8 @@
*/
#[CoversClass( AssertClosedResource::class )]
#[CoversClass( ResourceHelper::class )]
#[RequiresPhp( '< 8.0' )]
#[RequiresPhpExtension( 'libxml' )]
final class AssertClosedResourceXmlParserTest extends AssertClosedResourceTestCase {

use AssertClosedResource;
Expand Down
2 changes: 2 additions & 0 deletions tests/Polyfills/AssertClosedResourceZipTest.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\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use Yoast\PHPUnitPolyfills\Helpers\ResourceHelper;
use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource;

Expand All @@ -27,6 +28,7 @@
*/
#[CoversClass( AssertClosedResource::class )]
#[CoversClass( ResourceHelper::class )]
#[RequiresPhpExtension( 'zip' )]
final class AssertClosedResourceZipTest extends AssertClosedResourceTestCase {

use AssertClosedResource;
Expand Down
2 changes: 2 additions & 0 deletions tests/Polyfills/AssertClosedResourceZlibTest.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\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use Yoast\PHPUnitPolyfills\Helpers\ResourceHelper;
use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource;

Expand All @@ -19,6 +20,7 @@
*/
#[CoversClass( AssertClosedResource::class )]
#[CoversClass( ResourceHelper::class )]
#[RequiresPhpExtension( 'zlib' )]
final class AssertClosedResourceZlibTest extends AssertClosedResourceTestCase {

use AssertClosedResource;
Expand Down
2 changes: 2 additions & 0 deletions tests/Polyfills/AssertObjectEqualsPHPUnitLt940Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\RequiresPhp;
use PHPUnit\Framework\TestCase;
use PHPUnit\Runner\Version as PHPUnit_Version;
use PHPUnit_Framework_AssertionFailedError;
Expand Down Expand Up @@ -209,6 +210,7 @@ public function testAssertObjectEqualsFailsOnMethodParamMissingTypeDeclaration()
*
* @return void
*/
#[RequiresPhp( '8.0' )]
public function testAssertObjectEqualsFailsOnMethodParamHasUnionTypeDeclaration() {
$msg = 'Parameter of comparison method Yoast\PHPUnitPolyfills\Tests\Polyfills\Fixtures\ValueObjectUnionNoReturnType::equalsParamUnionType() does not have a declared type.';

Expand Down
4 changes: 4 additions & 0 deletions tests/Polyfills/AssertObjectEqualsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\RequiresPhp;
use PHPUnit\Framework\ComparisonMethodDoesNotAcceptParameterTypeException;
use PHPUnit\Framework\ComparisonMethodDoesNotDeclareBoolReturnTypeException;
use PHPUnit\Framework\ComparisonMethodDoesNotDeclareExactlyOneParameterException;
Expand Down Expand Up @@ -35,6 +36,7 @@
* @requires PHP 7.0
*/
#[CoversClass( AssertObjectEquals::class )]
#[RequiresPhp( '7.0' )]
final class AssertObjectEqualsTest extends TestCase {

use AssertObjectEquals;
Expand Down Expand Up @@ -218,6 +220,7 @@ public function testAssertObjectEqualsFailsOnMethodAllowsForMoreParams() {
*
* @return void
*/
#[RequiresPhp( '7.1' )]
public function testAssertObjectEqualsFailsOnMethodParamNotRequired() {
$msg = 'Comparison method Yoast\PHPUnitPolyfills\Tests\Polyfills\Fixtures\ValueObjectParamNotRequired::equalsParamNotRequired() does not declare exactly one parameter.';

Expand Down Expand Up @@ -266,6 +269,7 @@ public function testAssertObjectEqualsFailsOnMethodParamMissingTypeDeclaration()
*
* @return void
*/
#[RequiresPhp( '8.0' )]
public function testAssertObjectEqualsFailsOnMethodParamHasUnionTypeDeclaration() {
$msg = 'Parameter of comparison method Yoast\PHPUnitPolyfills\Tests\Polyfills\Fixtures\ValueObjectUnion::equalsParamUnionType() does not have a declared type.';

Expand Down
2 changes: 2 additions & 0 deletions tests/TestCases/TestCaseTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Yoast\PHPUnitPolyfills\Tests\TestCases;

use Exception;
use PHPUnit\Framework\Attributes\RequiresPhp;
use stdClass;
use Yoast\PHPUnitPolyfills\Tests\Polyfills\AssertFileEqualsSpecializationsTest;
use Yoast\PHPUnitPolyfills\Tests\Polyfills\Fixtures\ValueObject;
Expand Down Expand Up @@ -129,6 +130,7 @@ final public function testAvailabilityEqualToSpecializations() {
*
* @return void
*/
#[RequiresPhp( '7.0' )]
final public function testAvailabilityAssertObjectEquals() {
$expected = new ValueObject( 'test' );
$actual = new ValueObject( 'test' );
Expand Down
2 changes: 2 additions & 0 deletions tests/TestListeners/Fixtures/FailurePHPUnitGte7.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures;

use PHPUnit\Framework\Attributes\RequiresPhpunit;
use PHPUnit\Framework\TestCase;

/**
* Fixture to generate a "failed test" to pass to the test listener.
*
* @requires PHPUnit 7.0
*/
#[RequiresPhpunit( '7.0' )]
class FailurePHPUnitGte7 extends TestCase {

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/TestListeners/Fixtures/IncompletePHPUnitGte7.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures;

use PHPUnit\Framework\Attributes\RequiresPhpunit;
use PHPUnit\Framework\TestCase;

/**
* Fixture to generate an "incomplete test" to pass to the test listener.
*
* @requires PHPUnit 7.0
*/
#[RequiresPhpunit( '7.0' )]
class IncompletePHPUnitGte7 extends TestCase {

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/TestListeners/Fixtures/RiskyPHPUnitGte7.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures;

use PHPUnit\Framework\Attributes\RequiresPhpunit;
use PHPUnit\Framework\TestCase;

/**
* Fixture to generate a "risky test" to pass to the test listener.
*
* @requires PHPUnit 7.0
*/
#[RequiresPhpunit( '7.0' )]
class RiskyPHPUnitGte7 extends TestCase {

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/TestListeners/Fixtures/SkippedPHPUnitGte7.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures;

use PHPUnit\Framework\Attributes\RequiresPhpunit;
use PHPUnit\Framework\TestCase;

/**
* Fixture to generate a "skipped test" to pass to the test listener.
*
* @requires PHPUnit 7.0
*/
#[RequiresPhpunit( '7.0' )]
class SkippedPHPUnitGte7 extends TestCase {

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/TestListeners/Fixtures/SuccessPHPUnitGte7.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures;

use PHPUnit\Framework\Attributes\RequiresPhpunit;
use PHPUnit\Framework\TestCase;

/**
* Fixture to generate a "successfull test" to pass to the test listener.
*
* @requires PHPUnit 7.0
*/
#[RequiresPhpunit( '7.0' )]
class SuccessPHPUnitGte7 extends TestCase {

/**
Expand Down
1 change: 1 addition & 0 deletions tests/TestListeners/Fixtures/TestErrorPHPUnitGte7.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* @coversNothing
*/
#[CoversNothing]
#[RequiresPhpunit( '7.0' )]
class TestErrorPHPUnitGte7 extends TestCase {

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/TestListeners/Fixtures/WarningPHPUnitGte7.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures;

use PHPUnit\Framework\Attributes\RequiresPhpunit;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Warning as PHPUnit_Warning;

Expand All @@ -10,6 +11,7 @@
*
* @requires PHPUnit 7.0
*/
#[RequiresPhpunit( '7.0' )]
class WarningPHPUnitGte7 extends TestCase {

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/TestListeners/TestListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Yoast\PHPUnitPolyfills\Tests\TestListeners;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\RequiresPhpunit;
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
use PHPUnit\Framework\TestResult;
use Yoast\PHPUnitPolyfills\Autoload;
Expand All @@ -21,6 +22,7 @@
*/
#[CoversClass( TestListenerDefaultImplementation::class )]
#[CoversClass( TestListenerSnakeCaseMethods::class )]
#[RequiresPhpunit( '< 10' )]
final class TestListenerTest extends TestCase {

/**
Expand Down Expand Up @@ -73,6 +75,7 @@ public function testError() {
*
* @return void
*/
#[RequiresPhpunit( '5' )]
public function testWarning() {
$test = $this->getTestObject( 'Warning' );
$test->run( $this->result );
Expand Down Expand Up @@ -119,6 +122,7 @@ public function testIncomplete() {
*
* @return void
*/
#[RequiresPhpunit( '6' )]
public function testRisky() {
$test = $this->getTestObject( 'Risky' );
$test->run( $this->result );
Expand Down

0 comments on commit 61ce3e9

Please sign in to comment.