• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / PHPUnit-Polyfills / 5190983416

pending completion
5190983416

Pull #133

github

web-flow
Merge cf3c4bff1 into 3b59adeef
Pull Request #133: Release version 2.0.0

120 of 122 new or added lines in 4 files covered. (98.36%)

359 of 373 relevant lines covered (96.25%)

116.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.24
/src/Polyfills/AssertIsList.php
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
 * @since 2.0.0
15
 */
16
trait AssertIsList {
17

18
        /**
19
         * Asserts that an array is list.
20
         *
21
         * @param mixed  $array   The value to test.
22
         * @param string $message Optional failure message to display.
23
         *
24
         * @return void
25
         */
26
        final public static function assertIsList( $array, $message = '' ) {
27
                $msg = self::assertIsListFailureDescription( $array );
306✔
28
                if ( $message !== '' ) {
306✔
29
                        $msg = $message . \PHP_EOL . $msg;
16✔
30
                }
2✔
31

32
                if ( \is_array( $array ) === false ) {
306✔
33
                        if ( \method_exists( Assert::class, 'assertIsArray' ) ) {
112✔
34
                                static::assertIsArray( $array, $msg );
63✔
NEW
35
                                return;
×
36
                        }
37

38
                        static::assertInternalType( 'array', $array, $msg );
49✔
NEW
39
                        return;
×
40
                }
41

42
                if ( $array === [] ) {
194✔
43
                        static::assertSame( $array, $array, $msg );
16✔
44
                        return;
16✔
45
                }
46

47
                if ( \function_exists( 'array_is_list' ) ) {
178✔
48
                        // phpcs:ignore PHPCompatibility.FunctionUse.NewFunctions.array_is_listFound -- PHP 8.1+.
49
                        static::assertTrue( \array_is_list( $array ), $msg );
24✔
50
                        return;
12✔
51
                }
52

53
                $expected = \range( 0, ( \count( $array ) - 1 ) );
154✔
54

55
                static::assertSame( $expected, \array_keys( $array ), $msg );
154✔
56
        }
46✔
57

58
        /**
59
         * Returns the description of the failure.
60
         *
61
         * @param mixed $other The value under test.
62
         *
63
         * @return string
64
         */
65
        private static function assertIsListFailureDescription( $other ) {
66
                $type = \strtolower( \gettype( $other ) );
306✔
67

68
                switch ( $type ) {
76✔
69
                        case 'double':
306✔
70
                                $description = 'a float';
16✔
71
                                break;
16✔
72

73
                        case 'resource (closed)':
290✔
74
                                $description = 'a closed resource';
10✔
75
                                break;
10✔
76

77
                        case 'array':
280✔
78
                        case 'integer':
108✔
79
                        case 'object':
94✔
80
                                $description = 'an ' . $type;
226✔
81
                                break;
226✔
82

83
                        case 'boolean':
54✔
84
                        case 'closed resource':
40✔
85
                        case 'float':
40✔
86
                        case 'resource':
40✔
87
                        case 'string':
40✔
88
                                $description = 'a ' . $type;
32✔
89
                                break;
32✔
90

91
                        case 'null':
22✔
92
                                $description = 'null';
16✔
93
                                break;
16✔
94

95
                        default:
2✔
96
                                $description = 'a value of ' . $type;
6✔
97
                                break;
6✔
98
                }
2✔
99

100
                return \sprintf( 'Failed asserting that %s is a list.', $description );
306✔
101
        }
102
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc