|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Yoast\PHPUnitPolyfills\Polyfills; |
| 4 | + |
| 5 | +use PHPUnit\SebastianBergmann\Exporter\Exporter as Exporter_In_Phar; |
| 6 | +use SebastianBergmann\Exporter\Exporter; |
| 7 | +use TypeError; |
| 8 | + |
| 9 | +/** |
| 10 | + * Polyfill the Assert::assertStringEqualsStringIgnoringLineEndings() and the |
| 11 | + * Assert::assertStringContainsStringIgnoringLineEndings() methods. |
| 12 | + * |
| 13 | + * Introduced in PHPUnit 10.0.0. |
| 14 | + * |
| 15 | + * @link https://github.com/sebastianbergmann/phpunit/issues/4641 |
| 16 | + * @link https://github.com/sebastianbergmann/phpunit/pull/4670 |
| 17 | + * @link https://github.com/sebastianbergmann/phpunit/issues/4935 |
| 18 | + * @link https://github.com/sebastianbergmann/phpunit/pull/5279 |
| 19 | + */ |
| 20 | +trait AssertIgnoringLineEndings { |
| 21 | + |
| 22 | + /** |
| 23 | + * Asserts that two strings are equal except for line endings. |
| 24 | + * |
| 25 | + * @param string $expected Expected value. |
| 26 | + * @param string $actual The value to test. |
| 27 | + * @param string $message Optional failure message to display. |
| 28 | + * |
| 29 | + * @return void |
| 30 | + * |
| 31 | + * @throws TypeError When any of the passed arguments do not meet the required type. |
| 32 | + */ |
| 33 | + final public static function assertStringEqualsStringIgnoringLineEndings( $expected, $actual, $message = '' ) { |
| 34 | + /* |
| 35 | + * Parameter input validation. |
| 36 | + * In PHPUnit this is done via PHP native type declarations. Emulating this for the polyfill. |
| 37 | + * Note: using `is_scalar()` instead of `is_string()` as test files may not be using strict_types. |
| 38 | + */ |
| 39 | + if ( \is_scalar( $expected ) === false ) { |
| 40 | + throw new TypeError( |
| 41 | + \sprintf( |
| 42 | + 'Argument 1 passed to assertStringEqualsStringIgnoringLineEndings() must be of type string, %s given', |
| 43 | + \gettype( $expected ) |
| 44 | + ) |
| 45 | + ); |
| 46 | + } |
| 47 | + if ( \is_scalar( $actual ) === false ) { |
| 48 | + throw new TypeError( |
| 49 | + \sprintf( |
| 50 | + 'Argument 2 passed to assertStringEqualsStringIgnoringLineEndings() must be of type string, %s given', |
| 51 | + \gettype( $actual ) |
| 52 | + ) |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + $expected = self::normalizeLineEndingsForIgnoringLineEndingsAssertions( (string) $expected ); |
| 58 | + $exporter = \class_exists( Exporter::class ) ? new Exporter() : new Exporter_In_Phar(); |
| 59 | + $msg = \sprintf( |
| 60 | + 'Failed asserting that %s is equal to "%s" ignoring line endings.', |
| 61 | + $exporter->export( $actual ), |
| 62 | + $expected |
| 63 | + ); |
| 64 | + |
| 65 | + if ( $message !== '' ) { |
| 66 | + $msg = $message . \PHP_EOL . $msg; |
| 67 | + } |
| 68 | + |
| 69 | + $actual = self::normalizeLineEndingsForIgnoringLineEndingsAssertions( (string) $actual ); |
| 70 | + |
| 71 | + static::assertSame( $expected, $actual, $msg ); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Asserts that two variables are equal (ignoring case). |
| 76 | + * |
| 77 | + * @param string $needle The string to search for. |
| 78 | + * @param string $haystack The string to treat as the haystack. |
| 79 | + * @param string $message Optional failure message to display. |
| 80 | + * |
| 81 | + * @return void |
| 82 | + * |
| 83 | + * @throws TypeError When any of the passed arguments do not meet the required type. |
| 84 | + */ |
| 85 | + final public static function assertStringContainsStringIgnoringLineEndings( $needle, $haystack, $message = '' ) { |
| 86 | + /* |
| 87 | + * Parameter input validation. |
| 88 | + * In PHPUnit this is done via PHP native type declarations. Emulating this for the polyfill. |
| 89 | + * Note: using `is_scalar()` instead of `is_string()` as test files may not be using strict_types. |
| 90 | + */ |
| 91 | + if ( \is_scalar( $needle ) === false ) { |
| 92 | + throw new TypeError( |
| 93 | + \sprintf( |
| 94 | + 'Argument 1 passed to assertStringContainsStringIgnoringLineEndings() must be of type string, %s given', |
| 95 | + \gettype( $needle ) |
| 96 | + ) |
| 97 | + ); |
| 98 | + } |
| 99 | + if ( \is_scalar( $haystack ) === false ) { |
| 100 | + throw new TypeError( |
| 101 | + \sprintf( |
| 102 | + 'Argument 2 passed to assertStringContainsStringIgnoringLineEndings() must be of type string, %s given', |
| 103 | + \gettype( $haystack ) |
| 104 | + ) |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + $needle = self::normalizeLineEndingsForIgnoringLineEndingsAssertions( (string) $needle ); |
| 109 | + $haystack = self::normalizeLineEndingsForIgnoringLineEndingsAssertions( (string) $haystack ); |
| 110 | + |
| 111 | + static::assertStringContainsString( $needle, $haystack, $message ); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Normalize line endings. |
| 116 | + * |
| 117 | + * @param string $value The text to normalize. |
| 118 | + * |
| 119 | + * @return string |
| 120 | + */ |
| 121 | + private static function normalizeLineEndingsForIgnoringLineEndingsAssertions( $value ) { |
| 122 | + return \strtr( |
| 123 | + $value, |
| 124 | + [ |
| 125 | + "\r\n" => "\n", |
| 126 | + "\r" => "\n", |
| 127 | + ] |
| 128 | + ); |
| 129 | + } |
| 130 | +} |
0 commit comments