-
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 10 | AssertIsList trait: polyfill the Assert::assertIsList() …
…method PHPUnit 10.0.0 introduces the new `Assert::assertIsList()` method. 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. * Functional tests for the functionality polyfilled. Note: the methods use `static::` to call the PHPUnit native functionality. This allows for existing method overloads in a child class of the PHPUnit native `TestCase` to be respected. Includes: * Adding the new polyfill to the existing `TestCases` classes and adding a test for the polyfill availability to the `TestCaseTestTrait`. Refs: * sebastianbergmann/phpunit#4818 * sebastianbergmann/phpunit@71f5074 * sebastianbergmann/phpunit@e04a947 * sebastianbergmann/phpunit@2bc1aea Co-authored-by: Jaroslav Hanslík <kukulich@kukulich.cz> Co-authored-by: Sebastian Bergmann <sb@sebastian-bergmann.de>
- Loading branch information
1 parent
53764b3
commit 74ee1d5
Showing
9 changed files
with
368 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
namespace Yoast\PHPUnitPolyfills\Polyfills; | ||
|
||
use PHPUnit\Framework\Assert; | ||
|
||
/** | ||
* Polyfill the Assert::assertIsList() method. | ||
* | ||
* Introduced in PHPUnit 10.0.0. | ||
* | ||
* @link https://github.com/sebastianbergmann/phpunit/pull/4818 | ||
*/ | ||
trait AssertIsList { | ||
|
||
/** | ||
* Asserts that an array is list. | ||
* | ||
* @param mixed $array The value to test. | ||
* @param string $message Optional failure message to display. | ||
* | ||
* @return void | ||
*/ | ||
final public static function assertIsList( $array, $message = '' ) { | ||
$msg = self::assertIsListFailureDescription( $array ); | ||
if ( $message !== '' ) { | ||
$msg = $message . \PHP_EOL . $msg; | ||
} | ||
|
||
if ( \is_array( $array ) === false ) { | ||
if ( \method_exists( Assert::class, 'assertIsArray' ) ) { | ||
static::assertIsArray( $array, $msg ); | ||
return; | ||
} | ||
|
||
static::assertInternalType( 'array', $array, $msg ); | ||
return; | ||
} | ||
|
||
if ( $array === [] ) { | ||
static::assertSame( $array, $array, $msg ); | ||
return; | ||
} | ||
|
||
if ( \function_exists( 'array_is_list' ) ) { | ||
// phpcs:ignore PHPCompatibility.FunctionUse.NewFunctions.array_is_listFound -- PHP 8.1+. | ||
static::assertTrue( \array_is_list( $array ), $msg ); | ||
return; | ||
} | ||
|
||
$expected = \range( 0, ( \count( $array ) - 1 ) ); | ||
|
||
static::assertSame( $expected, \array_keys( $array ), $msg ); | ||
} | ||
|
||
/** | ||
* Returns the description of the failure. | ||
* | ||
* @param mixed $other The value under test. | ||
* | ||
* @return string | ||
*/ | ||
private static function assertIsListFailureDescription( $other ) { | ||
$type = \strtolower( \gettype( $other ) ); | ||
|
||
switch ( $type ) { | ||
case 'double': | ||
$description = 'a float'; | ||
break; | ||
|
||
case 'resource (closed)': | ||
$description = 'a closed resource'; | ||
break; | ||
|
||
case 'array': | ||
case 'integer': | ||
case 'object': | ||
$description = 'an ' . $type; | ||
break; | ||
|
||
case 'boolean': | ||
case 'closed resource': | ||
case 'float': | ||
case 'resource': | ||
case 'string': | ||
$description = 'a ' . $type; | ||
break; | ||
|
||
case 'null': | ||
$description = 'null'; | ||
break; | ||
|
||
default: | ||
$description = 'a value of ' . $type; | ||
break; | ||
} | ||
|
||
return \sprintf( 'Failed asserting that %s is a list.', $description ); | ||
} | ||
} |
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 >= 10.0.0 in which this polyfill is not needed. | ||
*/ | ||
trait AssertIsList {} |
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.