From 9d0f48897b590438258347f690114cab22f08035 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 25 Feb 2024 09:55:50 +0100 Subject: [PATCH 1/4] Tests: add `Covers*` attributes 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 `Covers*` attributes in all the appropriate places. The `@covers` annotations remain as code coverage also still needs to be measure 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. --- tests/Exceptions/InvalidComparisonMethodExceptionTest.php | 2 ++ tests/Helpers/AssertAttributesHelperTest.php | 3 +++ tests/Polyfills/AssertClosedResourceBzip2Test.php | 4 ++++ tests/Polyfills/AssertClosedResourceCurlTest.php | 4 ++++ tests/Polyfills/AssertClosedResourceDirTest.php | 4 ++++ tests/Polyfills/AssertClosedResourceEnchantTest.php | 4 ++++ tests/Polyfills/AssertClosedResourceFileTest.php | 4 ++++ tests/Polyfills/AssertClosedResourceFinfoTest.php | 4 ++++ tests/Polyfills/AssertClosedResourceGdTest.php | 4 ++++ tests/Polyfills/AssertClosedResourceNotResourceTest.php | 4 ++++ tests/Polyfills/AssertClosedResourceProcessTest.php | 4 ++++ tests/Polyfills/AssertClosedResourceShmopTest.php | 4 ++++ tests/Polyfills/AssertClosedResourceWddxTest.php | 4 ++++ tests/Polyfills/AssertClosedResourceXmlParserTest.php | 3 +++ tests/Polyfills/AssertClosedResourceZipTest.php | 4 ++++ tests/Polyfills/AssertClosedResourceZlibTest.php | 4 ++++ tests/Polyfills/AssertEqualsSpecializationsTest.php | 2 ++ tests/Polyfills/AssertFileEqualsSpecializationsTest.php | 2 ++ tests/Polyfills/AssertIgnoringLineEndingsTest.php | 2 ++ tests/Polyfills/AssertIsListTest.php | 2 ++ tests/Polyfills/AssertIsTypeTest.php | 2 ++ tests/Polyfills/AssertObjectEqualsPHPUnitLt940Test.php | 2 ++ tests/Polyfills/AssertObjectEqualsTest.php | 2 ++ tests/Polyfills/AssertObjectPropertyTest.php | 2 ++ tests/Polyfills/AssertStringContainsTest.php | 2 ++ tests/Polyfills/AssertionRenamesTest.php | 2 ++ tests/Polyfills/EqualToSpecializationsTest.php | 2 ++ tests/Polyfills/ExpectExceptionMessageMatchesTest.php | 2 ++ tests/Polyfills/ExpectExceptionObjectTest.php | 2 ++ tests/TestCases/TestCaseTest.php | 2 ++ tests/TestCases/XTestCaseTest.php | 2 ++ tests/TestListeners/Fixtures/TestErrorPHPUnitGte7.php | 2 ++ tests/TestListeners/TestListenerTest.php | 5 +++++ 33 files changed, 97 insertions(+) diff --git a/tests/Exceptions/InvalidComparisonMethodExceptionTest.php b/tests/Exceptions/InvalidComparisonMethodExceptionTest.php index 6572d18..fa20efe 100644 --- a/tests/Exceptions/InvalidComparisonMethodExceptionTest.php +++ b/tests/Exceptions/InvalidComparisonMethodExceptionTest.php @@ -2,6 +2,7 @@ namespace Yoast\PHPUnitPolyfills\Tests\Exceptions; +use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; use Yoast\PHPUnitPolyfills\Exceptions\InvalidComparisonMethodException; @@ -10,6 +11,7 @@ * * @covers \Yoast\PHPUnitPolyfills\Exceptions\InvalidComparisonMethodException */ +#[CoversClass( InvalidComparisonMethodException::class )] final class InvalidComparisonMethodExceptionTest extends TestCase { /** diff --git a/tests/Helpers/AssertAttributesHelperTest.php b/tests/Helpers/AssertAttributesHelperTest.php index 7e194ba..cf383e1 100644 --- a/tests/Helpers/AssertAttributesHelperTest.php +++ b/tests/Helpers/AssertAttributesHelperTest.php @@ -2,7 +2,9 @@ namespace Yoast\PHPUnitPolyfills\Tests\Helpers; +use PHPUnit\Framework\Attributes\CoversClass; use ReflectionException; +use Yoast\PHPUnitPolyfills\Helpers\AssertAttributeHelper; use Yoast\PHPUnitPolyfills\TestCases\TestCase; use Yoast\PHPUnitPolyfills\Tests\Helpers\Fixtures\ClassWithProperties; @@ -11,6 +13,7 @@ * * @covers \Yoast\PHPUnitPolyfills\Helpers\AssertAttributeHelper */ +#[CoversClass( AssertAttributeHelper::class )] final class AssertAttributesHelperTest extends TestCase { /** diff --git a/tests/Polyfills/AssertClosedResourceBzip2Test.php b/tests/Polyfills/AssertClosedResourceBzip2Test.php index cdc6c25..bc8b152 100644 --- a/tests/Polyfills/AssertClosedResourceBzip2Test.php +++ b/tests/Polyfills/AssertClosedResourceBzip2Test.php @@ -2,6 +2,8 @@ namespace Yoast\PHPUnitPolyfills\Tests\Polyfills; +use PHPUnit\Framework\Attributes\CoversClass; +use Yoast\PHPUnitPolyfills\Helpers\ResourceHelper; use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource; /** @@ -15,6 +17,8 @@ * * @requires extension bz2 */ +#[CoversClass( AssertClosedResource::class )] +#[CoversClass( ResourceHelper::class )] final class AssertClosedResourceBzip2Test extends AssertClosedResourceTestCase { use AssertClosedResource; diff --git a/tests/Polyfills/AssertClosedResourceCurlTest.php b/tests/Polyfills/AssertClosedResourceCurlTest.php index f2190f8..38e598e 100644 --- a/tests/Polyfills/AssertClosedResourceCurlTest.php +++ b/tests/Polyfills/AssertClosedResourceCurlTest.php @@ -2,6 +2,8 @@ namespace Yoast\PHPUnitPolyfills\Tests\Polyfills; +use PHPUnit\Framework\Attributes\CoversClass; +use Yoast\PHPUnitPolyfills\Helpers\ResourceHelper; use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource; /** @@ -19,6 +21,8 @@ * @requires extension curl * @requires PHP < 8.0 */ +#[CoversClass( AssertClosedResource::class )] +#[CoversClass( ResourceHelper::class )] final class AssertClosedResourceCurlTest extends AssertClosedResourceTestCase { use AssertClosedResource; diff --git a/tests/Polyfills/AssertClosedResourceDirTest.php b/tests/Polyfills/AssertClosedResourceDirTest.php index e14cf29..fb25e12 100644 --- a/tests/Polyfills/AssertClosedResourceDirTest.php +++ b/tests/Polyfills/AssertClosedResourceDirTest.php @@ -2,6 +2,8 @@ namespace Yoast\PHPUnitPolyfills\Tests\Polyfills; +use PHPUnit\Framework\Attributes\CoversClass; +use Yoast\PHPUnitPolyfills\Helpers\ResourceHelper; use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource; /** @@ -13,6 +15,8 @@ * @covers \Yoast\PHPUnitPolyfills\Helpers\ResourceHelper * @covers \Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource */ +#[CoversClass( AssertClosedResource::class )] +#[CoversClass( ResourceHelper::class )] final class AssertClosedResourceDirTest extends AssertClosedResourceTestCase { use AssertClosedResource; diff --git a/tests/Polyfills/AssertClosedResourceEnchantTest.php b/tests/Polyfills/AssertClosedResourceEnchantTest.php index 9c05df2..39d03d6 100644 --- a/tests/Polyfills/AssertClosedResourceEnchantTest.php +++ b/tests/Polyfills/AssertClosedResourceEnchantTest.php @@ -2,6 +2,8 @@ namespace Yoast\PHPUnitPolyfills\Tests\Polyfills; +use PHPUnit\Framework\Attributes\CoversClass; +use Yoast\PHPUnitPolyfills\Helpers\ResourceHelper; use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource; /** @@ -22,6 +24,8 @@ * @phpcs:disable Generic.PHP.DeprecatedFunctions.Deprecated * @phpcs:disable PHPCompatibility.FunctionUse.RemovedFunctions.enchant_broker_freeDeprecated */ +#[CoversClass( AssertClosedResource::class )] +#[CoversClass( ResourceHelper::class )] final class AssertClosedResourceEnchantTest extends AssertClosedResourceTestCase { use AssertClosedResource; diff --git a/tests/Polyfills/AssertClosedResourceFileTest.php b/tests/Polyfills/AssertClosedResourceFileTest.php index d259336..c9f4c6e 100644 --- a/tests/Polyfills/AssertClosedResourceFileTest.php +++ b/tests/Polyfills/AssertClosedResourceFileTest.php @@ -2,6 +2,8 @@ namespace Yoast\PHPUnitPolyfills\Tests\Polyfills; +use PHPUnit\Framework\Attributes\CoversClass; +use Yoast\PHPUnitPolyfills\Helpers\ResourceHelper; use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource; /** @@ -13,6 +15,8 @@ * @covers \Yoast\PHPUnitPolyfills\Helpers\ResourceHelper * @covers \Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource */ +#[CoversClass( AssertClosedResource::class )] +#[CoversClass( ResourceHelper::class )] final class AssertClosedResourceFileTest extends AssertClosedResourceTestCase { use AssertClosedResource; diff --git a/tests/Polyfills/AssertClosedResourceFinfoTest.php b/tests/Polyfills/AssertClosedResourceFinfoTest.php index 1f7489e..50801e0 100644 --- a/tests/Polyfills/AssertClosedResourceFinfoTest.php +++ b/tests/Polyfills/AssertClosedResourceFinfoTest.php @@ -2,6 +2,8 @@ namespace Yoast\PHPUnitPolyfills\Tests\Polyfills; +use PHPUnit\Framework\Attributes\CoversClass; +use Yoast\PHPUnitPolyfills\Helpers\ResourceHelper; use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource; /** @@ -19,6 +21,8 @@ * @requires extension finfo * @requires PHP < 8.1 */ +#[CoversClass( AssertClosedResource::class )] +#[CoversClass( ResourceHelper::class )] final class AssertClosedResourceFinfoTest extends AssertClosedResourceTestCase { use AssertClosedResource; diff --git a/tests/Polyfills/AssertClosedResourceGdTest.php b/tests/Polyfills/AssertClosedResourceGdTest.php index 30758c3..9048260 100644 --- a/tests/Polyfills/AssertClosedResourceGdTest.php +++ b/tests/Polyfills/AssertClosedResourceGdTest.php @@ -2,6 +2,8 @@ namespace Yoast\PHPUnitPolyfills\Tests\Polyfills; +use PHPUnit\Framework\Attributes\CoversClass; +use Yoast\PHPUnitPolyfills\Helpers\ResourceHelper; use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource; /** @@ -19,6 +21,8 @@ * @requires extension gd * @requires PHP < 8.0 */ +#[CoversClass( AssertClosedResource::class )] +#[CoversClass( ResourceHelper::class )] final class AssertClosedResourceGdTest extends AssertClosedResourceTestCase { use AssertClosedResource; diff --git a/tests/Polyfills/AssertClosedResourceNotResourceTest.php b/tests/Polyfills/AssertClosedResourceNotResourceTest.php index 7b7779c..a3c4b53 100644 --- a/tests/Polyfills/AssertClosedResourceNotResourceTest.php +++ b/tests/Polyfills/AssertClosedResourceNotResourceTest.php @@ -3,9 +3,11 @@ namespace Yoast\PHPUnitPolyfills\Tests\Polyfills; use PHPUnit\Framework\AssertionFailedError; +use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; use PHPUnit_Framework_AssertionFailedError; use stdClass; +use Yoast\PHPUnitPolyfills\Helpers\ResourceHelper; use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource; use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionMessageMatches; @@ -15,6 +17,8 @@ * @covers \Yoast\PHPUnitPolyfills\Helpers\ResourceHelper * @covers \Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource */ +#[CoversClass( AssertClosedResource::class )] +#[CoversClass( ResourceHelper::class )] final class AssertClosedResourceNotResourceTest extends TestCase { use AssertClosedResource; diff --git a/tests/Polyfills/AssertClosedResourceProcessTest.php b/tests/Polyfills/AssertClosedResourceProcessTest.php index 3af50c3..21481dc 100644 --- a/tests/Polyfills/AssertClosedResourceProcessTest.php +++ b/tests/Polyfills/AssertClosedResourceProcessTest.php @@ -2,6 +2,8 @@ namespace Yoast\PHPUnitPolyfills\Tests\Polyfills; +use PHPUnit\Framework\Attributes\CoversClass; +use Yoast\PHPUnitPolyfills\Helpers\ResourceHelper; use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource; /** @@ -13,6 +15,8 @@ * @covers \Yoast\PHPUnitPolyfills\Helpers\ResourceHelper * @covers \Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource */ +#[CoversClass( AssertClosedResource::class )] +#[CoversClass( ResourceHelper::class )] final class AssertClosedResourceProcessTest extends AssertClosedResourceTestCase { use AssertClosedResource; diff --git a/tests/Polyfills/AssertClosedResourceShmopTest.php b/tests/Polyfills/AssertClosedResourceShmopTest.php index d4bb37f..f19d0f5 100644 --- a/tests/Polyfills/AssertClosedResourceShmopTest.php +++ b/tests/Polyfills/AssertClosedResourceShmopTest.php @@ -2,6 +2,8 @@ namespace Yoast\PHPUnitPolyfills\Tests\Polyfills; +use PHPUnit\Framework\Attributes\CoversClass; +use Yoast\PHPUnitPolyfills\Helpers\ResourceHelper; use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource; /** @@ -24,6 +26,8 @@ * @phpcs:disable Generic.PHP.DeprecatedFunctions.Deprecated * @phpcs:disable PHPCompatibility.FunctionUse.RemovedFunctions.shmop_closeDeprecated */ +#[CoversClass( AssertClosedResource::class )] +#[CoversClass( ResourceHelper::class )] final class AssertClosedResourceShmopTest extends AssertClosedResourceTestCase { use AssertClosedResource; diff --git a/tests/Polyfills/AssertClosedResourceWddxTest.php b/tests/Polyfills/AssertClosedResourceWddxTest.php index a2598d9..faf607b 100644 --- a/tests/Polyfills/AssertClosedResourceWddxTest.php +++ b/tests/Polyfills/AssertClosedResourceWddxTest.php @@ -2,6 +2,8 @@ namespace Yoast\PHPUnitPolyfills\Tests\Polyfills; +use PHPUnit\Framework\Attributes\CoversClass; +use Yoast\PHPUnitPolyfills\Helpers\ResourceHelper; use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource; /** @@ -22,6 +24,8 @@ * @phpcs:disable PHPCompatibility.FunctionUse.RemovedFunctions.wddx_packet_startRemoved * @phpcs:disable PHPCompatibility.FunctionUse.RemovedFunctions.wddx_packet_endRemoved */ +#[CoversClass( AssertClosedResource::class )] +#[CoversClass( ResourceHelper::class )] final class AssertClosedResourceWddxTest extends AssertClosedResourceTestCase { use AssertClosedResource; diff --git a/tests/Polyfills/AssertClosedResourceXmlParserTest.php b/tests/Polyfills/AssertClosedResourceXmlParserTest.php index 5159e42..31143a9 100644 --- a/tests/Polyfills/AssertClosedResourceXmlParserTest.php +++ b/tests/Polyfills/AssertClosedResourceXmlParserTest.php @@ -2,6 +2,7 @@ namespace Yoast\PHPUnitPolyfills\Tests\Polyfills; +use PHPUnit\Framework\Attributes\CoversClass; use Yoast\PHPUnitPolyfills\Helpers\ResourceHelper; use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource; @@ -20,6 +21,8 @@ * @requires extension libxml * @requires PHP < 8.0 */ +#[CoversClass( AssertClosedResource::class )] +#[CoversClass( ResourceHelper::class )] final class AssertClosedResourceXmlParserTest extends AssertClosedResourceTestCase { use AssertClosedResource; diff --git a/tests/Polyfills/AssertClosedResourceZipTest.php b/tests/Polyfills/AssertClosedResourceZipTest.php index 85defd5..a17a051 100644 --- a/tests/Polyfills/AssertClosedResourceZipTest.php +++ b/tests/Polyfills/AssertClosedResourceZipTest.php @@ -2,6 +2,8 @@ namespace Yoast\PHPUnitPolyfills\Tests\Polyfills; +use PHPUnit\Framework\Attributes\CoversClass; +use Yoast\PHPUnitPolyfills\Helpers\ResourceHelper; use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource; /** @@ -23,6 +25,8 @@ * @phpcs:disable PHPCompatibility.FunctionUse.RemovedFunctions.zip_closeDeprecated * @phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged */ +#[CoversClass( AssertClosedResource::class )] +#[CoversClass( ResourceHelper::class )] final class AssertClosedResourceZipTest extends AssertClosedResourceTestCase { use AssertClosedResource; diff --git a/tests/Polyfills/AssertClosedResourceZlibTest.php b/tests/Polyfills/AssertClosedResourceZlibTest.php index e7709af..b91d441 100644 --- a/tests/Polyfills/AssertClosedResourceZlibTest.php +++ b/tests/Polyfills/AssertClosedResourceZlibTest.php @@ -2,6 +2,8 @@ namespace Yoast\PHPUnitPolyfills\Tests\Polyfills; +use PHPUnit\Framework\Attributes\CoversClass; +use Yoast\PHPUnitPolyfills\Helpers\ResourceHelper; use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource; /** @@ -15,6 +17,8 @@ * * @requires extension zlib */ +#[CoversClass( AssertClosedResource::class )] +#[CoversClass( ResourceHelper::class )] final class AssertClosedResourceZlibTest extends AssertClosedResourceTestCase { use AssertClosedResource; diff --git a/tests/Polyfills/AssertEqualsSpecializationsTest.php b/tests/Polyfills/AssertEqualsSpecializationsTest.php index 4b06361..67fe8d7 100644 --- a/tests/Polyfills/AssertEqualsSpecializationsTest.php +++ b/tests/Polyfills/AssertEqualsSpecializationsTest.php @@ -2,6 +2,7 @@ namespace Yoast\PHPUnitPolyfills\Tests\Polyfills; +use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; use Yoast\PHPUnitPolyfills\Polyfills\AssertEqualsSpecializations; @@ -10,6 +11,7 @@ * * @covers \Yoast\PHPUnitPolyfills\Polyfills\AssertEqualsSpecializations */ +#[CoversClass( AssertEqualsSpecializations::class )] final class AssertEqualsSpecializationsTest extends TestCase { use AssertEqualsSpecializations; diff --git a/tests/Polyfills/AssertFileEqualsSpecializationsTest.php b/tests/Polyfills/AssertFileEqualsSpecializationsTest.php index a511d05..3b2004c 100644 --- a/tests/Polyfills/AssertFileEqualsSpecializationsTest.php +++ b/tests/Polyfills/AssertFileEqualsSpecializationsTest.php @@ -2,6 +2,7 @@ namespace Yoast\PHPUnitPolyfills\Tests\Polyfills; +use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; use Yoast\PHPUnitPolyfills\Polyfills\AssertFileEqualsSpecializations; @@ -10,6 +11,7 @@ * * @covers \Yoast\PHPUnitPolyfills\Polyfills\AssertFileEqualsSpecializations */ +#[CoversClass( AssertFileEqualsSpecializations::class )] final class AssertFileEqualsSpecializationsTest extends TestCase { use AssertFileEqualsSpecializations; diff --git a/tests/Polyfills/AssertIgnoringLineEndingsTest.php b/tests/Polyfills/AssertIgnoringLineEndingsTest.php index ab4fe15..e226eae 100644 --- a/tests/Polyfills/AssertIgnoringLineEndingsTest.php +++ b/tests/Polyfills/AssertIgnoringLineEndingsTest.php @@ -3,6 +3,7 @@ namespace Yoast\PHPUnitPolyfills\Tests\Polyfills; use PHPUnit\Framework\AssertionFailedError; +use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; use PHPUnit\Runner\Version as PHPUnit_Version; use PHPUnit\SebastianBergmann\Exporter\Exporter as Exporter_In_Phar_Old; @@ -20,6 +21,7 @@ * * @covers \Yoast\PHPUnitPolyfills\Polyfills\AssertIgnoringLineEndings */ +#[CoversClass( AssertIgnoringLineEndings::class )] final class AssertIgnoringLineEndingsTest extends TestCase { use AssertIgnoringLineEndings; diff --git a/tests/Polyfills/AssertIsListTest.php b/tests/Polyfills/AssertIsListTest.php index 56d44fa..16d1174 100644 --- a/tests/Polyfills/AssertIsListTest.php +++ b/tests/Polyfills/AssertIsListTest.php @@ -3,6 +3,7 @@ namespace Yoast\PHPUnitPolyfills\Tests\Polyfills; use PHPUnit\Framework\AssertionFailedError; +use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; use PHPUnit_Framework_AssertionFailedError; use stdClass; @@ -14,6 +15,7 @@ * * @covers \Yoast\PHPUnitPolyfills\Polyfills\AssertIsList */ +#[CoversClass( AssertIsList::class )] final class AssertIsListTest extends TestCase { use AssertIsList; diff --git a/tests/Polyfills/AssertIsTypeTest.php b/tests/Polyfills/AssertIsTypeTest.php index b7b7545..ddd8f95 100644 --- a/tests/Polyfills/AssertIsTypeTest.php +++ b/tests/Polyfills/AssertIsTypeTest.php @@ -2,6 +2,7 @@ namespace Yoast\PHPUnitPolyfills\Tests\Polyfills; +use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; use stdClass; use Yoast\PHPUnitPolyfills\Polyfills\AssertIsType; @@ -11,6 +12,7 @@ * * @covers \Yoast\PHPUnitPolyfills\Polyfills\AssertIsType */ +#[CoversClass( AssertIsType::class )] final class AssertIsTypeTest extends TestCase { use AssertIsType; diff --git a/tests/Polyfills/AssertObjectEqualsPHPUnitLt940Test.php b/tests/Polyfills/AssertObjectEqualsPHPUnitLt940Test.php index 055dbdf..55544ad 100644 --- a/tests/Polyfills/AssertObjectEqualsPHPUnitLt940Test.php +++ b/tests/Polyfills/AssertObjectEqualsPHPUnitLt940Test.php @@ -3,6 +3,7 @@ namespace Yoast\PHPUnitPolyfills\Tests\Polyfills; use PHPUnit\Framework\AssertionFailedError; +use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; use PHPUnit\Runner\Version as PHPUnit_Version; use PHPUnit_Framework_AssertionFailedError; @@ -30,6 +31,7 @@ * * @covers \Yoast\PHPUnitPolyfills\Polyfills\AssertObjectEquals */ +#[CoversClass( AssertObjectEquals::class )] final class AssertObjectEqualsPHPUnitLt940Test extends TestCase { use AssertObjectEquals; diff --git a/tests/Polyfills/AssertObjectEqualsTest.php b/tests/Polyfills/AssertObjectEqualsTest.php index 4540c48..37abbda 100644 --- a/tests/Polyfills/AssertObjectEqualsTest.php +++ b/tests/Polyfills/AssertObjectEqualsTest.php @@ -3,6 +3,7 @@ namespace Yoast\PHPUnitPolyfills\Tests\Polyfills; use PHPUnit\Framework\AssertionFailedError; +use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\ComparisonMethodDoesNotAcceptParameterTypeException; use PHPUnit\Framework\ComparisonMethodDoesNotDeclareBoolReturnTypeException; use PHPUnit\Framework\ComparisonMethodDoesNotDeclareExactlyOneParameterException; @@ -33,6 +34,7 @@ * * @requires PHP 7.0 */ +#[CoversClass( AssertObjectEquals::class )] final class AssertObjectEqualsTest extends TestCase { use AssertObjectEquals; diff --git a/tests/Polyfills/AssertObjectPropertyTest.php b/tests/Polyfills/AssertObjectPropertyTest.php index 8dcedc4..364afa8 100644 --- a/tests/Polyfills/AssertObjectPropertyTest.php +++ b/tests/Polyfills/AssertObjectPropertyTest.php @@ -3,6 +3,7 @@ namespace Yoast\PHPUnitPolyfills\Tests\Polyfills; use PHPUnit\Framework\AssertionFailedError; +use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; use PHPUnit\Runner\Version as PHPUnit_Version; use PHPUnit_Framework_AssertionFailedError; @@ -19,6 +20,7 @@ * * @covers \Yoast\PHPUnitPolyfills\Polyfills\AssertObjectProperty */ +#[CoversClass( AssertObjectProperty::class )] final class AssertObjectPropertyTest extends TestCase { use AssertObjectProperty; diff --git a/tests/Polyfills/AssertStringContainsTest.php b/tests/Polyfills/AssertStringContainsTest.php index 6602749..6ca8ef8 100644 --- a/tests/Polyfills/AssertStringContainsTest.php +++ b/tests/Polyfills/AssertStringContainsTest.php @@ -3,6 +3,7 @@ namespace Yoast\PHPUnitPolyfills\Tests\Polyfills; use PHPUnit\Framework\AssertionFailedError; +use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; use PHPUnit_Framework_AssertionFailedError; use Yoast\PHPUnitPolyfills\Polyfills\AssertStringContains; @@ -13,6 +14,7 @@ * * @covers \Yoast\PHPUnitPolyfills\Polyfills\AssertStringContains */ +#[CoversClass( AssertStringContains::class )] final class AssertStringContainsTest extends TestCase { use AssertStringContains; diff --git a/tests/Polyfills/AssertionRenamesTest.php b/tests/Polyfills/AssertionRenamesTest.php index e4baed3..a9129b0 100644 --- a/tests/Polyfills/AssertionRenamesTest.php +++ b/tests/Polyfills/AssertionRenamesTest.php @@ -2,6 +2,7 @@ namespace Yoast\PHPUnitPolyfills\Tests\Polyfills; +use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; use Yoast\PHPUnitPolyfills\Polyfills\AssertionRenames; use Yoast\PHPUnitPolyfills\Polyfills\AssertIsType; @@ -11,6 +12,7 @@ * * @covers \Yoast\PHPUnitPolyfills\Polyfills\AssertionRenames */ +#[CoversClass( AssertionRenames::class )] final class AssertionRenamesTest extends TestCase { use AssertionRenames; diff --git a/tests/Polyfills/EqualToSpecializationsTest.php b/tests/Polyfills/EqualToSpecializationsTest.php index 9ba001b..7fc99ec 100644 --- a/tests/Polyfills/EqualToSpecializationsTest.php +++ b/tests/Polyfills/EqualToSpecializationsTest.php @@ -2,6 +2,7 @@ namespace Yoast\PHPUnitPolyfills\Tests\Polyfills; +use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; use Yoast\PHPUnitPolyfills\Polyfills\EqualToSpecializations; @@ -10,6 +11,7 @@ * * @covers \Yoast\PHPUnitPolyfills\Polyfills\EqualToSpecializations */ +#[CoversClass( EqualToSpecializations::class )] final class EqualToSpecializationsTest extends TestCase { use EqualToSpecializations; diff --git a/tests/Polyfills/ExpectExceptionMessageMatchesTest.php b/tests/Polyfills/ExpectExceptionMessageMatchesTest.php index c6cf0ba..47ffa34 100644 --- a/tests/Polyfills/ExpectExceptionMessageMatchesTest.php +++ b/tests/Polyfills/ExpectExceptionMessageMatchesTest.php @@ -3,6 +3,7 @@ namespace Yoast\PHPUnitPolyfills\Tests\Polyfills; use Exception; +use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionMessageMatches; @@ -11,6 +12,7 @@ * * @covers \Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionMessageMatches */ +#[CoversClass( ExpectExceptionMessageMatches::class )] final class ExpectExceptionMessageMatchesTest extends TestCase { use ExpectExceptionMessageMatches; diff --git a/tests/Polyfills/ExpectExceptionObjectTest.php b/tests/Polyfills/ExpectExceptionObjectTest.php index 888b516..3d23c52 100644 --- a/tests/Polyfills/ExpectExceptionObjectTest.php +++ b/tests/Polyfills/ExpectExceptionObjectTest.php @@ -3,6 +3,7 @@ namespace Yoast\PHPUnitPolyfills\Tests\Polyfills; use Exception; +use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionObject; @@ -11,6 +12,7 @@ * * @covers \Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionObject */ +#[CoversClass( ExpectExceptionObject::class )] final class ExpectExceptionObjectTest extends TestCase { use ExpectExceptionObject; diff --git a/tests/TestCases/TestCaseTest.php b/tests/TestCases/TestCaseTest.php index 1792cec..d7ac30b 100644 --- a/tests/TestCases/TestCaseTest.php +++ b/tests/TestCases/TestCaseTest.php @@ -2,6 +2,7 @@ namespace Yoast\PHPUnitPolyfills\Tests\TestCases; +use PHPUnit\Framework\Attributes\CoversClass; use Yoast\PHPUnitPolyfills\TestCases\TestCase; /** @@ -9,6 +10,7 @@ * * @covers \Yoast\PHPUnitPolyfills\TestCases\TestCase */ +#[CoversClass( TestCase::class )] final class TestCaseTest extends TestCase { use TestCaseTestTrait; diff --git a/tests/TestCases/XTestCaseTest.php b/tests/TestCases/XTestCaseTest.php index 18afe92..099572b 100644 --- a/tests/TestCases/XTestCaseTest.php +++ b/tests/TestCases/XTestCaseTest.php @@ -2,6 +2,7 @@ namespace Yoast\PHPUnitPolyfills\Tests\TestCases; +use PHPUnit\Framework\Attributes\CoversClass; use Yoast\PHPUnitPolyfills\TestCases\XTestCase; /** @@ -9,6 +10,7 @@ * * @covers \Yoast\PHPUnitPolyfills\TestCases\XTestCase */ +#[CoversClass( XTestCase::class )] final class XTestCaseTest extends XTestCase { use TestCaseTestTrait; diff --git a/tests/TestListeners/Fixtures/TestErrorPHPUnitGte7.php b/tests/TestListeners/Fixtures/TestErrorPHPUnitGte7.php index b114f4f..a20d292 100644 --- a/tests/TestListeners/Fixtures/TestErrorPHPUnitGte7.php +++ b/tests/TestListeners/Fixtures/TestErrorPHPUnitGte7.php @@ -3,6 +3,7 @@ namespace Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures; use Exception; +use PHPUnit\Framework\Attributes\CoversNothing; use PHPUnit\Framework\TestCase; /** @@ -12,6 +13,7 @@ * * @coversNothing */ +#[CoversNothing] class TestErrorPHPUnitGte7 extends TestCase { /** diff --git a/tests/TestListeners/TestListenerTest.php b/tests/TestListeners/TestListenerTest.php index 9f95f63..5417688 100644 --- a/tests/TestListeners/TestListenerTest.php +++ b/tests/TestListeners/TestListenerTest.php @@ -2,10 +2,13 @@ namespace Yoast\PHPUnitPolyfills\Tests\TestListeners; +use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase as PHPUnitTestCase; use PHPUnit\Framework\TestResult; use Yoast\PHPUnitPolyfills\Autoload; use Yoast\PHPUnitPolyfills\TestCases\TestCase; +use Yoast\PHPUnitPolyfills\TestListeners\TestListenerDefaultImplementation; +use Yoast\PHPUnitPolyfills\TestListeners\TestListenerSnakeCaseMethods; use Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures\TestListenerImplementation; /** @@ -16,6 +19,8 @@ * * @requires PHPUnit < 10 */ +#[CoversClass( TestListenerDefaultImplementation::class )] +#[CoversClass( TestListenerSnakeCaseMethods::class )] final class TestListenerTest extends TestCase { /** From 78464dce44ce93e0d70cea67fce99a96ac765abd Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 25 Feb 2024 10:14:24 +0100 Subject: [PATCH 2/4] Tests: add `DataProvider` attributes 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 `DataProvider` attributes in all the appropriate places. The `@dataProvider` 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. --- .../Polyfills/AssertClosedResourceNotResourceTest.php | 4 ++++ tests/Polyfills/AssertIgnoringLineEndingsTest.php | 11 +++++++++++ tests/Polyfills/AssertIsListTest.php | 4 ++++ tests/Polyfills/AssertObjectPropertyTest.php | 9 +++++++++ tests/Polyfills/AssertStringContainsTest.php | 3 +++ tests/TestCases/TestCaseTest.php | 2 ++ tests/TestCases/XTestCaseTest.php | 2 ++ 7 files changed, 35 insertions(+) diff --git a/tests/Polyfills/AssertClosedResourceNotResourceTest.php b/tests/Polyfills/AssertClosedResourceNotResourceTest.php index a3c4b53..722a230 100644 --- a/tests/Polyfills/AssertClosedResourceNotResourceTest.php +++ b/tests/Polyfills/AssertClosedResourceNotResourceTest.php @@ -4,6 +4,7 @@ use PHPUnit\Framework\AssertionFailedError; use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use PHPUnit_Framework_AssertionFailedError; use stdClass; @@ -34,6 +35,7 @@ final class AssertClosedResourceNotResourceTest extends TestCase { * * @return void */ + #[DataProvider( 'dataNotResource' )] public function testAssertIsClosedResource( $value ) { $pattern = '`^Failed asserting that .+? is of type ["]?resource \(closed\)["]?`s'; @@ -68,6 +70,7 @@ public function testAssertIsClosedResourceFailsWithCustomMessage() { * * @return void */ + #[DataProvider( 'dataNotResource' )] public function testAssertIsNotClosedResource( $value ) { self::assertIsNotClosedResource( $value ); } @@ -99,6 +102,7 @@ public function testAssertIsNotClosedResourceFailsWithCustomMessage() { * * @return void */ + #[DataProvider( 'dataNotResource' )] public function testShouldClosedResourceAssertionBeSkipped( $value ) { $this->assertFalse( self::shouldClosedResourceAssertionBeSkipped( $value ) ); } diff --git a/tests/Polyfills/AssertIgnoringLineEndingsTest.php b/tests/Polyfills/AssertIgnoringLineEndingsTest.php index e226eae..8f45b06 100644 --- a/tests/Polyfills/AssertIgnoringLineEndingsTest.php +++ b/tests/Polyfills/AssertIgnoringLineEndingsTest.php @@ -4,6 +4,7 @@ use PHPUnit\Framework\AssertionFailedError; use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use PHPUnit\Runner\Version as PHPUnit_Version; use PHPUnit\SebastianBergmann\Exporter\Exporter as Exporter_In_Phar_Old; @@ -38,6 +39,7 @@ final class AssertIgnoringLineEndingsTest extends TestCase { * * @return void */ + #[DataProvider( 'dataThrowsTypeErrorOnInvalidType' )] public function testAssertStringEqualsStringIgnoringLineEndingsThrowsTypeErrorOnInvalidTypeArg1( $input ) { if ( \PHP_VERSION_ID >= 80100 && \version_compare( PHPUnit_Version::id(), '10.0.0', '>=' ) @@ -65,6 +67,7 @@ public function testAssertStringEqualsStringIgnoringLineEndingsThrowsTypeErrorOn * * @return void */ + #[DataProvider( 'dataThrowsTypeErrorOnInvalidType' )] public function testAssertStringEqualsStringIgnoringLineEndingsThrowsTypeErrorOnInvalidTypeArg2( $input ) { if ( \PHP_VERSION_ID >= 80100 && \version_compare( PHPUnit_Version::id(), '10.0.0', '>=' ) @@ -93,6 +96,8 @@ public function testAssertStringEqualsStringIgnoringLineEndingsThrowsTypeErrorOn * * @return void */ + #[DataProvider( 'dataAllLineEndingVariations' )] + #[DataProvider( 'dataAssertStringEqualsStringIgnoringLineEndingsTypeVariations' )] public function testAssertStringEqualsStringIgnoringLineEndings( $expected, $actual ) { self::assertStringEqualsStringIgnoringLineEndings( $expected, $actual ); } @@ -141,6 +146,7 @@ public static function dataAssertStringEqualsStringIgnoringLineEndingsTypeVariat * * @return void */ + #[DataProvider( 'dataAssertStringEqualsStringIgnoringLineEndingsFails' )] public function testAssertStringEqualsStringIgnoringLineEndingsFails( $expected, $actual ) { $exporter = self::getPHPUnitExporterObjectForIgnoringLineEndingsForTests(); @@ -207,6 +213,7 @@ public function testAssertStringEqualsStringIgnoringLineEndingsFailsWithCustomMe * * @return void */ + #[DataProvider( 'dataThrowsTypeErrorOnInvalidType' )] public function testAssertStringContainsStringIgnoringLineEndingsThrowsTypeErrorOnInvalidTypeArg1( $input ) { if ( \PHP_VERSION_ID >= 80100 && \version_compare( PHPUnit_Version::id(), '10.0.0', '>=' ) @@ -234,6 +241,7 @@ public function testAssertStringContainsStringIgnoringLineEndingsThrowsTypeError * * @return void */ + #[DataProvider( 'dataThrowsTypeErrorOnInvalidType' )] public function testAssertStringContainsStringIgnoringLineEndingsThrowsTypeErrorOnInvalidTypeArg2( $input ) { if ( \PHP_VERSION_ID >= 80100 && \version_compare( PHPUnit_Version::id(), '10.0.0', '>=' ) @@ -261,6 +269,7 @@ public function testAssertStringContainsStringIgnoringLineEndingsThrowsTypeError * * @return void */ + #[DataProvider( 'dataAssertStringContainsStringIgnoringLineEndings' )] public function testAssertStringContainsStringIgnoringLineEndings( $needle, $haystack ) { $this->assertStringContainsStringIgnoringLineEndings( $needle, $haystack ); } @@ -296,6 +305,7 @@ public static function dataAssertStringContainsStringIgnoringLineEndings() { * * @return void */ + #[DataProvider( 'dataAllLineEndingVariations' )] public function testAssertStringContainsStringIgnoringLineEndingsBug5279( $needle, $haystack ) { if ( \version_compare( PHPUnit_Version::id(), '10.0.0', '>=' ) && \version_compare( PHPUnit_Version::id(), '10.0.16', '<' ) @@ -318,6 +328,7 @@ public function testAssertStringContainsStringIgnoringLineEndingsBug5279( $needl * * @return void */ + #[DataProvider( 'dataAssertStringContainsStringIgnoringLineEndingsFails' )] public function testAssertStringContainsStringIgnoringLineEndingsFails( $needle, $haystack ) { $exporter = self::getPHPUnitExporterObjectForIgnoringLineEndingsForTests(); $pattern = \sprintf( diff --git a/tests/Polyfills/AssertIsListTest.php b/tests/Polyfills/AssertIsListTest.php index 16d1174..4f2516f 100644 --- a/tests/Polyfills/AssertIsListTest.php +++ b/tests/Polyfills/AssertIsListTest.php @@ -4,6 +4,7 @@ use PHPUnit\Framework\AssertionFailedError; use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use PHPUnit_Framework_AssertionFailedError; use stdClass; @@ -31,6 +32,7 @@ final class AssertIsListTest extends TestCase { * * @return void */ + #[DataProvider( 'dataAssertIsListFailsOnInvalidInputType' )] public function testAssertIsListFailsOnInvalidInputType( $actual, $type ) { $this->expectException( $this->getAssertionFailedExceptionName() ); $this->expectExceptionMessageMatches( '`^Failed asserting that ' . $type . ' is a list`' ); @@ -89,6 +91,7 @@ public static function dataAssertIsListFailsOnInvalidInputType() { * * @return void */ + #[DataProvider( 'dataAssertIsListPass' )] public function testAssertIsListPass( $actual ) { $this->assertIsList( $actual ); } @@ -128,6 +131,7 @@ public static function dataAssertIsListPass() { * * @return void */ + #[DataProvider( 'dataAssertIsListFail' )] public function testAssertIsListFail( $actual ) { $this->expectException( $this->getAssertionFailedExceptionName() ); $this->expectExceptionMessage( 'Failed asserting that an array is a list' ); diff --git a/tests/Polyfills/AssertObjectPropertyTest.php b/tests/Polyfills/AssertObjectPropertyTest.php index 364afa8..8ccc2f0 100644 --- a/tests/Polyfills/AssertObjectPropertyTest.php +++ b/tests/Polyfills/AssertObjectPropertyTest.php @@ -4,6 +4,7 @@ use PHPUnit\Framework\AssertionFailedError; use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use PHPUnit\Runner\Version as PHPUnit_Version; use PHPUnit_Framework_AssertionFailedError; @@ -47,6 +48,7 @@ private function usesNativePHPUnitAssertion() { * * @return void */ + #[DataProvider( 'dataAssertObjectPropertyFailsOnInvalidInputTypePropertyName' )] public function testAssertObjectHasPropertyFailsOnInvalidInputTypePropertyName( $input ) { if ( \is_scalar( $input ) && $this->usesNativePHPUnitAssertion() ) { $this->markTestSkipped( 'PHPUnit native implementation relies on strict_types and when not used will accept scalar inputs' ); @@ -76,6 +78,7 @@ public function testAssertObjectHasPropertyFailsOnInvalidInputTypePropertyName( * * @return void */ + #[DataProvider( 'dataAssertObjectPropertyFailsOnInvalidInputTypePropertyName' )] public function testAssertObjectNotHasPropertyFailsOnInvalidInputTypePropertyName( $input ) { if ( \is_scalar( $input ) && $this->usesNativePHPUnitAssertion() ) { $this->markTestSkipped( 'PHPUnit native implementation relies on strict_types and when not used will accept scalar inputs' ); @@ -126,6 +129,7 @@ public static function dataAssertObjectPropertyFailsOnInvalidInputTypePropertyNa * * @return void */ + #[DataProvider( 'dataAssertObjectPropertyFailsOnInvalidInputTypeObject' )] public function testAssertObjectHasPropertyFailsOnInvalidInputTypeObject( $input ) { $this->expectException( TypeError::class ); @@ -151,6 +155,7 @@ public function testAssertObjectHasPropertyFailsOnInvalidInputTypeObject( $input * * @return void */ + #[DataProvider( 'dataAssertObjectPropertyFailsOnInvalidInputTypeObject' )] public function testAssertObjectNotHasPropertyFailsOnInvalidInputTypeObject( $input ) { $this->expectException( TypeError::class ); @@ -197,6 +202,7 @@ public static function dataAssertObjectPropertyFailsOnInvalidInputTypeObject() { * * @return void */ + #[DataProvider( 'dataAssertObjectPropertyDeclaredProps' )] public function testAssertObjectHasPropertyPass( $name ) { $this->assertObjectHasProperty( $name, new ObjectWithProperties() ); } @@ -210,6 +216,7 @@ public function testAssertObjectHasPropertyPass( $name ) { * * @return void */ + #[DataProvider( 'dataAssertObjectPropertyUnavailableProps' )] public function testAssertObjectNotHasPropertyPass( $name ) { self::assertObjectNotHasProperty( $name, new ObjectWithProperties() ); } @@ -223,6 +230,7 @@ public function testAssertObjectNotHasPropertyPass( $name ) { * * @return void */ + #[DataProvider( 'dataAssertObjectPropertyUnavailableProps' )] public function testAssertObjectHasPropertyFails( $name ) { $pattern = \sprintf( '`^Failed asserting that object of class "[^\s]*ObjectWithProperties" has (?:property|attribute) "%s"\.`', @@ -244,6 +252,7 @@ public function testAssertObjectHasPropertyFails( $name ) { * * @return void */ + #[DataProvider( 'dataAssertObjectPropertyDeclaredProps' )] public function testAssertObjectNotHasPropertyFails( $name ) { $pattern = \sprintf( '`^Failed asserting that object of class "[^\s]*ObjectWithProperties" does not have (?:property|attribute) "%s"\.`', diff --git a/tests/Polyfills/AssertStringContainsTest.php b/tests/Polyfills/AssertStringContainsTest.php index 6ca8ef8..5cf7c14 100644 --- a/tests/Polyfills/AssertStringContainsTest.php +++ b/tests/Polyfills/AssertStringContainsTest.php @@ -4,6 +4,7 @@ use PHPUnit\Framework\AssertionFailedError; use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use PHPUnit_Framework_AssertionFailedError; use Yoast\PHPUnitPolyfills\Polyfills\AssertStringContains; @@ -71,6 +72,7 @@ public function testAssertStringNotContainsStringIgnoringCase() { * * @return void */ + #[DataProvider( 'dataHaystacks' )] public function testAssertStringContainsStringEmptyNeedle( $haystack ) { $this->assertStringContainsString( '', $haystack ); } @@ -105,6 +107,7 @@ public function testAssertStringContainsStringIgnoringCaseEmptyNeedle() { * * @return void */ + #[DataProvider( 'dataHaystacks' )] public function testAssertStringNotContainsStringEmptyNeedle( $haystack ) { $pattern = "`^Failed asserting that '{$haystack}'( \[[^\]]+\]\(length: [0-9]+\))? does not contain \"\"( \[[^\]]+\]\(length: [0-9]+\))?\.`"; diff --git a/tests/TestCases/TestCaseTest.php b/tests/TestCases/TestCaseTest.php index d7ac30b..c38759e 100644 --- a/tests/TestCases/TestCaseTest.php +++ b/tests/TestCases/TestCaseTest.php @@ -3,6 +3,7 @@ namespace Yoast\PHPUnitPolyfills\Tests\TestCases; use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; use Yoast\PHPUnitPolyfills\TestCases\TestCase; /** @@ -129,6 +130,7 @@ public static function tear_down_after_class() { * * @return void */ + #[DataProvider( 'dataHaveFixtureMethodsBeenTriggered' )] public function testHaveFixtureMethodsBeenTriggered( $expectedBeforeClass, $expectedBefore, diff --git a/tests/TestCases/XTestCaseTest.php b/tests/TestCases/XTestCaseTest.php index 099572b..0ddfda8 100644 --- a/tests/TestCases/XTestCaseTest.php +++ b/tests/TestCases/XTestCaseTest.php @@ -3,6 +3,7 @@ namespace Yoast\PHPUnitPolyfills\Tests\TestCases; use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; use Yoast\PHPUnitPolyfills\TestCases\XTestCase; /** @@ -107,6 +108,7 @@ public static function tearDownFixturesAfterClass() { * * @return void */ + #[DataProvider( 'dataHaveFixtureMethodsBeenTriggered' )] public function testHaveFixtureMethodsBeenTriggered( $expectedBeforeClass, $expectedBefore, $expectedAfter ) { $this->assertSame( $expectedBeforeClass, From 61ce3e9ea659f39084c0d40dcba231e9449af3d0 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 25 Feb 2024 10:21:44 +0100 Subject: [PATCH 3/4] Tests: add `Requires*` attributes 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. --- tests/Polyfills/AssertClosedResourceBzip2Test.php | 2 ++ tests/Polyfills/AssertClosedResourceCurlTest.php | 4 ++++ tests/Polyfills/AssertClosedResourceEnchantTest.php | 4 ++++ tests/Polyfills/AssertClosedResourceFinfoTest.php | 4 ++++ tests/Polyfills/AssertClosedResourceGdTest.php | 4 ++++ tests/Polyfills/AssertClosedResourceShmopTest.php | 2 ++ tests/Polyfills/AssertClosedResourceWddxTest.php | 4 ++++ tests/Polyfills/AssertClosedResourceXmlParserTest.php | 4 ++++ tests/Polyfills/AssertClosedResourceZipTest.php | 2 ++ tests/Polyfills/AssertClosedResourceZlibTest.php | 2 ++ tests/Polyfills/AssertObjectEqualsPHPUnitLt940Test.php | 2 ++ tests/Polyfills/AssertObjectEqualsTest.php | 4 ++++ tests/TestCases/TestCaseTestTrait.php | 2 ++ tests/TestListeners/Fixtures/FailurePHPUnitGte7.php | 2 ++ tests/TestListeners/Fixtures/IncompletePHPUnitGte7.php | 2 ++ tests/TestListeners/Fixtures/RiskyPHPUnitGte7.php | 2 ++ tests/TestListeners/Fixtures/SkippedPHPUnitGte7.php | 2 ++ tests/TestListeners/Fixtures/SuccessPHPUnitGte7.php | 2 ++ tests/TestListeners/Fixtures/TestErrorPHPUnitGte7.php | 1 + tests/TestListeners/Fixtures/WarningPHPUnitGte7.php | 2 ++ tests/TestListeners/TestListenerTest.php | 4 ++++ 21 files changed, 57 insertions(+) diff --git a/tests/Polyfills/AssertClosedResourceBzip2Test.php b/tests/Polyfills/AssertClosedResourceBzip2Test.php index bc8b152..2ab8de1 100644 --- a/tests/Polyfills/AssertClosedResourceBzip2Test.php +++ b/tests/Polyfills/AssertClosedResourceBzip2Test.php @@ -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; @@ -19,6 +20,7 @@ */ #[CoversClass( AssertClosedResource::class )] #[CoversClass( ResourceHelper::class )] +#[RequiresPhpExtension( 'bz2' )] final class AssertClosedResourceBzip2Test extends AssertClosedResourceTestCase { use AssertClosedResource; diff --git a/tests/Polyfills/AssertClosedResourceCurlTest.php b/tests/Polyfills/AssertClosedResourceCurlTest.php index 38e598e..0c95fdf 100644 --- a/tests/Polyfills/AssertClosedResourceCurlTest.php +++ b/tests/Polyfills/AssertClosedResourceCurlTest.php @@ -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; @@ -23,6 +25,8 @@ */ #[CoversClass( AssertClosedResource::class )] #[CoversClass( ResourceHelper::class )] +#[RequiresPhp( '< 8.0' )] +#[RequiresPhpExtension( 'curl' )] final class AssertClosedResourceCurlTest extends AssertClosedResourceTestCase { use AssertClosedResource; diff --git a/tests/Polyfills/AssertClosedResourceEnchantTest.php b/tests/Polyfills/AssertClosedResourceEnchantTest.php index 39d03d6..7b6ccf5 100644 --- a/tests/Polyfills/AssertClosedResourceEnchantTest.php +++ b/tests/Polyfills/AssertClosedResourceEnchantTest.php @@ -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; @@ -26,6 +28,8 @@ */ #[CoversClass( AssertClosedResource::class )] #[CoversClass( ResourceHelper::class )] +#[RequiresPhp( '< 8.0' )] +#[RequiresPhpExtension( 'enchant' )] final class AssertClosedResourceEnchantTest extends AssertClosedResourceTestCase { use AssertClosedResource; diff --git a/tests/Polyfills/AssertClosedResourceFinfoTest.php b/tests/Polyfills/AssertClosedResourceFinfoTest.php index 50801e0..ced302a 100644 --- a/tests/Polyfills/AssertClosedResourceFinfoTest.php +++ b/tests/Polyfills/AssertClosedResourceFinfoTest.php @@ -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; @@ -23,6 +25,8 @@ */ #[CoversClass( AssertClosedResource::class )] #[CoversClass( ResourceHelper::class )] +#[RequiresPhp( '< 8.1' )] +#[RequiresPhpExtension( 'finfo' )] final class AssertClosedResourceFinfoTest extends AssertClosedResourceTestCase { use AssertClosedResource; diff --git a/tests/Polyfills/AssertClosedResourceGdTest.php b/tests/Polyfills/AssertClosedResourceGdTest.php index 9048260..7af4440 100644 --- a/tests/Polyfills/AssertClosedResourceGdTest.php +++ b/tests/Polyfills/AssertClosedResourceGdTest.php @@ -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; @@ -23,6 +25,8 @@ */ #[CoversClass( AssertClosedResource::class )] #[CoversClass( ResourceHelper::class )] +#[RequiresPhp( '< 8.0' )] +#[RequiresPhpExtension( 'gd' )] final class AssertClosedResourceGdTest extends AssertClosedResourceTestCase { use AssertClosedResource; diff --git a/tests/Polyfills/AssertClosedResourceShmopTest.php b/tests/Polyfills/AssertClosedResourceShmopTest.php index f19d0f5..37bdb55 100644 --- a/tests/Polyfills/AssertClosedResourceShmopTest.php +++ b/tests/Polyfills/AssertClosedResourceShmopTest.php @@ -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; @@ -28,6 +29,7 @@ */ #[CoversClass( AssertClosedResource::class )] #[CoversClass( ResourceHelper::class )] +#[RequiresPhpExtension( 'shmop' )] final class AssertClosedResourceShmopTest extends AssertClosedResourceTestCase { use AssertClosedResource; diff --git a/tests/Polyfills/AssertClosedResourceWddxTest.php b/tests/Polyfills/AssertClosedResourceWddxTest.php index faf607b..5cd8d2a 100644 --- a/tests/Polyfills/AssertClosedResourceWddxTest.php +++ b/tests/Polyfills/AssertClosedResourceWddxTest.php @@ -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; @@ -26,6 +28,8 @@ */ #[CoversClass( AssertClosedResource::class )] #[CoversClass( ResourceHelper::class )] +#[RequiresPhp( '< 7.4' )] +#[RequiresPhpExtension( 'wddx' )] final class AssertClosedResourceWddxTest extends AssertClosedResourceTestCase { use AssertClosedResource; diff --git a/tests/Polyfills/AssertClosedResourceXmlParserTest.php b/tests/Polyfills/AssertClosedResourceXmlParserTest.php index 31143a9..56f7b2e 100644 --- a/tests/Polyfills/AssertClosedResourceXmlParserTest.php +++ b/tests/Polyfills/AssertClosedResourceXmlParserTest.php @@ -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; @@ -23,6 +25,8 @@ */ #[CoversClass( AssertClosedResource::class )] #[CoversClass( ResourceHelper::class )] +#[RequiresPhp( '< 8.0' )] +#[RequiresPhpExtension( 'libxml' )] final class AssertClosedResourceXmlParserTest extends AssertClosedResourceTestCase { use AssertClosedResource; diff --git a/tests/Polyfills/AssertClosedResourceZipTest.php b/tests/Polyfills/AssertClosedResourceZipTest.php index a17a051..f999686 100644 --- a/tests/Polyfills/AssertClosedResourceZipTest.php +++ b/tests/Polyfills/AssertClosedResourceZipTest.php @@ -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; @@ -27,6 +28,7 @@ */ #[CoversClass( AssertClosedResource::class )] #[CoversClass( ResourceHelper::class )] +#[RequiresPhpExtension( 'zip' )] final class AssertClosedResourceZipTest extends AssertClosedResourceTestCase { use AssertClosedResource; diff --git a/tests/Polyfills/AssertClosedResourceZlibTest.php b/tests/Polyfills/AssertClosedResourceZlibTest.php index b91d441..66f2937 100644 --- a/tests/Polyfills/AssertClosedResourceZlibTest.php +++ b/tests/Polyfills/AssertClosedResourceZlibTest.php @@ -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; @@ -19,6 +20,7 @@ */ #[CoversClass( AssertClosedResource::class )] #[CoversClass( ResourceHelper::class )] +#[RequiresPhpExtension( 'zlib' )] final class AssertClosedResourceZlibTest extends AssertClosedResourceTestCase { use AssertClosedResource; diff --git a/tests/Polyfills/AssertObjectEqualsPHPUnitLt940Test.php b/tests/Polyfills/AssertObjectEqualsPHPUnitLt940Test.php index 55544ad..77f36fd 100644 --- a/tests/Polyfills/AssertObjectEqualsPHPUnitLt940Test.php +++ b/tests/Polyfills/AssertObjectEqualsPHPUnitLt940Test.php @@ -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; @@ -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.'; diff --git a/tests/Polyfills/AssertObjectEqualsTest.php b/tests/Polyfills/AssertObjectEqualsTest.php index 37abbda..ca754ff 100644 --- a/tests/Polyfills/AssertObjectEqualsTest.php +++ b/tests/Polyfills/AssertObjectEqualsTest.php @@ -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; @@ -35,6 +36,7 @@ * @requires PHP 7.0 */ #[CoversClass( AssertObjectEquals::class )] +#[RequiresPhp( '7.0' )] final class AssertObjectEqualsTest extends TestCase { use AssertObjectEquals; @@ -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.'; @@ -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.'; diff --git a/tests/TestCases/TestCaseTestTrait.php b/tests/TestCases/TestCaseTestTrait.php index 6034975..b41c8be 100644 --- a/tests/TestCases/TestCaseTestTrait.php +++ b/tests/TestCases/TestCaseTestTrait.php @@ -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; @@ -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' ); diff --git a/tests/TestListeners/Fixtures/FailurePHPUnitGte7.php b/tests/TestListeners/Fixtures/FailurePHPUnitGte7.php index f1e333c..5fa2dff 100644 --- a/tests/TestListeners/Fixtures/FailurePHPUnitGte7.php +++ b/tests/TestListeners/Fixtures/FailurePHPUnitGte7.php @@ -2,6 +2,7 @@ namespace Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures; +use PHPUnit\Framework\Attributes\RequiresPhpunit; use PHPUnit\Framework\TestCase; /** @@ -9,6 +10,7 @@ * * @requires PHPUnit 7.0 */ +#[RequiresPhpunit( '7.0' )] class FailurePHPUnitGte7 extends TestCase { /** diff --git a/tests/TestListeners/Fixtures/IncompletePHPUnitGte7.php b/tests/TestListeners/Fixtures/IncompletePHPUnitGte7.php index 543b3ad..cf45762 100644 --- a/tests/TestListeners/Fixtures/IncompletePHPUnitGte7.php +++ b/tests/TestListeners/Fixtures/IncompletePHPUnitGte7.php @@ -2,6 +2,7 @@ namespace Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures; +use PHPUnit\Framework\Attributes\RequiresPhpunit; use PHPUnit\Framework\TestCase; /** @@ -9,6 +10,7 @@ * * @requires PHPUnit 7.0 */ +#[RequiresPhpunit( '7.0' )] class IncompletePHPUnitGte7 extends TestCase { /** diff --git a/tests/TestListeners/Fixtures/RiskyPHPUnitGte7.php b/tests/TestListeners/Fixtures/RiskyPHPUnitGte7.php index 78d482a..cc33c0f 100644 --- a/tests/TestListeners/Fixtures/RiskyPHPUnitGte7.php +++ b/tests/TestListeners/Fixtures/RiskyPHPUnitGte7.php @@ -2,6 +2,7 @@ namespace Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures; +use PHPUnit\Framework\Attributes\RequiresPhpunit; use PHPUnit\Framework\TestCase; /** @@ -9,6 +10,7 @@ * * @requires PHPUnit 7.0 */ +#[RequiresPhpunit( '7.0' )] class RiskyPHPUnitGte7 extends TestCase { /** diff --git a/tests/TestListeners/Fixtures/SkippedPHPUnitGte7.php b/tests/TestListeners/Fixtures/SkippedPHPUnitGte7.php index 7b62132..ba982cb 100644 --- a/tests/TestListeners/Fixtures/SkippedPHPUnitGte7.php +++ b/tests/TestListeners/Fixtures/SkippedPHPUnitGte7.php @@ -2,6 +2,7 @@ namespace Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures; +use PHPUnit\Framework\Attributes\RequiresPhpunit; use PHPUnit\Framework\TestCase; /** @@ -9,6 +10,7 @@ * * @requires PHPUnit 7.0 */ +#[RequiresPhpunit( '7.0' )] class SkippedPHPUnitGte7 extends TestCase { /** diff --git a/tests/TestListeners/Fixtures/SuccessPHPUnitGte7.php b/tests/TestListeners/Fixtures/SuccessPHPUnitGte7.php index a4e6d63..b9b3fcb 100644 --- a/tests/TestListeners/Fixtures/SuccessPHPUnitGte7.php +++ b/tests/TestListeners/Fixtures/SuccessPHPUnitGte7.php @@ -2,6 +2,7 @@ namespace Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures; +use PHPUnit\Framework\Attributes\RequiresPhpunit; use PHPUnit\Framework\TestCase; /** @@ -9,6 +10,7 @@ * * @requires PHPUnit 7.0 */ +#[RequiresPhpunit( '7.0' )] class SuccessPHPUnitGte7 extends TestCase { /** diff --git a/tests/TestListeners/Fixtures/TestErrorPHPUnitGte7.php b/tests/TestListeners/Fixtures/TestErrorPHPUnitGte7.php index a20d292..81b89c8 100644 --- a/tests/TestListeners/Fixtures/TestErrorPHPUnitGte7.php +++ b/tests/TestListeners/Fixtures/TestErrorPHPUnitGte7.php @@ -14,6 +14,7 @@ * @coversNothing */ #[CoversNothing] +#[RequiresPhpunit( '7.0' )] class TestErrorPHPUnitGte7 extends TestCase { /** diff --git a/tests/TestListeners/Fixtures/WarningPHPUnitGte7.php b/tests/TestListeners/Fixtures/WarningPHPUnitGte7.php index c57262d..10cbf49 100644 --- a/tests/TestListeners/Fixtures/WarningPHPUnitGte7.php +++ b/tests/TestListeners/Fixtures/WarningPHPUnitGte7.php @@ -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; @@ -10,6 +11,7 @@ * * @requires PHPUnit 7.0 */ +#[RequiresPhpunit( '7.0' )] class WarningPHPUnitGte7 extends TestCase { /** diff --git a/tests/TestListeners/TestListenerTest.php b/tests/TestListeners/TestListenerTest.php index 5417688..9584854 100644 --- a/tests/TestListeners/TestListenerTest.php +++ b/tests/TestListeners/TestListenerTest.php @@ -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; @@ -21,6 +22,7 @@ */ #[CoversClass( TestListenerDefaultImplementation::class )] #[CoversClass( TestListenerSnakeCaseMethods::class )] +#[RequiresPhpunit( '< 10' )] final class TestListenerTest extends TestCase { /** @@ -73,6 +75,7 @@ public function testError() { * * @return void */ + #[RequiresPhpunit( '5' )] public function testWarning() { $test = $this->getTestObject( 'Warning' ); $test->run( $this->result ); @@ -119,6 +122,7 @@ public function testIncomplete() { * * @return void */ + #[RequiresPhpunit( '6' )] public function testRisky() { $test = $this->getTestObject( 'Risky' ); $test->run( $this->result ); From 5cecae1437572a8f8e9258723a2e6fa237e41234 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 25 Feb 2024 10:25:13 +0100 Subject: [PATCH 4/4] Tests: add `Before/After[Class]` attributes 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. --- src/TestCases/XTestCase.php | 8 ++++++++ tests/Polyfills/AssertClosedResourceShmopTest.php | 2 ++ tests/Polyfills/AssertObjectEqualsPHPUnitLt940Test.php | 2 ++ tests/TestCases/XTestCaseTest.php | 8 ++++++++ 4 files changed, 20 insertions(+) diff --git a/src/TestCases/XTestCase.php b/src/TestCases/XTestCase.php index 4d4ea25..328f4f9 100644 --- a/src/TestCases/XTestCase.php +++ b/src/TestCases/XTestCase.php @@ -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; @@ -55,6 +59,7 @@ abstract class XTestCase extends PHPUnit_TestCase { * * @return void */ + #[BeforeClass] public static function setUpFixturesBeforeClass() { parent::setUpBeforeClass(); } @@ -68,6 +73,7 @@ public static function setUpFixturesBeforeClass() { * * @return void */ + #[Before] protected function setUpFixtures() { parent::setUp(); } @@ -81,6 +87,7 @@ protected function setUpFixtures() { * * @return void */ + #[After] protected function tearDownFixtures() { parent::tearDown(); } @@ -94,6 +101,7 @@ protected function tearDownFixtures() { * * @return void */ + #[AfterClass] public static function tearDownFixturesAfterClass() { parent::tearDownAfterClass(); } diff --git a/tests/Polyfills/AssertClosedResourceShmopTest.php b/tests/Polyfills/AssertClosedResourceShmopTest.php index 37bdb55..272c7f6 100644 --- a/tests/Polyfills/AssertClosedResourceShmopTest.php +++ b/tests/Polyfills/AssertClosedResourceShmopTest.php @@ -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; @@ -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.' ); diff --git a/tests/Polyfills/AssertObjectEqualsPHPUnitLt940Test.php b/tests/Polyfills/AssertObjectEqualsPHPUnitLt940Test.php index 77f36fd..596def2 100644 --- a/tests/Polyfills/AssertObjectEqualsPHPUnitLt940Test.php +++ b/tests/Polyfills/AssertObjectEqualsPHPUnitLt940Test.php @@ -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; @@ -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()' ); diff --git a/tests/TestCases/XTestCaseTest.php b/tests/TestCases/XTestCaseTest.php index 0ddfda8..23b607b 100644 --- a/tests/TestCases/XTestCaseTest.php +++ b/tests/TestCases/XTestCaseTest.php @@ -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; @@ -47,6 +51,7 @@ final class XTestCaseTest extends XTestCase { * * @return void */ + #[BeforeClass] public static function setUpFixturesBeforeClass() { parent::setUpFixturesBeforeClass(); @@ -60,6 +65,7 @@ public static function setUpFixturesBeforeClass() { * * @return void */ + #[Before] protected function setUpFixtures() { parent::setUpFixtures(); @@ -73,6 +79,7 @@ protected function setUpFixtures() { * * @return void */ + #[After] protected function tearDownFixtures() { ++self::$after; @@ -86,6 +93,7 @@ protected function tearDownFixtures() { * * @return void */ + #[AfterClass] public static function tearDownFixturesAfterClass() { // Reset. self::$beforeClass = 0;