Skip to content

Commit

Permalink
AssertObjectProperty: minor tweaks to allow for PHPUnit backport
Browse files Browse the repository at this point in the history
PHPUnit has backported the new `assertObjectHasProperty()` and `assertObjectNotHasProperty()` methods PHPUnit 9 and released these backports in PHPUnit 9.6.11.

This commit:
* Updates the test expectations to take into account that the PHPUnit native versions will be also used in PHPUnit 9.6.11 < 10.0.0.
* Updates the polyfill trait documentation, both in the traits and the README.
  • Loading branch information
jrfnl committed Aug 19, 2023
1 parent d4a0748 commit 42d81fa
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 34 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions src/Polyfills/AssertObjectProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down
68 changes: 38 additions & 30 deletions tests/Polyfills/AssertObjectPropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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() );
}

Expand All @@ -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() );
}

Expand Down Expand Up @@ -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 );
}

Expand All @@ -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 );
}

Expand Down

0 comments on commit 42d81fa

Please sign in to comment.