sebastianbergmann/comparator · error · ComparisonFailure

Failed asserting that

Error message

Failed asserting that %s matches expected %s.

What it means

Thrown by ScalarComparator::assertEquals when two string values differ. Before throwing, the comparator strips the overlong common prefix and suffix of the two strings so the ComparisonFailure diff shows only the diverging portion; the %s placeholders carry the exported actual and expected strings.

Solutions

  1. Read the ComparisonFailure diff; it already trims the overlong common prefix/suffix to show just the diverging region of the two strings.
  2. Inspect both strings for invisible differences: trailing whitespace, newline style, encoding, or multi-byte characters.
  3. Use mb_strtolower or normalize both strings if case or encoding differences are expected.
  4. Compare a substring or a computed value rather than full strings when the assertion targets specific content.
  5. Correct the code path producing the actual string so it matches the expected value.
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at src/ScalarComparator.php:84 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of sebastianbergmann/comparator@00837a9d22 (2026-09-15). Data as JSON: /api/errors/ab0068191857e7b7. Report an issue: GitHub.

Appendix: source

Thrown at src/ScalarComparator.php:84

        }

        if ($expectedToCompare !== $actualToCompare && is_string($expected) && is_string($actual)) {
            [$cutExpected, $cutActual] = self::removeOverlongCommonPrefix($expected, $actual);
            [$cutExpected, $cutActual] = self::removeOverlongCommonSuffix($cutExpected, $cutActual);

            throw new ComparisonFailure(
                $expected,
                $actual,
                $exporter->export($cutExpected),
                $exporter->export($cutActual),
                'Failed asserting that two strings are equal.',
                $this->contextLines(),
            );
        }

        /** @phpstan-ignore notEqual.notAllowed */
        if ($expectedToCompare != $actualToCompare) {
            throw new ComparisonFailure(
                $expected,
                $actual,
                // no diff is required
                '',
                '',
                sprintf(
                    'Failed asserting that %s matches expected %s.',
                    $exporter->export($actual),
                    $exporter->export($expected),
                ),
                $this->contextLines(),
            );
        }
    }

    /**
     * @return array{string, string}
     */

View on GitHub (pinned to 00837a9d22)