Skip to content

Commit 3eeec05

Browse files
authored
Merge pull request #110 from Yoast/feature/phpunit-10-add-assertislisttrait
PHPUnit 10 | AssertIsList trait: polyfill the Assert::assertIsList() method
2 parents 53764b3 + 74ee1d5 commit 3eeec05

9 files changed

+368
-0
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,17 @@ These methods were introduced in PHPUnit 10.0.0.
406406
[`Assert::assertStringEqualsStringIgnoringLineEndings()`]: https://docs.phpunit.de/en/main/assertions.html#assertstringequalsstringignoringlineendings
407407
[`Assert::assertStringContainsStringIgnoringLineEndings()`]: https://docs.phpunit.de/en/main/assertions.html#assertstringcontainsstringignoringlineendings
408408

409+
#### PHPUnit < 10.0.0: `Yoast\PHPUnitPolyfills\Polyfills\AssertIsList`
410+
411+
Polyfills the following method:
412+
| |
413+
|---------------------------------|
414+
| [`Assert::assertIsList()`] |
415+
416+
This method was introduced in PHPUnit 10.0.0.
417+
418+
[`Assert::assertIsList()`]: https://docs.phpunit.de/en/main/assertions.html#assertislist
419+
409420

410421
### Helper traits
411422

phpunitpolyfills-autoload.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ public static function load( $className ) {
9191
self::loadAssertObjectEquals();
9292
return true;
9393

94+
case 'Yoast\PHPUnitPolyfills\Polyfills\AssertIsList':
95+
self::loadAssertIsList();
96+
return true;
97+
9498
case 'Yoast\PHPUnitPolyfills\Polyfills\AssertIgnoringLineEndings':
9599
self::loadAssertIgnoringLineEndings();
96100
return true;
@@ -293,6 +297,23 @@ public static function loadAssertObjectEquals() {
293297
require_once __DIR__ . '/src/Polyfills/AssertObjectEquals_Empty.php';
294298
}
295299

300+
/**
301+
* Load the AssertIsList polyfill or an empty trait with the same name
302+
* if a PHPUnit version is used which already contains this functionality.
303+
*
304+
* @return void
305+
*/
306+
public static function loadAssertIsList() {
307+
if ( \method_exists( Assert::class, 'assertIsList' ) === false ) {
308+
// PHPUnit < 10.0.0.
309+
require_once __DIR__ . '/src/Polyfills/AssertIsList.php';
310+
return;
311+
}
312+
313+
// PHPUnit >= 10.0.0.
314+
require_once __DIR__ . '/src/Polyfills/AssertIsList_Empty.php';
315+
}
316+
296317
/**
297318
* Load the AssertIgnoringLineEndings polyfill or an empty trait with the same name
298319
* if a PHPUnit version is used which already contains this functionality.

src/Polyfills/AssertIsList.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
namespace Yoast\PHPUnitPolyfills\Polyfills;
4+
5+
use PHPUnit\Framework\Assert;
6+
7+
/**
8+
* Polyfill the Assert::assertIsList() method.
9+
*
10+
* Introduced in PHPUnit 10.0.0.
11+
*
12+
* @link https://github.com/sebastianbergmann/phpunit/pull/4818
13+
*/
14+
trait AssertIsList {
15+
16+
/**
17+
* Asserts that an array is list.
18+
*
19+
* @param mixed $array The value to test.
20+
* @param string $message Optional failure message to display.
21+
*
22+
* @return void
23+
*/
24+
final public static function assertIsList( $array, $message = '' ) {
25+
$msg = self::assertIsListFailureDescription( $array );
26+
if ( $message !== '' ) {
27+
$msg = $message . \PHP_EOL . $msg;
28+
}
29+
30+
if ( \is_array( $array ) === false ) {
31+
if ( \method_exists( Assert::class, 'assertIsArray' ) ) {
32+
static::assertIsArray( $array, $msg );
33+
return;
34+
}
35+
36+
static::assertInternalType( 'array', $array, $msg );
37+
return;
38+
}
39+
40+
if ( $array === [] ) {
41+
static::assertSame( $array, $array, $msg );
42+
return;
43+
}
44+
45+
if ( \function_exists( 'array_is_list' ) ) {
46+
// phpcs:ignore PHPCompatibility.FunctionUse.NewFunctions.array_is_listFound -- PHP 8.1+.
47+
static::assertTrue( \array_is_list( $array ), $msg );
48+
return;
49+
}
50+
51+
$expected = \range( 0, ( \count( $array ) - 1 ) );
52+
53+
static::assertSame( $expected, \array_keys( $array ), $msg );
54+
}
55+
56+
/**
57+
* Returns the description of the failure.
58+
*
59+
* @param mixed $other The value under test.
60+
*
61+
* @return string
62+
*/
63+
private static function assertIsListFailureDescription( $other ) {
64+
$type = \strtolower( \gettype( $other ) );
65+
66+
switch ( $type ) {
67+
case 'double':
68+
$description = 'a float';
69+
break;
70+
71+
case 'resource (closed)':
72+
$description = 'a closed resource';
73+
break;
74+
75+
case 'array':
76+
case 'integer':
77+
case 'object':
78+
$description = 'an ' . $type;
79+
break;
80+
81+
case 'boolean':
82+
case 'closed resource':
83+
case 'float':
84+
case 'resource':
85+
case 'string':
86+
$description = 'a ' . $type;
87+
break;
88+
89+
case 'null':
90+
$description = 'null';
91+
break;
92+
93+
default:
94+
$description = 'a value of ' . $type;
95+
break;
96+
}
97+
98+
return \sprintf( 'Failed asserting that %s is a list.', $description );
99+
}
100+
}

src/Polyfills/AssertIsList_Empty.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Yoast\PHPUnitPolyfills\Polyfills;
4+
5+
/**
6+
* Empty trait for use with PHPUnit >= 10.0.0 in which this polyfill is not needed.
7+
*/
8+
trait AssertIsList {}

src/TestCases/TestCasePHPUnitGte8.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Yoast\PHPUnitPolyfills\Polyfills\AssertFileEqualsSpecializations;
99
use Yoast\PHPUnitPolyfills\Polyfills\AssertIgnoringLineEndings;
1010
use Yoast\PHPUnitPolyfills\Polyfills\AssertionRenames;
11+
use Yoast\PHPUnitPolyfills\Polyfills\AssertIsList;
1112
use Yoast\PHPUnitPolyfills\Polyfills\AssertObjectEquals;
1213
use Yoast\PHPUnitPolyfills\Polyfills\EqualToSpecializations;
1314
use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionMessageMatches;
@@ -28,6 +29,7 @@ abstract class TestCase extends PHPUnit_TestCase {
2829
use AssertFileEqualsSpecializations;
2930
use AssertIgnoringLineEndings;
3031
use AssertionRenames;
32+
use AssertIsList;
3133
use AssertObjectEquals;
3234
use EqualToSpecializations;
3335
use ExpectExceptionMessageMatches;

src/TestCases/TestCasePHPUnitLte7.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Yoast\PHPUnitPolyfills\Polyfills\AssertFileEqualsSpecializations;
1010
use Yoast\PHPUnitPolyfills\Polyfills\AssertIgnoringLineEndings;
1111
use Yoast\PHPUnitPolyfills\Polyfills\AssertionRenames;
12+
use Yoast\PHPUnitPolyfills\Polyfills\AssertIsList;
1213
use Yoast\PHPUnitPolyfills\Polyfills\AssertIsType;
1314
use Yoast\PHPUnitPolyfills\Polyfills\AssertObjectEquals;
1415
use Yoast\PHPUnitPolyfills\Polyfills\AssertStringContains;
@@ -33,6 +34,7 @@ abstract class TestCase extends PHPUnit_TestCase {
3334
use AssertFileEqualsSpecializations;
3435
use AssertIgnoringLineEndings;
3536
use AssertionRenames;
37+
use AssertIsList;
3638
use AssertIsType;
3739
use AssertObjectEquals;
3840
use AssertStringContains;

src/TestCases/XTestCase.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Yoast\PHPUnitPolyfills\Polyfills\AssertFileEqualsSpecializations;
1010
use Yoast\PHPUnitPolyfills\Polyfills\AssertIgnoringLineEndings;
1111
use Yoast\PHPUnitPolyfills\Polyfills\AssertionRenames;
12+
use Yoast\PHPUnitPolyfills\Polyfills\AssertIsList;
1213
use Yoast\PHPUnitPolyfills\Polyfills\AssertIsType;
1314
use Yoast\PHPUnitPolyfills\Polyfills\AssertObjectEquals;
1415
use Yoast\PHPUnitPolyfills\Polyfills\AssertStringContains;
@@ -35,6 +36,7 @@ abstract class XTestCase extends PHPUnit_TestCase {
3536
use AssertFileEqualsSpecializations;
3637
use AssertIgnoringLineEndings;
3738
use AssertionRenames;
39+
use AssertIsList;
3840
use AssertIsType;
3941
use AssertObjectEquals;
4042
use AssertStringContains;

0 commit comments

Comments
 (0)