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

Yoast / PHPUnit-Polyfills / 12731053486

12 Jan 2025 06:11AM UTC coverage: 97.912%. Remained the same
12731053486

Pull #234

github

web-flow
Merge b818fae1d into 33d5b8d5d
Pull Request #234: Composer/lint: remove redundant duplicate script

797 of 814 relevant lines covered (97.91%)

127.58 hits per line

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

100.0
/src/Polyfills/AssertArrayWithListKeys.php
1
<?php
2

3
namespace Yoast\PHPUnitPolyfills\Polyfills;
4

5
/**
6
 * Polyfill the Assert::assertArrayIsEqualToArrayOnlyConsideringListOfKeys(),
7
 * Assert::assertArrayIsEqualToArrayIgnoringListOfKeys(),
8
 * Assert::assertArrayIsIdenticalToArrayOnlyConsideringListOfKeys(),
9
 * and Assert::assertArrayIsIdenticalToArrayIgnoringListOfKeys() methods.
10
 *
11
 * Introduced in PHPUnit 11.0.0.
12
 *
13
 * This functionality resembles the functionality previously offered by the `Assert::assertArraySubset()`
14
 * assertion, which was removed in PHPUnit 9.0.0, but with higher precision.
15
 *
16
 * Refactoring tests which still use `Assert::assertArraySubset()` to use the new assertions should be
17
 * considered as an upgrade path.
18
 *
19
 * @link https://github.com/sebastianbergmann/phpunit/pull/5600
20
 * @link https://github.com/sebastianbergmann/phpunit/pull/5716 Included in PHPUnit 11.0.4.
21
 * @link https://github.com/sebastianbergmann/phpunit/pull/5729 Included in PHPUnit 11.0.6.
22
 *
23
 * @since 3.0.0
24
 */
25
trait AssertArrayWithListKeys {
26

27
        /**
28
         * Asserts that two arrays are equal while only considering array elements for which the keys have been specified.
29
         *
30
         * {@internal As the array type declarations don't lead to type juggling, even without strict_types,
31
         * it is safe to let PHP handle the parameter validation.}
32
         *
33
         * @param array<mixed>      $expected           Expected value.
34
         * @param array<mixed>      $actual             The variable to test.
35
         * @param array<int|string> $keysToBeConsidered The array keys to take into account.
36
         * @param string            $message            Optional failure message to display.
37
         *
38
         * @return void
39
         */
40
        final public static function assertArrayIsEqualToArrayOnlyConsideringListOfKeys( array $expected, array $actual, array $keysToBeConsidered, string $message = '' ) {
132✔
41
                $filteredExpected = [];
132✔
42
                foreach ( $keysToBeConsidered as $key ) {
132✔
43
                        if ( isset( $expected[ $key ] ) ) {
110✔
44
                                $filteredExpected[ $key ] = $expected[ $key ];
110✔
45
                        }
46
                }
47

48
                $filteredActual = [];
132✔
49
                foreach ( $keysToBeConsidered as $key ) {
132✔
50
                        if ( isset( $actual[ $key ] ) ) {
110✔
51
                                $filteredActual[ $key ] = $actual[ $key ];
74✔
52
                        }
53
                }
54

55
                static::assertEquals( $filteredExpected, $filteredActual, $message );
132✔
56
        }
65✔
57

58
        /**
59
         * Asserts that two arrays are equal while ignoring array elements for which the keys have been specified.
60
         *
61
         * {@internal As the array type declarations don't lead to type juggling, even without strict_types,
62
         * it is safe to let PHP handle the parameter validation.}
63
         *
64
         * @param array<mixed>      $expected        Expected value.
65
         * @param array<mixed>      $actual          The variable to test.
66
         * @param array<int|string> $keysToBeIgnored The array keys to ignore.
67
         * @param string            $message         Optional failure message to display.
68
         *
69
         * @return void
70
         */
71
        final public static function assertArrayIsEqualToArrayIgnoringListOfKeys( array $expected, array $actual, array $keysToBeIgnored, string $message = '' ) {
132✔
72
                foreach ( $keysToBeIgnored as $key ) {
132✔
73
                        unset( $expected[ $key ], $actual[ $key ] );
110✔
74
                }
75

76
                static::assertEquals( $expected, $actual, $message );
132✔
77
        }
65✔
78

79
        /**
80
         * Asserts that two arrays are identical while only considering array elements for which the keys have been specified.
81
         *
82
         * {@internal As the array type declarations don't lead to type juggling, even without strict_types,
83
         * it is safe to let PHP handle the parameter validation.}
84
         *
85
         * @param array<mixed>      $expected           Expected value.
86
         * @param array<mixed>      $actual             The variable to test.
87
         * @param array<int|string> $keysToBeConsidered The array keys to take into account.
88
         * @param string            $message            Optional failure message to display.
89
         *
90
         * @return void
91
         */
92
        final public static function assertArrayIsIdenticalToArrayOnlyConsideringListOfKeys( array $expected, array $actual, array $keysToBeConsidered, string $message = '' ) {
164✔
93
                $keysToBeConsidered = \array_combine( $keysToBeConsidered, $keysToBeConsidered );
164✔
94
                $expected           = \array_intersect_key( $expected, $keysToBeConsidered );
164✔
95
                $actual             = \array_intersect_key( $actual, $keysToBeConsidered );
164✔
96

97
                static::assertSame( $expected, $actual, $message );
164✔
98
        }
66✔
99

100
        /**
101
         * Asserts that two arrays are identical while ignoring array elements for which the keys have been specified.
102
         *
103
         * {@internal As the array type declarations don't lead to type juggling, even without strict_types,
104
         * it is safe to let PHP handle the parameter validation.}
105
         *
106
         * @param array<mixed>      $expected        Expected value.
107
         * @param array<mixed>      $actual          The variable to test.
108
         * @param array<int|string> $keysToBeIgnored The array keys to ignore.
109
         * @param string            $message         Optional failure message to display.
110
         *
111
         * @return void
112
         */
113
        final public static function assertArrayIsIdenticalToArrayIgnoringListOfKeys( array $expected, array $actual, array $keysToBeIgnored, string $message = '' ) {
132✔
114
                foreach ( $keysToBeIgnored as $key ) {
132✔
115
                        unset( $expected[ $key ], $actual[ $key ] );
110✔
116
                }
117

118
                static::assertSame( $expected, $actual, $message );
132✔
119
        }
52✔
120
}
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

© 2026 Coveralls, Inc