diff --git a/README.md b/README.md index 9282c0b..26731e6 100644 --- a/README.md +++ b/README.md @@ -432,6 +432,8 @@ Polyfills the following method: These methods were introduced in PHPUnit 10.1.0 as alternatives to the `Assert::assertObjectHasAttribute()` and `Assert::assertObjectNotHasAttribute()` methods, which were hard deprecated (warning) in PHPUnit 9.6.1 and removed in PHPUnit 10.0.0. +These methods were later backported to the PHPUnit 9 branch and included in the PHPUnit 9.6.11 release. + [`Assert::assertObjectHasProperty()`]: https://docs.phpunit.de/en/main/assertions.html#assertObjectHasProperty [`Assert::assertObjectNotHasProperty()`]: https://docs.phpunit.de/en/main/assertions.html#assertObjectHasProperty diff --git a/src/Polyfills/AssertObjectProperty.php b/src/Polyfills/AssertObjectProperty.php index 59261e6..5e5a06e 100644 --- a/src/Polyfills/AssertObjectProperty.php +++ b/src/Polyfills/AssertObjectProperty.php @@ -65,8 +65,8 @@ final public static function assertObjectHasProperty( $propertyName, $object, $m } /* - * PHPUnit 9.6.1+ and PHPUnit 10.0.x. - * Note: letting this polyfill code kick in for PHPUnit 9.6.1+ as well + * PHPUnit 9.6.1 < 9.6.11 and PHPUnit 10.0.x. + * Note: letting this polyfill code kick in for PHPUnit 9.6.1 < 9.6.11 as well * to prevent the PHPUnit deprecation notice showing. */ $msg = self::assertObjectHasPropertyFailureDescription( $object ); @@ -123,8 +123,8 @@ final public static function assertObjectNotHasProperty( $propertyName, $object, } /* - * PHPUnit 9.6.1+ and PHPUnit 10.0.x. - * Note: letting this polyfill code kick in for PHPUnit 9.6.1+ as well + * PHPUnit 9.6.1 < 9.6.11 and PHPUnit 10.0.x. + * Note: letting this polyfill code kick in for PHPUnit 9.6.1 < 9.6.11 as well * to prevent the PHPUnit deprecation notice showing. */ $msg = self::assertObjectHasPropertyFailureDescription( $object ); diff --git a/tests/Polyfills/AssertObjectPropertyTest.php b/tests/Polyfills/AssertObjectPropertyTest.php index 19af4fc..0782f5b 100644 --- a/tests/Polyfills/AssertObjectPropertyTest.php +++ b/tests/Polyfills/AssertObjectPropertyTest.php @@ -24,6 +24,18 @@ final class AssertObjectPropertyTest extends TestCase { use AssertObjectProperty; use ExpectExceptionMessageMatches; + /** + * Check whether native PHPUnit assertions will be used or the polyfill. + * + * @return bool + */ + private function usesNativePHPUnitAssertion() { + $phpunit_version = PHPUnit_Version::id(); + return ( \version_compare( $phpunit_version, '10.1.0', '>=' ) + || ( \version_compare( $phpunit_version, '9.6.11', '>=' ) && \version_compare( $phpunit_version, '10.0.0', '<' ) ) + ); + } + /** * Verify that the assertObjectHasProperty() method throws an error when the $propertyName parameter is not a scalar. * @@ -34,23 +46,22 @@ final class AssertObjectPropertyTest extends TestCase { * @return void */ public function testAssertObjectHasPropertyFailsOnInvalidInputTypePropertyName( $input ) { - if ( \is_scalar( $input ) && \version_compare( PHPUnit_Version::id(), '10.1.0', '>=' ) ) { + if ( \is_scalar( $input ) && $this->usesNativePHPUnitAssertion() ) { $this->markTestSkipped( 'PHPUnit native implementation relies on strict_types and when not used will accept scalar inputs' ); } - if ( \PHP_VERSION_ID >= 80100 - && \version_compare( PHPUnit_Version::id(), '10.1.0', '>=' ) - ) { + $this->expectException( TypeError::class ); + + if ( \PHP_VERSION_ID >= 80000 && $this->usesNativePHPUnitAssertion() ) { $msg = 'assertObjectHasProperty(): Argument #1 ($propertyName) must be of type string, '; + $this->expectExceptionMessage( $msg ); } else { // PHP 5/7. - $msg = 'Argument 1 passed to assertObjectHasProperty() must be of type string, '; + $pattern = '`^Argument 1 passed to [^\s]*assertObjectHasProperty\(\) must be of (the )?type string, `'; + $this->expectExceptionMessageMatches( $pattern ); } - $this->expectException( TypeError::class ); - $this->expectExceptionMessage( $msg ); - $this->assertObjectHasProperty( $input, new stdClass() ); } @@ -64,23 +75,22 @@ public function testAssertObjectHasPropertyFailsOnInvalidInputTypePropertyName( * @return void */ public function testAssertObjectNotHasPropertyFailsOnInvalidInputTypePropertyName( $input ) { - if ( \is_scalar( $input ) && \version_compare( PHPUnit_Version::id(), '10.1.0', '>=' ) ) { + if ( \is_scalar( $input ) && $this->usesNativePHPUnitAssertion() ) { $this->markTestSkipped( 'PHPUnit native implementation relies on strict_types and when not used will accept scalar inputs' ); } - if ( \PHP_VERSION_ID >= 80100 - && \version_compare( PHPUnit_Version::id(), '10.1.0', '>=' ) - ) { + $this->expectException( TypeError::class ); + + if ( \PHP_VERSION_ID >= 80000 && $this->usesNativePHPUnitAssertion() ) { $msg = 'assertObjectNotHasProperty(): Argument #1 ($propertyName) must be of type string, '; + $this->expectExceptionMessage( $msg ); } else { // PHP 5/7. - $msg = 'Argument 1 passed to assertObjectNotHasProperty() must be of type string, '; + $pattern = '`^Argument 1 passed to [^\s]*assertObjectNotHasProperty\(\) must be of (the )?type string, `'; + $this->expectExceptionMessageMatches( $pattern ); } - $this->expectException( TypeError::class ); - $this->expectExceptionMessage( $msg ); - $this->assertObjectNotHasProperty( $input, new stdClass() ); } @@ -115,19 +125,18 @@ public static function dataAssertObjectPropertyFailsOnInvalidInputTypePropertyNa * @return void */ public function testAssertObjectHasPropertyFailsOnInvalidInputTypeObject( $input ) { - if ( \PHP_VERSION_ID >= 80100 - && \version_compare( PHPUnit_Version::id(), '10.1.0', '>=' ) - ) { + $this->expectException( TypeError::class ); + + if ( \PHP_VERSION_ID >= 80000 && $this->usesNativePHPUnitAssertion() ) { $msg = 'assertObjectHasProperty(): Argument #2 ($object) must be of type object, '; + $this->expectExceptionMessage( $msg ); } else { // PHP 5/7. - $msg = 'Argument 2 passed to assertObjectHasProperty() must be of type object, '; + $pattern = '`^Argument 2 passed to [^\s]*assertObjectHasProperty\(\) must be (of type|an) object, `'; + $this->expectExceptionMessageMatches( $pattern ); } - $this->expectException( TypeError::class ); - $this->expectExceptionMessage( $msg ); - $this->assertObjectHasProperty( 'propertyName', $input ); } @@ -141,19 +150,18 @@ public function testAssertObjectHasPropertyFailsOnInvalidInputTypeObject( $input * @return void */ public function testAssertObjectNotHasPropertyFailsOnInvalidInputTypeObject( $input ) { - if ( \PHP_VERSION_ID >= 80100 - && \version_compare( PHPUnit_Version::id(), '10.1.0', '>=' ) - ) { + $this->expectException( TypeError::class ); + + if ( \PHP_VERSION_ID >= 80000 && $this->usesNativePHPUnitAssertion() ) { $msg = 'assertObjectNotHasProperty(): Argument #2 ($object) must be of type object, '; + $this->expectExceptionMessage( $msg ); } else { // PHP 5/7. - $msg = 'Argument 2 passed to assertObjectNotHasProperty() must be of type object, '; + $pattern = '`^Argument 2 passed to [^\s]*assertObjectNotHasProperty\(\) must be (of type|an) object, `'; + $this->expectExceptionMessageMatches( $pattern ); } - $this->expectException( TypeError::class ); - $this->expectExceptionMessage( $msg ); - static::assertObjectNotHasProperty( 'propertyName', $input ); }