-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PHPUnit 9.6.11 | AssertObjectProperty trait: polyfill the Assert::ass…
…ertObject[Not]HasProperty() methods PHPUnit 10.1.0 introduced the new `Assert::assertObjectHasProperty()` and `Assert::assertObjectNotHasProperty()` methods. These methods have now been backported to PHPUnit 9.6.11, so should be made available in the PHPUnit Polyfills 1.x series. This commit: * Adds two traits with the same name. One to polyfill the methods when not available in PHPUnit. The other - an empty trait - to allow for `use`-ing the trait in PHPUnit versions in which the methods are already natively available. * Logic to the custom autoloader which will load the correct trait depending on the PHPUnit version used. * An availability test and functional tests for the functionality polyfilled. Includes: * Adding the new polyfill to the existing `TestCases` classes. Refs: * sebastianbergmann/phpunit#5220 * sebastianbergmann/phpunit#5231 (and follow up commits/PRs) * sebastianbergmann/phpunit#5478 Co-authored-by: Jan-Sverre Riksfjord <[email protected]> Co-authored-by: Sebastian Bergmann <[email protected]>
- Loading branch information
1 parent
f4c425a
commit 2810188
Showing
11 changed files
with
637 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
<?php | ||
|
||
namespace Yoast\PHPUnitPolyfills\Polyfills; | ||
|
||
use PHPUnit\Framework\Assert; | ||
use ReflectionObject; | ||
use TypeError; | ||
use Yoast\PHPUnitPolyfills\Autoload; | ||
|
||
/** | ||
* Polyfill the Assert::assertObjectHasProperty() and Assert::assertObjectNotHasProperty() methods, | ||
* which replace the Assert::assertObjectHasAttribute() and Assert::assertObjectNotHasAttribute() methods. | ||
* | ||
* Introduced in PHPUnit 10.1.0 and PHPUnit 9.6.11. | ||
* | ||
* The Assert::assertObjectHasAttribute() and Assert::assertObjectNotHasAttribute() methods | ||
* were deprecated in PHPUnit 9.6.1 and removed in PHPUnit 10.0.0. | ||
* | ||
* @link https://github.com/sebastianbergmann/phpunit/pull/5231 | ||
* @link https://github.com/sebastianbergmann/phpunit/issues/5478 | ||
* | ||
* @since 1.1.0 | ||
*/ | ||
trait AssertObjectProperty { | ||
|
||
/** | ||
* Asserts that an object has a specified property. | ||
* | ||
* @param string $propertyName The name of the property. | ||
* @param object $object The object on which to check whether the property exists. | ||
* @param string $message Optional failure message to display. | ||
* | ||
* @return void | ||
* | ||
* @throws TypeError When any of the passed arguments do not meet the required type. | ||
*/ | ||
final public static function assertObjectHasProperty( $propertyName, $object, $message = '' ) { | ||
/* | ||
* Parameter input validation. | ||
* In PHPUnit this is done via PHP native type declarations. Emulating this for the polyfill, | ||
* including for those PHPUnit versions where we hand off to a native PHPUnit alternative, as | ||
* otherwise the method referenced in the error message would get very confusing and inconsistent. | ||
*/ | ||
if ( \is_string( $propertyName ) === false ) { | ||
throw new TypeError( | ||
\sprintf( | ||
'Argument 1 passed to assertObjectHasProperty() must be of type string, %s given', | ||
\gettype( $propertyName ) | ||
) | ||
); | ||
} | ||
if ( \is_object( $object ) === false ) { | ||
throw new TypeError( | ||
\sprintf( | ||
'Argument 2 passed to assertObjectHasProperty() must be of type object, %s given', | ||
\gettype( $object ) | ||
) | ||
); | ||
} | ||
|
||
if ( \method_exists( '\PHPUnit\Framework\Assert', 'assertObjectHasAttribute' ) | ||
&& \version_compare( Autoload::getPHPUnitVersion(), '9.6.0', '<=' ) | ||
) { | ||
// PHPUnit <= 9.6.0. | ||
static::assertObjectHasAttribute( $propertyName, $object, $message ); | ||
return; | ||
} | ||
|
||
/* | ||
* PHPUnit 9.6.1+. | ||
* Note: letting this polyfill code kick in for PHPUnit 9.6.1+ as well | ||
* to prevent the PHPUnit deprecation notice showing. | ||
*/ | ||
$msg = self::assertObjectHasPropertyFailureDescription( $object ); | ||
$msg .= \sprintf( ' has property "%s".', $propertyName ); | ||
if ( $message !== '' ) { | ||
$msg = $message . \PHP_EOL . $msg; | ||
} | ||
|
||
$hasProperty = ( new ReflectionObject( $object ) )->hasProperty( $propertyName ); | ||
static::assertTrue( $hasProperty, $msg ); | ||
} | ||
|
||
/** | ||
* Asserts that an object does not have a specified property. | ||
* | ||
* @param string $propertyName The name of the property. | ||
* @param object $object The object on which to check whether the property exists. | ||
* @param string $message Optional failure message to display. | ||
* | ||
* @return void | ||
* | ||
* @throws TypeError When any of the passed arguments do not meet the required type. | ||
*/ | ||
final public static function assertObjectNotHasProperty( $propertyName, $object, $message = '' ) { | ||
/* | ||
* Parameter input validation. | ||
* In PHPUnit this is done via PHP native type declarations. Emulating this for the polyfill, | ||
* including for those PHPUnit versions where we hand off to a native PHPUnit alternative, as | ||
* otherwise the method referenced in the error message would get very confusing and inconsistent. | ||
*/ | ||
if ( \is_string( $propertyName ) === false ) { | ||
throw new TypeError( | ||
\sprintf( | ||
'Argument 1 passed to assertObjectNotHasProperty() must be of type string, %s given', | ||
\gettype( $propertyName ) | ||
) | ||
); | ||
} | ||
if ( \is_object( $object ) === false ) { | ||
throw new TypeError( | ||
\sprintf( | ||
'Argument 2 passed to assertObjectNotHasProperty() must be of type object, %s given', | ||
\gettype( $object ) | ||
) | ||
); | ||
} | ||
|
||
if ( \method_exists( '\PHPUnit\Framework\Assert', 'assertObjectNotHasAttribute' ) | ||
&& \version_compare( Autoload::getPHPUnitVersion(), '9.6.0', '<=' ) | ||
) { | ||
// PHPUnit <= 9.6.0. | ||
static::assertObjectNotHasAttribute( $propertyName, $object, $message ); | ||
return; | ||
} | ||
|
||
/* | ||
* PHPUnit 9.6.1+. | ||
* Note: letting this polyfill code kick in for PHPUnit 9.6.1+ as well | ||
* to prevent the PHPUnit deprecation notice showing. | ||
*/ | ||
$msg = self::assertObjectHasPropertyFailureDescription( $object ); | ||
$msg .= \sprintf( ' does not have property "%s".', $propertyName ); | ||
if ( $message !== '' ) { | ||
$msg = $message . \PHP_EOL . $msg; | ||
} | ||
|
||
$hasProperty = ( new ReflectionObject( $object ) )->hasProperty( $propertyName ); | ||
static::assertFalse( $hasProperty, $msg ); | ||
} | ||
|
||
/** | ||
* Returns the description of the failure. | ||
* | ||
* @param object $object The object under test. | ||
* | ||
* @return string | ||
*/ | ||
private static function assertObjectHasPropertyFailureDescription( $object ) { | ||
return \sprintf( | ||
'Failed asserting that object of class "%s"', | ||
\get_class( $object ) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Yoast\PHPUnitPolyfills\Polyfills; | ||
|
||
/** | ||
* Empty trait for use with PHPUnit >= 9.6.11 in which this polyfill is not needed. | ||
* | ||
* @since 1.1.0 | ||
*/ | ||
trait AssertObjectProperty {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.