sebastianbergmann/comparator · error · ComparisonFailure

Failed asserting that two strings are equal.

Error message

Failed asserting that two strings are equal.

What it means

Thrown by ScalarComparator::assertEquals when two scalar values, compared after being cast to strings, are not equal. The guard casts both operands to string (deliberately, to avoid PHP's loose 0 == 'Foobar' comparison) and applies case-folding when ignoreCase is set; the failure fires when the resulting string representations differ. It is a generic comparison failure, so the faulting input is whichever of expected/actual fails strict string comparison.

Solutions

  1. Print both values with var_export and their types; a match that looks correct may differ in whitespace, encoding, or invisible characters.
  2. Verify types first: a scalar like 0 or false stringifies differently than expected, so compare with strict typing in production code.
  3. Pass ignoreCase=true if the only difference is letter casing.
  4. Normalize encodings/line endings (mb_strtolower, trim, str_replace "\r\n") before comparison when data comes from external sources.
  5. Fix the producer of the actual value so it yields the expected string.
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at src/ScalarComparator.php:72 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/065c9e348262a793. Report an issue: GitHub.

Appendix: source

Thrown at src/ScalarComparator.php:72

        // otherwise 0 == 'Foobar'
        if ((is_string($expected) && !is_bool($actual)) || (is_string($actual) && !is_bool($expected))) {
            /** @phpstan-ignore cast.string */
            $expectedToCompare = @(string) $expectedToCompare;

            /** @phpstan-ignore cast.string */
            $actualToCompare = @(string) $actualToCompare;

            if ($ignoreCase) {
                $expectedToCompare = mb_strtolower($expectedToCompare, 'UTF-8');
                $actualToCompare   = mb_strtolower($actualToCompare, 'UTF-8');
            }
        }

        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(

View on GitHub (pinned to 00837a9d22)