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

Yoast / PHPUnit-Polyfills / 16579531037

28 Jul 2025 08:25PM UTC coverage: 63.368% (-32.5%) from 95.846%
16579531037

push

github

jrfnl
Merge branch '1.x' into 2.x

365 of 576 relevant lines covered (63.37%)

17.41 hits per line

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

0.0
/src/Polyfills/AssertStringContains.php
1
<?php
2

3
namespace Yoast\PHPUnitPolyfills\Polyfills;
4

5
/**
6
 * Polyfill the Assert::assertStringContainsString() et al methods, which replace the use of
7
 * Assert::assertContains() and Assert::assertNotContains() with string haystacks.
8
 *
9
 * Introduced in PHPUnit 7.5.0.
10
 * Use of Assert::assertContains() and Assert::assertNotContains() with string haystacks was
11
 * deprecated in PHPUnit 7.5.0 and removed in PHPUnit 9.0.0.
12
 *
13
 * Note: this polyfill accounts for a bug in PHPUnit < 6.4.2.
14
 * Prior to PHPUnit 6.4.2, when the $needle was an empty string, a PHP native
15
 * "mb_strpos(): Empty delimiter" warning would be thrown, which would result
16
 * in the test failing.
17
 * This polyfill prevents that warning and emulates the PHPUnit >= 6.4.2 behaviour.
18
 *
19
 * @link https://github.com/sebastianbergmann/phpunit/issues/3422
20
 * @link https://github.com/sebastianbergmann/phpunit/issues/2520
21
 * @link https://github.com/sebastianbergmann/phpunit/pull/2778
22
 *
23
 * @since 0.1.0
24
 */
25
trait AssertStringContains {
26

27
        /**
28
         * Asserts that a string haystack contains a needle.
29
         *
30
         * @param string $needle   The string to search for.
31
         * @param string $haystack The string to treat as the haystack.
32
         * @param string $message  Optional failure message to display.
33
         *
34
         * @return void
35
         */
36
        final public static function assertStringContainsString( $needle, $haystack, $message = '' ) {
×
37
                if ( $needle === '' ) {
×
38
                        static::assertSame( $needle, $needle, $message );
×
39
                        return;
×
40
                }
41

42
                static::assertContains( $needle, $haystack, $message );
×
43
        }
44

45
        /**
46
         * Asserts that a string haystack contains a needle (case-insensitive).
47
         *
48
         * @param string $needle   The string to search for.
49
         * @param string $haystack The string to treat as the haystack.
50
         * @param string $message  Optional failure message to display.
51
         *
52
         * @return void
53
         */
54
        final public static function assertStringContainsStringIgnoringCase( $needle, $haystack, $message = '' ) {
×
55
                if ( $needle === '' ) {
×
56
                        static::assertSame( $needle, $needle, $message );
×
57
                        return;
×
58
                }
59

60
                static::assertContains( $needle, $haystack, $message, true );
×
61
        }
62

63
        /**
64
         * Asserts that a string haystack does NOT contain a needle.
65
         *
66
         * @param string $needle   The string to search for.
67
         * @param string $haystack The string to treat as the haystack.
68
         * @param string $message  Optional failure message to display.
69
         *
70
         * @return void
71
         */
72
        final public static function assertStringNotContainsString( $needle, $haystack, $message = '' ) {
×
73
                if ( $needle === '' ) {
×
74
                        $msg = "Failed asserting that '{$haystack}' does not contain \"\".";
×
75
                        if ( $message !== '' ) {
×
76
                                $msg = $message . \PHP_EOL . $msg;
×
77
                        }
78

79
                        static::fail( $msg );
×
80
                }
81

82
                static::assertNotContains( $needle, $haystack, $message );
×
83
        }
84

85
        /**
86
         * Asserts that a string haystack does NOT contain a needle (case-insensitive).
87
         *
88
         * @param string $needle   The string to search for.
89
         * @param string $haystack The string to treat as the haystack.
90
         * @param string $message  Optional failure message to display.
91
         *
92
         * @return void
93
         */
94
        final public static function assertStringNotContainsStringIgnoringCase( $needle, $haystack, $message = '' ) {
×
95
                if ( $needle === '' ) {
×
96
                        $msg = "Failed asserting that '{$haystack}' does not contain \"\".";
×
97
                        if ( $message !== '' ) {
×
98
                                $msg = $message . \PHP_EOL . $msg;
×
99
                        }
100

101
                        static::fail( $msg );
×
102
                }
103

104
                static::assertNotContains( $needle, $haystack, $message, true );
×
105
        }
106
}
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