-
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.
AssertObjectNotEquals trait: polyfill the Assert::assertObjectNotEqua…
…ls() method PHPUnit 11.2.0 introduced the new `Assert::assertObjectNotEquals()` method. This commit: * Adds two traits with the same name. One to polyfill the method when not available in PHPUnit. The other to allow for `use`-ing the trait in PHPUnit versions in which the method is already natively available. * Logic to the custom autoloader which will load the correct trait depending on the PHPUnit version used. * Adds tests. As the polyfill contains logic to match the PHPUnit native implementation as closely as possible, while still being PHP and PHPUnit cross-version compatible, extensive unit tests have been added to ensure the behaviour of the polyfill matches that of the original function. Includes: * Adding information on the new polyfill to the README. * Adding the new polyfill to the existing `TestCases` classes. * Updating the class docs for the `InvalidComparisonMethodException` and the `ComparatorValidator` classes. Refs: * sebastianbergmann/phpunit#5811 * sebastianbergmann/phpunit@8e3b7c1 Co-authored-by: Sebastian Bergmann <[email protected]>
- Loading branch information
1 parent
b3f61d7
commit 632617a
Showing
11 changed files
with
677 additions
and
3 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
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,111 @@ | ||
<?php | ||
|
||
namespace Yoast\PHPUnitPolyfills\Polyfills; | ||
|
||
use TypeError; | ||
use Yoast\PHPUnitPolyfills\Exceptions\InvalidComparisonMethodException; | ||
use Yoast\PHPUnitPolyfills\Helpers\ComparatorValidator; | ||
|
||
/** | ||
* Polyfill the Assert::assertObjectNotEquals() method. | ||
* | ||
* Introduced in PHPUnit 11.2.0. | ||
* | ||
* The polyfill implementation closely matches the PHPUnit native implementation with the exception | ||
* of the thrown exceptions. | ||
* | ||
* @link https://github.com/sebastianbergmann/phpunit/issues/5811 | ||
* @link https://github.com/sebastianbergmann/phpunit/commit/8e3b7c18506312df0676f2e079c414cc56b49f69 | ||
*/ | ||
trait AssertObjectNotEquals { | ||
|
||
/** | ||
* Asserts that two objects are considered _not_ equal based on a custom object comparison | ||
* using a comparator method in the target object. | ||
* | ||
* The custom comparator method is expected to have the following method | ||
* signature: `equals(self $other): bool` (or similar with a different method name). | ||
* | ||
* Basically, the assertion checks the following: | ||
* - A method with name $method must exist on the $actual object. | ||
* - The method must accept exactly one argument and this argument must be required. | ||
* - This parameter must have a classname-based declared type. | ||
* - The $expected object must be compatible with this declared type. | ||
* - The method must have a declared bool return type. | ||
* | ||
* @param object $expected Expected value. | ||
* @param object $actual The value to test. | ||
* @param string $method The name of the comparator method within the object. | ||
* @param string $message Optional failure message to display. | ||
* | ||
* @return void | ||
* | ||
* @throws TypeError When any of the passed arguments do not meet the required type. | ||
* @throws InvalidComparisonMethodException When the comparator method does not comply with the requirements. | ||
*/ | ||
final public static function assertObjectNotEquals( $expected, $actual, $method = 'equals', $message = '' ) { | ||
/* | ||
* Parameter input validation. | ||
* In PHPUnit this is done via PHP native type declarations. Emulating this for the polyfill. | ||
*/ | ||
if ( \is_object( $expected ) === false ) { | ||
throw new TypeError( | ||
\sprintf( | ||
'Argument 1 passed to assertObjectNotEquals() must be an object, %s given', | ||
\gettype( $expected ) | ||
) | ||
); | ||
} | ||
|
||
if ( \is_object( $actual ) === false ) { | ||
throw new TypeError( | ||
\sprintf( | ||
'Argument 2 passed to assertObjectNotEquals() must be an object, %s given', | ||
\gettype( $actual ) | ||
) | ||
); | ||
} | ||
|
||
if ( \is_scalar( $method ) === false ) { | ||
throw new TypeError( | ||
\sprintf( | ||
'Argument 3 passed to assertObjectNotEquals() must be of the type string, %s given', | ||
\gettype( $method ) | ||
) | ||
); | ||
} | ||
else { | ||
$method = (string) $method; | ||
} | ||
|
||
/* | ||
* Validate the comparator method requirements. | ||
* | ||
* If the method does not validate, an InvalidComparisonMethodException is thrown, | ||
* which will cause the test to error out. | ||
*/ | ||
try { | ||
ComparatorValidator::isValid( $expected, $actual, $method ); | ||
} catch ( InvalidComparisonMethodException $e ) { | ||
// Rethrow to ensure a stack trace shows the exception comes from this method, not the helper. | ||
throw $e; | ||
} | ||
|
||
/* | ||
* Execute the comparator method. | ||
*/ | ||
$result = $actual->{$method}( $expected ); | ||
|
||
$msg = \sprintf( | ||
'Failed asserting that two objects are not equal. The objects are equal according to %s::%s()', | ||
\get_class( $actual ), | ||
$method | ||
); | ||
|
||
if ( $message !== '' ) { | ||
$msg = $message . \PHP_EOL . $msg; | ||
} | ||
|
||
static::assertFalse( $result, $msg ); | ||
} | ||
} |
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,8 @@ | ||
<?php | ||
|
||
namespace Yoast\PHPUnitPolyfills\Polyfills; | ||
|
||
/** | ||
* Empty trait for use with PHPUnit >= 11.2.0 in which this polyfill is not needed. | ||
*/ | ||
trait AssertObjectNotEquals {} |
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.